github.com/phillinzzz/newBsc@v1.1.6/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  	"errors"
    21  	"fmt"
    22  	"time"
    23  
    24  	"github.com/phillinzzz/newBsc/common"
    25  	"github.com/phillinzzz/newBsc/core/rawdb"
    26  	"github.com/phillinzzz/newBsc/ethdb"
    27  	"github.com/phillinzzz/newBsc/trie"
    28  	lru "github.com/hashicorp/golang-lru"
    29  )
    30  
    31  const (
    32  	// Number of codehash->size associations to keep.
    33  	codeSizeCacheSize = 100000
    34  
    35  	// Number of state trie in cache
    36  	accountTrieCacheSize = 32
    37  
    38  	// Number of storage Trie in cache
    39  	storageTrieCacheSize = 2000
    40  
    41  	// Cache size granted for caching clean code.
    42  	codeCacheSize = 64 * 1024 * 1024
    43  
    44  	purgeInterval = 600
    45  
    46  	maxAccountTrieSize = 1024 * 1024
    47  )
    48  
    49  // Database wraps access to tries and contract code.
    50  type Database interface {
    51  	// OpenTrie opens the main account trie.
    52  	OpenTrie(root common.Hash) (Trie, error)
    53  
    54  	// OpenStorageTrie opens the storage trie of an account.
    55  	OpenStorageTrie(addrHash, root common.Hash) (Trie, error)
    56  
    57  	// CopyTrie returns an independent copy of the given trie.
    58  	CopyTrie(Trie) Trie
    59  
    60  	// ContractCode retrieves a particular contract's code.
    61  	ContractCode(addrHash, codeHash common.Hash) ([]byte, error)
    62  
    63  	// ContractCodeSize retrieves a particular contracts code's size.
    64  	ContractCodeSize(addrHash, codeHash common.Hash) (int, error)
    65  
    66  	// TrieDB retrieves the low level trie database used for data storage.
    67  	TrieDB() *trie.Database
    68  
    69  	// Cache the account trie tree
    70  	CacheAccount(root common.Hash, t Trie)
    71  
    72  	// Cache the storage trie tree
    73  	CacheStorage(addrHash common.Hash, root common.Hash, t Trie)
    74  
    75  	// Purge cache
    76  	Purge()
    77  }
    78  
    79  // Trie is a Ethereum Merkle Patricia trie.
    80  type Trie interface {
    81  	// GetKey returns the sha3 preimage of a hashed key that was previously used
    82  	// to store a value.
    83  	//
    84  	// TODO(fjl): remove this when SecureTrie is removed
    85  	GetKey([]byte) []byte
    86  
    87  	// TryGet returns the value for key stored in the trie. The value bytes must
    88  	// not be modified by the caller. If a node was not found in the database, a
    89  	// trie.MissingNodeError is returned.
    90  	TryGet(key []byte) ([]byte, error)
    91  
    92  	// TryUpdate associates key with value in the trie. If value has length zero, any
    93  	// existing value is deleted from the trie. The value bytes must not be modified
    94  	// by the caller while they are stored in the trie. If a node was not found in the
    95  	// database, a trie.MissingNodeError is returned.
    96  	TryUpdate(key, value []byte) error
    97  
    98  	// TryDelete removes any existing value for key from the trie. If a node was not
    99  	// found in the database, a trie.MissingNodeError is returned.
   100  	TryDelete(key []byte) error
   101  
   102  	// Hash returns the root hash of the trie. It does not write to the database and
   103  	// can be used even if the trie doesn't have one.
   104  	Hash() common.Hash
   105  
   106  	// Commit writes all nodes to the trie's memory database, tracking the internal
   107  	// and external (for account tries) references.
   108  	Commit(onleaf trie.LeafCallback) (common.Hash, error)
   109  
   110  	// NodeIterator returns an iterator that returns nodes of the trie. Iteration
   111  	// starts at the key after the given start key.
   112  	NodeIterator(startKey []byte) trie.NodeIterator
   113  
   114  	// Prove constructs a Merkle proof for key. The result contains all encoded nodes
   115  	// on the path to the value at key. The value itself is also included in the last
   116  	// node and can be retrieved by verifying the proof.
   117  	//
   118  	// If the trie does not contain a value for key, the returned proof contains all
   119  	// nodes of the longest existing prefix of the key (at least the root), ending
   120  	// with the node that proves the absence of the key.
   121  	Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error
   122  }
   123  
   124  // NewDatabase creates a backing store for state. The returned database is safe for
   125  // concurrent use, but does not retain any recent trie nodes in memory. To keep some
   126  // historical state in memory, use the NewDatabaseWithConfig constructor.
   127  func NewDatabase(db ethdb.Database) Database {
   128  	return NewDatabaseWithConfig(db, nil)
   129  }
   130  
   131  // NewDatabaseWithConfig creates a backing store for state. The returned database
   132  // is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
   133  // large memory cache.
   134  func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
   135  	csc, _ := lru.New(codeSizeCacheSize)
   136  	cc, _ := lru.New(codeCacheSize)
   137  	return &cachingDB{
   138  		db:            trie.NewDatabaseWithConfig(db, config),
   139  		codeSizeCache: csc,
   140  		codeCache:     cc,
   141  	}
   142  }
   143  
   144  func NewDatabaseWithConfigAndCache(db ethdb.Database, config *trie.Config) Database {
   145  	csc, _ := lru.New(codeSizeCacheSize)
   146  	cc, _ := lru.New(codeCacheSize)
   147  	atc, _ := lru.New(accountTrieCacheSize)
   148  	stc, _ := lru.New(storageTrieCacheSize)
   149  
   150  	database := &cachingDB{
   151  		db:               trie.NewDatabaseWithConfig(db, config),
   152  		codeSizeCache:    csc,
   153  		codeCache:        cc,
   154  		accountTrieCache: atc,
   155  		storageTrieCache: stc,
   156  	}
   157  	go database.purgeLoop()
   158  	return database
   159  }
   160  
   161  type cachingDB struct {
   162  	db               *trie.Database
   163  	codeSizeCache    *lru.Cache
   164  	codeCache        *lru.Cache
   165  	accountTrieCache *lru.Cache
   166  	storageTrieCache *lru.Cache
   167  }
   168  
   169  type triePair struct {
   170  	root common.Hash
   171  	trie Trie
   172  }
   173  
   174  func (db *cachingDB) purgeLoop() {
   175  	for {
   176  		time.Sleep(purgeInterval * time.Second)
   177  		_, accounts, ok := db.accountTrieCache.GetOldest()
   178  		if !ok {
   179  			continue
   180  		}
   181  		tr := accounts.(*trie.SecureTrie).GetRawTrie()
   182  		if tr.Size() > maxAccountTrieSize {
   183  			db.Purge()
   184  		}
   185  	}
   186  }
   187  
   188  // OpenTrie opens the main account trie at a specific root hash.
   189  func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
   190  	if db.accountTrieCache != nil {
   191  		if tr, exist := db.accountTrieCache.Get(root); exist {
   192  			return tr.(Trie).(*trie.SecureTrie).Copy(), nil
   193  		}
   194  	}
   195  	tr, err := trie.NewSecure(root, db.db)
   196  	if err != nil {
   197  		return nil, err
   198  	}
   199  	return tr, nil
   200  }
   201  
   202  // OpenStorageTrie opens the storage trie of an account.
   203  func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
   204  	if db.storageTrieCache != nil {
   205  		if tries, exist := db.storageTrieCache.Get(addrHash); exist {
   206  			triesPairs := tries.([3]*triePair)
   207  			for _, triePair := range triesPairs {
   208  				if triePair != nil && triePair.root == root {
   209  					return triePair.trie.(*trie.SecureTrie).Copy(), nil
   210  				}
   211  			}
   212  		}
   213  	}
   214  
   215  	tr, err := trie.NewSecure(root, db.db)
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  	return tr, nil
   220  }
   221  
   222  func (db *cachingDB) CacheAccount(root common.Hash, t Trie) {
   223  	if db.accountTrieCache == nil {
   224  		return
   225  	}
   226  	tr := t.(*trie.SecureTrie)
   227  	db.accountTrieCache.Add(root, tr.ResetCopy())
   228  }
   229  
   230  func (db *cachingDB) CacheStorage(addrHash common.Hash, root common.Hash, t Trie) {
   231  	if db.storageTrieCache == nil {
   232  		return
   233  	}
   234  	tr := t.(*trie.SecureTrie)
   235  	if tries, exist := db.storageTrieCache.Get(addrHash); exist {
   236  		triesArray := tries.([3]*triePair)
   237  		newTriesArray := [3]*triePair{
   238  			{root: root, trie: tr.ResetCopy()},
   239  			triesArray[0],
   240  			triesArray[1],
   241  		}
   242  		db.storageTrieCache.Add(addrHash, newTriesArray)
   243  	} else {
   244  		triesArray := [3]*triePair{{root: root, trie: tr.ResetCopy()}, nil, nil}
   245  		db.storageTrieCache.Add(addrHash, triesArray)
   246  	}
   247  }
   248  
   249  func (db *cachingDB) Purge() {
   250  	if db.storageTrieCache != nil {
   251  		db.storageTrieCache.Purge()
   252  	}
   253  	if db.accountTrieCache != nil {
   254  		db.accountTrieCache.Purge()
   255  	}
   256  }
   257  
   258  // CopyTrie returns an independent copy of the given trie.
   259  func (db *cachingDB) CopyTrie(t Trie) Trie {
   260  	switch t := t.(type) {
   261  	case *trie.SecureTrie:
   262  		return t.Copy()
   263  	default:
   264  		panic(fmt.Errorf("unknown trie type %T", t))
   265  	}
   266  }
   267  
   268  // ContractCode retrieves a particular contract's code.
   269  func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
   270  	if cached, ok := db.codeCache.Get(codeHash); ok {
   271  		code := cached.([]byte)
   272  		if len(code) > 0 {
   273  			return code, nil
   274  		}
   275  	}
   276  	code := rawdb.ReadCode(db.db.DiskDB(), codeHash)
   277  	if len(code) > 0 {
   278  
   279  		db.codeCache.Add(codeHash, code)
   280  		db.codeSizeCache.Add(codeHash, len(code))
   281  		return code, nil
   282  	}
   283  	return nil, errors.New("not found")
   284  }
   285  
   286  // ContractCodeWithPrefix retrieves a particular contract's code. If the
   287  // code can't be found in the cache, then check the existence with **new**
   288  // db scheme.
   289  func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]byte, error) {
   290  	if cached, ok := db.codeCache.Get(codeHash); ok {
   291  		code := cached.([]byte)
   292  		if len(code) > 0 {
   293  			return code, nil
   294  		}
   295  	}
   296  	code := rawdb.ReadCodeWithPrefix(db.db.DiskDB(), codeHash)
   297  	if len(code) > 0 {
   298  		db.codeCache.Add(codeHash, code)
   299  		db.codeSizeCache.Add(codeHash, len(code))
   300  		return code, nil
   301  	}
   302  	return nil, errors.New("not found")
   303  }
   304  
   305  // ContractCodeSize retrieves a particular contracts code's size.
   306  func (db *cachingDB) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) {
   307  	if cached, ok := db.codeSizeCache.Get(codeHash); ok {
   308  		return cached.(int), nil
   309  	}
   310  	code, err := db.ContractCode(addrHash, codeHash)
   311  	return len(code), err
   312  }
   313  
   314  // TrieDB retrieves any intermediate trie-node caching layer.
   315  func (db *cachingDB) TrieDB() *trie.Database {
   316  	return db.db
   317  }