github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/core/datareduction/database.go (about) 1 package datareduction 2 3 import ( 4 "fmt" 5 "github.com/intfoundation/intchain/common" 6 "github.com/intfoundation/intchain/core/state" 7 "github.com/intfoundation/intchain/intdb" 8 "github.com/intfoundation/intchain/trie" 9 ) 10 11 // PruneDatabase wraps access to prune tries. 12 type PruneDatabase interface { 13 // OpenPruneTrie opens the prune trie. 14 OpenPruneTrie(root common.Hash) (state.Trie, error) 15 16 // CopyTrie returns an independent copy of the given trie. 17 CopyTrie(state.Trie) state.Trie 18 19 // TrieDB retrieves the low level trie database used for data storage. 20 TrieDB() *trie.Database 21 } 22 23 // NewDatabase creates a backing store for prune trie. The returned database is safe for 24 // concurrent use, but does not retain any recent trie nodes in memory. To keep some 25 // historical state in memory, use the NewDatabaseWithCache constructor. 26 func NewDatabase(db intdb.Database) PruneDatabase { 27 return NewDatabaseWithCache(db, 0) 28 } 29 30 // NewDatabaseWithCache creates a backing store for prune trie. The returned database 31 // is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a 32 // large memory cache. 33 func NewDatabaseWithCache(db intdb.Database, cache int) PruneDatabase { 34 return &pruneDB{ 35 db: trie.NewDatabaseWithCache(db, cache), 36 } 37 } 38 39 type pruneDB struct { 40 db *trie.Database 41 } 42 43 // OpenTrie opens the Prune trie. 44 func (db *pruneDB) OpenPruneTrie(root common.Hash) (state.Trie, error) { 45 return trie.NewSecure(root, db.db) 46 } 47 48 // CopyTrie returns an independent copy of the given trie. 49 func (db *pruneDB) CopyTrie(t state.Trie) state.Trie { 50 switch t := t.(type) { 51 case *trie.SecureTrie: 52 return t.Copy() 53 default: 54 panic(fmt.Errorf("unknown trie type %T", t)) 55 } 56 } 57 58 // TrieDB retrieves any intermediate trie-node caching layer. 59 func (db *pruneDB) TrieDB() *trie.Database { 60 return db.db 61 }