
In the last blog, we've learned about the theory of Blockchain Bridges, Why they're necessary, and also about common types and working models.
Today, we'll be going to "Deep-Dive" into the technical details of Blockchain Bridges, specifically into "Polygon PoS SideChain" - As we believe Polygon makes it simpler to understand the intricacies of Bridges.
As I say every time, it's mandatory to have a strong foundation of any protocol that you're willing to learn. And today, as we're learning about the Polygon PoS Bridge, let's learn about the utter foundations and the parts that make up the bridge.
Proof-of-Stake is a consensus mechanism used in the blockchain networks to validate the transactions and add new blocks to the blockchain.
Unlike the Pow(Proof-of-Work) mechanism, which requires more computational power, PoS selects participants(also called Validators) to create new blocks based on the number of coins they hold and are willing to "stake" as collateral.
Simply speaking, the more coins that you're willing to put, the more the chances of being selected to validate the transaction and earn rewards(Of course, right?)
Side-chain is a separate blockchain connected to the main blockchain via a two-way peg mechanism.
Wait a minute?
Separate blockchain? - Yes, which means it'll have its own set of validators and consensus mechanisms.
By the way, a two-way peg allows digital assets to transfer back and forth between the child chain and the parents chain.
Don't forget that, Any two-pegged operation needs to assume that the validators are acting honestly.
So is it centralized? - We'll keep that topic out of this blog.
Simply put, Polygon PoS is an Evm-compatible, Proof-of-Stake, SideChain for the Ethereum blockchain.
In other words, Polygon is an L2(Layer 2) network to Ethereum, employing a Proof-of-Stake consensus mechanism. And it's composed of Two Layers:
-> Heimdall Layer
-> Bor Layer
-> Ethereum Layer
Acts as consensus layer consisting of a set of Proof-of-Stake Heimdall nodes for monitoring staking contracts deployed on the Ethereum mainnet, and committing to the Polygon Network checkpoints to the Ethereum mainnet. (OR)
In other words, it's the consensus layer, which is based on a Proof-of-Stake network of Heimdall validators, which monitor the Ethereum contract state, coordinate block producer selection, and push new snapshots of the chain state known as checkpoints back to Ethereum.
But why monitoring staking contracts on Ethereum? Because, In order to become a validator/node, we're required to stake MATIC[Polygon's own token] tokens on Staking Contracts deployed on Ethereum.
In Simple, the Heimdall Layer does these 3 things:
Monitors the staking contracts on the Ethereum Mainnet
Verifies all state transactions on the Bor Chain
Commits the Bor Chain state checkpoints to the Ethereum mainnet.
We'll discuss about 2nd and 3rd points in detail later in this session.
This acts as an Execution layer, which uses Bor, a fork of geth, and is responsible for producing new blocks.
In Simple, the Bor Layer does the main activity of:
Producing blocks on the Polygon Network
a set of smart contracts on the Ethereum mainnet for managing staking, checkpoints, and validator rewards.
In Simple, the Core Contracts on Ethereum hold to main functions:
Fraud Proofs: These act as a Security Layer, enabling the validation of transactions and state changes to ensure transparency and security across operations.
Exit Queue: Manages the safe and efficient transfer of assets back to Ethereum Mainnet
These nodes act as validators in the Polygon PoS architecture.
These validators have two main functions:
Transaction Validation: When the transaction is initiated on the Polygon PoS chain, these nodes validate the transaction against the current state of the Polygon Chain
CheckPoint Creation: After validating a set number of transactions, these nodes create a Merkel root of the transaction hashes, known as "checkpoint", and submit it to the core contracts on the Ethereum mainnet.
These nodes typically acts as a bridge between the Parent Chain(Ethereum) and Child Chain(Polygon). As with the 'SideChains', these nodes must be trusted. Any hack with these nodes, leads to the devastation of the network.
Now that we have some decent knowledge of all the parts included in the Bridging process, we can delve into the technicality of the Bridges.
While Bridging Data, there are two scenarios:
Child Chain --> Parent Chain
Parent Chain --> Child Chain

