Skip to main content

State Sync Mechanism

State Sync is the native mechanism to read Ethereum data from Matic EVM chain.

Validators on the Heimdall layer pickup the StateSynced event and pass it on to the Bor layer (read more about the architecture here.

The receiver contract inherits IStateReceiver, and custom logic sits inside onStateReceive function.

This is the flow required from dapps / users to work with state-sync:

  1. Call the smart contract function present here: https://github.com/maticnetwork/contracts/blob/19163ddecf91db17333859ae72dd73c91bee6191/contracts/root/stateSyncer/StateSender.sol#L33
  2. Which emits an event called StateSynced(uint256 indexed id, address indexed contractAddress, bytes data);
  3. All the validators on the Heimdall chain receive this event and one of them, whoever wishes to get the tx fees for state sync sends this transaction to Heimdall.
  4. Once state-sync transaction on Heimdall has been included in a block, it is added to pending state-sync list.
  5. After every sprint on bor, the Bor node fetches the pending state-sync events from Heimdall via an API call.
  6. The receiver contract inherits IStateReceiver interface, and custom logic of decoding the data bytes and performing any action sits inside onStateReceive function: https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/IStateReceiver.sol