github.com/baptiste-b-pegasys/quorum/v22@v22.4.2/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  	"sort"
    25  	"sync"
    26  	"time"
    27  
    28  	"github.com/ethereum/go-ethereum/common"
    29  	"github.com/ethereum/go-ethereum/core/rawdb"
    30  	"github.com/ethereum/go-ethereum/core/state/snapshot"
    31  	"github.com/ethereum/go-ethereum/core/types"
    32  	"github.com/ethereum/go-ethereum/crypto"
    33  	"github.com/ethereum/go-ethereum/log"
    34  	"github.com/ethereum/go-ethereum/metrics"
    35  	"github.com/ethereum/go-ethereum/rlp"
    36  	"github.com/ethereum/go-ethereum/trie"
    37  )
    38  
    39  type revision struct {
    40  	id           int
    41  	journalIndex int
    42  }
    43  
    44  var (
    45  	// emptyRoot is the known root hash of an empty trie.
    46  	emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
    47  )
    48  
    49  type proofList [][]byte
    50  
    51  func (n *proofList) Put(key []byte, value []byte) error {
    52  	*n = append(*n, value)
    53  	return nil
    54  }
    55  
    56  func (n *proofList) Delete(key []byte) error {
    57  	panic("not supported")
    58  }
    59  
    60  // StateDB structs within the ethereum protocol are used to store anything
    61  // within the merkle trie. StateDBs take care of caching and storing
    62  // nested states. It's the general query interface to retrieve:
    63  // * Contracts
    64  // * Accounts
    65  type StateDB struct {
    66  	db           Database
    67  	prefetcher   *triePrefetcher
    68  	originalRoot common.Hash // The pre-state root, before any changes were made
    69  	trie         Trie
    70  	hasher       crypto.KeccakState
    71  
    72  	snaps         *snapshot.Tree
    73  	snap          snapshot.Snapshot
    74  	snapDestructs map[common.Hash]struct{}
    75  	snapAccounts  map[common.Hash][]byte
    76  	snapStorage   map[common.Hash]map[common.Hash][]byte
    77  
    78  	mutex        sync.Mutex
    79  	journalMutex sync.Mutex
    80  
    81  	// Quorum - a trie to hold extra account information that cannot be stored in the accounts trie
    82  	accountExtraDataTrie Trie
    83  
    84  	// This map holds 'live' objects, which will get modified while processing a state transition.
    85  	stateObjects        map[common.Address]*stateObject
    86  	stateObjectsPending map[common.Address]struct{} // State objects finalized but not yet written to the trie
    87  	stateObjectsDirty   map[common.Address]struct{} // State objects modified in the current execution
    88  
    89  	// DB error.
    90  	// State objects are used by the consensus core and VM which are
    91  	// unable to deal with database-level errors. Any error that occurs
    92  	// during a database read is memoized here and will eventually be returned
    93  	// by StateDB.Commit.
    94  	dbErr error
    95  
    96  	// The refund counter, also used by state transitioning.
    97  	refund uint64
    98  
    99  	thash, bhash common.Hash
   100  	txIndex      int
   101  	logs         map[common.Hash][]*types.Log
   102  	logSize      uint
   103  
   104  	preimages map[common.Hash][]byte
   105  
   106  	// Per-transaction access list
   107  	accessList *accessList
   108  
   109  	// Journal of state modifications. This is the backbone of
   110  	// Snapshot and RevertToSnapshot.
   111  	journal        *journal
   112  	validRevisions []revision
   113  	nextRevisionId int
   114  
   115  	// Measurements gathered during execution for debugging purposes
   116  	AccountReads         time.Duration
   117  	AccountHashes        time.Duration
   118  	AccountUpdates       time.Duration
   119  	AccountCommits       time.Duration
   120  	StorageReads         time.Duration
   121  	StorageHashes        time.Duration
   122  	StorageUpdates       time.Duration
   123  	StorageCommits       time.Duration
   124  	SnapshotAccountReads time.Duration
   125  	SnapshotStorageReads time.Duration
   126  	SnapshotCommits      time.Duration
   127  }
   128  
   129  // New creates a new state from a given trie.
   130  func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) {
   131  	tr, err := db.OpenTrie(root)
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  
   136  	// Quorum - Privacy Enhancements - retrieve the privacy metadata root corresponding to the account state root
   137  	extraDataRoot := db.AccountExtraDataLinker().GetAccountExtraDataRoot(root)
   138  	log.Debug("Account Extra Data root", "hash", extraDataRoot)
   139  	accountExtraDataTrie, err := db.OpenTrie(extraDataRoot)
   140  	if err != nil {
   141  		return nil, fmt.Errorf("Unable to open privacy metadata trie: %v", err)
   142  	}
   143  	// End Quorum - Privacy Enhancements
   144  
   145  	sdb := &StateDB{
   146  		db:                  db,
   147  		trie:                tr,
   148  		originalRoot:        root,
   149  		snaps:               snaps,
   150  		stateObjects:        make(map[common.Address]*stateObject),
   151  		stateObjectsPending: make(map[common.Address]struct{}),
   152  		stateObjectsDirty:   make(map[common.Address]struct{}),
   153  		logs:                make(map[common.Hash][]*types.Log),
   154  		preimages:           make(map[common.Hash][]byte),
   155  		journal:             newJournal(),
   156  		accessList:          newAccessList(),
   157  		hasher:              crypto.NewKeccakState(),
   158  		// Quorum - Privacy Enhancements
   159  		accountExtraDataTrie: accountExtraDataTrie,
   160  	}
   161  	if sdb.snaps != nil {
   162  		if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap != nil {
   163  			sdb.snapDestructs = make(map[common.Hash]struct{})
   164  			sdb.snapAccounts = make(map[common.Hash][]byte)
   165  			sdb.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
   166  		}
   167  	}
   168  	return sdb, nil
   169  }
   170  
   171  // StartPrefetcher initializes a new trie prefetcher to pull in nodes from the
   172  // state trie concurrently while the state is mutated so that when we reach the
   173  // commit phase, most of the needed data is already hot.
   174  func (s *StateDB) StartPrefetcher(namespace string) {
   175  	if s.prefetcher != nil {
   176  		s.prefetcher.close()
   177  		s.prefetcher = nil
   178  	}
   179  	if s.snap != nil {
   180  		s.prefetcher = newTriePrefetcher(s.db, s.originalRoot, namespace)
   181  	}
   182  }
   183  
   184  // StopPrefetcher terminates a running prefetcher and reports any leftover stats
   185  // from the gathered metrics.
   186  func (s *StateDB) StopPrefetcher() {
   187  	if s.prefetcher != nil {
   188  		s.prefetcher.close()
   189  		s.prefetcher = nil
   190  	}
   191  }
   192  
   193  // setError remembers the first non-nil error it is called with.
   194  func (s *StateDB) setError(err error) {
   195  	if s.dbErr == nil {
   196  		s.dbErr = err
   197  	}
   198  }
   199  
   200  func (s *StateDB) Error() error {
   201  	return s.dbErr
   202  }
   203  
   204  func (s *StateDB) AddLog(log *types.Log) {
   205  	s.journal.append(addLogChange{txhash: s.thash})
   206  
   207  	log.TxHash = s.thash
   208  	log.BlockHash = s.bhash
   209  	log.TxIndex = uint(s.txIndex)
   210  	log.Index = s.logSize
   211  	s.logs[s.thash] = append(s.logs[s.thash], log)
   212  	s.logSize++
   213  }
   214  
   215  func (s *StateDB) GetLogs(hash common.Hash) []*types.Log {
   216  	return s.logs[hash]
   217  }
   218  
   219  func (s *StateDB) Logs() []*types.Log {
   220  	var logs []*types.Log
   221  	for _, lgs := range s.logs {
   222  		logs = append(logs, lgs...)
   223  	}
   224  	return logs
   225  }
   226  
   227  // AddPreimage records a SHA3 preimage seen by the VM.
   228  func (s *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
   229  	if _, ok := s.preimages[hash]; !ok {
   230  		s.journal.append(addPreimageChange{hash: hash})
   231  		pi := make([]byte, len(preimage))
   232  		copy(pi, preimage)
   233  		s.preimages[hash] = pi
   234  	}
   235  }
   236  
   237  // Preimages returns a list of SHA3 preimages that have been submitted.
   238  func (s *StateDB) Preimages() map[common.Hash][]byte {
   239  	return s.preimages
   240  }
   241  
   242  // AddRefund adds gas to the refund counter
   243  func (s *StateDB) AddRefund(gas uint64) {
   244  	s.journal.append(refundChange{prev: s.refund})
   245  	s.refund += gas
   246  }
   247  
   248  // SubRefund removes gas from the refund counter.
   249  // This method will panic if the refund counter goes below zero
   250  func (s *StateDB) SubRefund(gas uint64) {
   251  	s.journal.append(refundChange{prev: s.refund})
   252  	if gas > s.refund {
   253  		panic(fmt.Sprintf("Refund counter below zero (gas: %d > refund: %d)", gas, s.refund))
   254  	}
   255  	s.refund -= gas
   256  }
   257  
   258  // Exist reports whether the given account address exists in the state.
   259  // Notably this also returns true for suicided accounts.
   260  func (s *StateDB) Exist(addr common.Address) bool {
   261  	return s.getStateObject(addr) != nil
   262  }
   263  
   264  // Empty returns whether the state object is either non-existent
   265  // or empty according to the EIP161 specification (balance = nonce = code = 0)
   266  func (s *StateDB) Empty(addr common.Address) bool {
   267  	so := s.getStateObject(addr)
   268  	return so == nil || so.empty()
   269  }
   270  
   271  // GetBalance retrieves the balance from the given address or 0 if object not found
   272  func (s *StateDB) GetBalance(addr common.Address) *big.Int {
   273  	stateObject := s.getStateObject(addr)
   274  	if stateObject != nil {
   275  		return stateObject.Balance()
   276  	}
   277  	return common.Big0
   278  }
   279  
   280  func (s *StateDB) GetNonce(addr common.Address) uint64 {
   281  	stateObject := s.getStateObject(addr)
   282  	if stateObject != nil {
   283  		return stateObject.Nonce()
   284  	}
   285  
   286  	return 0
   287  }
   288  
   289  // Quorum
   290  func (s *StateDB) GetPrivacyMetadata(addr common.Address) (*PrivacyMetadata, error) {
   291  	stateObject := s.getStateObject(addr)
   292  	if stateObject != nil {
   293  		return stateObject.PrivacyMetadata()
   294  	}
   295  	return nil, nil
   296  }
   297  
   298  // Quorum
   299  func (s *StateDB) GetCommittedStatePrivacyMetadata(addr common.Address) (*PrivacyMetadata, error) {
   300  	stateObject := s.getStateObject(addr)
   301  	if stateObject != nil {
   302  		return stateObject.GetCommittedPrivacyMetadata()
   303  	}
   304  	return nil, nil
   305  }
   306  
   307  // Quorum
   308  func (self *StateDB) GetManagedParties(addr common.Address) ([]string, error) {
   309  	stateObject := self.getStateObject(addr)
   310  	if stateObject != nil {
   311  		return stateObject.ManagedParties()
   312  	}
   313  	return nil, nil
   314  }
   315  
   316  func (s *StateDB) GetRLPEncodedStateObject(addr common.Address) ([]byte, error) {
   317  	stateObject := s.getStateObject(addr)
   318  	if stateObject == nil {
   319  		return nil, fmt.Errorf("no state found for %s", addr.Hex())
   320  	}
   321  	// When calculating the execution hash or simulating the transaction the stateOject state is not committed/updated
   322  	// In order to reflect the updated state invoke stateObject.updateRoot on a copy of the state object.
   323  	if len(stateObject.pendingStorage) > 0 || len(stateObject.dirtyStorage) > 0 || stateObject.dirtyCode {
   324  		cpy := stateObject.deepCopy(s)
   325  		cpy.updateRoot(s.db)
   326  		return rlp.EncodeToBytes(cpy)
   327  	}
   328  	return rlp.EncodeToBytes(stateObject)
   329  }
   330  
   331  // Reset clears out all ephemeral state objects from the state db, but keeps
   332  // the underlying state trie to avoid reloading data for the next operations.
   333  func (s *StateDB) Reset(root common.Hash) error {
   334  	tr, err := s.db.OpenTrie(root)
   335  	if err != nil {
   336  		return err
   337  	}
   338  	s.trie = tr
   339  	s.mutex.Lock()
   340  	s.stateObjects = make(map[common.Address]*stateObject)
   341  	s.stateObjectsPending = make(map[common.Address]struct{})
   342  	s.stateObjectsDirty = make(map[common.Address]struct{})
   343  	s.mutex.Unlock()
   344  	s.thash = common.Hash{}
   345  	s.bhash = common.Hash{}
   346  	s.txIndex = 0
   347  	s.logs = make(map[common.Hash][]*types.Log)
   348  	s.logSize = 0
   349  	s.preimages = make(map[common.Hash][]byte)
   350  	s.clearJournalAndRefund()
   351  
   352  	if s.snaps != nil {
   353  		s.snapAccounts, s.snapDestructs, s.snapStorage = nil, nil, nil
   354  		if s.snap = s.snaps.Snapshot(root); s.snap != nil {
   355  			s.snapDestructs = make(map[common.Hash]struct{})
   356  			s.snapAccounts = make(map[common.Hash][]byte)
   357  			s.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
   358  		}
   359  	}
   360  	s.accessList = newAccessList()
   361  	return nil
   362  }
   363  
   364  // End Quorum
   365  
   366  // TxIndex returns the current transaction index set by Prepare.
   367  func (s *StateDB) TxIndex() int {
   368  	return s.txIndex
   369  }
   370  
   371  // BlockHash returns the current block hash set by Prepare.
   372  func (s *StateDB) BlockHash() common.Hash {
   373  	return s.bhash
   374  }
   375  
   376  func (s *StateDB) GetCode(addr common.Address) []byte {
   377  	stateObject := s.getStateObject(addr)
   378  	if stateObject != nil {
   379  		return stateObject.Code(s.db)
   380  	}
   381  	return nil
   382  }
   383  
   384  func (s *StateDB) GetCodeSize(addr common.Address) int {
   385  	stateObject := s.getStateObject(addr)
   386  	if stateObject != nil {
   387  		return stateObject.CodeSize(s.db)
   388  	}
   389  	return 0
   390  }
   391  
   392  func (s *StateDB) GetCodeHash(addr common.Address) common.Hash {
   393  	stateObject := s.getStateObject(addr)
   394  	if stateObject == nil {
   395  		return common.Hash{}
   396  	}
   397  	return common.BytesToHash(stateObject.CodeHash())
   398  }
   399  
   400  // GetState retrieves a value from the given account's storage trie.
   401  func (s *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
   402  	stateObject := s.getStateObject(addr)
   403  	if stateObject != nil {
   404  		return stateObject.GetState(s.db, hash)
   405  	}
   406  	return common.Hash{}
   407  }
   408  
   409  // GetProof returns the Merkle proof for a given account.
   410  func (s *StateDB) GetProof(addr common.Address) ([][]byte, error) {
   411  	return s.GetProofByHash(crypto.Keccak256Hash(addr.Bytes()))
   412  }
   413  
   414  // GetProofByHash returns the Merkle proof for a given account.
   415  func (s *StateDB) GetProofByHash(addrHash common.Hash) ([][]byte, error) {
   416  	var proof proofList
   417  	err := s.trie.Prove(addrHash[:], 0, &proof)
   418  	return proof, err
   419  }
   420  
   421  // GetStorageProof returns the Merkle proof for given storage slot.
   422  func (s *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) {
   423  	var proof proofList
   424  	trie := s.StorageTrie(a)
   425  	if trie == nil {
   426  		return proof, errors.New("storage trie for requested address does not exist")
   427  	}
   428  	err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
   429  	return proof, err
   430  }
   431  
   432  // GetStorageProofByHash returns the Merkle proof for given storage slot.
   433  func (s *StateDB) GetStorageProofByHash(a common.Address, key common.Hash) ([][]byte, error) {
   434  	var proof proofList
   435  	trie := s.StorageTrie(a)
   436  	if trie == nil {
   437  		return proof, errors.New("storage trie for requested address does not exist")
   438  	}
   439  	err := trie.Prove(crypto.Keccak256(key.Bytes()), 0, &proof)
   440  	return proof, err
   441  }
   442  
   443  // GetCommittedState retrieves a value from the given account's committed storage trie.
   444  func (s *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
   445  	stateObject := s.getStateObject(addr)
   446  	if stateObject != nil {
   447  		return stateObject.GetCommittedState(s.db, hash)
   448  	}
   449  	return common.Hash{}
   450  }
   451  
   452  // Database retrieves the low level database supporting the lower level trie ops.
   453  func (s *StateDB) Database() Database {
   454  	return s.db
   455  }
   456  
   457  // StorageTrie returns the storage trie of an account.
   458  // The return value is a copy and is nil for non-existent accounts.
   459  func (s *StateDB) StorageTrie(addr common.Address) Trie {
   460  	stateObject := s.getStateObject(addr)
   461  	if stateObject == nil {
   462  		return nil
   463  	}
   464  	cpy := stateObject.deepCopy(s)
   465  	cpy.updateTrie(s.db)
   466  	return cpy.getTrie(s.db)
   467  }
   468  
   469  func (s *StateDB) HasSuicided(addr common.Address) bool {
   470  	stateObject := s.getStateObject(addr)
   471  	if stateObject != nil {
   472  		return stateObject.suicided
   473  	}
   474  	return false
   475  }
   476  
   477  // Quorum
   478  // GetStorageRoot returns the root of the storage associated with the given address.
   479  func (s *StateDB) GetStorageRoot(addr common.Address) (common.Hash, error) {
   480  	so := s.getStateObject(addr)
   481  	if so == nil {
   482  		return common.Hash{}, fmt.Errorf("can't find state object")
   483  	}
   484  	return so.storageRoot(s.db), nil
   485  }
   486  
   487  /*
   488   * SETTERS
   489   */
   490  
   491  // AddBalance adds amount to the account associated with addr.
   492  func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) {
   493  	stateObject := s.GetOrNewStateObject(addr)
   494  	if stateObject != nil {
   495  		stateObject.AddBalance(amount)
   496  	}
   497  }
   498  
   499  // SubBalance subtracts amount from the account associated with addr.
   500  func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) {
   501  	stateObject := s.GetOrNewStateObject(addr)
   502  	if stateObject != nil {
   503  		stateObject.SubBalance(amount)
   504  	}
   505  }
   506  
   507  func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) {
   508  	stateObject := s.GetOrNewStateObject(addr)
   509  	if stateObject != nil {
   510  		stateObject.SetBalance(amount)
   511  	}
   512  }
   513  
   514  func (s *StateDB) SetNonce(addr common.Address, nonce uint64) {
   515  	stateObject := s.GetOrNewStateObject(addr)
   516  	if stateObject != nil {
   517  		stateObject.SetNonce(nonce)
   518  	}
   519  }
   520  
   521  func (s *StateDB) SetPrivacyMetadata(addr common.Address, metadata *PrivacyMetadata) {
   522  	stateObject := s.GetOrNewStateObject(addr)
   523  	if stateObject != nil {
   524  		stateObject.SetStatePrivacyMetadata(metadata)
   525  	}
   526  }
   527  
   528  func (self *StateDB) SetManagedParties(addr common.Address, managedParties []string) {
   529  	stateObject := self.GetOrNewStateObject(addr)
   530  	if stateObject != nil && len(managedParties) > 0 {
   531  		stateObject.SetManagedParties(managedParties)
   532  	}
   533  }
   534  
   535  func (s *StateDB) SetCode(addr common.Address, code []byte) {
   536  	stateObject := s.GetOrNewStateObject(addr)
   537  	if stateObject != nil {
   538  		stateObject.SetCode(crypto.Keccak256Hash(code), code)
   539  	}
   540  }
   541  
   542  func (s *StateDB) SetState(addr common.Address, key, value common.Hash) {
   543  	stateObject := s.GetOrNewStateObject(addr)
   544  	if stateObject != nil {
   545  		stateObject.SetState(s.db, key, value)
   546  	}
   547  }
   548  
   549  // SetStorage replaces the entire storage for the specified account with given
   550  // storage. This function should only be used for debugging.
   551  func (s *StateDB) SetStorage(addr common.Address, storage map[common.Hash]common.Hash) {
   552  	stateObject := s.GetOrNewStateObject(addr)
   553  	if stateObject != nil {
   554  		stateObject.SetStorage(storage)
   555  	}
   556  }
   557  
   558  // Suicide marks the given account as suicided.
   559  // This clears the account balance.
   560  //
   561  // The account's state object is still available until the state is committed,
   562  // getStateObject will return a non-nil account after Suicide.
   563  func (s *StateDB) Suicide(addr common.Address) bool {
   564  	stateObject := s.getStateObject(addr)
   565  	if stateObject == nil {
   566  		return false
   567  	}
   568  	s.journal.append(suicideChange{
   569  		account:     &addr,
   570  		prev:        stateObject.suicided,
   571  		prevbalance: new(big.Int).Set(stateObject.Balance()),
   572  	})
   573  	stateObject.markSuicided()
   574  	stateObject.data.Balance = new(big.Int)
   575  
   576  	return true
   577  }
   578  
   579  //
   580  // Setting, updating & deleting state object methods.
   581  //
   582  
   583  // updateStateObject writes the given object to the trie.
   584  // Quorum:
   585  // - update AccountExtraData trie
   586  func (s *StateDB) updateStateObject(obj *stateObject) {
   587  	// Track the amount of time wasted on updating the account from the trie
   588  	if metrics.EnabledExpensive {
   589  		defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
   590  	}
   591  	// Encode the account and update the account trie
   592  	addr := obj.Address()
   593  
   594  	data, err := rlp.EncodeToBytes(obj)
   595  	if err != nil {
   596  		panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
   597  	}
   598  	if err = s.trie.TryUpdate(addr[:], data); err != nil {
   599  		s.setError(fmt.Errorf("updateStateObject (%x) error: %v", addr[:], err))
   600  	}
   601  
   602  	// If state snapshotting is active, cache the data til commit. Note, this
   603  	// update mechanism is not symmetric to the deletion, because whereas it is
   604  	// enough to track account updates at commit time, deletions need tracking
   605  	// at transaction boundary level to ensure we capture state clearing.
   606  	if s.snap != nil {
   607  		s.snapAccounts[obj.addrHash] = snapshot.SlimAccountRLP(obj.data.Nonce, obj.data.Balance, obj.data.Root, obj.data.CodeHash)
   608  	}
   609  
   610  	// Quorum - Privacy Enhancements - update the privacy metadata trie in case the privacy metadata is dirty
   611  	if err != nil {
   612  		return
   613  	}
   614  
   615  	if obj.dirtyAccountExtraData && obj.accountExtraData != nil {
   616  		extraDataBytes, err := rlp.EncodeToBytes(obj.accountExtraData)
   617  		if err != nil {
   618  			panic(fmt.Errorf("can't encode privacy metadata at %x: %v", addr[:], err))
   619  		}
   620  		err = s.accountExtraDataTrie.TryUpdate(addr[:], extraDataBytes)
   621  		if err != nil {
   622  			s.setError(err)
   623  			return
   624  		}
   625  	}
   626  
   627  }
   628  
   629  // deleteStateObject removes the given object from the state trie.
   630  // Quorum:
   631  // - delete the data from the extra data trie corresponding to the account address
   632  func (s *StateDB) deleteStateObject(obj *stateObject) {
   633  	// Track the amount of time wasted on deleting the account from the trie
   634  	if metrics.EnabledExpensive {
   635  		defer func(start time.Time) { s.AccountUpdates += time.Since(start) }(time.Now())
   636  	}
   637  	// Delete the account from the trie
   638  	addr := obj.Address()
   639  	if err := s.trie.TryDelete(addr[:]); err != nil {
   640  		s.setError(fmt.Errorf("deleteStateObject (%x) error: %v", addr[:], err))
   641  		return
   642  	}
   643  	s.setError(s.accountExtraDataTrie.TryDelete(addr[:]))
   644  }
   645  
   646  // getStateObject retrieves a state object given by the address, returning nil if
   647  // the object is not found or was deleted in this execution context. If you need
   648  // to differentiate between non-existent/just-deleted, use getDeletedStateObject.
   649  func (s *StateDB) getStateObject(addr common.Address) *stateObject {
   650  	if obj := s.getDeletedStateObject(addr); obj != nil && !obj.deleted {
   651  		return obj
   652  	}
   653  	return nil
   654  }
   655  
   656  // getDeletedStateObject is similar to getStateObject, but instead of returning
   657  // nil for a deleted state object, it returns the actual object with the deleted
   658  // flag set. This is needed by the state journal to revert to the correct s-
   659  // destructed object instead of wiping all knowledge about the state object.
   660  func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
   661  	// Prefer live objects if any is available
   662  	if obj := s.stateObjects[addr]; obj != nil {
   663  		return obj
   664  	}
   665  	// If no live objects are available, attempt to use snapshots
   666  	var (
   667  		data *Account
   668  		err  error
   669  	)
   670  	if s.snap != nil {
   671  		if metrics.EnabledExpensive {
   672  			defer func(start time.Time) { s.SnapshotAccountReads += time.Since(start) }(time.Now())
   673  		}
   674  		var acc *snapshot.Account
   675  		if acc, err = s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())); err == nil {
   676  			if acc == nil {
   677  				return nil
   678  			}
   679  			data = &Account{
   680  				Nonce:    acc.Nonce,
   681  				Balance:  acc.Balance,
   682  				CodeHash: acc.CodeHash,
   683  				Root:     common.BytesToHash(acc.Root),
   684  			}
   685  			if len(data.CodeHash) == 0 {
   686  				data.CodeHash = emptyCodeHash
   687  			}
   688  			if data.Root == (common.Hash{}) {
   689  				data.Root = emptyRoot
   690  			}
   691  		}
   692  	}
   693  	// If snapshot unavailable or reading from it failed, load from the database
   694  	if s.snap == nil || err != nil {
   695  		if metrics.EnabledExpensive {
   696  			defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
   697  		}
   698  		enc, err := s.trie.TryGet(addr.Bytes())
   699  		if err != nil {
   700  			s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr.Bytes(), err))
   701  			return nil
   702  		}
   703  		if len(enc) == 0 {
   704  			return nil
   705  		}
   706  		data = new(Account)
   707  		if err := rlp.DecodeBytes(enc, data); err != nil {
   708  			log.Error("Failed to decode state object", "addr", addr, "err", err)
   709  			return nil
   710  		}
   711  	}
   712  	// Insert into the live set
   713  	obj := newObject(s, addr, *data)
   714  	s.setStateObject(obj)
   715  	return obj
   716  }
   717  
   718  func (s *StateDB) setStateObject(object *stateObject) {
   719  	defer s.mutex.Unlock()
   720  	s.mutex.Lock()
   721  	s.stateObjects[object.Address()] = object
   722  }
   723  
   724  // GetOrNewStateObject retrieves a state object or create a new state object if nil.
   725  func (s *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
   726  	stateObject := s.getStateObject(addr)
   727  	if stateObject == nil {
   728  		stateObject, _ = s.createObject(addr)
   729  	}
   730  	return stateObject
   731  }
   732  
   733  // createObject creates a new state object. If there is an existing account with
   734  // the given address, it is overwritten and returned as the second return value.
   735  func (s *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
   736  	prev = s.getDeletedStateObject(addr) // Note, prev might have been deleted, we need that!
   737  
   738  	var prevdestruct bool
   739  	if s.snap != nil && prev != nil {
   740  		_, prevdestruct = s.snapDestructs[prev.addrHash]
   741  		if !prevdestruct {
   742  			s.snapDestructs[prev.addrHash] = struct{}{}
   743  		}
   744  	}
   745  	newobj = newObject(s, addr, Account{})
   746  	newobj.setNonce(0) // sets the object to dirty
   747  	if prev == nil {
   748  		s.journal.append(createObjectChange{account: &addr})
   749  	} else {
   750  		s.journal.append(resetObjectChange{prev: prev, prevdestruct: prevdestruct})
   751  	}
   752  	s.setStateObject(newobj)
   753  	if prev != nil && !prev.deleted {
   754  		return newobj, prev
   755  	}
   756  	return newobj, nil
   757  }
   758  
   759  // CreateAccount explicitly creates a state object. If a state object with the address
   760  // already exists the balance is carried over to the new account.
   761  //
   762  // CreateAccount is called during the EVM CREATE operation. The situation might arise that
   763  // a contract does the following:
   764  //
   765  //   1. sends funds to sha(account ++ (nonce + 1))
   766  //   2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
   767  //
   768  // Carrying over the balance ensures that Ether doesn't disappear.
   769  func (s *StateDB) CreateAccount(addr common.Address) {
   770  	newObj, prev := s.createObject(addr)
   771  	if prev != nil {
   772  		newObj.setBalance(prev.data.Balance)
   773  	}
   774  }
   775  
   776  func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) error {
   777  	so := db.getStateObject(addr)
   778  	if so == nil {
   779  		return nil
   780  	}
   781  	it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
   782  
   783  	for it.Next() {
   784  		key := common.BytesToHash(db.trie.GetKey(it.Key))
   785  		if value, dirty := so.dirtyStorage[key]; dirty {
   786  			if !cb(key, value) {
   787  				return nil
   788  			}
   789  			continue
   790  		}
   791  
   792  		if len(it.Value) > 0 {
   793  			_, content, _, err := rlp.Split(it.Value)
   794  			if err != nil {
   795  				return err
   796  			}
   797  			if !cb(key, common.BytesToHash(content)) {
   798  				return nil
   799  			}
   800  		}
   801  	}
   802  	return nil
   803  }
   804  
   805  // Copy creates a deep, independent copy of the state.
   806  // Snapshots of the copied state cannot be applied to the copy.
   807  func (s *StateDB) Copy() *StateDB {
   808  	s.journalMutex.Lock()
   809  	journal := s.journal
   810  	s.journalMutex.Unlock()
   811  
   812  	journal.mutex.Lock()
   813  	size := len(journal.dirties)
   814  	dirties := make([]common.Address, 0, size)
   815  	for addr := range journal.dirties {
   816  		dirties = append(dirties, addr)
   817  	}
   818  	journal.mutex.Unlock()
   819  
   820  	// Copy all the basic fields, initialize the memory ones
   821  	state := &StateDB{
   822  		db:                  s.db,
   823  		trie:                s.db.CopyTrie(s.trie),
   824  		stateObjects:        make(map[common.Address]*stateObject, size),
   825  		stateObjectsPending: make(map[common.Address]struct{}, len(s.stateObjectsPending)),
   826  		stateObjectsDirty:   make(map[common.Address]struct{}, size),
   827  		refund:              s.refund,
   828  		logs:                make(map[common.Hash][]*types.Log, len(s.logs)),
   829  		logSize:             s.logSize,
   830  		preimages:           make(map[common.Hash][]byte, len(s.preimages)),
   831  		journal:             newJournal(),
   832  		hasher:              crypto.NewKeccakState(),
   833  		// Quorum - Privacy Enhancements
   834  		accountExtraDataTrie: s.db.CopyTrie(s.accountExtraDataTrie),
   835  	}
   836  
   837  	s.mutex.Lock()
   838  	// Copy the dirty states, logs, and preimages
   839  	for _, addr := range dirties {
   840  		// As documented [here](https://github.com/ethereum/go-ethereum/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  	s.mutex.Unlock()
   870  	for hash, logs := range s.logs {
   871  		cpy := make([]*types.Log, len(logs))
   872  		for i, l := range logs {
   873  			cpy[i] = new(types.Log)
   874  			*cpy[i] = *l
   875  		}
   876  		state.logs[hash] = cpy
   877  	}
   878  	for hash, preimage := range s.preimages {
   879  		state.preimages[hash] = preimage
   880  	}
   881  	// Do we need to copy the access list? In practice: No. At the start of a
   882  	// transaction, the access list is empty. In practice, we only ever copy state
   883  	// _between_ transactions/blocks, never in the middle of a transaction.
   884  	// However, it doesn't cost us much to copy an empty list, so we do it anyway
   885  	// to not blow up if we ever decide copy it in the middle of a transaction
   886  	state.accessList = s.accessList.Copy()
   887  
   888  	// If there's a prefetcher running, make an inactive copy of it that can
   889  	// only access data but does not actively preload (since the user will not
   890  	// know that they need to explicitly terminate an active copy).
   891  	if s.prefetcher != nil {
   892  		state.prefetcher = s.prefetcher.copy()
   893  	}
   894  	if s.snaps != nil {
   895  		// In order for the miner to be able to use and make additions
   896  		// to the snapshot tree, we need to copy that aswell.
   897  		// Otherwise, any block mined by ourselves will cause gaps in the tree,
   898  		// and force the miner to operate trie-backed only
   899  		state.snaps = s.snaps
   900  		state.snap = s.snap
   901  		// deep copy needed
   902  		state.snapDestructs = make(map[common.Hash]struct{})
   903  		for k, v := range s.snapDestructs {
   904  			state.snapDestructs[k] = v
   905  		}
   906  		state.snapAccounts = make(map[common.Hash][]byte)
   907  		for k, v := range s.snapAccounts {
   908  			state.snapAccounts[k] = v
   909  		}
   910  		state.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
   911  		for k, v := range s.snapStorage {
   912  			temp := make(map[common.Hash][]byte)
   913  			for kk, vv := range v {
   914  				temp[kk] = vv
   915  			}
   916  			state.snapStorage[k] = temp
   917  		}
   918  	}
   919  	return state
   920  }
   921  
   922  // Snapshot returns an identifier for the current revision of the state.
   923  func (s *StateDB) Snapshot() int {
   924  	id := s.nextRevisionId
   925  	s.nextRevisionId++
   926  	s.validRevisions = append(s.validRevisions, revision{id, s.journal.length()})
   927  	return id
   928  }
   929  
   930  // RevertToSnapshot reverts all state changes made since the given revision.
   931  func (s *StateDB) RevertToSnapshot(revid int) {
   932  	// Find the snapshot in the stack of valid snapshots.
   933  	idx := sort.Search(len(s.validRevisions), func(i int) bool {
   934  		return s.validRevisions[i].id >= revid
   935  	})
   936  	if idx == len(s.validRevisions) || s.validRevisions[idx].id != revid {
   937  		panic(fmt.Errorf("revision id %v cannot be reverted", revid))
   938  	}
   939  	snapshot := s.validRevisions[idx].journalIndex
   940  
   941  	// Replay the journal to undo changes and remove invalidated snapshots
   942  	s.journal.revert(s, snapshot)
   943  	s.validRevisions = s.validRevisions[:idx]
   944  }
   945  
   946  // GetRefund returns the current value of the refund counter.
   947  func (s *StateDB) GetRefund() uint64 {
   948  	return s.refund
   949  }
   950  
   951  // Finalise finalises the state by removing the s destructed objects and clears
   952  // the journal as well as the refunds. Finalise, however, will not push any updates
   953  // into the tries just yet. Only IntermediateRoot or Commit will do that.
   954  func (s *StateDB) Finalise(deleteEmptyObjects bool) {
   955  	addressesToPrefetch := make([][]byte, 0, len(s.journal.dirties))
   956  	s.journal.mutex.Lock()
   957  	dirties := make([]common.Address, 0, len(s.journal.dirties))
   958  	for addr := range s.journal.dirties {
   959  		dirties = append(dirties, addr)
   960  	}
   961  	s.journal.mutex.Unlock()
   962  	for _, addr := range dirties {
   963  		obj, exist := s.stateObjects[addr]
   964  		if !exist {
   965  			// ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
   966  			// That tx goes out of gas, and although the notion of 'touched' does not exist there, the
   967  			// touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
   968  			// it will persist in the journal even though the journal is reverted. In this special circumstance,
   969  			// it may exist in `s.journal.dirties` but not in `s.stateObjects`.
   970  			// Thus, we can safely ignore it here
   971  			continue
   972  		}
   973  		if obj.suicided || (deleteEmptyObjects && obj.empty()) {
   974  			obj.deleted = true
   975  
   976  			// If state snapshotting is active, also mark the destruction there.
   977  			// Note, we can't do this only at the end of a block because multiple
   978  			// transactions within the same block might self destruct and then
   979  			// ressurrect an account; but the snapshotter needs both events.
   980  			if s.snap != nil {
   981  				s.snapDestructs[obj.addrHash] = struct{}{} // We need to maintain account deletions explicitly (will remain set indefinitely)
   982  				delete(s.snapAccounts, obj.addrHash)       // Clear out any previously updated account data (may be recreated via a ressurrect)
   983  				delete(s.snapStorage, obj.addrHash)        // Clear out any previously updated storage data (may be recreated via a ressurrect)
   984  			}
   985  		} else {
   986  			obj.finalise(true) // Prefetch slots in the background
   987  		}
   988  		s.mutex.Lock()
   989  		s.stateObjectsPending[addr] = struct{}{}
   990  		s.stateObjectsDirty[addr] = struct{}{}
   991  		s.mutex.Unlock()
   992  
   993  		// At this point, also ship the address off to the precacher. The precacher
   994  		// will start loading tries, and when the change is eventually committed,
   995  		// the commit-phase will be a lot faster
   996  		addressesToPrefetch = append(addressesToPrefetch, common.CopyBytes(addr[:])) // Copy needed for closure
   997  	}
   998  	if s.prefetcher != nil && len(addressesToPrefetch) > 0 {
   999  		s.prefetcher.prefetch(s.originalRoot, addressesToPrefetch)
  1000  	}
  1001  	// Invalidate journal because reverting across transactions is not allowed.
  1002  	s.clearJournalAndRefund()
  1003  }
  1004  
  1005  // IntermediateRoot computes the current root hash of the state trie.
  1006  // It is called in between transactions to get the root hash that
  1007  // goes into transaction receipts.
  1008  func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
  1009  	// Finalise all the dirty storage states and write them into the tries
  1010  	s.Finalise(deleteEmptyObjects)
  1011  
  1012  	s.mutex.Lock()
  1013  	defer s.mutex.Unlock()
  1014  	// If there was a trie prefetcher operating, it gets aborted and irrevocably
  1015  	// modified after we start retrieving tries. Remove it from the statedb after
  1016  	// this round of use.
  1017  	//
  1018  	// This is weird pre-byzantium since the first tx runs with a prefetcher and
  1019  	// the remainder without, but pre-byzantium even the initial prefetcher is
  1020  	// useless, so no sleep lost.
  1021  	prefetcher := s.prefetcher
  1022  	if s.prefetcher != nil {
  1023  		defer func() {
  1024  			s.prefetcher.close()
  1025  			s.prefetcher = nil
  1026  		}()
  1027  	}
  1028  	// Although naively it makes sense to retrieve the account trie and then do
  1029  	// the contract storage and account updates sequentially, that short circuits
  1030  	// the account prefetcher. Instead, let's process all the storage updates
  1031  	// first, giving the account prefeches just a few more milliseconds of time
  1032  	// to pull useful data from disk.
  1033  	for addr := range s.stateObjectsPending {
  1034  		if obj := s.stateObjects[addr]; !obj.deleted {
  1035  			obj.updateRoot(s.db)
  1036  		}
  1037  	}
  1038  	// Now we're about to start to write changes to the trie. The trie is so far
  1039  	// _untouched_. We can check with the prefetcher, if it can give us a trie
  1040  	// which has the same root, but also has some content loaded into it.
  1041  	if prefetcher != nil {
  1042  		if trie := prefetcher.trie(s.originalRoot); trie != nil {
  1043  			s.trie = trie
  1044  		}
  1045  	}
  1046  	usedAddrs := make([][]byte, 0, len(s.stateObjectsPending))
  1047  	for addr := range s.stateObjectsPending {
  1048  		if obj := s.stateObjects[addr]; obj.deleted {
  1049  			s.deleteStateObject(obj)
  1050  		} else {
  1051  			s.updateStateObject(obj)
  1052  		}
  1053  		usedAddrs = append(usedAddrs, common.CopyBytes(addr[:])) // Copy needed for closure
  1054  	}
  1055  	if prefetcher != nil {
  1056  		prefetcher.used(s.originalRoot, usedAddrs)
  1057  	}
  1058  	if len(s.stateObjectsPending) > 0 {
  1059  		s.stateObjectsPending = make(map[common.Address]struct{})
  1060  	}
  1061  	// Track the amount of time wasted on hashing the account trie
  1062  	if metrics.EnabledExpensive {
  1063  		defer func(start time.Time) { s.AccountHashes += time.Since(start) }(time.Now())
  1064  	}
  1065  	return s.trie.Hash()
  1066  }
  1067  
  1068  // Prepare sets the current transaction hash and index and block hash which is
  1069  // used when the EVM emits new state logs.
  1070  func (s *StateDB) Prepare(thash, bhash common.Hash, ti int) {
  1071  	s.thash = thash
  1072  	s.bhash = bhash
  1073  	s.txIndex = ti
  1074  	s.accessList = newAccessList()
  1075  }
  1076  
  1077  func (s *StateDB) clearJournalAndRefund() {
  1078  	defer s.journalMutex.Unlock()
  1079  	s.journalMutex.Lock()
  1080  	if len(s.journal.entries) > 0 {
  1081  		s.journal = newJournal()
  1082  		s.refund = 0
  1083  	}
  1084  	s.validRevisions = s.validRevisions[:0] // Snapshots can be created without journal entires
  1085  }
  1086  
  1087  // Commit writes the state to the underlying in-memory trie database.
  1088  // Quorum:
  1089  // - linking state root and the AccountExtraData root
  1090  func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
  1091  	if s.dbErr != nil {
  1092  		return common.Hash{}, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr)
  1093  	}
  1094  	// Finalize any pending changes and merge everything into the tries
  1095  	s.IntermediateRoot(deleteEmptyObjects)
  1096  
  1097  	s.mutex.Lock()
  1098  	// Commit objects to the trie, measuring the elapsed time
  1099  	codeWriter := s.db.TrieDB().DiskDB().NewBatch()
  1100  	for addr := range s.stateObjectsDirty {
  1101  		if obj := s.stateObjects[addr]; !obj.deleted {
  1102  			// Write any contract code associated with the state object
  1103  			if obj.code != nil && obj.dirtyCode {
  1104  				rawdb.WriteCode(codeWriter, common.BytesToHash(obj.CodeHash()), obj.code)
  1105  				obj.dirtyCode = false
  1106  			}
  1107  			// Write any storage changes in the state object to its storage trie
  1108  			if err := obj.CommitTrie(s.db); err != nil {
  1109  				return common.Hash{}, err
  1110  			}
  1111  		}
  1112  	}
  1113  	if len(s.stateObjectsDirty) > 0 {
  1114  		s.stateObjectsDirty = make(map[common.Address]struct{})
  1115  	}
  1116  	s.mutex.Unlock()
  1117  	if codeWriter.ValueSize() > 0 {
  1118  		if err := codeWriter.Write(); err != nil {
  1119  			log.Crit("Failed to commit dirty codes", "error", err)
  1120  		}
  1121  	}
  1122  	// Write the account trie changes, measuing the amount of wasted time
  1123  	var start time.Time
  1124  	if metrics.EnabledExpensive {
  1125  		start = time.Now()
  1126  	}
  1127  	// The onleaf func is called _serially_, so we can reuse the same account
  1128  	// for unmarshalling every time.
  1129  	var account Account
  1130  	root, err := s.trie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
  1131  		if err := rlp.DecodeBytes(leaf, &account); err != nil {
  1132  			return nil
  1133  		}
  1134  		if account.Root != emptyRoot {
  1135  			s.db.TrieDB().Reference(account.Root, parent)
  1136  		}
  1137  		return nil
  1138  	})
  1139  	if metrics.EnabledExpensive {
  1140  		s.AccountCommits += time.Since(start)
  1141  	}
  1142  	// If snapshotting is enabled, update the snapshot tree with this new version
  1143  	if s.snap != nil {
  1144  		if metrics.EnabledExpensive {
  1145  			defer func(start time.Time) { s.SnapshotCommits += time.Since(start) }(time.Now())
  1146  		}
  1147  		// Only update if there's a state transition (skip empty Clique blocks)
  1148  		if parent := s.snap.Root(); parent != root {
  1149  			if err := s.snaps.Update(root, parent, s.snapDestructs, s.snapAccounts, s.snapStorage); err != nil {
  1150  				log.Warn("Failed to update snapshot tree", "from", parent, "to", root, "err", err)
  1151  			}
  1152  			// Keep 128 diff layers in the memory, persistent layer is 129th.
  1153  			// - head layer is paired with HEAD state
  1154  			// - head-1 layer is paired with HEAD-1 state
  1155  			// - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state
  1156  			if err := s.snaps.Cap(root, 128); err != nil {
  1157  				log.Warn("Failed to cap snapshot tree", "root", root, "layers", 128, "err", err)
  1158  			}
  1159  		}
  1160  		s.snap, s.snapDestructs, s.snapAccounts, s.snapStorage = nil, nil, nil, nil
  1161  	}
  1162  
  1163  	// Quorum
  1164  	// linking the state root and the AccountExtraData root
  1165  	if err == nil {
  1166  		// commit the AccountExtraData trie
  1167  		extraDataRoot, err := s.accountExtraDataTrie.Commit(nil)
  1168  		if err != nil {
  1169  			return common.Hash{}, fmt.Errorf("unable to commit the AccountExtraData trie: %v", err)
  1170  		}
  1171  		log.Debug("AccountExtraData root after trie commit", "root", extraDataRoot)
  1172  		// link the new state root to the AccountExtraData root
  1173  		err = s.db.AccountExtraDataLinker().Link(root, extraDataRoot)
  1174  		if err != nil {
  1175  			return common.Hash{}, fmt.Errorf("Unable to link the state root to the privacy metadata root: %v", err)
  1176  		}
  1177  		// add a reference from the AccountExtraData root to the state root so that when the state root is written
  1178  		// to the DB the the AccountExtraData root is also written
  1179  		s.db.TrieDB().Reference(extraDataRoot, root)
  1180  	}
  1181  	return root, err
  1182  }
  1183  
  1184  // PrepareAccessList handles the preparatory steps for executing a state transition with
  1185  // regards to both EIP-2929 and EIP-2930:
  1186  //
  1187  // - Add sender to access list (2929)
  1188  // - Add destination to access list (2929)
  1189  // - Add precompiles to access list (2929)
  1190  // - Add the contents of the optional tx access list (2930)
  1191  //
  1192  // This method should only be called if Yolov3/Berlin/2929+2930 is applicable at the current number.
  1193  func (s *StateDB) PrepareAccessList(sender common.Address, dst *common.Address, precompiles []common.Address, list types.AccessList) {
  1194  	s.AddAddressToAccessList(sender)
  1195  	if dst != nil {
  1196  		s.AddAddressToAccessList(*dst)
  1197  		// If it's a create-tx, the destination will be added inside evm.create
  1198  	}
  1199  	for _, addr := range precompiles {
  1200  		s.AddAddressToAccessList(addr)
  1201  	}
  1202  	for _, el := range list {
  1203  		s.AddAddressToAccessList(el.Address)
  1204  		for _, key := range el.StorageKeys {
  1205  			s.AddSlotToAccessList(el.Address, key)
  1206  		}
  1207  	}
  1208  }
  1209  
  1210  // AddAddressToAccessList adds the given address to the access list
  1211  func (s *StateDB) AddAddressToAccessList(addr common.Address) {
  1212  	if s.accessList.AddAddress(addr) {
  1213  		s.journal.append(accessListAddAccountChange{&addr})
  1214  	}
  1215  }
  1216  
  1217  // AddSlotToAccessList adds the given (address, slot)-tuple to the access list
  1218  func (s *StateDB) AddSlotToAccessList(addr common.Address, slot common.Hash) {
  1219  	addrMod, slotMod := s.accessList.AddSlot(addr, slot)
  1220  	if addrMod {
  1221  		// In practice, this should not happen, since there is no way to enter the
  1222  		// scope of 'address' without having the 'address' become already added
  1223  		// to the access list (via call-variant, create, etc).
  1224  		// Better safe than sorry, though
  1225  		s.journal.append(accessListAddAccountChange{&addr})
  1226  	}
  1227  	if slotMod {
  1228  		s.journal.append(accessListAddSlotChange{
  1229  			address: &addr,
  1230  			slot:    &slot,
  1231  		})
  1232  	}
  1233  }
  1234  
  1235  // AddressInAccessList returns true if the given address is in the access list.
  1236  func (s *StateDB) AddressInAccessList(addr common.Address) bool {
  1237  	return s.accessList.ContainsAddress(addr)
  1238  }
  1239  
  1240  // SlotInAccessList returns true if the given (address, slot)-tuple is in the access list.
  1241  func (s *StateDB) SlotInAccessList(addr common.Address, slot common.Hash) (addressPresent bool, slotPresent bool) {
  1242  	return s.accessList.Contains(addr, slot)
  1243  }