2

Using geth, I can access any block's state root hash. Is there any way we can get access to the storage root hash inside it and also retrieve state variables from the storage trie itself.

I have tried merkle-patricia-tree package but couldn't figure out exactly how we can retrieve upto the state variables.

If it is not possible then please provide the reason and if it is possible then a small code snippet example will be highly appreciated.

1 Answer 1

1

Yes, it is possible, but you may not know how to read the data since it may not be in the order declared in the contract and it will be encoded, so you would need to know the indices of the state variable in the contract and some other details like if that value was added to a mapping, array, struct, etc., to be able to write code to figure out what data belongs to which variable and try to decode it.

There's a good example of how to get all the data directly here: is there an easy way to get the full state of a contract?

You may be better off using something like web3.eth.getStorageAt(contractAddress, indexOfTheVariable). Write functionality that knows how to get each data of the smart contract.

Here is an article that explains step by step how the variables are laid out in storage and how to get state storage variable values directly using web3.eth.getStorageAt and decode them. Maybe this article would help you create a tool to decode any storage trie data given the abi of a contract or something like that.

Your Answer

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

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