github.com/clem109/go-ethereum@v1.8.3-0.20180316121352-fe6cf00f480a/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  	"fmt"
    22  	"math/big"
    23  	"sort"
    24  	"sync"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/core/types"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/log"
    30  	"github.com/ethereum/go-ethereum/rlp"
    31  	"github.com/ethereum/go-ethereum/trie"
    32  )
    33  
    34  type revision struct {
    35  	id           int
    36  	journalIndex int
    37  }
    38  
    39  var (
    40  	// emptyState is the known hash of an empty state trie entry.
    41  	emptyState = crypto.Keccak256Hash(nil)
    42  
    43  	// emptyCode is the known hash of the empty EVM bytecode.
    44  	emptyCode = crypto.Keccak256Hash(nil)
    45  )
    46  
    47  // StateDBs within the ethereum protocol are used to store anything
    48  // within the merkle trie. StateDBs take care of caching and storing
    49  // nested states. It's the general query interface to retrieve:
    50  // * Contracts
    51  // * Accounts
    52  type StateDB struct {
    53  	db   Database
    54  	trie Trie
    55  
    56  	// This map holds 'live' objects, which will get modified while processing a state transition.
    57  	stateObjects      map[common.Address]*stateObject
    58  	stateObjectsDirty map[common.Address]struct{}
    59  
    60  	// DB error.
    61  	// State objects are used by the consensus core and VM which are
    62  	// unable to deal with database-level errors. Any error that occurs
    63  	// during a database read is memoized here and will eventually be returned
    64  	// by StateDB.Commit.
    65  	dbErr error
    66  
    67  	// The refund counter, also used by state transitioning.
    68  	refund uint64
    69  
    70  	thash, bhash common.Hash
    71  	txIndex      int
    72  	logs         map[common.Hash][]*types.Log
    73  	logSize      uint
    74  
    75  	preimages map[common.Hash][]byte
    76  
    77  	// Journal of state modifications. This is the backbone of
    78  	// Snapshot and RevertToSnapshot.
    79  	journal        journal
    80  	validRevisions []revision
    81  	nextRevisionId int
    82  
    83  	lock sync.Mutex
    84  }
    85  
    86  // Create a new state from a given trie
    87  func New(root common.Hash, db Database) (*StateDB, error) {
    88  	tr, err := db.OpenTrie(root)
    89  	if err != nil {
    90  		return nil, err
    91  	}
    92  	return &StateDB{
    93  		db:                db,
    94  		trie:              tr,
    95  		stateObjects:      make(map[common.Address]*stateObject),
    96  		stateObjectsDirty: make(map[common.Address]struct{}),
    97  		logs:              make(map[common.Hash][]*types.Log),
    98  		preimages:         make(map[common.Hash][]byte),
    99  	}, nil
   100  }
   101  
   102  // setError remembers the first non-nil error it is called with.
   103  func (self *StateDB) setError(err error) {
   104  	if self.dbErr == nil {
   105  		self.dbErr = err
   106  	}
   107  }
   108  
   109  func (self *StateDB) Error() error {
   110  	return self.dbErr
   111  }
   112  
   113  // Reset clears out all emphemeral state objects from the state db, but keeps
   114  // the underlying state trie to avoid reloading data for the next operations.
   115  func (self *StateDB) Reset(root common.Hash) error {
   116  	tr, err := self.db.OpenTrie(root)
   117  	if err != nil {
   118  		return err
   119  	}
   120  	self.trie = tr
   121  	self.stateObjects = make(map[common.Address]*stateObject)
   122  	self.stateObjectsDirty = make(map[common.Address]struct{})
   123  	self.thash = common.Hash{}
   124  	self.bhash = common.Hash{}
   125  	self.txIndex = 0
   126  	self.logs = make(map[common.Hash][]*types.Log)
   127  	self.logSize = 0
   128  	self.preimages = make(map[common.Hash][]byte)
   129  	self.clearJournalAndRefund()
   130  	return nil
   131  }
   132  
   133  func (self *StateDB) AddLog(log *types.Log) {
   134  	self.journal = append(self.journal, addLogChange{txhash: self.thash})
   135  
   136  	log.TxHash = self.thash
   137  	log.BlockHash = self.bhash
   138  	log.TxIndex = uint(self.txIndex)
   139  	log.Index = self.logSize
   140  	self.logs[self.thash] = append(self.logs[self.thash], log)
   141  	self.logSize++
   142  }
   143  
   144  func (self *StateDB) GetLogs(hash common.Hash) []*types.Log {
   145  	return self.logs[hash]
   146  }
   147  
   148  func (self *StateDB) Logs() []*types.Log {
   149  	var logs []*types.Log
   150  	for _, lgs := range self.logs {
   151  		logs = append(logs, lgs...)
   152  	}
   153  	return logs
   154  }
   155  
   156  // AddPreimage records a SHA3 preimage seen by the VM.
   157  func (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
   158  	if _, ok := self.preimages[hash]; !ok {
   159  		self.journal = append(self.journal, addPreimageChange{hash: hash})
   160  		pi := make([]byte, len(preimage))
   161  		copy(pi, preimage)
   162  		self.preimages[hash] = pi
   163  	}
   164  }
   165  
   166  // Preimages returns a list of SHA3 preimages that have been submitted.
   167  func (self *StateDB) Preimages() map[common.Hash][]byte {
   168  	return self.preimages
   169  }
   170  
   171  func (self *StateDB) AddRefund(gas uint64) {
   172  	self.journal = append(self.journal, refundChange{prev: self.refund})
   173  	self.refund += gas
   174  }
   175  
   176  // Exist reports whether the given account address exists in the state.
   177  // Notably this also returns true for suicided accounts.
   178  func (self *StateDB) Exist(addr common.Address) bool {
   179  	return self.getStateObject(addr) != nil
   180  }
   181  
   182  // Empty returns whether the state object is either non-existent
   183  // or empty according to the EIP161 specification (balance = nonce = code = 0)
   184  func (self *StateDB) Empty(addr common.Address) bool {
   185  	so := self.getStateObject(addr)
   186  	return so == nil || so.empty()
   187  }
   188  
   189  // Retrieve the balance from the given address or 0 if object not found
   190  func (self *StateDB) GetBalance(addr common.Address) *big.Int {
   191  	stateObject := self.getStateObject(addr)
   192  	if stateObject != nil {
   193  		return stateObject.Balance()
   194  	}
   195  	return common.Big0
   196  }
   197  
   198  func (self *StateDB) GetNonce(addr common.Address) uint64 {
   199  	stateObject := self.getStateObject(addr)
   200  	if stateObject != nil {
   201  		return stateObject.Nonce()
   202  	}
   203  
   204  	return 0
   205  }
   206  
   207  func (self *StateDB) GetCode(addr common.Address) []byte {
   208  	stateObject := self.getStateObject(addr)
   209  	if stateObject != nil {
   210  		return stateObject.Code(self.db)
   211  	}
   212  	return nil
   213  }
   214  
   215  func (self *StateDB) GetCodeSize(addr common.Address) int {
   216  	stateObject := self.getStateObject(addr)
   217  	if stateObject == nil {
   218  		return 0
   219  	}
   220  	if stateObject.code != nil {
   221  		return len(stateObject.code)
   222  	}
   223  	size, err := self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
   224  	if err != nil {
   225  		self.setError(err)
   226  	}
   227  	return size
   228  }
   229  
   230  func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
   231  	stateObject := self.getStateObject(addr)
   232  	if stateObject == nil {
   233  		return common.Hash{}
   234  	}
   235  	return common.BytesToHash(stateObject.CodeHash())
   236  }
   237  
   238  func (self *StateDB) GetState(a common.Address, b common.Hash) common.Hash {
   239  	stateObject := self.getStateObject(a)
   240  	if stateObject != nil {
   241  		return stateObject.GetState(self.db, b)
   242  	}
   243  	return common.Hash{}
   244  }
   245  
   246  // Database retrieves the low level database supporting the lower level trie ops.
   247  func (self *StateDB) Database() Database {
   248  	return self.db
   249  }
   250  
   251  // StorageTrie returns the storage trie of an account.
   252  // The return value is a copy and is nil for non-existent accounts.
   253  func (self *StateDB) StorageTrie(a common.Address) Trie {
   254  	stateObject := self.getStateObject(a)
   255  	if stateObject == nil {
   256  		return nil
   257  	}
   258  	cpy := stateObject.deepCopy(self, nil)
   259  	return cpy.updateTrie(self.db)
   260  }
   261  
   262  func (self *StateDB) HasSuicided(addr common.Address) bool {
   263  	stateObject := self.getStateObject(addr)
   264  	if stateObject != nil {
   265  		return stateObject.suicided
   266  	}
   267  	return false
   268  }
   269  
   270  /*
   271   * SETTERS
   272   */
   273  
   274  // AddBalance adds amount to the account associated with addr
   275  func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) {
   276  	stateObject := self.GetOrNewStateObject(addr)
   277  	if stateObject != nil {
   278  		stateObject.AddBalance(amount)
   279  	}
   280  }
   281  
   282  // SubBalance subtracts amount from the account associated with addr
   283  func (self *StateDB) SubBalance(addr common.Address, amount *big.Int) {
   284  	stateObject := self.GetOrNewStateObject(addr)
   285  	if stateObject != nil {
   286  		stateObject.SubBalance(amount)
   287  	}
   288  }
   289  
   290  func (self *StateDB) SetBalance(addr common.Address, amount *big.Int) {
   291  	stateObject := self.GetOrNewStateObject(addr)
   292  	if stateObject != nil {
   293  		stateObject.SetBalance(amount)
   294  	}
   295  }
   296  
   297  func (self *StateDB) SetNonce(addr common.Address, nonce uint64) {
   298  	stateObject := self.GetOrNewStateObject(addr)
   299  	if stateObject != nil {
   300  		stateObject.SetNonce(nonce)
   301  	}
   302  }
   303  
   304  func (self *StateDB) SetCode(addr common.Address, code []byte) {
   305  	stateObject := self.GetOrNewStateObject(addr)
   306  	if stateObject != nil {
   307  		stateObject.SetCode(crypto.Keccak256Hash(code), code)
   308  	}
   309  }
   310  
   311  func (self *StateDB) SetState(addr common.Address, key common.Hash, value common.Hash) {
   312  	stateObject := self.GetOrNewStateObject(addr)
   313  	if stateObject != nil {
   314  		stateObject.SetState(self.db, key, value)
   315  	}
   316  }
   317  
   318  // Suicide marks the given account as suicided.
   319  // This clears the account balance.
   320  //
   321  // The account's state object is still available until the state is committed,
   322  // getStateObject will return a non-nil account after Suicide.
   323  func (self *StateDB) Suicide(addr common.Address) bool {
   324  	stateObject := self.getStateObject(addr)
   325  	if stateObject == nil {
   326  		return false
   327  	}
   328  	self.journal = append(self.journal, suicideChange{
   329  		account:     &addr,
   330  		prev:        stateObject.suicided,
   331  		prevbalance: new(big.Int).Set(stateObject.Balance()),
   332  	})
   333  	stateObject.markSuicided()
   334  	stateObject.data.Balance = new(big.Int)
   335  
   336  	return true
   337  }
   338  
   339  //
   340  // Setting, updating & deleting state object methods
   341  //
   342  
   343  // updateStateObject writes the given object to the trie.
   344  func (self *StateDB) updateStateObject(stateObject *stateObject) {
   345  	addr := stateObject.Address()
   346  	data, err := rlp.EncodeToBytes(stateObject)
   347  	if err != nil {
   348  		panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err))
   349  	}
   350  	self.setError(self.trie.TryUpdate(addr[:], data))
   351  }
   352  
   353  // deleteStateObject removes the given object from the state trie.
   354  func (self *StateDB) deleteStateObject(stateObject *stateObject) {
   355  	stateObject.deleted = true
   356  	addr := stateObject.Address()
   357  	self.setError(self.trie.TryDelete(addr[:]))
   358  }
   359  
   360  // Retrieve a state object given my the address. Returns nil if not found.
   361  func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) {
   362  	// Prefer 'live' objects.
   363  	if obj := self.stateObjects[addr]; obj != nil {
   364  		if obj.deleted {
   365  			return nil
   366  		}
   367  		return obj
   368  	}
   369  
   370  	// Load the object from the database.
   371  	enc, err := self.trie.TryGet(addr[:])
   372  	if len(enc) == 0 {
   373  		self.setError(err)
   374  		return nil
   375  	}
   376  	var data Account
   377  	if err := rlp.DecodeBytes(enc, &data); err != nil {
   378  		log.Error("Failed to decode state object", "addr", addr, "err", err)
   379  		return nil
   380  	}
   381  	// Insert into the live set.
   382  	obj := newObject(self, addr, data, self.MarkStateObjectDirty)
   383  	self.setStateObject(obj)
   384  	return obj
   385  }
   386  
   387  func (self *StateDB) setStateObject(object *stateObject) {
   388  	self.stateObjects[object.Address()] = object
   389  }
   390  
   391  // Retrieve a state object or create a new state object if nil
   392  func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject {
   393  	stateObject := self.getStateObject(addr)
   394  	if stateObject == nil || stateObject.deleted {
   395  		stateObject, _ = self.createObject(addr)
   396  	}
   397  	return stateObject
   398  }
   399  
   400  // MarkStateObjectDirty adds the specified object to the dirty map to avoid costly
   401  // state object cache iteration to find a handful of modified ones.
   402  func (self *StateDB) MarkStateObjectDirty(addr common.Address) {
   403  	self.stateObjectsDirty[addr] = struct{}{}
   404  }
   405  
   406  // createObject creates a new state object. If there is an existing account with
   407  // the given address, it is overwritten and returned as the second return value.
   408  func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
   409  	prev = self.getStateObject(addr)
   410  	newobj = newObject(self, addr, Account{}, self.MarkStateObjectDirty)
   411  	newobj.setNonce(0) // sets the object to dirty
   412  	if prev == nil {
   413  		self.journal = append(self.journal, createObjectChange{account: &addr})
   414  	} else {
   415  		self.journal = append(self.journal, resetObjectChange{prev: prev})
   416  	}
   417  	self.setStateObject(newobj)
   418  	return newobj, prev
   419  }
   420  
   421  // CreateAccount explicitly creates a state object. If a state object with the address
   422  // already exists the balance is carried over to the new account.
   423  //
   424  // CreateAccount is called during the EVM CREATE operation. The situation might arise that
   425  // a contract does the following:
   426  //
   427  //   1. sends funds to sha(account ++ (nonce + 1))
   428  //   2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
   429  //
   430  // Carrying over the balance ensures that Ether doesn't disappear.
   431  func (self *StateDB) CreateAccount(addr common.Address) {
   432  	new, prev := self.createObject(addr)
   433  	if prev != nil {
   434  		new.setBalance(prev.data.Balance)
   435  	}
   436  }
   437  
   438  func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) {
   439  	so := db.getStateObject(addr)
   440  	if so == nil {
   441  		return
   442  	}
   443  
   444  	// When iterating over the storage check the cache first
   445  	for h, value := range so.cachedStorage {
   446  		cb(h, value)
   447  	}
   448  
   449  	it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
   450  	for it.Next() {
   451  		// ignore cached values
   452  		key := common.BytesToHash(db.trie.GetKey(it.Key))
   453  		if _, ok := so.cachedStorage[key]; !ok {
   454  			cb(key, common.BytesToHash(it.Value))
   455  		}
   456  	}
   457  }
   458  
   459  // Copy creates a deep, independent copy of the state.
   460  // Snapshots of the copied state cannot be applied to the copy.
   461  func (self *StateDB) Copy() *StateDB {
   462  	self.lock.Lock()
   463  	defer self.lock.Unlock()
   464  
   465  	// Copy all the basic fields, initialize the memory ones
   466  	state := &StateDB{
   467  		db:                self.db,
   468  		trie:              self.db.CopyTrie(self.trie),
   469  		stateObjects:      make(map[common.Address]*stateObject, len(self.stateObjectsDirty)),
   470  		stateObjectsDirty: make(map[common.Address]struct{}, len(self.stateObjectsDirty)),
   471  		refund:            self.refund,
   472  		logs:              make(map[common.Hash][]*types.Log, len(self.logs)),
   473  		logSize:           self.logSize,
   474  		preimages:         make(map[common.Hash][]byte),
   475  	}
   476  	// Copy the dirty states, logs, and preimages
   477  	for addr := range self.stateObjectsDirty {
   478  		state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state, state.MarkStateObjectDirty)
   479  		state.stateObjectsDirty[addr] = struct{}{}
   480  	}
   481  	for hash, logs := range self.logs {
   482  		state.logs[hash] = make([]*types.Log, len(logs))
   483  		copy(state.logs[hash], logs)
   484  	}
   485  	for hash, preimage := range self.preimages {
   486  		state.preimages[hash] = preimage
   487  	}
   488  	return state
   489  }
   490  
   491  // Snapshot returns an identifier for the current revision of the state.
   492  func (self *StateDB) Snapshot() int {
   493  	id := self.nextRevisionId
   494  	self.nextRevisionId++
   495  	self.validRevisions = append(self.validRevisions, revision{id, len(self.journal)})
   496  	return id
   497  }
   498  
   499  // RevertToSnapshot reverts all state changes made since the given revision.
   500  func (self *StateDB) RevertToSnapshot(revid int) {
   501  	// Find the snapshot in the stack of valid snapshots.
   502  	idx := sort.Search(len(self.validRevisions), func(i int) bool {
   503  		return self.validRevisions[i].id >= revid
   504  	})
   505  	if idx == len(self.validRevisions) || self.validRevisions[idx].id != revid {
   506  		panic(fmt.Errorf("revision id %v cannot be reverted", revid))
   507  	}
   508  	snapshot := self.validRevisions[idx].journalIndex
   509  
   510  	// Replay the journal to undo changes.
   511  	for i := len(self.journal) - 1; i >= snapshot; i-- {
   512  		self.journal[i].undo(self)
   513  	}
   514  	self.journal = self.journal[:snapshot]
   515  
   516  	// Remove invalidated snapshots from the stack.
   517  	self.validRevisions = self.validRevisions[:idx]
   518  }
   519  
   520  // GetRefund returns the current value of the refund counter.
   521  func (self *StateDB) GetRefund() uint64 {
   522  	return self.refund
   523  }
   524  
   525  // Finalise finalises the state by removing the self destructed objects
   526  // and clears the journal as well as the refunds.
   527  func (s *StateDB) Finalise(deleteEmptyObjects bool) {
   528  	for addr := range s.stateObjectsDirty {
   529  		stateObject := s.stateObjects[addr]
   530  		if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) {
   531  			s.deleteStateObject(stateObject)
   532  		} else {
   533  			stateObject.updateRoot(s.db)
   534  			s.updateStateObject(stateObject)
   535  		}
   536  	}
   537  	// Invalidate journal because reverting across transactions is not allowed.
   538  	s.clearJournalAndRefund()
   539  }
   540  
   541  // IntermediateRoot computes the current root hash of the state trie.
   542  // It is called in between transactions to get the root hash that
   543  // goes into transaction receipts.
   544  func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
   545  	s.Finalise(deleteEmptyObjects)
   546  	return s.trie.Hash()
   547  }
   548  
   549  // Prepare sets the current transaction hash and index and block hash which is
   550  // used when the EVM emits new state logs.
   551  func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) {
   552  	self.thash = thash
   553  	self.bhash = bhash
   554  	self.txIndex = ti
   555  }
   556  
   557  // DeleteSuicides flags the suicided objects for deletion so that it
   558  // won't be referenced again when called / queried up on.
   559  //
   560  // DeleteSuicides should not be used for consensus related updates
   561  // under any circumstances.
   562  func (s *StateDB) DeleteSuicides() {
   563  	// Reset refund so that any used-gas calculations can use this method.
   564  	s.clearJournalAndRefund()
   565  
   566  	for addr := range s.stateObjectsDirty {
   567  		stateObject := s.stateObjects[addr]
   568  
   569  		// If the object has been removed by a suicide
   570  		// flag the object as deleted.
   571  		if stateObject.suicided {
   572  			stateObject.deleted = true
   573  		}
   574  		delete(s.stateObjectsDirty, addr)
   575  	}
   576  }
   577  
   578  func (s *StateDB) clearJournalAndRefund() {
   579  	s.journal = nil
   580  	s.validRevisions = s.validRevisions[:0]
   581  	s.refund = 0
   582  }
   583  
   584  // Commit writes the state to the underlying in-memory trie database.
   585  func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) {
   586  	defer s.clearJournalAndRefund()
   587  
   588  	// Commit objects to the trie.
   589  	for addr, stateObject := range s.stateObjects {
   590  		_, isDirty := s.stateObjectsDirty[addr]
   591  		switch {
   592  		case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()):
   593  			// If the object has been removed, don't bother syncing it
   594  			// and just mark it for deletion in the trie.
   595  			s.deleteStateObject(stateObject)
   596  		case isDirty:
   597  			// Write any contract code associated with the state object
   598  			if stateObject.code != nil && stateObject.dirtyCode {
   599  				s.db.TrieDB().Insert(common.BytesToHash(stateObject.CodeHash()), stateObject.code)
   600  				stateObject.dirtyCode = false
   601  			}
   602  			// Write any storage changes in the state object to its storage trie.
   603  			if err := stateObject.CommitTrie(s.db); err != nil {
   604  				return common.Hash{}, err
   605  			}
   606  			// Update the object in the main account trie.
   607  			s.updateStateObject(stateObject)
   608  		}
   609  		delete(s.stateObjectsDirty, addr)
   610  	}
   611  	// Write trie changes.
   612  	root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error {
   613  		var account Account
   614  		if err := rlp.DecodeBytes(leaf, &account); err != nil {
   615  			return nil
   616  		}
   617  		if account.Root != emptyState {
   618  			s.db.TrieDB().Reference(account.Root, parent)
   619  		}
   620  		code := common.BytesToHash(account.CodeHash)
   621  		if code != emptyCode {
   622  			s.db.TrieDB().Reference(code, parent)
   623  		}
   624  		return nil
   625  	})
   626  	log.Debug("Trie cache stats after commit", "misses", trie.CacheMisses(), "unloads", trie.CacheUnloads())
   627  	return root, err
   628  }