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:
- Call the smart contract function present here: https://github.com/maticnetwork/contracts/blob/19163ddecf91db17333859ae72dd73c91bee6191/contracts/root/stateSyncer/StateSender.sol#L33
- Which emits an event called
StateSynced(uint256 indexed id, address indexed contractAddress, bytes data);
- 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.
- Once
state-sync
transaction on Heimdall has been included in a block, it is added to pending state-sync list. - After every sprint on
bor
, the Bor node fetches the pending state-sync events from Heimdall via an API call. - The receiver contract inherits
IStateReceiver
interface, and custom logic of decoding the data bytes and performing any action sits insideonStateReceive
function: https://github.com/maticnetwork/genesis-contracts/blob/master/contracts/IStateReceiver.sol