Let's say we want to transfer a message, "Hello Polygon - From Ethereum" message from Ethereum to Polygon, we've to develop two contracts respectively named "Sender.sol" - which will be deployed on Ethereum, and "Receiver.sol" which will be deployed on Polygon.
Here is how the technical flow works:
On Sender.sol, we've to first implement an Interface named 'IStateSender' so that we can send our data to the StateSyncer contract.
Wait a minute, what is StateSyncer contract?
StateSyncer contract will play a main role in emitting event logs which will be captured by the validators to transfer data across chains.
For Example, Our Sender.sol code might look like this:
pragma solidity ^0.8.0;
interface IStateSender{
function syncState(address receiver, bytes calldata data)external;
}
contract Sender{
address StateSyncer = '0xAA.......A';
address Receiver = '0xBB........B'; // Here we've to provide our Receiver.sol address that is deployed on Polygon
bytes memory Data = bytes(abi.encode("Hello Polygon - From Ethereum"));
function sendData()external{
IStateSender(StateSyncer).syncState(Receiver,Data);
}
} Once we initiate the 'sendData' transaction, the function will call the 'syncState' function on the StateSyncer contract with our given calldata.
The 'StateSyncer.sol' contract - deployed on Ethereum - upon receiving these data, emits an event log with the respective data.
All the Validators in the Heimdall Chain receive these transactions, And one of them adds this transaction to the Queue to process this transaction.
At this instance, this transaction will be included in a block and stored on the Heimdall Chain and it will be picked up by validators on Bor chain for every few mins.
Meanwhile, as said earlier we also need to have our Receiver.sol deployed on Polygon and our sample code might look like this:
pragma solidity ^0.8.0;
interface IStateReceiver{
function onStateReceived(uint256 stateId, bytes calldata Data)external;
}
contract Receiver{
function onStateReceived(uint256 stateId, bytes calldata Data)external{
// We can write our own logic Here.
// Simply we can emit a log by decoding the received Data to string which will be saying "Hello Polygon - From Ethereum";
}
}Validators on the Bor chain pick up the transaction and call the 'onStateReceived' function on the Receiver.sol contract.
Upon calling the 'onStateReceived' function, we can write our custom logic to do anything.
Overall, this simply sums up the flow from Ethereum to Polygon.
NOTE: Wondering how the events that are emitted by the StateSyncer contract will be picked up by the Heimdall validators? - The Bridge module plays a vital role here.
To give a glance, there will be two components in the Bridge module namely "listener" & "processor". By the name, the listener component listens for the events coming from respective chains & processor component processes the events emitted by the chains.
In our case, the listener/rootchain.go listens for the events on the Ethereum Chain, Once our Sender.sol calls the 'syncState' function with our data, the StateSyncer contract emits an event i.e. picked up the rootchain.go.
Upon verification, the validators add this event to the Queue so that the processor component can take charge.
Though we've learned about the technical flow of transferring data from Ethereum to Polygon, the reverse process is somewhat different. There will be some checks before modifying the data on the RootChain.

Before anything else, we need to understand the fact that our contracts must emit an "Event" log with the data that we're required to pass to another chain.
We'll have two contracts deployed named "ChildContract.sol" & "RootContract.sol" which will be deployed on Polygon Chain and Ethereum Chain respectively. As said earlier, the ChildContract must emit an event to send data to the RootContract.
For example, this is how our ChildContract.sol might look like (Deployed on Polygon):
pragma solidity ^0.8.0;
contract ChildContract{
event Data(address from, bytes _data);
function sendData(bytes memory _data)external{
emit Data(msg.sender, _data);
}
}Once we send data by using the 'sendData' function, it emits an Event log that will be captured by Validators on the Polygon Chain.
Then, Validators - on the Polygon Chain - after validating(might take around 10-30mins), pick up this transaction hash and add it into the checkpoint by interacting with the 'submitCheckPoint()' function on the 'RootChain' contract deployed on Ethereum.
What does RootChain contract does? Along with other contracts named 'RootChainManager' & 'Predicate' contracts.
Remember when I initially said that, the core contracts on the Ethereum have two main functions: Fraud Proofs & Exit Queue. These contracts serves these purposes. (If you don't remember, Kindly go back and read again to understand better)
Once the Tx was added into Checkpoint, Validators call the 'exit()' function on the 'RootChainManager' contract to verify that the transaction has happened on the Polygon Chain.
| NOTE: Our RootContract.sol must contain some type of onlyPredicate() modifier so that only the Predicate contract can make state changes in our contract.
For example, our code might look like:
pragma solidity ^0.8.0;
contract RootContract{
address public predicate;
constructor(address _predicate){
require(_predicate != address(0),"Null Address");
predicate = _predicate;
}
modifier OnlyPredicate(){
require(msg.sender == predicate, "You're not predicate contract");
_;
}
function modifyRootContractData()external OnlyPredicate(){
// We can write our own custom logic
}
}Upon verifying the Transaction, the 'RootChainManager' contract calls the 'exitTokens()' function on the 'CustomPredicate' contract which in return makes a call to our 'RootContract.sol' contract to make any respective state changes on our contract.
That's it for this blog, and hope that you learned about the technical details of "How Bridges Work - More specifically about Polygon PoS Bridge".
As said, In the next blog, we'll try to understand common vulnerabilities in Bridges that developers & security researchers tend to miss out on during development or auditing respectively.
Finally, this blog is a knowledge of open information and a personal understanding of the protocol. For further information, you can read more on:
See you again next time in another awesome blog. Good Luck & WAGMI :)
Read more Blockchain Bridges - Part 1 [ How Do They Work? ] - For Beginners
TECHFUNDのセキュリティリサーチャー