I would like to know if is possible to inquiry variable values using older blocks.

For example I have

contract Simple {
    string32 message public;

    function Simple() {
        message = msg.sender;

Is it possible to retrieve the value of the message 1 month ago? Is there a method that let me inspect such variable on particular blockheight?

Is there a method that retrieves all different values that are found on the blockchain?

Thank you very much.

3 Answers 3


There is no way for a contract to access a variable that is no longer located in storage. You can access these variables from web3, however.

I would recommend using Events, which you could index by timestamp to make searching easier.

With web3, there is a possible solution (probably not the fastest or the most elegant, but it is to show how it can be done). We retrieve the first variable of the contract (index 0), which is of type uint:

contract = "0x6d363cd2eb21ebd39e50c9a2f94a9724bf907d13";
maxBlocks = 1000;

startBlock = eth.blockNumber;
for (var i = 1; i < maxBlocks; i++) { /* Be careful: we go *back* in time */
    current = web3.eth.getStorageAt(contract, 0, startBlock-i);
    if (current != previous) {
        /* TODO Where to find msg.sender? We probably have to loop
         * over the transactions in the block can call
         * web3.eth.getTransaction */
        blockDate = new Date(web3.eth.getBlock(startBlock-i+1).timestamp*1000);
        console.log("Block #" + (startBlock-i+1) +  " (" + web3.eth.getBlock(startBlock-i+1).timestamp + " " + blockDate.toString()
            +  ") : " + web3.toDecimal(previous));
        /* What if there are two changes in a single block? The
         * documentation of getStorageAt seems silent about that */
        previous = current;
blockDate = new Date(web3.eth.getBlock(startBlock-maxBlocks).timestamp*1000);
console.log("Somewhere before block #" +(startBlock-maxBlocks) +  " (block of " + blockDate.toString()
        +  ") : " + web3.toDecimal(previous));

You can try it on Testnet, where the contract with this address has been deployed.

  • I'm unsure but approved the edit; undo it if desired and @bortzmeyer should post a different answer.
    – eth
    Commented Jun 26, 2016 at 23:20

You could theoretically come up with an incredibly long and complex Merkle proof to prove to a contract that a given account had a particular value at a particular block number, and you could increase efficiency quite a bit if you have a contract that stores as many previous block hashes as possible; however, this would be rather unwieldy to implement. Aside from that, the simplest short-term solution may be to just figure out exactly which values you want to be able to permanently access from inside the contract, and just permanently keep them in storage in an array. If you just want to access past values from the interface, then I recommend events as the above poster mentioned.

  • Maybe a low tech option is to install a new instance of geth and download the blockchain from the main geth instance, examining the data at each new block. I don't know if one can control / stop syncing after each block though. On second thought, I might have just ELI5ed what the previous poster was saying.
    – shiso
    Commented Feb 5, 2016 at 22:53
  • Hi Vitalik and others, thank you. I saw Events and already had in mind to store rewrites of the variable in another array, but I think it would grow up a lot the storage needed. Since for this use case previous values are not an information that I need to use in the contract itself, do you think that using external software like a blockexplorer I could easily retrieve the history of variables values? If I use ether.camp I can see storage modifications for every transaction. Do you think it would be better than storing previous values in a different array inside the contract?
    – croll83
    Commented Feb 6, 2016 at 0:53

It depends on your needs but I take it that something like, say: debug.setHead(800000) may help.

It "rewinds" your blockchain's last block to block 800 000 (just an example).

It don't know if it works to query past smart contracts but I suppose it shall work: I've often used it (at the geth JS console) to query past balances for particular addresses.

The issue is that then you need to re-synch up to the latest block: that's why I'm saying it depends on your use case. The technique doesn't scale, but if your requirements are simple it may be sufficient.

For example if I want to query an address's balance (address which may be a smart contract, so it's more advanced than simply checking the amount of ETHs in a "normal" address), approximately weekly, I start from the current block (the latest one) then I debug.setHead(...) one week before current date, then one week before that, rinse and repeat. That is very fast. But at the end you need to re-synch from wherever you ended up.

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.