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