github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/core/state/statedb.go (about)

     1  // Copyright 2014 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 provides a caching layer atop the Ethereum state trie.
    18  package state
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"math/big"
    24  	"runtime"
    25  	"sort"
    26  	"sync"
    27  	"time"
    28  
    29  	"github.com/fff-chain/go-fff/common"
    30  	"github.com/fff-chain/go-fff/common/gopool"
    31  	"github.com/fff-chain/go-fff/core/rawdb"
    32  	"github.com/fff-chain/go-fff/core/state/snapshot"
    33  	"github.com/fff-chain/go-fff/core/types"
    34  	"github.com/fff-chain/go-fff/crypto"
    35  	"github.com/fff-chain/go-fff/ethdb"
    36  	"github.com/fff-chain/go-fff/log"
    37  	"github.com/fff-chain/go-fff/metrics"
    38  	"github.com/fff-chain/go-fff/rlp"
    39  	"github.com/fff-chain/go-fff/trie"
    40  )
    41  
    42  const (
    43  	preLoadLimit      = 128
    44  	defaultNumOfSlots = 100
    45  )
    46  
    47  type revision struct {
    48  	id           int
    49  	journalIndex int
    50  }
    51  
    52  var (
    53  	// emptyRoot is the known root hash of an empty trie.
    54  	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
    55  
    56  	emptyAddr = crypto.Keccak256Hash(common.Address{}.Bytes())
    57  )
    58  
    59  type proofList [][]byte
    60  
    61  func (n *proofList) Put(key []byte, value []byte) error {
    62  	*n = append(*n, value)
    63  	return nil
    64  }
    65  
    66  func (n *proofList) Delete(key []byte) error {
    67  	panic("not supported")
    68  }
    69  
    70  // StateDB structs within the ethereum protocol are used to store anything
    71  // within the merkle trie. StateDBs take care of caching and storing
    72  // nested states. It's the general query interface to retrieve:
    73  // * Contracts
    74  // * Accounts
    75  type StateDB struct {
    76  	db             Database
    77  	prefetcherLock sync.Mutex
    78  	prefetcher     *triePrefetcher
    79  	originalRoot   common.Hash // The pre-state root, before any changes were made
    80  	expectedRoot   common.Hash // The state root in the block header
    81  	stateRoot      common.Hash // The calculation result of IntermediateRoot
    82  
    83  	trie           Trie
    84  	hasher         crypto.KeccakState
    85  	diffLayer      *types.DiffLayer
    86  	diffTries      map[common.Address]Trie
    87  	diffCode       map[common.Hash][]byte
    88  	lightProcessed bool
    89  	fullProcessed  bool
    90  	pipeCommit     bool
    91  
    92  	snapMux       sync.Mutex
    93  	snaps         *snapshot.Tree
    94  	snap          snapshot.Snapshot
    95  	snapDestructs map[common.Address]struct{}
    96  	snapAccounts  map[common.Address][]byte
    97  	snapStorage   map[common.Address]map[string][]byte
    98  
    99  	// This map holds 'live' objects, which will get modified while processing a state transition.
   100  	stateObjects        map[common.Address]*StateObject
   101  	stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie
   102  	stateObjectsDirty   map[common.Address]struct{} // State objects modified in the current execution
   103  
   104  	// DB error.
   105  	// State objects are used by the consensus core and VM which are
   106  	// unable to deal with database-level errors. Any error that occurs
   107  	// during a database read is memoized here and will eventually be returned
   108  	// by StateDB.Commit.
   109  	dbErr error
   110  
   111  	// The refund counter, also used by state transitioning.
   112  	refund uint64
   113  
   114  	thash, bhash common.Hash
   115  	txIndex      int
   116  	logs         map[common.Hash][]*types.Log
   117  	logSize      uint
   118  
   119  	preimages map[common.Hash][]byte
   120  
   121  	// Per-transaction access list
   122  	accessList *accessList
   123  
   124  	// Journal of state modifications. This is the backbone of
   125  	// Snapshot and RevertToSnapshot.
   126  	journal        *journal
   127  	validRevisions []revision
   128  	nextRevisionId int
   129  
   130  	// Measurements gathered during execution for debugging purposes
   131  	MetricsMux           sync.Mutex
   132  	AccountReads         time.Duration
   133  	AccountHashes        time.Duration
   134  	AccountUpdates       time.Duration
   135  	AccountCommits       time.Duration
   136  	StorageReads         time.Duration
   137  	StorageHashes        time.Duration
   138  	StorageUpdates       time.Duration
   139  	StorageCommits       time.Duration
   140  	SnapshotAccountReads time.Duration
   141  	SnapshotStorageReads time.Duration
   142  	SnapshotCommits      time.Duration
   143  }
   144  
   145  // New creates a new state from a given trie.
   146  func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
   147  	return newStateDB(root, db, snaps)
   148  }
   149  
   150  func newStateDB(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
   151  	sdb := &StateDB{
   152  		db:                  db,
   153  		originalRoot:        root,
   154  		snaps:               snaps,
   155  		stateObjects:        make(map[common.Address]*StateObject, defaultNumOfSlots),
   156  		stateObjectsPending: make(map[common.Address]struct{}, defaultNumOfSlots),
   157  		stateObjectsDirty:   make(map[common.Address]struct{}, defaultNumOfSlots),
   158  		logs:                make(map[common.Hash][]*types.Log, defaultNumOfSlots),
   159  		preimages:           make(map[common.Hash][]byte),
   160  		journal:             newJournal(),
   161  		hasher:              crypto.NewKeccakState(),
   162  	}
   163  	if sdb.snaps != nil {
   164  		if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil {
   165  			sdb.snapDestructs = make(map[common.Address]struct{})
   166  			sdb.snapAccounts = make(map[common.Address][]byte)
   167  			sdb.snapStorage = make(map[common.Address]map[string][]byte)
   168  		}
   169  	}
   170  
   171  	snapVerified := sdb.snap != nil && sdb.snap.Verified()
   172  	tr, err := db.OpenTrie(root)
   173  	// return error when 1. failed to open trie and 2. the snap is nil or the snap is not nil and done verification
   174  	if err != nil && (sdb.snap == nil || snapVerified) {
   175  		return nil, err
   176  	}
   177  	sdb.trie = tr
   178  	return sdb, nil
   179  }
   180  
   181  // StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
   182  // state trie concurrently while the state is mutated so that when we reach the
   183  // commit phase, most of the needed data is already hot.
   184  func (s *StateDB) StartPrefetcher(namespace string) {
   185  	s.prefetcherLock.Lock()
   186  	defer s.prefetcherLock.Unlock()
   187  	if s.prefetcher != nil {
   188  		s.prefetcher.close()
   189  		s.prefetcher = nil
   190  	}
   191  	if s.snap != nil {
   192  		s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
   193  	}
   194  }
   195  
   196  // StopPrefetcher terminates a running prefetcher and reports any leftover stats
   197  // from the gathered metrics.
   198  func (s *StateDB) StopPrefetcher() {
   199  	s.prefetcherLock.Lock()
   200  	defer s.prefetcherLock.Unlock()
   201  	if s.prefetcher != nil {
   202  		s.prefetcher.close()
   203  		s.prefetcher = nil
   204  	}
   205  }
   206  
   207  // Mark that the block is processed by diff layer
   208  func (s *StateDB) SetExpectedStateRoot(root common.Hash) {
   209  	s.expectedRoot = root
   210  }
   211  
   212  // Mark that the block is processed by diff layer
   213  func (s *StateDB) MarkLightProcessed() {
   214  	s.lightProcessed = true
   215  }
   216  
   217  // Enable the pipeline commit function of statedb
   218  func (s *StateDB) EnablePipeCommit() {
   219  	if s.snap != nil {
   220  		s.pipeCommit = true
   221  	}
   222  }
   223  
   224  // Mark that the block is full processed
   225  func (s *StateDB) MarkFullProcessed() {
   226  	s.fullProcessed = true
   227  }
   228  
   229  func (s *StateDB) IsLightProcessed() bool {
   230  	return s.lightProcessed
   231  }
   232  
   233  // setError remembers the first non-nil error it is called with.
   234  func (s *StateDB) setError(err error) {
   235  	if s.dbErr == nil {
   236  		s.dbErr = err
   237  	}
   238  }
   239  
   240  func (s *StateDB) Error() error {
   241  	return s.dbErr
   242  }
   243  
   244  // Not thread safe
   245  func (s *StateDB) Trie() (Trie, error) {
   246  	if s.trie == nil {
   247  		err := s.WaitPipeVerification()
   248  		if err != nil {
   249  			return nil, err
   250  		}
   251  		tr, err := s.db.OpenTrie(s.originalRoot)
   252  		if err != nil {
   253  			return nil, err
   254  		}
   255  		s.trie = tr
   256  	}
   257  	return s.trie, nil
   258  }
   259  
   260  func (s *StateDB) SetDiff(diffLayer *types.DiffLayer, diffTries map[common.Address]Trie, diffCode map[common.Hash][]byte) {
   261  	s.diffLayer, s.diffTries, s.diffCode = diffLayer, diffTries, diffCode
   262  }
   263  
   264  func (s *StateDB) SetSnapData(snapDestructs map[common.Address]struct{}, snapAccounts map[common.Address][]byte,
   265  	snapStorage map[common.Address]map[string][]byte) {
   266  	s.snapDestructs, s.snapAccounts, s.snapStorage = snapDestructs, snapAccounts, snapStorage
   267  }
   268  
   269  func (s *StateDB) AddLog(log *types.Log) {
   270  	s.journal.append(addLogChange{txhash: s.thash})
   271  
   272  	log.TxHash = s.thash
   273  	log.BlockHash = s.bhash
   274  	log.TxIndex = uint(s.txIndex)
   275  	log.Index = s.logSize
   276  	s.logs[s.thash] = append(s.logs[s.thash], log)
   277  	s.logSize++
   278  }
   279  
   280  func (s *StateDB) GetLogs(hash common.Hash) []*types.Log {
   281  	return s.logs[hash]
   282  }
   283  
   284  func (s *StateDB) Logs() []*types.Log {
   285  	var logs []*types.Log
   286  	for _, lgs := range s.logs {
   287  		logs = append(logs, lgs...)
   288  	}
   289  	return logs
   290  }
   291  
   292  // AddPreimage records a SHA3 preimage seen by the VM.
   293  func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
   294  	if _, ok := s.preimages[hash]; !ok {
   295  		s.journal.append(addPreimageChange{hash: hash})
   296  		pi := make([]byte, len(preimage))
   297  		copy(pi, preimage)
   298  		s.preimages[hash] = pi
   299  	}
   300  }
   301  
   302  // Preimages returns a list of SHA3 preimages that have been submitted.
   303  func (s *StateDB) Preimages() map[common.Hash][]byte {
   304  	return s.preimages
   305  }
   306  
   307  // AddRefund adds gas to the refund counter
   308  func (s *StateDB) AddRefund(gas uint64) {
   309  	s.journal.append(refundChange{prev: s.refund})
   310  	s.refund += gas
   311  }
   312  
   313  // SubRefund removes gas from the refund counter.
   314  // This method will panic if the refund counter goes below zero
   315  func (s *StateDB) SubRefund(gas uint64) {
   316  	s.journal.append(refundChange{prev: s.refund})
   317  	if gas > s.refund {
   318  		panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
   319  	}
   320  	s.refund -= gas
   321  }
   322  
   323  // Exist reports whether the given account address exists in the state.
   324  // Notably this also returns true for suicided accounts.
   325  func (s *StateDB) Exist(addr common.Address) bool {
   326  	return s.getStateObject(addr) != nil
   327  }
   328  
   329  // Empty returns whether the state object is either non-existent
   330  // or empty according to the EIP161 specification (balance = nonce = code = 0)
   331  func (s *StateDB) Empty(addr common.Address) bool {
   332  	so := s.getStateObject(addr)
   333  	return so == nil || so.empty()
   334  }
   335  
   336  // GetBalance retrieves the balance from the given address or 0 if object not found
   337  func (s *StateDB) GetBalance(addr common.Address) *big.Int {
   338  	stateObject := s.getStateObject(addr)
   339  	if stateObject != nil {
   340  		return stateObject.Balance()
   341  	}
   342  	return common.Big0
   343  }
   344  
   345  func (s *StateDB) GetNonce(addr common.Address) uint64 {
   346  	stateObject := s.getStateObject(addr)
   347  	if stateObject != nil {
   348  		return stateObject.Nonce()
   349  	}
   350  
   351  	return 0
   352  }
   353  
   354  // TxIndex returns the current transaction index set by Prepare.
   355  func (s *StateDB) TxIndex() int {
   356  	return s.txIndex
   357  }
   358  
   359  // BlockHash returns the current block hash set by Prepare.
   360  func (s *StateDB) BlockHash() common.Hash {
   361  	return s.bhash
   362  }
   363  
   364  func (s *StateDB) GetCode(addr common.Address) []byte {
   365  	stateObject := s.getStateObject(addr)
   366  	if stateObject != nil {
   367  		return stateObject.Code(s.db)
   368  	}
   369  	return nil
   370  }
   371  
   372  func (s *StateDB) GetCodeSize(addr common.Address) int {
   373  	stateObject := s.getStateObject(addr)
   374  	if stateObject != nil {
   375  		return stateObject.CodeSize(s.db)
   376  	}
   377  	return 0
   378  }
   379  
   380  func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {
   381  	stateObject := s.getStateObject(addr)
   382  	if stateObject == nil {
   383  		return common.Hash{}
   384  	}
   385  	return common.BytesToHash(stateObject.CodeHash())
   386  }
   387  
   388  // GetState retrieves a value from the given account's storage trie.
   389  func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
   390  	stateObject := s.getStateObject(addr)
   391  	if stateObject != nil {
   392  		return stateObject.GetState(s.db, hash)
   393  	}
   394  	return common.Hash{}
   395  }
   396  
   397  // GetProof returns the Merkle proof for a given account.
   398  func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) {
   399  	return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes()))
   400  }
   401  
   402  // GetProofByHash returns the Merkle proof for a given account.
   403  func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) {
   404  	var proof proofList
   405  	if _, err := s.Trie(); err != nil {
   406  		return nil, err
   407  	}
   408  	err := s.trie.Prove(addrHash[:], 0, &proof)
   409  	return proof, err
   410  }
   411  
   412  // GetStorageProof returns the Merkle proof for given storage slot.
   413  func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
   414  	var proof proofList
   415  	trie := s.StorageTrie(a)
   416  	if trie == nil {
   417  		return proof, errors.New("storage trie for requested address does not exist")
   418  	}
   419  	err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
   420  	return proof, err
   421  }
   422  
   423  // GetStorageProofByHash returns the Merkle proof for given storage slot.
   424  func (s *StateDB) GetStorageProofByHash(a common.Address, key common.Hash) ([][]byte, error) {
   425  	var proof proofList
   426  	trie := s.StorageTrie(a)
   427  	if trie == nil {
   428  		return proof, errors.New("storage trie for requested address does not exist")
   429  	}
   430  	err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
   431  	return proof, err
   432  }
   433  
   434  // GetCommittedState retrieves a value from the given account's committed storage trie.
   435  func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
   436  	stateObject := s.getStateObject(addr)
   437  	if stateObject != nil {
   438  		return stateObject.GetCommittedState(s.db, hash)
   439  	}
   440  	return common.Hash{}
   441  }
   442  
   443  // Database retrieves the low level database supporting the lower level trie ops.
   444  func (s *StateDB) Database() Database {
   445  	return s.db
   446  }
   447  
   448  // StorageTrie returns the storage trie of an account.
   449  // The return value is a copy and is nil for non-existent accounts.
   450  func (s *StateDB) StorageTrie(addr common.Address) Trie {
   451  	stateObject := s.getStateObject(addr)
   452  	if stateObject == nil {
   453  		return nil
   454  	}
   455  	cpy := stateObject.deepCopy(s)
   456  	cpy.updateTrie(s.db)
   457  	return cpy.getTrie(s.db)
   458  }
   459  
   460  func (s *StateDB) HasSuicided(addr common.Address) bool {
   461  	stateObject := s.getStateObject(addr)
   462  	if stateObject != nil {
   463  		return stateObject.suicided
   464  	}
   465  	return false
   466  }
   467  
   468  /*
   469   * SETTERS
   470   */
   471  
   472  // AddBalance adds amount to the account associated with addr.
   473  func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
   474  	stateObject := s.GetOrNewStateObject(addr)
   475  	if stateObject != nil {
   476  		stateObject.AddBalance(amount)
   477  	}
   478  }
   479  
   480  // SubBalance subtracts amount from the account associated with addr.
   481  func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
   482  	stateObject := s.GetOrNewStateObject(addr)
   483  	if stateObject != nil {
   484  		stateObject.SubBalance(amount)
   485  	}
   486  }
   487  
   488  func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
   489  	stateObject := s.GetOrNewStateObject(addr)
   490  	if stateObject != nil {
   491  		stateObject.SetBalance(amount)
   492  	}
   493  }
   494  
   495  func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
   496  	stateObject := s.GetOrNewStateObject(addr)
   497  	if stateObject != nil {
   498  		stateObject.SetNonce(nonce)
   499  	}
   500  }
   501  
   502  func (s *StateDB) SetCode(addr common.Address, code []byte) {
   503  	stateObject := s.GetOrNewStateObject(addr)
   504  	if stateObject != nil {
   505  		stateObject.SetCode(crypto.Keccak256Hash(code), code)
   506  	}
   507  }
   508  
   509  func (s *StateDB) SetState(addr common.Address, key, value common.Hash) {
   510  	stateObject := s.GetOrNewStateObject(addr)
   511  	if stateObject != nil {
   512  		stateObject.SetState(s.db, key, value)
   513  	}
   514  }
   515  
   516  // SetStorage replaces the entire storage for the specified account with given
   517  // storage. This function should only be used for debugging.
   518  func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
   519  	stateObject := s.GetOrNewStateObject(addr)
   520  	if stateObject != nil {
   521  		stateObject.SetStorage(storage)
   522  	}
   523  }
   524  
   525  // Suicide marks the given account as suicided.
   526  // This clears the account balance.
   527  //
   528  // The account's state object is still available until the state is committed,
   529  // getStateObject will return a non-nil account after Suicide.
   530  func (s *StateDB) Suicide(addr common.Address) bool {
   531  	stateObject := s.getStateObject(addr)
   532  	if stateObject == nil {
   533  		return false
   534  	}
   535  	s.journal.append(suicideChange{
   536  		account:     &addr,
   537  		prev:        stateObject.suicided,
   538  		prevbalance: new(big.Int).Set(stateObject.Balance()),
   539  	})
   540  	stateObject.markSuicided()
   541  	stateObject.data.Balance = new(big.Int)
   542  
   543  	return true
   544  }
   545  
   546  //
   547  // Setting, updating & deleting state object methods.
   548  //
   549  
   550  // updateStateObject writes the given object to the trie.
   551  func (s *StateDB) updateStateObject(obj *StateObject) {
   552  	// Track the amount of time wasted on updating the account from the trie
   553  	if metrics.EnabledExpensive {
   554  		defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
   555  	}
   556  	// Encode the account and update the account trie
   557  	addr := obj.Address()
   558  	data := obj.encodeData
   559  	var err error
   560  	if data == nil {
   561  		data, err = rlp.EncodeToBytes(obj)
   562  		if err != nil {
   563  			panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
   564  		}
   565  	}
   566  	if err = s.trie.TryUpdate(addr[:], data); err != nil {
   567  		s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
   568  	}
   569  }
   570  
   571  // deleteStateObject removes the given object from the state trie.
   572  func (s *StateDB) deleteStateObject(obj *StateObject) {
   573  	// Track the amount of time wasted on deleting the account from the trie
   574  	if metrics.EnabledExpensive {
   575  		defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
   576  	}
   577  	// Delete the account from the trie
   578  	addr := obj.Address()
   579  	if err := s.trie.TryDelete(addr[:]); err != nil {
   580  		s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
   581  	}
   582  }
   583  
   584  // getStateObject retrieves a state object given by the address, returning nil if
   585  // the object is not found or was deleted in this execution context. If you need
   586  // to differentiate between non-existent/just-deleted, use getDeletedStateObject.
   587  func (s *StateDB) getStateObject(addr common.Address) *StateObject {
   588  	if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted {
   589  		return obj
   590  	}
   591  	return nil
   592  }
   593  
   594  func (s *StateDB) TryPreload(block *types.Block, signer types.Signer) {
   595  	accounts := make(map[common.Address]bool, block.Transactions().Len())
   596  	accountsSlice := make([]common.Address, 0, block.Transactions().Len())
   597  	for _, tx := range block.Transactions() {
   598  		from, err := types.Sender(signer, tx)
   599  		if err != nil {
   600  			break
   601  		}
   602  		accounts[from] = true
   603  		if tx.To() != nil {
   604  			accounts[*tx.To()] = true
   605  		}
   606  	}
   607  	for account := range accounts {
   608  		accountsSlice = append(accountsSlice, account)
   609  	}
   610  	if len(accountsSlice) >= preLoadLimit && len(accountsSlice) > runtime.NumCPU() {
   611  		objsChan := make(chan []*StateObject, runtime.NumCPU())
   612  		for i := 0; i < runtime.NumCPU(); i++ {
   613  			start := i * len(accountsSlice) / runtime.NumCPU()
   614  			end := (i + 1) * len(accountsSlice) / runtime.NumCPU()
   615  			if i+1 == runtime.NumCPU() {
   616  				end = len(accountsSlice)
   617  			}
   618  			go func(start, end int) {
   619  				objs := s.preloadStateObject(accountsSlice[start:end])
   620  				objsChan <- objs
   621  			}(start, end)
   622  		}
   623  		for i := 0; i < runtime.NumCPU(); i++ {
   624  			objs := <-objsChan
   625  			for _, obj := range objs {
   626  				s.SetStateObject(obj)
   627  			}
   628  		}
   629  	}
   630  }
   631  
   632  func (s *StateDB) preloadStateObject(address []common.Address) []*StateObject {
   633  	// Prefer live objects if any is available
   634  	if s.snap == nil {
   635  		return nil
   636  	}
   637  	hasher := crypto.NewKeccakState()
   638  	objs := make([]*StateObject, 0, len(address))
   639  	for _, addr := range address {
   640  		// If no live objects are available, attempt to use snapshots
   641  		if acc, err := s.snap.Account(crypto.HashData(hasher, addr.Bytes())); err == nil {
   642  			if acc == nil {
   643  				continue
   644  			}
   645  			data := &Account{
   646  				Nonce:    acc.Nonce,
   647  				Balance:  acc.Balance,
   648  				CodeHash: acc.CodeHash,
   649  				Root:     common.BytesToHash(acc.Root),
   650  			}
   651  			if len(data.CodeHash) == 0 {
   652  				data.CodeHash = emptyCodeHash
   653  			}
   654  			if data.Root == (common.Hash{}) {
   655  				data.Root = emptyRoot
   656  			}
   657  			// Insert into the live set
   658  			obj := newObject(s, addr, *data)
   659  			objs = append(objs, obj)
   660  		}
   661  		// Do not enable this feature when snapshot is not enabled.
   662  	}
   663  	return objs
   664  }
   665  
   666  // getDeletedStateObject is similar to getStateObject, but instead of returning
   667  // nil for a deleted state object, it returns the actual object with the deleted
   668  // flag set. This is needed by the state journal to revert to the correct s-
   669  // destructed object instead of wiping all knowledge about the state object.
   670  func (s *StateDB) getDeletedStateObject(addr common.Address) *StateObject {
   671  	// Prefer live objects if any is available
   672  	if obj := s.stateObjects[addr]; obj != nil {
   673  		return obj
   674  	}
   675  	// If no live objects are available, attempt to use snapshots
   676  	var (
   677  		data *Account
   678  		err  error
   679  	)
   680  	if s.snap != nil {
   681  		if metrics.EnabledExpensive {
   682  			defer func(start time.Time) { s.SnapshotAccountReads += time.Since(start) }(time.Now())
   683  		}
   684  		var acc *snapshot.Account
   685  		if acc, err = s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())); err == nil {
   686  			if acc == nil {
   687  				return nil
   688  			}
   689  			data = &Account{
   690  				Nonce:    acc.Nonce,
   691  				Balance:  acc.Balance,
   692  				CodeHash: acc.CodeHash,
   693  				Root:     common.BytesToHash(acc.Root),
   694  			}
   695  			if len(data.CodeHash) == 0 {
   696  				data.CodeHash = emptyCodeHash
   697  			}
   698  			if data.Root == (common.Hash{}) {
   699  				data.Root = emptyRoot
   700  			}
   701  		}
   702  	}
   703  	// If snapshot unavailable or reading from it failed, load from the database
   704  	if s.snap == nil || err != nil {
   705  		if s.trie == nil {
   706  			tr, err := s.db.OpenTrie(s.originalRoot)
   707  			if err != nil {
   708  				s.setError(fmt.Errorf("failed to open trie tree"))
   709  				return nil
   710  			}
   711  			s.trie = tr
   712  		}
   713  		if metrics.EnabledExpensive {
   714  			defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
   715  		}
   716  		enc, err := s.trie.TryGet(addr.Bytes())
   717  		if err != nil {
   718  			s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr.Bytes(), err))
   719  			return nil
   720  		}
   721  		if len(enc) == 0 {
   722  			return nil
   723  		}
   724  		data = new(Account)
   725  		if err := rlp.DecodeBytes(enc, data); err != nil {
   726  			log.Error("Failed to decode state object", "addr", addr, "err", err)
   727  			return nil
   728  		}
   729  	}
   730  	// Insert into the live set
   731  	obj := newObject(s, addr, *data)
   732  	s.SetStateObject(obj)
   733  	return obj
   734  }
   735  
   736  func (s *StateDB) SetStateObject(object *StateObject) {
   737  	s.stateObjects[object.Address()] = object
   738  }
   739  
   740  // GetOrNewStateObject retrieves a state object or create a new state object if nil.
   741  func (s *StateDB) GetOrNewStateObject(addr common.Address) *StateObject {
   742  	stateObject := s.getStateObject(addr)
   743  	if stateObject == nil {
   744  		stateObject, _ = s.createObject(addr)
   745  	}
   746  	return stateObject
   747  }
   748  
   749  // createObject creates a new state object. If there is an existing account with
   750  // the given address, it is overwritten and returned as the second return value.
   751  func (s *StateDB) createObject(addr common.Address) (newobj, prev *StateObject) {
   752  	prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!
   753  
   754  	var prevdestruct bool
   755  	if s.snap != nil && prev != nil {
   756  		_, prevdestruct = s.snapDestructs[prev.address]
   757  		if !prevdestruct {
   758  			s.snapDestructs[prev.address] = struct{}{}
   759  		}
   760  	}
   761  	newobj = newObject(s, addr, Account{})
   762  	newobj.setNonce(0) // sets the object to dirty
   763  	if prev == nil {
   764  		s.journal.append(createObjectChange{account: &addr})
   765  	} else {
   766  		s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct})
   767  	}
   768  	s.SetStateObject(newobj)
   769  	if prev != nil && !prev.deleted {
   770  		return newobj, prev
   771  	}
   772  	return newobj, nil
   773  }
   774  
   775  // CreateAccount explicitly creates a state object. If a state object with the address
   776  // already exists the balance is carried over to the new account.
   777  //
   778  // CreateAccount is called during the EVM CREATE operation. The situation might arise that
   779  // a contract does the following:
   780  //
   781  //   1. sends funds to sha(account ++ (nonce + 1))
   782  //   2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
   783  //
   784  // Carrying over the balance ensures that Ether doesn't disappear.
   785  func (s *StateDB) CreateAccount(addr common.Address) {
   786  	newObj, prev := s.createObject(addr)
   787  	if prev != nil {
   788  		newObj.setBalance(prev.data.Balance)
   789  	}
   790  }
   791  
   792  func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
   793  	so := db.getStateObject(addr)
   794  	if so == nil {
   795  		return nil
   796  	}
   797  	it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
   798  
   799  	for it.Next() {
   800  		key := common.BytesToHash(db.trie.GetKey(it.Key))
   801  		if value, dirty := so.dirtyStorage[key]; dirty {
   802  			if !cb(key, value) {
   803  				return nil
   804  			}
   805  			continue
   806  		}
   807  
   808  		if len(it.Value) > 0 {
   809  			_, content, _, err := rlp.Split(it.Value)
   810  			if err != nil {
   811  				return err
   812  			}
   813  			if !cb(key, common.BytesToHash(content)) {
   814  				return nil
   815  			}
   816  		}
   817  	}
   818  	return nil
   819  }
   820  
   821  // Copy creates a deep, independent copy of the state.
   822  // Snapshots of the copied state cannot be applied to the copy.
   823  func (s *StateDB) Copy() *StateDB {
   824  	// Copy all the basic fields, initialize the memory ones
   825  	state := &StateDB{
   826  		db:                  s.db,
   827  		trie:                s.db.CopyTrie(s.trie),
   828  		stateObjects:        make(map[common.Address]*StateObject, len(s.journal.dirties)),
   829  		stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
   830  		stateObjectsDirty:   make(map[common.Address]struct{}, len(s.journal.dirties)),
   831  		refund:              s.refund,
   832  		logs:                make(map[common.Hash][]*types.Log, len(s.logs)),
   833  		logSize:             s.logSize,
   834  		preimages:           make(map[common.Hash][]byte, len(s.preimages)),
   835  		journal:             newJournal(),
   836  		hasher:              crypto.NewKeccakState(),
   837  	}
   838  	// Copy the dirty states, logs, and preimages
   839  	for addr := range s.journal.dirties {
   840  		// As documented [here](https://github.com/fff-chain/go-fff/pull/16485#issuecomment-380438527),
   841  		// and in the Finalise-method, there is a case where an object is in the journal but not
   842  		// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
   843  		// nil
   844  		if object, exist := s.stateObjects[addr]; exist {
   845  			// Even though the original object is dirty, we are not copying the journal,
   846  			// so we need to make sure that anyside effect the journal would have caused
   847  			// during a commit (or similar op) is already applied to the copy.
   848  			state.stateObjects[addr] = object.deepCopy(state)
   849  
   850  			state.stateObjectsDirty[addr] = struct{}{}   // Mark the copy dirty to force internal (code/state) commits
   851  			state.stateObjectsPending[addr] = struct{}{} // Mark the copy pending to force external (account) commits
   852  		}
   853  	}
   854  	// Above, we don't copy the actual journal. This means that if the copy is copied, the
   855  	// loop above will be a no-op, since the copy's journal is empty.
   856  	// Thus, here we iterate over stateObjects, to enable copies of copies
   857  	for addr := range s.stateObjectsPending {
   858  		if _, exist := state.stateObjects[addr]; !exist {
   859  			state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
   860  		}
   861  		state.stateObjectsPending[addr] = struct{}{}
   862  	}
   863  	for addr := range s.stateObjectsDirty {
   864  		if _, exist := state.stateObjects[addr]; !exist {
   865  			state.stateObjects[addr] = s.stateObjects[addr].deepCopy(state)
   866  		}
   867  		state.stateObjectsDirty[addr] = struct{}{}
   868  	}
   869  	for hash, logs := range s.logs {
   870  		cpy := make([]*types.Log, len(logs))
   871  		for i, l := range logs {
   872  			cpy[i] = new(types.Log)
   873  			*cpy[i] = *l
   874  		}
   875  		state.logs[hash] = cpy
   876  	}
   877  	for hash, preimage := range s.preimages {
   878  		state.preimages[hash] = preimage
   879  	}
   880  	// Do we need to copy the access list? In practice: No. At the start of a
   881  	// transaction, the access list is empty. In practice, we only ever copy state
   882  	// _between_ transactions/blocks, never in the middle of a transaction.
   883  	// However, it doesn't cost us much to copy an empty list, so we do it anyway
   884  	// to not blow up if we ever decide copy it in the middle of a transaction
   885  	if s.accessList != nil {
   886  		state.accessList = s.accessList.Copy()
   887  	}
   888  
   889  	// If there's a prefetcher running, make an inactive copy of it that can
   890  	// only access data but does not actively preload (since the user will not
   891  	// know that they need to explicitly terminate an active copy).
   892  	if s.prefetcher != nil {
   893  		state.prefetcher = s.prefetcher.copy()
   894  	}
   895  	if s.snaps != nil {
   896  		// In order for the miner to be able to use and make additions
   897  		// to the snapshot tree, we need to copy that aswell.
   898  		// Otherwise, any block mined by ourselves will cause gaps in the tree,
   899  		// and force the miner to operate trie-backed only
   900  		state.snaps = s.snaps
   901  		state.snap = s.snap
   902  		// deep copy needed
   903  		state.snapDestructs = make(map[common.Address]struct{})
   904  		for k, v := range s.snapDestructs {
   905  			state.snapDestructs[k] = v
   906  		}
   907  		state.snapAccounts = make(map[common.Address][]byte)
   908  		for k, v := range s.snapAccounts {
   909  			state.snapAccounts[k] = v
   910  		}
   911  		state.snapStorage = make(map[common.Address]map[string][]byte)
   912  		for k, v := range s.snapStorage {
   913  			temp := make(map[string][]byte)
   914  			for kk, vv := range v {
   915  				temp[kk] = vv
   916  			}
   917  			state.snapStorage[k] = temp
   918  		}
   919  	}
   920  	return state
   921  }
   922  
   923  // Snapshot returns an identifier for the current revision of the state.
   924  func (s *StateDB) Snapshot() int {
   925  	id := s.nextRevisionId
   926  	s.nextRevisionId++
   927  	s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
   928  	return id
   929  }
   930  
   931  // RevertToSnapshot reverts all state changes made since the given revision.
   932  func (s *StateDB) RevertToSnapshot(revid int) {
   933  	// Find the snapshot in the stack of valid snapshots.
   934  	idx := sort.Search(len(s.validRevisions), func(i int) bool {
   935  		return s.validRevisions[i].id >= revid
   936  	})
   937  	if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
   938  		panic(fmt.Errorf("revision id %v cannot be reverted", revid))
   939  	}
   940  	snapshot := s.validRevisions[idx].journalIndex
   941  
   942  	// Replay the journal to undo changes and remove invalidated snapshots
   943  	s.journal.revert(s, snapshot)
   944  	s.validRevisions = s.validRevisions[:idx]
   945  }
   946  
   947  // GetRefund returns the current value of the refund counter.
   948  func (s *StateDB) GetRefund() uint64 {
   949  	return s.refund
   950  }
   951  
   952  // GetRefund returns the current value of the refund counter.
   953  func (s *StateDB) WaitPipeVerification() error {
   954  	// We need wait for the parent trie to commit
   955  	if s.snap != nil {
   956  		if valid := s.snap.WaitAndGetVerifyRes(); !valid {
   957  			return fmt.Errorf("verification on parent snap failed")
   958  		}
   959  	}
   960  	return nil
   961  }
   962  
   963  // Finalise finalises the state by removing the s destructed objects and clears
   964  // the journal as well as the refunds. Finalise, however, will not push any updates
   965  // into the tries just yet. Only IntermediateRoot or Commit will do that.
   966  func (s *StateDB) Finalise(deleteEmptyObjects bool) {
   967  	addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties))
   968  	for addr := range s.journal.dirties {
   969  		obj, exist := s.stateObjects[addr]
   970  		if !exist {
   971  			// ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
   972  			// That tx goes out of gas, and although the notion of 'touched' does not exist there, the
   973  			// touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
   974  			// it will persist in the journal even though the journal is reverted. In this special circumstance,
   975  			// it may exist in `s.journal.dirties` but not in `s.stateObjects`.
   976  			// Thus, we can safely ignore it here
   977  			continue
   978  		}
   979  		if obj.suicided || (deleteEmptyObjects && obj.empty()) {
   980  			obj.deleted = true
   981  
   982  			// If state snapshotting is active, also mark the destruction there.
   983  			// Note, we can't do this only at the end of a block because multiple
   984  			// transactions within the same block might self destruct and then
   985  			// ressurrect an account; but the snapshotter needs both events.
   986  			if s.snap != nil {
   987  				s.snapDestructs[obj.address] = struct{}{} // We need to maintain account deletions explicitly (will remain set indefinitely)
   988  				delete(s.snapAccounts, obj.address)       // Clear out any previously updated account data (may be recreated via a ressurrect)
   989  				delete(s.snapStorage, obj.address)        // Clear out any previously updated storage data (may be recreated via a ressurrect)
   990  			}
   991  		} else {
   992  			obj.finalise(true) // Prefetch slots in the background
   993  		}
   994  		if _, exist := s.stateObjectsPending[addr]; !exist {
   995  			s.stateObjectsPending[addr] = struct{}{}
   996  		}
   997  		if _, exist := s.stateObjectsDirty[addr]; !exist {
   998  			s.stateObjectsDirty[addr] = struct{}{}
   999  			// At this point, also ship the address off to the precacher. The precacher
  1000  			// will start loading tries, and when the change is eventually committed,
  1001  			// the commit-phase will be a lot faster
  1002  			addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure
  1003  		}
  1004  	}
  1005  	if s.prefetcher != nil && len(addressesToPrefetch) > 0 {
  1006  		s.prefetcher.prefetch(s.originalRoot, addressesToPrefetch, emptyAddr)
  1007  	}
  1008  	// Invalidate journal because reverting across transactions is not allowed.
  1009  	s.clearJournalAndRefund()
  1010  }
  1011  
  1012  // IntermediateRoot computes the current root hash of the state trie.
  1013  // It is called in between transactions to get the root hash that
  1014  // goes into transaction receipts.
  1015  func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
  1016  	if s.lightProcessed {
  1017  		s.StopPrefetcher()
  1018  		return s.trie.Hash()
  1019  	}
  1020  	// Finalise all the dirty storage states and write them into the tries
  1021  	s.Finalise(deleteEmptyObjects)
  1022  	s.AccountsIntermediateRoot()
  1023  	return s.StateIntermediateRoot()
  1024  }
  1025  
  1026  func (s *StateDB) AccountsIntermediateRoot() {
  1027  	tasks := make(chan func())
  1028  	finishCh := make(chan struct{})
  1029  	defer close(finishCh)
  1030  	wg := sync.WaitGroup{}
  1031  	for i := 0; i < runtime.NumCPU(); i++ {
  1032  		go func() {
  1033  			for {
  1034  				select {
  1035  				case task := <-tasks:
  1036  					task()
  1037  				case <-finishCh:
  1038  					return
  1039  				}
  1040  			}
  1041  		}()
  1042  	}
  1043  
  1044  	// Although naively it makes sense to retrieve the account trie and then do
  1045  	// the contract storage and account updates sequentially, that short circuits
  1046  	// the account prefetcher. Instead, let's process all the storage updates
  1047  	// first, giving the account prefeches just a few more milliseconds of time
  1048  	// to pull useful data from disk.
  1049  	for addr := range s.stateObjectsPending {
  1050  		if obj := s.stateObjects[addr]; !obj.deleted {
  1051  			wg.Add(1)
  1052  			tasks <- func() {
  1053  				obj.updateRoot(s.db)
  1054  
  1055  				// If state snapshotting is active, cache the data til commit. Note, this
  1056  				// update mechanism is not symmetric to the deletion, because whereas it is
  1057  				// enough to track account updates at commit time, deletions need tracking
  1058  				// at transaction boundary level to ensure we capture state clearing.
  1059  				if s.snap != nil && !obj.deleted {
  1060  					s.snapMux.Lock()
  1061  					// It is possible to add unnecessary change, but it is fine.
  1062  					s.snapAccounts[obj.address] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash)
  1063  					s.snapMux.Unlock()
  1064  				}
  1065  				data, err := rlp.EncodeToBytes(obj)
  1066  				if err != nil {
  1067  					panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
  1068  				}
  1069  				obj.encodeData = data
  1070  				wg.Done()
  1071  			}
  1072  		}
  1073  	}
  1074  	wg.Wait()
  1075  }
  1076  
  1077  func (s *StateDB) StateIntermediateRoot() common.Hash {
  1078  	// If there was a trie prefetcher operating, it gets aborted and irrevocably
  1079  	// modified after we start retrieving tries. Remove it from the statedb after
  1080  	// this round of use.
  1081  	//
  1082  	// This is weird pre-byzantium since the first tx runs with a prefetcher and
  1083  	// the remainder without, but pre-byzantium even the initial prefetcher is
  1084  	// useless, so no sleep lost.
  1085  	prefetcher := s.prefetcher
  1086  	defer func() {
  1087  		s.prefetcherLock.Lock()
  1088  		if s.prefetcher != nil {
  1089  			s.prefetcher.close()
  1090  			s.prefetcher = nil
  1091  		}
  1092  		// try not use defer inside defer
  1093  		s.prefetcherLock.Unlock()
  1094  	}()
  1095  
  1096  	// Now we're about to start to write changes to the trie. The trie is so far
  1097  	// _untouched_. We can check with the prefetcher, if it can give us a trie
  1098  	// which has the same root, but also has some content loaded into it.
  1099  	if prefetcher != nil {
  1100  		if trie := prefetcher.trie(s.originalRoot); trie != nil {
  1101  			s.trie = trie
  1102  		}
  1103  	}
  1104  	if s.trie == nil {
  1105  		tr, err := s.db.OpenTrie(s.originalRoot)
  1106  		if err != nil {
  1107  			panic(fmt.Sprintf("Failed to open trie tree %s", s.originalRoot))
  1108  		}
  1109  		s.trie = tr
  1110  	}
  1111  	usedAddrs := make([][]byte, 0, len(s.stateObjectsPending))
  1112  	for addr := range s.stateObjectsPending {
  1113  		if obj := s.stateObjects[addr]; obj.deleted {
  1114  			s.deleteStateObject(obj)
  1115  		} else {
  1116  			s.updateStateObject(obj)
  1117  		}
  1118  		usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure
  1119  	}
  1120  	if prefetcher != nil {
  1121  		prefetcher.used(s.originalRoot, usedAddrs)
  1122  	}
  1123  	if len(s.stateObjectsPending) > 0 {
  1124  		s.stateObjectsPending = make(map[common.Address]struct{})
  1125  	}
  1126  	// Track the amount of time wasted on hashing the account trie
  1127  	if metrics.EnabledExpensive {
  1128  		defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
  1129  	}
  1130  	root := s.trie.Hash()
  1131  	return root
  1132  }
  1133  
  1134  // Prepare sets the current transaction hash and index and block hash which is
  1135  // used when the EVM emits new state logs.
  1136  func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) {
  1137  	s.thash = thash
  1138  	s.bhash = bhash
  1139  	s.txIndex = ti
  1140  	s.accessList = nil
  1141  }
  1142  
  1143  func (s *StateDB) clearJournalAndRefund() {
  1144  	if len(s.journal.entries) > 0 {
  1145  		s.journal = newJournal()
  1146  		s.refund = 0
  1147  	}
  1148  	s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entires
  1149  }
  1150  
  1151  func (s *StateDB) LightCommit() (common.Hash, *types.DiffLayer, error) {
  1152  	codeWriter := s.db.TrieDB().DiskDB().NewBatch()
  1153  
  1154  	// light process already verified it, expectedRoot is trustworthy.
  1155  	root := s.expectedRoot
  1156  
  1157  	commitFuncs := []func() error{
  1158  		func() error {
  1159  			for codeHash, code := range s.diffCode {
  1160  				rawdb.WriteCode(codeWriter, codeHash, code)
  1161  				if codeWriter.ValueSize() >= ethdb.IdealBatchSize {
  1162  					if err := codeWriter.Write(); err != nil {
  1163  						return err
  1164  					}
  1165  					codeWriter.Reset()
  1166  				}
  1167  			}
  1168  			if codeWriter.ValueSize() > 0 {
  1169  				if err := codeWriter.Write(); err != nil {
  1170  					return err
  1171  				}
  1172  			}
  1173  			return nil
  1174  		},
  1175  		func() error {
  1176  			tasks := make(chan func())
  1177  			taskResults := make(chan error, len(s.diffTries))
  1178  			tasksNum := 0
  1179  			finishCh := make(chan struct{})
  1180  			defer close(finishCh)
  1181  			threads := gopool.Threads(len(s.diffTries))
  1182  
  1183  			for i := 0; i < threads; i++ {
  1184  				go func() {
  1185  					for {
  1186  						select {
  1187  						case task := <-tasks:
  1188  							task()
  1189  						case <-finishCh:
  1190  							return
  1191  						}
  1192  					}
  1193  				}()
  1194  			}
  1195  
  1196  			for account, diff := range s.diffTries {
  1197  				tmpAccount := account
  1198  				tmpDiff := diff
  1199  				tasks <- func() {
  1200  					root, err := tmpDiff.Commit(nil)
  1201  					if err != nil {
  1202  						taskResults <- err
  1203  						return
  1204  					}
  1205  					s.db.CacheStorage(crypto.Keccak256Hash(tmpAccount[:]), root, tmpDiff)
  1206  					taskResults <- nil
  1207  				}
  1208  				tasksNum++
  1209  			}
  1210  
  1211  			for i := 0; i < tasksNum; i++ {
  1212  				err := <-taskResults
  1213  				if err != nil {
  1214  					return err
  1215  				}
  1216  			}
  1217  
  1218  			// commit account trie
  1219  			var account Account
  1220  			root, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
  1221  				if err := rlp.DecodeBytes(leaf, &account); err != nil {
  1222  					return nil
  1223  				}
  1224  				if account.Root != emptyRoot {
  1225  					s.db.TrieDB().Reference(account.Root, parent)
  1226  				}
  1227  				return nil
  1228  			})
  1229  			if err != nil {
  1230  				return err
  1231  			}
  1232  			if root != emptyRoot {
  1233  				s.db.CacheAccount(root, s.trie)
  1234  			}
  1235  			return nil
  1236  		},
  1237  		func() error {
  1238  			if s.snap != nil {
  1239  				if metrics.EnabledExpensive {
  1240  					defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now())
  1241  				}
  1242  				// Only update if there's a state transition (skip empty Clique blocks)
  1243  				if parent := s.snap.Root(); parent != root {
  1244  					// for light commit, always do sync commit
  1245  					if err := s.snaps.Update(root, parent, s.snapDestructs, s.snapAccounts, s.snapStorage, nil); err != nil {
  1246  						log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
  1247  					}
  1248  					// Keep n diff layers in the memory
  1249  					// - head layer is paired with HEAD state
  1250  					// - head-1 layer is paired with HEAD-1 state
  1251  					// - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state
  1252  					if err := s.snaps.Cap(root, s.snaps.CapLimit()); err != nil {
  1253  						log.Warn("Failed to cap snapshot tree", "root", root, "layers", s.snaps.CapLimit(), "err", err)
  1254  					}
  1255  				}
  1256  			}
  1257  			return nil
  1258  		},
  1259  	}
  1260  	commitRes := make(chan error, len(commitFuncs))
  1261  	for _, f := range commitFuncs {
  1262  		tmpFunc := f
  1263  		go func() {
  1264  			commitRes <- tmpFunc()
  1265  		}()
  1266  	}
  1267  	for i := 0; i < len(commitFuncs); i++ {
  1268  		r := <-commitRes
  1269  		if r != nil {
  1270  			return common.Hash{}, nil, r
  1271  		}
  1272  	}
  1273  	s.snap, s.snapDestructs, s.snapAccounts, s.snapStorage = nil, nil, nil, nil
  1274  	s.diffTries, s.diffCode = nil, nil
  1275  	return root, s.diffLayer, nil
  1276  }
  1277  
  1278  // Commit writes the state to the underlying in-memory trie database.
  1279  func (s *StateDB) Commit(failPostCommitFunc func(), postCommitFuncs ...func() error) (common.Hash, *types.DiffLayer, error) {
  1280  	if s.dbErr != nil {
  1281  		return common.Hash{}, nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
  1282  	}
  1283  	// Finalize any pending changes and merge everything into the tries
  1284  	if s.lightProcessed {
  1285  		root, diff, err := s.LightCommit()
  1286  		if err != nil {
  1287  			return root, diff, err
  1288  		}
  1289  		for _, postFunc := range postCommitFuncs {
  1290  			err = postFunc()
  1291  			if err != nil {
  1292  				return root, diff, err
  1293  			}
  1294  		}
  1295  		return root, diff, nil
  1296  	}
  1297  	var diffLayer *types.DiffLayer
  1298  	var verified chan struct{}
  1299  	var snapUpdated chan struct{}
  1300  	if s.snap != nil {
  1301  		diffLayer = &types.DiffLayer{}
  1302  	}
  1303  	if s.pipeCommit {
  1304  		// async commit the MPT
  1305  		verified = make(chan struct{})
  1306  		snapUpdated = make(chan struct{})
  1307  	}
  1308  
  1309  	commmitTrie := func() error {
  1310  		commitErr := func() error {
  1311  			if s.stateRoot = s.StateIntermediateRoot(); s.fullProcessed && s.expectedRoot != s.stateRoot {
  1312  				return fmt.Errorf("invalid merkle root (remote: %x local: %x)", s.expectedRoot, s.stateRoot)
  1313  			}
  1314  			tasks := make(chan func())
  1315  			taskResults := make(chan error, len(s.stateObjectsDirty))
  1316  			tasksNum := 0
  1317  			finishCh := make(chan struct{})
  1318  
  1319  			threads := gopool.Threads(len(s.stateObjectsDirty))
  1320  			wg := sync.WaitGroup{}
  1321  			for i := 0; i < threads; i++ {
  1322  				wg.Add(1)
  1323  				go func() {
  1324  					defer wg.Done()
  1325  					for {
  1326  						select {
  1327  						case task := <-tasks:
  1328  							task()
  1329  						case <-finishCh:
  1330  							return
  1331  						}
  1332  					}
  1333  				}()
  1334  			}
  1335  
  1336  			if s.snap != nil {
  1337  				for addr := range s.stateObjectsDirty {
  1338  					if obj := s.stateObjects[addr]; !obj.deleted {
  1339  						if obj.code != nil && obj.dirtyCode {
  1340  							diffLayer.Codes = append(diffLayer.Codes, types.DiffCode{
  1341  								Hash: common.BytesToHash(obj.CodeHash()),
  1342  								Code: obj.code,
  1343  							})
  1344  						}
  1345  					}
  1346  				}
  1347  			}
  1348  
  1349  			for addr := range s.stateObjectsDirty {
  1350  				if obj := s.stateObjects[addr]; !obj.deleted {
  1351  					// Write any contract code associated with the state object
  1352  					tasks <- func() {
  1353  						// Write any storage changes in the state object to its storage trie
  1354  						if err := obj.CommitTrie(s.db); err != nil {
  1355  							taskResults <- err
  1356  						}
  1357  						taskResults <- nil
  1358  					}
  1359  					tasksNum++
  1360  				}
  1361  			}
  1362  
  1363  			for i := 0; i < tasksNum; i++ {
  1364  				err := <-taskResults
  1365  				if err != nil {
  1366  					close(finishCh)
  1367  					return err
  1368  				}
  1369  			}
  1370  			close(finishCh)
  1371  
  1372  			// The onleaf func is called _serially_, so we can reuse the same account
  1373  			// for unmarshalling every time.
  1374  			var account Account
  1375  			root, err := s.trie.Commit(func(_ [][]byte, _ []byte, leaf []byte, parent common.Hash) error {
  1376  				if err := rlp.DecodeBytes(leaf, &account); err != nil {
  1377  					return nil
  1378  				}
  1379  				if account.Root != emptyRoot {
  1380  					s.db.TrieDB().Reference(account.Root, parent)
  1381  				}
  1382  				return nil
  1383  			})
  1384  			if err != nil {
  1385  				return err
  1386  			}
  1387  			if root != emptyRoot {
  1388  				s.db.CacheAccount(root, s.trie)
  1389  			}
  1390  			for _, postFunc := range postCommitFuncs {
  1391  				err = postFunc()
  1392  				if err != nil {
  1393  					return err
  1394  				}
  1395  			}
  1396  			wg.Wait()
  1397  			return nil
  1398  		}()
  1399  
  1400  		if s.pipeCommit {
  1401  			if commitErr == nil {
  1402  				<-snapUpdated
  1403  				s.snaps.Snapshot(s.stateRoot).MarkValid()
  1404  			} else {
  1405  				// The blockchain will do the further rewind if write block not finish yet
  1406  				if failPostCommitFunc != nil {
  1407  					<-snapUpdated
  1408  					failPostCommitFunc()
  1409  				}
  1410  				log.Error("state verification failed", "err", commitErr)
  1411  			}
  1412  			close(verified)
  1413  		}
  1414  		return commitErr
  1415  	}
  1416  
  1417  	commitFuncs := []func() error{
  1418  		func() error {
  1419  			codeWriter := s.db.TrieDB().DiskDB().NewBatch()
  1420  			for addr := range s.stateObjectsDirty {
  1421  				if obj := s.stateObjects[addr]; !obj.deleted {
  1422  					if obj.code != nil && obj.dirtyCode {
  1423  						rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code)
  1424  						obj.dirtyCode = false
  1425  						if codeWriter.ValueSize() > ethdb.IdealBatchSize {
  1426  							if err := codeWriter.Write(); err != nil {
  1427  								return err
  1428  							}
  1429  							codeWriter.Reset()
  1430  						}
  1431  					}
  1432  				}
  1433  			}
  1434  			if codeWriter.ValueSize() > 0 {
  1435  				if err := codeWriter.Write(); err != nil {
  1436  					log.Crit("Failed to commit dirty codes", "error", err)
  1437  					return err
  1438  				}
  1439  			}
  1440  			return nil
  1441  		},
  1442  		func() error {
  1443  			// If snapshotting is enabled, update the snapshot tree with this new version
  1444  			if s.snap != nil {
  1445  				if metrics.EnabledExpensive {
  1446  					defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now())
  1447  				}
  1448  				if s.pipeCommit {
  1449  					defer close(snapUpdated)
  1450  				}
  1451  				// Only update if there's a state transition (skip empty Clique blocks)
  1452  				if parent := s.snap.Root(); parent != s.expectedRoot {
  1453  					if err := s.snaps.Update(s.expectedRoot, parent, s.snapDestructs, s.snapAccounts, s.snapStorage, verified); err != nil {
  1454  						log.Warn("Failed to update snapshot tree", "from", parent, "to", s.expectedRoot, "err", err)
  1455  					}
  1456  					// Keep n diff layers in the memory
  1457  					// - head layer is paired with HEAD state
  1458  					// - head-1 layer is paired with HEAD-1 state
  1459  					// - head-(n-1) layer(bottom-most diff layer) is paired with HEAD-(n-1)state
  1460  					go func() {
  1461  						if err := s.snaps.Cap(s.expectedRoot, s.snaps.CapLimit()); err != nil {
  1462  							log.Warn("Failed to cap snapshot tree", "root", s.expectedRoot, "layers", s.snaps.CapLimit(), "err", err)
  1463  						}
  1464  					}()
  1465  				}
  1466  			}
  1467  			return nil
  1468  		},
  1469  		func() error {
  1470  			if s.snap != nil {
  1471  				diffLayer.Destructs, diffLayer.Accounts, diffLayer.Storages = s.SnapToDiffLayer()
  1472  			}
  1473  			return nil
  1474  		},
  1475  	}
  1476  	if s.pipeCommit {
  1477  		go commmitTrie()
  1478  	} else {
  1479  		commitFuncs = append(commitFuncs, commmitTrie)
  1480  	}
  1481  	commitRes := make(chan error, len(commitFuncs))
  1482  	for _, f := range commitFuncs {
  1483  		tmpFunc := f
  1484  		go func() {
  1485  			commitRes <- tmpFunc()
  1486  		}()
  1487  	}
  1488  	for i := 0; i < len(commitFuncs); i++ {
  1489  		r := <-commitRes
  1490  		if r != nil {
  1491  			return common.Hash{}, nil, r
  1492  		}
  1493  	}
  1494  	root := s.stateRoot
  1495  	if s.pipeCommit {
  1496  		root = s.expectedRoot
  1497  	}
  1498  
  1499  	return root, diffLayer, nil
  1500  }
  1501  
  1502  func (s *StateDB) DiffLayerToSnap(diffLayer *types.DiffLayer) (map[common.Address]struct{}, map[common.Address][]byte, map[common.Address]map[string][]byte, error) {
  1503  	snapDestructs := make(map[common.Address]struct{})
  1504  	snapAccounts := make(map[common.Address][]byte)
  1505  	snapStorage := make(map[common.Address]map[string][]byte)
  1506  
  1507  	for _, des := range diffLayer.Destructs {
  1508  		snapDestructs[des] = struct{}{}
  1509  	}
  1510  	for _, account := range diffLayer.Accounts {
  1511  		snapAccounts[account.Account] = account.Blob
  1512  	}
  1513  	for _, storage := range diffLayer.Storages {
  1514  		// should never happen
  1515  		if len(storage.Keys) != len(storage.Vals) {
  1516  			return nil, nil, nil, errors.New("invalid diffLayer: length of keys and values mismatch")
  1517  		}
  1518  		snapStorage[storage.Account] = make(map[string][]byte, len(storage.Keys))
  1519  		n := len(storage.Keys)
  1520  		for i := 0; i < n; i++ {
  1521  			snapStorage[storage.Account][storage.Keys[i]] = storage.Vals[i]
  1522  		}
  1523  	}
  1524  	return snapDestructs, snapAccounts, snapStorage, nil
  1525  }
  1526  
  1527  func (s *StateDB) SnapToDiffLayer() ([]common.Address, []types.DiffAccount, []types.DiffStorage) {
  1528  	destructs := make([]common.Address, 0, len(s.snapDestructs))
  1529  	for account := range s.snapDestructs {
  1530  		destructs = append(destructs, account)
  1531  	}
  1532  	accounts := make([]types.DiffAccount, 0, len(s.snapAccounts))
  1533  	for accountHash, account := range s.snapAccounts {
  1534  		accounts = append(accounts, types.DiffAccount{
  1535  			Account: accountHash,
  1536  			Blob:    account,
  1537  		})
  1538  	}
  1539  	storages := make([]types.DiffStorage, 0, len(s.snapStorage))
  1540  	for accountHash, storage := range s.snapStorage {
  1541  		keys := make([]string, 0, len(storage))
  1542  		values := make([][]byte, 0, len(storage))
  1543  		for k, v := range storage {
  1544  			keys = append(keys, k)
  1545  			values = append(values, v)
  1546  		}
  1547  		storages = append(storages, types.DiffStorage{
  1548  			Account: accountHash,
  1549  			Keys:    keys,
  1550  			Vals:    values,
  1551  		})
  1552  	}
  1553  	return destructs, accounts, storages
  1554  }
  1555  
  1556  // PrepareAccessList handles the preparatory steps for executing a state transition with
  1557  // regards to both EIP-2929 and EIP-2930:
  1558  //
  1559  // - Add sender to access list (2929)
  1560  // - Add destination to access list (2929)
  1561  // - Add precompiles to access list (2929)
  1562  // - Add the contents of the optional tx access list (2930)
  1563  //
  1564  // This method should only be called if Yolov3/Berlin/2929+2930 is applicable at the current number.
  1565  func (s *StateDB) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
  1566  	s.AddAddressToAccessList(sender)
  1567  	if dst != nil {
  1568  		s.AddAddressToAccessList(*dst)
  1569  		// If it's a create-tx, the destination will be added inside evm.create
  1570  	}
  1571  	for _, addr := range precompiles {
  1572  		s.AddAddressToAccessList(addr)
  1573  	}
  1574  	for _, el := range list {
  1575  		s.AddAddressToAccessList(el.Address)
  1576  		for _, key := range el.StorageKeys {
  1577  			s.AddSlotToAccessList(el.Address, key)
  1578  		}
  1579  	}
  1580  }
  1581  
  1582  // AddAddressToAccessList adds the given address to the access list
  1583  func (s *StateDB) AddAddressToAccessList(addr common.Address) {
  1584  	if s.accessList == nil {
  1585  		s.accessList = newAccessList()
  1586  	}
  1587  	if s.accessList.AddAddress(addr) {
  1588  		s.journal.append(accessListAddAccountChange{&addr})
  1589  	}
  1590  }
  1591  
  1592  // AddSlotToAccessList adds the given (address, slot)-tuple to the access list
  1593  func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
  1594  	if s.accessList == nil {
  1595  		s.accessList = newAccessList()
  1596  	}
  1597  	addrMod, slotMod := s.accessList.AddSlot(addr, slot)
  1598  	if addrMod {
  1599  		// In practice, this should not happen, since there is no way to enter the
  1600  		// scope of 'address' without having the 'address' become already added
  1601  		// to the access list (via call-variant, create, etc).
  1602  		// Better safe than sorry, though
  1603  		s.journal.append(accessListAddAccountChange{&addr})
  1604  	}
  1605  	if slotMod {
  1606  		s.journal.append(accessListAddSlotChange{
  1607  			address: &addr,
  1608  			slot:    &slot,
  1609  		})
  1610  	}
  1611  }
  1612  
  1613  // AddressInAccessList returns true if the given address is in the access list.
  1614  func (s *StateDB) AddressInAccessList(addr common.Address) bool {
  1615  	if s.accessList == nil {
  1616  		return false
  1617  	}
  1618  	return s.accessList.ContainsAddress(addr)
  1619  }
  1620  
  1621  // SlotInAccessList returns true if the given (address, slot)-tuple is in the access list.
  1622  func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) {
  1623  	if s.accessList == nil {
  1624  		return false, false
  1625  	}
  1626  	return s.accessList.Contains(addr, slot)
  1627  }
  1628  
  1629  func (s *StateDB) GetDirtyAccounts() []common.Address {
  1630  	accounts := make([]common.Address, 0, len(s.stateObjectsDirty))
  1631  	for account := range s.stateObjectsDirty {
  1632  		accounts = append(accounts, account)
  1633  	}
  1634  	return accounts
  1635  }