4

I know it is possible to verify historical ETH balances (for the last 256 blocks) using a merkle proof on-chain - are there any known implementations?

NB - I'm looking for Solidity code (i.e. on-chain) ways of doing this.

1
  • Currently there's not much Solidity code implementing proofs, and this related question's answer links to Solidity code for proving events: ethereum.stackexchange.com/questions/16117/… (The code includes a reference to the EventStorage in Adam's answer below.)
    – eth
    Commented Apr 27, 2020 at 6:04

3 Answers 3

4

You probably need to build you own code on the top of these two libraries: https://github.com/hamdiallam/Solidity-RLP and https://github.com/commitground/solidity-patricia-tree

The former library allows decoding RLP encoded data structure. The second allows checking that certain data is stored in Merkle-Patricia tree.

Ethereum stores state of all addresses in Merkle-Patricia tree. Path to the leaf is an address, and leaf data is RLP encoding of four values nonce, balance, storageRoot, and codeHash.

Root hash of the tree is stored in block header among other things.

So here is what you need to do in your smart contract:

  1. Calculate hash of provided block header
  2. Check that this hash equals to the block hash of block provided block number
  3. Extract Merkle-Patricia root hash from block header
  4. Check that provided address state is actually in Merkle-Patricis tree at provided address
  5. Extract balance from address state
1
  • While this answer gives a good overview, step 4 is basically "do all the unexplained things". Recommend enhancing this answer with much more details on how to actually do step 4. Commented Mar 29, 2020 at 5:47
0

For anyone coming to this, I found the below - it is unproven though and has some limitations:

https://github.com/figs999/Ethereum/blob/master/EventStorage.sol

1
  • This code does not allow to do what you need. It seems to check for logged events rather than balances, and does so relying on bloom filter only which ma return false positives. So when this code says you that some data was logged that may or may not be the case, while is the code says that the data was not logged, then this is definitely so. Commented Oct 9, 2019 at 14:09
0

Just in case someone needs this, here's the onchain implementation of account and storage proof verification: https://github.com/lidofinance/curve-merkle-oracle/blob/main/contracts/StateProofVerifier.sol.

And if you need to verify transaction inclusion, here's another repo (which the repo linked above is actually based upon): https://github.com/lorenzb/proveth/blob/master/onchain/ProvethVerifier.sol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.