Doesn't that contradicts the whole idea of blockchain that data stored there is immutable. If someone can alter the data stored in the state of the contract then how is it any different from any other databases.
2 Answers
A contract has a storage space for data. While the code may indeed be immutible, the immutible process may include setting and updating data. As @goodvibration observes, the old state is still there after new transactions change it.
contract State {
uint public x; // state variable. inspect with State.x()
function setX(uint newX) public {
x = newX; // overwrite old value
}
}
This little contrived example lets you mutate the value of x
by signing and sending a transaction to the contract's function setX(uint)
. That transaction is part of the ordered history of changes and it runs at a certain block height.
We are most often interested in the latest/most current block but it is possible to access x()
and any block height and discover what it was then. It is always possible to recover a transaction history that explains why it has a certain value. It is _
because transaction _
from address _
set it to that at block _
, i.e. it is auditable.
Hope it helps.
Doesn't that contradicts the whole idea of blockchain that data stored there is immutable?
No, because the new state takes effect starting from the next block added to the blockchain, while the old state remains exactly as it was in previous blocks.
I believe the part you're probably missing here is that when you query a piece of data, you query it by block number (and it may attain different values on different blocks).