2

I know state is mutable but changes are held in the chaindata as leveldb data. How can we actually read the chaindata history? Are there some utilities for this?

if I have this code -

pragma solidity ^0.4.18;



contract RegisterContract {

    uint public contractWeight;

    mapping(bytes32=>string) public contracts;
    bytes32[] public myHashes;

    function addContract ( string jsonInput, bytes32 hashKey) public {
        contracts[hashKey] = jsonInput;
        myHashes.push(hashKey);
     }

    function addWeight (uint weight) public {
        contractWeight = weight;
    }

    function () payable public {
    }

}

And I want to check all the history of the hashes when they were added (and new code could delete them), then how would I do that?

3

There are a few approaches you could consider depending on who/what needs to know the past state.

First, it should be fairly obvious that miners and full nodes are fully aware of the state at any block height. They see the transactions and compute the state themselves. This doesn't mean past states are conveniently accessible inside contract logic.

Contract logic is geared toward current state, which is usually the most important. Get variable state at specific block number

When you say "I want to", it's unclear if you need the contract to access past states, or clients. Clients can observe event logs and compute past states. That is, if the smart contract author uses them appropriately. In my opinion, a good application of event logs is to create an audit trail that will make it possible for a client to reconstruct the state history of the contract.

I stress "possible" because immutable code in smart contracts implies that we should aim to keep it as simple as possible. Generally, don't clutter it up with too many client-side concerns.

Here's your contract with the logs added.

pragma solidity ^0.4.18;

contract RegisterContract {

    uint public contractWeight;

    mapping(bytes32=>string) public contracts;
    bytes32[] public myHashes;

    event LogAddContract(address sender, bytes32 hashKey, string jsonInput);
    event LogAddWeight(address sender, uint weight);

    function addContract ( string jsonInput, bytes32 hashKey) public {
        contracts[hashKey] = jsonInput;
        myHashes.push(hashKey);
        LogAddContract(msg.sender, hashKey, jsonInput);
     }

    function addWeight (uint weight) public {
        contractWeight = weight;
        LogAddWeight(msg.sender, weight);
    }

    function () payable public {
    }

}

I have seen an experimental project using assember to explore event logs from inside a contract. Other than that, event logs are generally out of reach for contract logic.

With a dependable state and an observable state history you might ask yourself why (or if) the contract really needs to look into the past. I haven't seen too many cases where this is actually needed.

If you do find that there is truly a need for an in-state history, then you will have to create it yourself and append records as you go.

Hope it helps.

  • Events causes a log to be created? Are logs then accessible? Thanks. – Trevor Lee Oakley Apr 2 '18 at 17:01
  • Yes. Events are a lot like normalised tables. Clients can "listen" to event logs, and also "filter". You can index up to three columns. They are super handy. A good intro over here: ethereum.stackexchange.com/questions/2024/… – Rob Hitchens - B9lab Apr 2 '18 at 17:20
  • So it's not possible to recover a old state without manually adding evmlogs/events or saving full "snapshots" on client side? @RobHitchensB9lab – noooooooob Dec 12 '18 at 3:45
  • Contracts are usually concerned witb the present state. Clients can explore contract states at previous block heights. In theory, a contract could also do that but I think it would require resorting to assembler. – Rob Hitchens - B9lab Dec 12 '18 at 4:42

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.