Suppose I have an array of some data let say
arr[15]="any value"
If I changed this value to any other value
arr[15] = "changed value"
How can I get the previous value of arr[15]=" any value"
Please Explain on the basis of immutable concept of Blockchain
Are there any way on solidity programming ?

  • think of any variable as a stack which take the last result but all the history(previous states) is saved in the blockchain – Badr Bellaj Jul 24 '17 at 11:55

A full copy of the blockchain contains a complete history of all the transactions that ever occurred in its history, so data is never erased from a node that downloads all the data in every block and keeps it without deleting any of it. So if you have a complete copy of the blockchain, you can discover that arr[15] used to have the value "any value".

However, as far as a Solidity contract is concerned, storage is like a regular database: It always sees the state at the point when your transaction is being executed, which is always the tip of the chain. So once you write a new value, the old value is gone. Designing it like this makes it much cheaper to run a node that can verify a block: To verify a transaction, you only need to know the current state of the database at that particular point in time, which is many times smaller than the complete history of state at every possible moment in the past. That allows you to run a node with far less data than you would need if contracts had arbitrary access to the state of the database at previous blocks.

Although it's not possible for a contract to read data that it has over-written or erased, it is theoretically possible to confirm data from a previous block. If you have a current block hash, you should be able to prove that a previous block hash came before it, and if you have that previous block hash, you should be able to make a merkle proof showing that the data was in the blockchain in that earlier block. So someone with a full copy of the blockchain could read the data in a previous block, construct the necessary proof, and send the data, along with data proving it's correct, to your contract. However, I don't know of any actual implementations of this method, and it may be that the gas costs of doing this largely make it impractical.

In practice if you really need your contract to be able to access old data, you normally just have to cough up the necessary gas costs and pay to keep the historical data in storage, so it never actually gets deleted from your contract.

| improve this answer | |
  • 2
    you want to say that if miner set the gas limit to null then we can track the previous transaction ? otherwise it is only possible for testnet not for real blockchain ? And what about the contract programming , how to do that ? – Gopal ojha Jul 25 '17 at 10:01
  • 1
    Well, that depends how much storage your historical data is using. For example each entry could be stored as a single hash, and you could have the user's application send the plaintext of the entry as a transaction parameter when the contract needs to process it. In that case an entry is only 32 bytes of storage, which shouldn't usually be unfeasible with current gas pricing. – Edmund Edgar Jul 25 '17 at 21:46
  • 2
    Oh ! ya I got it . I will be thankful if you elaborate how log is done and what is its phenomenon ? – Gopal ojha Jul 26 '17 at 6:23
  • 1
    I don't think I understand your question about logs but it sounds like a different question, so I suggest you post a new question. – Edmund Edgar Jul 26 '17 at 6:45

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.