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