github.com/dim4egster/coreth@v0.10.2/core/state/statedb.go (about)

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