Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trace history of ethereum state varaibles?

One of the much appraised features of blockchain among other things is traceability of data stored on the blockchain priced on records being immutable i think.

I am trying to find out how state changes can be traced practically on the ethereum blockchain. To explain my question, take the following smart contract as example

contract SimpleStorage {
    uint storedData;

    function set(uint x) public {
        storedData = x;
    }

    function get() public view returns (uint) {
        return storedData;
    }
}

In this contract the focus is on the storedData state variable. Lets assume that the value has changed severally at countless times overtime. So how can one trace out the history of this storedData to see all the values that has been assigned to it at different points in time. I am looking for a practical way this is done.

like image 411
mozenge Avatar asked Jan 01 '20 20:01

mozenge


People also ask

Where are state variables stored in Ethereum?

State variables These variables are declared outside of functions (like the attributes of a class) and are stored permanently in the Ethereum blockchain, more specifically in the storage Merkle Patricia tree, which is part of the information that forms the state of an account (that's why we call it a state variable).

Where are Ethereum events stored?

Events. Smart contracts have the ability to "emit" events during execution. Events are also known as "logs" in Ethereum. The output of the events are stored in transaction receipts under a logs section.

How do I view smart contracts on Ethereum?

The best way to view a token's smart contract is through Etherscan, a block explorer and analytics platform built on Ethereum. Block explorers like Etherscan allow users to search and index real-time and historical information about a blockchain.


1 Answers

You have two ways to potentially track how a state variable 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. ex. check this contract events: https://etherscan.io/address/0x3958b4ec427f8fa24eb60f42821760e88d485f7f#events

enter image description here

2) the contract has been developed so when the state variable is modified it fires an Event that logs said transaction. Take a look to this post in consensys about events:

https://media.consensys.net/technical-introduction-to-events-and-logs-in-ethereum-a074d65dd61e

The article presents three use cases for events:

"First, using an event to simply get a return value from a contract function invoked with sendTransaction(). Second, using an event as an asynchronous trigger with data, that can notify an observer such as a UI. Third, using an event to write logs in the blockchain as a cheaper form of storage."

like image 93
Diego B Avatar answered Oct 20 '22 02:10

Diego B