One of the main modules of the Polygon SDK are Blockchain and State.
Blockchain is the powerhouse that deals with block reorganizations. This means that it deals with all the logic that happens when a new block is included into the blockchain.
State represents the state transition object. It deals with how the state changes when a new block is included.
Among other things, State handles:
- Executing transactions
- Executing the EVM
- Changing the Merkle tries
- Much more, which is covered in the corresponding State section 🙂
The key takeaway is that these 2 parts are very connected, and they work closely together in order for the client to function.
For example, when the Blockchain layer receives a new block (and no reorganization occurred), it calls the State to perform a state transition.
Blockchain also has to deal with some parts relating to consensus (ex. is this ethHash correct?, is this PoW correct?).
In one sentence, it is the main core of logic through which all blocks are included.
One of the most important parts relating to the Blockchain layer is the WriteBlocks method:
The WriteBlocks method is the entry point to write blocks into the blockchain. As a parameter, it takes in a range of blocks.
Firstly, the blocks are validated. After that, they are written to the chain.
The actual state transition is performed by calling the processBlock method within WriteBlocks.
It is worth mentioning that, because it is the entry point for writing blocks to the blockchain, other modules (such as the Sealer) utilize this method.
There needs to be a way to monitor blockchain related changes.
This is where Subscriptions come in.
Subscriptions are a way to tap into blockchain event streams, and instantly receive meaningful data.
The Blockchain Events contain information regarding any changes made to the actual chain. This includes reorganizations, as well as new blocks:
Do you remember when we mentioned the monitor command in the CLI Commands?
The Blockchain Events are the original events that happen in Polygon SDK, and they're later mapped to a Protocol Buffers message format for easy transfer.