Ethereum Stack Exchange is a question and answer site for users of Ethereum, the crypto value and blockchain-based consensus network. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 8 down vote accepted

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 reccomend using Events, which you could index by timestamp to make searching easier.

share|improve this answer

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.

share|improve this answer
    
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 Feb 5 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 Feb 6 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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