github.com/cryptogateway/go-paymex@v0.0.0-20210204174735-96277fb1e602/core/state/statedb.go (about)

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