github.com/Gessiux/neatchain@v1.3.1/chain/core/state/database.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package state
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/Gessiux/neatchain/chain/trie"
    23  	"github.com/Gessiux/neatchain/neatdb"
    24  	"github.com/Gessiux/neatchain/utilities/common"
    25  	lru "github.com/hashicorp/golang-lru"
    26  )
    27  
    28  const (
    29  	// Number of codehash->size associations to keep.
    30  	codeSizeCacheSize = 100000
    31  )
    32  
    33  // Database wraps access to tries and contract code.
    34  type Database interface {
    35  	// OpenTrie opens the main account trie.
    36  	OpenTrie(root common.Hash) (Trie, error)
    37  
    38  	// OpenStorageTrie opens the storage trie of an account.
    39  	OpenStorageTrie(addrHash, root common.Hash) (Trie, error)
    40  
    41  	// OpenTX1Trie opens the tx1 trie of an account
    42  	OpenTX1Trie(addrHash, root common.Hash) (Trie, error)
    43  
    44  	// OpenTX3Trie opens the tx3 trie of an account
    45  	OpenTX3Trie(addrHash, root common.Hash) (Trie, error)
    46  
    47  	// OpenProxiedTrie opens the proxied trie of an account
    48  	OpenProxiedTrie(addrHash, root common.Hash) (Trie, error)
    49  
    50  	// OpenRewardTrie opens the reward trie of an account
    51  	OpenRewardTrie(addrHash, root common.Hash) (Trie, error)
    52  
    53  	// CopyTrie returns an independent copy of the given trie.
    54  	CopyTrie(Trie) Trie
    55  
    56  	// ContractCode retrieves a particular contract's code.
    57  	ContractCode(addrHash, codeHash common.Hash) ([]byte, error)
    58  
    59  	// ContractCodeSize retrieves a particular contracts code's size.
    60  	ContractCodeSize(addrHash, codeHash common.Hash) (int, error)
    61  
    62  	// TrieDB retrieves the low level trie database used for data storage.
    63  	TrieDB() *trie.Database
    64  }
    65  
    66  // Trie is a Ethereum Merkle Patricia trie.
    67  type Trie interface {
    68  	// GetKey returns the sha3 preimage of a hashed key that was previously used
    69  	// to store a value.
    70  	//
    71  	// TODO(fjl): remove this when SecureTrie is removed
    72  	GetKey([]byte) []byte
    73  
    74  	// TryGet returns the value for key stored in the trie. The value bytes must
    75  	// not be modified by the caller. If a node was not found in the database, a
    76  	// trie.MissingNodeError is returned.
    77  	TryGet(key []byte) ([]byte, error)
    78  
    79  	// TryUpdate associates key with value in the trie. If value has length zero, any
    80  	// existing value is deleted from the trie. The value bytes must not be modified
    81  	// by the caller while they are stored in the trie. If a node was not found in the
    82  	// database, a trie.MissingNodeError is returned.
    83  	TryUpdate(key, value []byte) error
    84  
    85  	// TryDelete removes any existing value for key from the trie. If a node was not
    86  	// found in the database, a trie.MissingNodeError is returned.
    87  	TryDelete(key []byte) error
    88  
    89  	// Hash returns the root hash of the trie. It does not write to the database and
    90  	// can be used even if the trie doesn't have one.
    91  	Hash() common.Hash
    92  
    93  	// Commit writes all nodes to the trie's memory database, tracking the internal
    94  	// and external (for account tries) references.
    95  	Commit(onleaf trie.LeafCallback) (common.Hash, error)
    96  
    97  	// NodeIterator returns an iterator that returns nodes of the trie. Iteration
    98  	// starts at the key after the given start key.
    99  	NodeIterator(startKey []byte) trie.NodeIterator
   100  
   101  	// Prove constructs a Merkle proof for key. The result contains all encoded nodes
   102  	// on the path to the value at key. The value itself is also included in the last
   103  	// node and can be retrieved by verifying the proof.
   104  	//
   105  	// If the trie does not contain a value for key, the returned proof contains all
   106  	// nodes of the longest existing prefix of the key (at least the root), ending
   107  	// with the node that proves the absence of the key.
   108  	Prove(key []byte, fromLevel uint, proofDb neatdb.Writer) error
   109  }
   110  
   111  // NewDatabase creates a backing store for state. The returned database is safe for
   112  // concurrent use, but does not retain any recent trie nodes in memory. To keep some
   113  // historical state in memory, use the NewDatabaseWithCache constructor.
   114  func NewDatabase(db neatdb.Database) Database {
   115  	return NewDatabaseWithCache(db, 0)
   116  }
   117  
   118  // NewDatabaseWithCache creates a backing store for state. The returned database
   119  // is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
   120  // large memory cache.
   121  func NewDatabaseWithCache(db neatdb.Database, cache int) Database {
   122  	csc, _ := lru.New(codeSizeCacheSize)
   123  	return &cachingDB{
   124  		db:            trie.NewDatabaseWithCache(db, cache),
   125  		codeSizeCache: csc,
   126  	}
   127  }
   128  
   129  type cachingDB struct {
   130  	db            *trie.Database
   131  	codeSizeCache *lru.Cache
   132  }
   133  
   134  // OpenTrie opens the main account trie.
   135  func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
   136  	return trie.NewSecure(root, db.db)
   137  }
   138  
   139  // OpenStorageTrie opens the storage trie of an account.
   140  func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
   141  	return trie.NewSecure(root, db.db)
   142  }
   143  
   144  // OpenTX1Trie opens the tx1 trie of an account
   145  func (db *cachingDB) OpenTX1Trie(addrHash, root common.Hash) (Trie, error) {
   146  	return trie.NewSecure(root, db.db)
   147  }
   148  
   149  // OpenTX3Trie opens the tx3 trie of an account
   150  func (db *cachingDB) OpenTX3Trie(addrHash, root common.Hash) (Trie, error) {
   151  	return trie.NewSecure(root, db.db)
   152  }
   153  
   154  // OpenProxiedTrie opens the proxied trie of an account
   155  func (db *cachingDB) OpenProxiedTrie(addrHash, root common.Hash) (Trie, error) {
   156  	return trie.NewSecure(root, db.db)
   157  }
   158  
   159  // OpenRewardTrie opens the reward trie of an account
   160  func (db *cachingDB) OpenRewardTrie(addrHash, root common.Hash) (Trie, error) {
   161  	return trie.NewSecure(root, db.db)
   162  }
   163  
   164  // CopyTrie returns an independent copy of the given trie.
   165  func (db *cachingDB) CopyTrie(t Trie) Trie {
   166  	switch t := t.(type) {
   167  	case *trie.SecureTrie:
   168  		return t.Copy()
   169  	default:
   170  		panic(fmt.Errorf("unknown trie type %T", t))
   171  	}
   172  }
   173  
   174  // ContractCode retrieves a particular contract's code.
   175  func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
   176  	code, err := db.db.Node(codeHash)
   177  	if err == nil {
   178  		db.codeSizeCache.Add(codeHash, len(code))
   179  	}
   180  	return code, err
   181  }
   182  
   183  // ContractCodeSize retrieves a particular contracts code's size.
   184  func (db *cachingDB) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) {
   185  	if cached, ok := db.codeSizeCache.Get(codeHash); ok {
   186  		return cached.(int), nil
   187  	}
   188  	code, err := db.ContractCode(addrHash, codeHash)
   189  	if err == nil {
   190  		db.codeSizeCache.Add(codeHash, len(code))
   191  	}
   192  	return len(code), err
   193  }
   194  
   195  // TrieDB retrieves any intermediate trie-node caching layer.
   196  func (db *cachingDB) TrieDB() *trie.Database {
   197  	return db.db
   198  }