1

I was researching how contract storage works. I know that the world state is a Merkle Patricia Trie, containing all active Ethereum adresses with their storageRoot (and their nonce, balance, andcodeHash).

I've gained a better understanding of how a Merkle Patricia Trie works, and the fact that if you store all the key-values (i.e. For contract storage, the storage indexes with their respective values) in a key-value DB in the form of a Merkle Patricia Trie, all you need is the root hash (stored in storageRoot) to find anc check/update all the key values.

I understand that each client has its own implementation. Go Ethereum uses levelDB (Google's key-value database) for instance.

The thing is that if a key-value is modified (because of contract execution), it creates a new node but doesn't delete the old one (which is good because if another contract has exactly the same node, it should have the same hash, so that would be a problem if the entry was deleted, if I understand correctly).

But I guess there are a lot of keys/values unique to each contract. And if every time a state variable is modified, a new node is created, but the old one isn't deleted, that's a crazy waste of space, isn't it? Is there something I'm missing here?

Thanks 🙏

1
  • Well, there is a reason the chain is now more than one terrabytes ;) Jul 4, 2023 at 12:57

1 Answer 1

1

After lots of research, here is my understanding:

In general, when information is updated (like the accounts balance, or contracts variable states values), a key/value database (which implements merkle patricia tries) will create a new node for the variable to be updated, update the rest of the tree (since all parent nodes will have a hash that will be updated, as in a Merkle tree), but the old node remains existing (and accessible).

This is useful, on the one hand, to have a state history (if you want to know the value of a variable in a contract in a previous block), but it's not required to keep this information.

So there are clients that allow you to synchronise a blockchain without this "history", but that also give you the possibility of synchronizing everything to have a complete history of everything that's happened.

1
  • Hi @Hackndo, I am wondering about deletions from trie myself. What makes you think that updated values create new nodes, instead of updating the "storage slot".
    – BipedalJoe
    Dec 13, 2023 at 15:00

Your Answer

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

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