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