github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/chain/core/datareduction/database.go (about)

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