3

Is it possible to get a variable state at a specific block number? Let's take the balance mapping in an ERC20 contact for example. I want to know which user had what balance at a specific time (blocknumber). Copying the balance into another map should be costing too much gas. is there a way to have a smart contract see the state of a variable at a specific block with solidity? Or any other gas efficient way to copy a map into another smart contract?

  • There is the MiniMe Token that implements historical balances at a given block number with the function balanceOfAt(). Even you can fork the token and keep the history. – Ismael Mar 8 '18 at 4:35
4

A contract can only see the current state when it executes, not previous states. This restriction allows validating nodes to work only with the current state, rather than needing to store and be able to access all the previous states.

In theory it should be possible to have a contract storing block hashes, and have the user provide a merkle proof showing that a given piece of data is in the state tree for a previous block. However, I don't know of any practical implementations of this.

If you control the contract where the data is written, one option would be to write a complete history to storage. However, this will make normal operations costly. You could do the same thing more cheaply by only writing the hash of the history, and requiring the user to resend the relevant history.

  • There's a javascript library eth-proof that can generate some proofs. – Ismael Mar 8 '18 at 4:32
  • What do you mean by'only writing the hash of the history, and requiring the user to resend the relevant history could you please give a small example? @Edmund Edgar – alper May 1 '18 at 8:22
  • How does eth_getStorageAt work then? Could we rely on eth_getStorageAt without writing the complete history? requiring the user to resend the relevant history, by this, are we expecting the user to save the entire history on the client side... – noooooooob Dec 12 '18 at 3:51
3

No It is not possible in Solidity.

curl --header "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0", "method": "eth_getStorageAt", "params": ["ox_YOUR_SMART_CONTRACT_ADDRESS", "0x_INTEGER_POSITION_OF_STATE_VARIABLE_IN_CONTRACT", "0x_BLOCK_NUMBER"], "id": 1}' localhost:8545

With the above curl, you will be able to get the state of each and every variable in the smart contract at the specified block number.

Hope will be useful for your first question.

  • Quite a detailed answer, the curl line included makes it very usable. But it looks like the question is specifically about Solidity, so probably the first line of the answer should start with a "No". – Utgarda Sep 26 '18 at 13:28
  • "Yes" you are right, updated the answer – M. Gopal Sep 27 '18 at 4:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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