State Sync Mechanism

Validators on the Heimdall layer pickup the StateSynced event and pass it on to the Bor layer (read more about the architecture here: https://hackmd.io/DFYomMhJSrWmHOqE3rj68w#Architecture).

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


Things 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