Skip to main content
State sync is the mechanism by which contract state on Ethereum is propagated to the Bor chain. It allows dApps on Polygon Chain to read and react to events that originate on Ethereum.

How state sync works

State transfer from Ethereum to Bor happens through a system call:
  1. A contract on Ethereum calls syncState on the StateSender.sol contract, emitting a StateSynced event.
  2. Heimdall validators listen for StateSynced events. One validator sends a state-sync transaction to Heimdall.
  3. Once the transaction is included in a Heimdall block, it is added to the pending state-sync list.
  4. After every sprint on Bor (currently 16 blocks), the Bor node fetches pending state-sync records from Heimdall via API.
  5. Bor commits these records at the start of each sprint by calling onStateReceive on the target contract. This is a system call from address 2^160-2.
The receiver contract on Bor must implement IStateReceiver. The onStateReceive function can only be called by StateReceiver.sol at address 0x0000000000000000000000000000000000001001.

State sender contract

Source: StateSender.sol The StateSender contract on Ethereum exposes a syncState function:
Calling syncState emits the following event:
Once the StateSynced event is emitted, Heimdall listens for it. After 2/3+ validators agree, the state-sync record is added to Heimdall state. Bor fetches and applies it at the start of the next sprint.

State receiver interface on Bor

The target contract on Bor must implement the following interface:
Only StateReceiver.sol at address 0x0000000000000000000000000000000000001001 is permitted to call onStateReceive on target contracts.

Gas for receivers

Since the Uburu upgrade, StateReceiver gives each onStateReceive call a bounded amount of gas. It reads the amount from the StateSyncGasOracle contract at 0x5e3a5eafd4501332c0bc2acfd38df3ebc5152147, capped at 5,000,000. If the oracle cannot be read, it uses 160,000. At the time of writing, gasLimit() on the oracle returns 160,000 on mainnet. Governance can change this value, so read it from the oracle instead of hard-coding it:

Failed state syncs and replay

If onStateReceive runs out of gas or reverts, the state sync is not lost. StateReceiver emits StateCommitted(stateId, false), stores a hash of the receiver address and the state data, and continues with the next record. Anyone can replay the failed state sync later.

Check whether a delivery failed

StateCommitted(uint256 indexed stateId, bool success) is emitted for every state sync delivered to a contract. A success value of false marks a failure. To check a specific state sync, read the stored hash:
A non-zero result means the state sync failed and has not been replayed. It is zero if the delivery succeeded, if the state sync was already replayed, or if the receiver was not a contract.

Replay a failed state sync

Call replayFailedStateSyncWithData on StateReceiver:
The arguments are the id, contractAddress and data of the StateSynced event that the StateSender contract emitted on Ethereum. You can also read them from the state-sync record on Heimdall; see the clerk module. The contract checks that keccak256(abi.encode(receiver, stateData)) matches the stored hash.
Keep the following in mind:
  • Anyone can call it. The caller pays the gas.
  • The replay is not limited by the oracle. The receiver gets the gas your transaction provides, so set a --gas-limit that is enough for onStateReceive.
  • If the receiver reverts again, the whole replay transaction reverts and the stored hash is kept, so you can retry with more gas or after fixing the receiver.
  • After a successful replay the stored hash is deleted and StateSyncReplay(stateId) is emitted. Replaying the same state sync again reverts with !found.
Failures recorded before the Uburu upgrade are replayed with replayFailedStateSync(uint256 stateId), which takes only the state ID.
Keep the work done inside onStateReceive small, or watch StateCommitted for success = false and replay failures with enough gas. For FxPortal receivers, the same applies to processMessageFromRoot.

System calls

The state sync commit on Bor uses a system call. Only the system address 2^160-2 can make a system call. Bor calls it internally with the system address as msg.sender, which changes contract state and updates the state root for the block, without requiring a user-initiated transaction. This is inspired by EIP-210.

State-sync logs and Bor block receipts

Events emitted by system calls are handled differently from normal logs. Bor produces a synthetic transaction and receipt for each state-sync event, so clients can query these events through standard Ethereum JSON-RPC methods. The transaction hash is derived from:
This synthetic transaction does not affect consensus logic. eth_getBlockByNumber, eth_getTransactionReceipt, and eth_getLogs include state-sync logs from the derived transaction. Note that the block bloom filter does not include state-sync log entries, and the derived transaction is not included in transactionRoot or receiptRoot.