ChainSafe / gossamer

🕸️ Go Implementation of the Polkadot Host
https://chainsafe.github.io/gossamer
GNU Lesser General Public License v3.0
432 stars 116 forks source link

feat(primitives/state-machine) `TrieBackend` implementation #4318

Closed timwu20 closed 1 week ago

timwu20 commented 2 weeks ago

Changes

statemachine.Backend

statemachine.Backend is the interface that statemachine.TrieBackend implements. It is used to read from state which is backed by TrieDB, as well as iteration through iterators, and calculate state roots.

statemachine.TrieBackend

statemachine.TrieBackend implements the statemachine.Backend interface. The constructor NewTrieBackend takes as parameters an implementation of the TrieCacheProvider interface as well as an optional recorder.Recorder for node recording and eventual state proof generation. The storage that is read from is an implementation of TrieBackendStorage with an associated root hash to look up.

statemachine.ephemeral

ephemeral is private type to facilitate overlayed changes provided by a trie.PrefixedMemoryDB as the overlay level, while fetching from the underlying TrieBackendStorage when the hash isn't found in the overlay. This implements HashDB and is used as a n ephemeral storage layer for TrieBackend.

statemachine.MemoryDBTrieBackend

MemoryDBTrieBackend is a helper type that creates a TrieBackend where a trie.PrefixedMemoryDB is used as underlying storage. Currently only used in tests but I've exported it for now since it will be used in future work.

trie.SharedTrieCache

SharedTrieCache is used to hold all the trie nodes and values for all operations to the state. Within SharedTrieCache consists of both sharedNodeCache and sharedValueCache instances. There should only be one instance of a SharedTrieCache for a node.

trie.LocalTrieCache

SharedTrieCache.LocalTrieCache() is used to create a LocalTrieCache instance which will get merged back to the parent SharedTrieCache after state changes are processed and LocalTrieCache.Commit() is called.

trie.TrieCache

LocalTrieCache.TrieCache(storageRoot H) is used to create a TrieCache instance which implements triedb.TrieCache interface. TrieCache.MergeInto(ltc *LocalTrieCache, storageRoot H) is used to merge it back to the LocalTrieCache.

costlru.LRU

costlru.LRU is a wrapper around freelru.LRU to constrain the LRU based on the cost. The cost used in the various caches is byte size.

recorder.Recorder

Recorder is used to generate a compatible triedb.TrieRecorder using the private trieRecorder type. The recorder supports transactions which can be rolled back.

Tests

go test -tags integration github.com/ChainSafe/gossamer

Issues

closes #3901 #3836