0

I want to understand the basic working of storing a value of a key that can periodically update its value in blockchain. For example, if the value of 'a' is 4 today and is stored in blockchain, then tomorrow i may update it to someother value and it may be recorded in some other block. Now, if i want to get the latest value of the key, how does it work?

1
0

You mean

contract Keys{
    mapping (string => uint256) dict;


    function get(string key) public view  returns(uint256){
        return dict[key];
    }

    function set(string key, uint256 val) public{
        dict[key] = val;
    }
}

You can get the current value of any key anytime with get("a") and set new values with a new transaction on set("a",newvalue);

1
  • I am not asking about how we retrieve a value, but how the system retrieves. I mean if i query for some data from my contract, the value can be stored in any of the blocks. Hence will ethereum traverse all the millions of blocks to get my latest state of my contract variable? – Madan May 11 '18 at 10:12
0

In addition to the existing answer, mappings are persistent, so when you're mapping a value to a key it works in the same way as when you're updating the content of state variables, state modifying functions effect storage in the same way for the two scenarios.

0

Ethereum uses merkle tree to store data of multiple blocks. This property is used for re-organizing block

However, the number of block stored depends on sync mode. Archive nodes store the state of all historical blocks while full nodes stores only state of 128 blocks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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