6

Blockchains are known to be immutable because all transactions of cryptocurrencies and state (data) changes. However, looking at ethscan does not provide any information on the output of a smart contract function that results in a state change. Is there a way to monitor the historic values of a state and if so how to do so?

1

3 Answers 3

4

There are two ways you could potentially track how a state vatiable has changed over time:

1- The contract has been developed so that when said variable is modified its previous state is added to an array which contains all previous states.

2- the contract has been developed so when the state variable is modified it fires an Event that logs said transaction. Here's more info about how to retrieve the logs. How can I view event logs for an ethereum contract?

Both depend on the developer of the contract caring for keeping track of previous values either by storing them or logging it.

4
  • Thank you so much for your answer. However, it seems to me that the contract needs to be designed in a way to keep track of changes. Otherwise, there's no way for someone to, say, spoof my contract's state history that I don't have any logs for. Is that right?
    – thalsky
    Commented Oct 27, 2017 at 19:39
  • What I'm trying to say is that there's no way for someone (not even the developer/owner of the contract) to access the history of a state variable (what values it has had over time) if the developer of the contract didn't code for that (in the form of logs or some kind of internal logic). Commented Oct 27, 2017 at 20:11
  • Does this make smart contracts "mutable" since there's no capability to verify the states? Also, given the method invocation history which includes input variables, I could potentially retrieve historical value of each transaction following the stack trace?
    – thalsky
    Commented Oct 31, 2017 at 19:57
  • Immutability of a smart contract refers to the fact that you can't modify the contract once deployed. If you didn't code functions that allow modifying a particular state variable, said variable will also become immutable. Commented Oct 31, 2017 at 20:25
5

There's a third method. You can scan the chain looking for transactions on the contract and querying the state of that variable at each transaction of interest. I'm not saying it's easy--in fact, it's exceptionally difficult given the way the data is stored--but you could do it. I call this idea "off-chain monitoring" of smart contracts.

2
  • Thank you so much for your answer. Does this "off-chain monitoring" method imply that in order to query a state's value after the 10th change transaction, you have to start from the first transaction on till you get to the 10th? In other words, you replay the methods that modified the state value?
    – thalsky
    Commented Oct 27, 2017 at 19:43
  • My understanding is that the state of the contract is something that happens after all transactions are executed and is stored in memory of nodes.
    – atok
    Commented Feb 24, 2018 at 12:50
1

To add to @pabloruiz55 's answer, I wanted to suggest yet another alternative that I think is better than storing an array, as it is O(1) in the number of historical updates: use a backwards linked list of ipfs hashes. Rather than storing the state variable itself on the smart contract, you store it on ipfs and then put it's hash in the smart contract. In addition to storing the content of the state variable on the ipfs file, you also store the hash of the previous ipfs state file in the next ipfs file. That way, you only need to store the hash of the most recent state variable on the smart contract, but you can recursively recover all of the previous states simply by querying that backwards linked list (or hash chain) that is stored off-chain on ipfs. The sequence of states stored off chain is cryptographically guaranteeed to have the same order as the real history of hashes that were pushed to the blockchain.

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.