0

I am trying to understand how to get the old values stored in blockchain. I need to prove the immutability and transparent feature to non-technical people. Here is my code.

    pragma solidity ^0.4.17;
        contract Inbox{
           string public message;

            function Inbox (string initialMessage) public {
                   message = initialMessage;
            }

           function setMessage(string newMessage) public{
                message = newMessage;
          }
 }

In above contract, whenever i call setMessage i set a new message for the message variable. For example, first time i had set it for "Test1". Second time i set it for "Test2" and so on. now i added a get function to read the value of message variable. The function always returns me the latest value set for message variable. Now my query is, is it possible to check all the values i set in past without using arrays. Just with the above example can i get all old logs or am I expecting something wrong.

2 Answers 2

5

Logs in Ethereum are a special construct, and have a specific meaning. You don't appear to be using any in your contract.

What you do seem to want is retrieving the state of the contract at some historical point. This is possible, provided the node you are connected to is able to access that state.

eth_call accepts a block number parameter (which defaults to the latest block), and will return the state at the given block.

3
  • So i need to know the block number to get the state.
    – SSS
    Sep 14, 2018 at 12:57
  • If you emit actual logs, you can scan a range of blocks for value changes. Without those, you will need the blocknumber Sep 14, 2018 at 13:06
  • Sure.....thank you very much. I also wanted to read about what exactly happens when we call a solidity function till the block gets mined. Would appreciate if i get some direction here. Thanks,
    – SSS
    Sep 15, 2018 at 5:44
3

It should be possible if you emit an event for every change

pragma solidity ^0.4.17;

contract Inbox {
    string public message;

    event NewMessage(string _message, uint _block, uint _timestamp, address _user);

    function Inbox (string initialMessage) public {
        message = initialMessage;
        emit NewMessage(initialMessage, block.number, block.timestamp, msg.sender);
    }

    function setMessage(string newMessage) public{
        message = newMessage;
        emit NewMessage(newMessage, block.number, block.timestamp, msg.sender);
    }
}

Now if you query NewMessage you should get the whole history of changes.

2
  • It seems to me you don’t even need the state variable. You could simply emit the event, and then query it or watch it. Later you can go back and view the historical events, so you have a zero cost associated with storage. I’m not sure how much events cost to emit, but they’re cheaper than storage. Sep 14, 2018 at 13:41
  • You are correct, the only case to keep it in storage is if you need to query it from another contract because you do not have access to events.
    – Ismael
    Sep 14, 2018 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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