github.com/dominant-strategies/go-quai@v0.28.2/core/state/statedb.go (about)

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