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
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?