2

Is it possible to get latest update time of contract variable without preserving the transaction hash,

contract Demo{
uint numb;

  function setNumb(uint var1){
    numb=var1
  }
}

in above contract whenever the numb is updated using setNumb(uint), transaction is fired and was saved in some block, if I want to know the time stamp of block in which the transaction is stored I can get the block by using Transaction hash and easily get the block time stamp,

But what if I don't know the transaction hash

Is it possible to get the latest transaction time(ie. block time stamp) which updates numb, Is there any way to get the block/blocks which stores transactions related to numb

3

Yes, here's an example that incorporates 2 ways: using and a state variable. One or both ways may be used depending on what fits the use case better.

contract Demo {
  uint _var;
  uint public _lastTimeVarWritten;

  event LogLastTimeVarWritten(uint timestamp);

  function setVar(uint var1){
    _var=var1;
    _lastTimeVarWritten = block.timestamp;
    LogLastTimeVarWritten(_lastTimeVarWritten);
  }
}

(Code in the question isn't valid because var is a keyword in Solidity.)

As @Michal explained, you have to implement it (in this case to store the block.timestamp) explicitly.

2

Short answer: no

Long answer:

Ethereum maintains the contract state as a subtree of the global state tree. The connection between a transaction and the state change it caused is maintained as a link between old global state root and new global state root.

Navigating through the history of the state changes and finding the transaction and the block containing this transaction is computationally expensive. Additionally - because of state tree pruning it might be not really possible without "reverse replaying" the transactions (state tree pruning is removal of history states to make the state storage requirements lower).

So if the history of state changes is part of your contract state - you have to implement it explicitly.

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.