Merkle proof verification for Ethereum Patricia tree

Account and contract variable queries

In this example we will use Web3.py to build a Merkle proof that some value for a key is included in the given state root.

Get contract variables storage proofs

How are contract variables stored in the Patricia trie ?

To store variables, the EVM uses a key based on the position at which the variable is defined in a contract : keccack(LeftPad32(key, 0), LeftPad32(map position, 0)). More details here : https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_getstorageat.

Proof verification code

Here is a great diagram explaining the different types of nodes in the Patricia tree :

Conclusion

This was a quick overview of how solidity contract variables can be queried with inclusion/exclusion proofs. eth_getStorageAt and eth_getProof actually remove the need to define getters in contract code as the getProof API queries the trie state db directly (getters are still needed for a contract getting another contracts’s variable). In a future article we’ll have a look at how these state proofs can be used for inter-blockchain communication with on-chain state verification of a sidechain.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store