To truly understand how State works, you must understand some basic Ethereum concepts.
We highly recommend reading the State in Ethereum guide.
Now that we've familiarized ourselves with basic Ethereum concepts, the next overview should be easy.
We mentioned that the World state trie has all the Ethereum accounts that exist.
These accounts are the leaves of the Merkle trie. Each leaf has encoded Account State information.
This enables the Polygon SDK to get a specific Merkle trie, for a specific point in time.
For example, we can get the hash of the state at block 10.
The Merkle trie, at any point in time, is called a Snapshot.
We can have Snapshots for the state trie, or for the storage trie - they are basically the same.
The only difference is in what the leaves represent:
- In the case of the storage trie, the leaves contain arbitrary state, which we cannot process or know what's in there
- In the case of the state trie, the leaves represent accounts
The Snapshot interface is defined as such:
The information that can be committed is defined by the Object struct:
The implementation for the Merkle trie is in the state/immutable-trie folder.
state/immutable-trie/state.go implements the State interface.
state/immutable-trie/trie.go is the main Merkle trie object. It represents an optimized version of the Merkle trie, which reuses as much memory as possible.
state/executor.go includes all the information needed for the Polygon SDK to decide how a block changes the current state. The implementation of ProcessBlock is located here.
The apply method does the actual state transition. The executor calls the EVM.
When a state transition is executed, the main module that executes the state transition is the EVM (located in state/runtime/evm).
The dispatch table does a match between the opcode and the instruction.
The core logic that powers the EVM is the Run loop.
This is the main entry point for the EVM. It does a loop and checks the current opcode, fetches the instruction, checks if it can be executed, consumes gas and executes the instruction until it either fails or stops.