|
|
@ -52,7 +52,7 @@ func (n *proofList) Put(key []byte, value []byte) error { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// StateDBs within the ethereum protocol are used to store anything
|
|
|
|
// StateDB within the ethereum protocol are used to store anything
|
|
|
|
// within the merkle trie. StateDBs take care of caching and storing
|
|
|
|
// within the merkle trie. StateDBs take care of caching and storing
|
|
|
|
// nested states. It's the general query interface to retrieve:
|
|
|
|
// nested states. It's the general query interface to retrieve:
|
|
|
|
// * Contracts
|
|
|
|
// * Contracts
|
|
|
@ -86,12 +86,12 @@ type StateDB struct { |
|
|
|
// Snapshot and RevertToSnapshot.
|
|
|
|
// Snapshot and RevertToSnapshot.
|
|
|
|
journal *journal |
|
|
|
journal *journal |
|
|
|
validRevisions []revision |
|
|
|
validRevisions []revision |
|
|
|
nextRevisionId int |
|
|
|
nextRevisionID int |
|
|
|
|
|
|
|
|
|
|
|
lock sync.Mutex |
|
|
|
lock sync.Mutex |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Create a new state from a given trie.
|
|
|
|
// New creates a new state from a given trie.
|
|
|
|
func New(root common.Hash, db Database) (*StateDB, error) { |
|
|
|
func New(root common.Hash, db Database) (*StateDB, error) { |
|
|
|
tr, err := db.OpenTrie(root) |
|
|
|
tr, err := db.OpenTrie(root) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -109,114 +109,118 @@ func New(root common.Hash, db Database) (*StateDB, error) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// setError remembers the first non-nil error it is called with.
|
|
|
|
// setError remembers the first non-nil error it is called with.
|
|
|
|
func (self *StateDB) setError(err error) { |
|
|
|
func (stateDB *StateDB) setError(err error) { |
|
|
|
if self.dbErr == nil { |
|
|
|
if stateDB.dbErr == nil { |
|
|
|
self.dbErr = err |
|
|
|
stateDB.dbErr = err |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) Error() error { |
|
|
|
func (stateDB *StateDB) Error() error { |
|
|
|
return self.dbErr |
|
|
|
return stateDB.dbErr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Reset clears out all ephemeral state objects from the state db, but keeps
|
|
|
|
// Reset clears out all ephemeral state objects from the state db, but keeps
|
|
|
|
// the underlying state trie to avoid reloading data for the next operations.
|
|
|
|
// the underlying state trie to avoid reloading data for the next operations.
|
|
|
|
func (self *StateDB) Reset(root common.Hash) error { |
|
|
|
func (stateDB *StateDB) Reset(root common.Hash) error { |
|
|
|
tr, err := self.db.OpenTrie(root) |
|
|
|
tr, err := stateDB.db.OpenTrie(root) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
return err |
|
|
|
} |
|
|
|
} |
|
|
|
self.trie = tr |
|
|
|
stateDB.trie = tr |
|
|
|
self.stateObjects = make(map[common.Address]*stateObject) |
|
|
|
stateDB.stateObjects = make(map[common.Address]*stateObject) |
|
|
|
self.stateObjectsDirty = make(map[common.Address]struct{}) |
|
|
|
stateDB.stateObjectsDirty = make(map[common.Address]struct{}) |
|
|
|
self.thash = common.Hash{} |
|
|
|
stateDB.thash = common.Hash{} |
|
|
|
self.bhash = common.Hash{} |
|
|
|
stateDB.bhash = common.Hash{} |
|
|
|
self.txIndex = 0 |
|
|
|
stateDB.txIndex = 0 |
|
|
|
self.logs = make(map[common.Hash][]*types.Log) |
|
|
|
stateDB.logs = make(map[common.Hash][]*types.Log) |
|
|
|
self.logSize = 0 |
|
|
|
stateDB.logSize = 0 |
|
|
|
self.preimages = make(map[common.Hash][]byte) |
|
|
|
stateDB.preimages = make(map[common.Hash][]byte) |
|
|
|
self.clearJournalAndRefund() |
|
|
|
stateDB.clearJournalAndRefund() |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) AddLog(log *types.Log) { |
|
|
|
// AddLog adds logs into stateDB
|
|
|
|
self.journal.append(addLogChange{txhash: self.thash}) |
|
|
|
func (stateDB *StateDB) AddLog(log *types.Log) { |
|
|
|
|
|
|
|
stateDB.journal.append(addLogChange{txhash: stateDB.thash}) |
|
|
|
|
|
|
|
|
|
|
|
log.TxHash = self.thash |
|
|
|
log.TxHash = stateDB.thash |
|
|
|
log.BlockHash = self.bhash |
|
|
|
log.BlockHash = stateDB.bhash |
|
|
|
log.TxIndex = uint(self.txIndex) |
|
|
|
log.TxIndex = uint(stateDB.txIndex) |
|
|
|
log.Index = self.logSize |
|
|
|
log.Index = stateDB.logSize |
|
|
|
self.logs[self.thash] = append(self.logs[self.thash], log) |
|
|
|
stateDB.logs[stateDB.thash] = append(stateDB.logs[stateDB.thash], log) |
|
|
|
self.logSize++ |
|
|
|
stateDB.logSize++ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) GetLogs(hash common.Hash) []*types.Log { |
|
|
|
// GetLogs gets logs from stateDB given a hash
|
|
|
|
return self.logs[hash] |
|
|
|
func (stateDB *StateDB) GetLogs(hash common.Hash) []*types.Log { |
|
|
|
|
|
|
|
return stateDB.logs[hash] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) Logs() []*types.Log { |
|
|
|
// Logs returns a list of Log.
|
|
|
|
|
|
|
|
func (stateDB *StateDB) Logs() []*types.Log { |
|
|
|
var logs []*types.Log |
|
|
|
var logs []*types.Log |
|
|
|
for _, lgs := range self.logs { |
|
|
|
for _, lgs := range stateDB.logs { |
|
|
|
logs = append(logs, lgs...) |
|
|
|
logs = append(logs, lgs...) |
|
|
|
} |
|
|
|
} |
|
|
|
return logs |
|
|
|
return logs |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AddPreimage records a SHA3 preimage seen by the VM.
|
|
|
|
// AddPreimage records a SHA3 preimage seen by the VM.
|
|
|
|
func (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) { |
|
|
|
func (stateDB *StateDB) AddPreimage(hash common.Hash, preimage []byte) { |
|
|
|
if _, ok := self.preimages[hash]; !ok { |
|
|
|
if _, ok := stateDB.preimages[hash]; !ok { |
|
|
|
self.journal.append(addPreimageChange{hash: hash}) |
|
|
|
stateDB.journal.append(addPreimageChange{hash: hash}) |
|
|
|
pi := make([]byte, len(preimage)) |
|
|
|
pi := make([]byte, len(preimage)) |
|
|
|
copy(pi, preimage) |
|
|
|
copy(pi, preimage) |
|
|
|
self.preimages[hash] = pi |
|
|
|
stateDB.preimages[hash] = pi |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Preimages returns a list of SHA3 preimages that have been submitted.
|
|
|
|
// Preimages returns a list of SHA3 preimages that have been submitted.
|
|
|
|
func (self *StateDB) Preimages() map[common.Hash][]byte { |
|
|
|
func (stateDB *StateDB) Preimages() map[common.Hash][]byte { |
|
|
|
return self.preimages |
|
|
|
return stateDB.preimages |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// AddRefund adds gas to the refund counter
|
|
|
|
// AddRefund adds gas to the refund counter
|
|
|
|
func (self *StateDB) AddRefund(gas uint64) { |
|
|
|
func (stateDB *StateDB) AddRefund(gas uint64) { |
|
|
|
self.journal.append(refundChange{prev: self.refund}) |
|
|
|
stateDB.journal.append(refundChange{prev: stateDB.refund}) |
|
|
|
self.refund += gas |
|
|
|
stateDB.refund += gas |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SubRefund removes gas from the refund counter.
|
|
|
|
// SubRefund removes gas from the refund counter.
|
|
|
|
// This method will panic if the refund counter goes below zero
|
|
|
|
// This method will panic if the refund counter goes below zero
|
|
|
|
func (self *StateDB) SubRefund(gas uint64) { |
|
|
|
func (stateDB *StateDB) SubRefund(gas uint64) { |
|
|
|
self.journal.append(refundChange{prev: self.refund}) |
|
|
|
stateDB.journal.append(refundChange{prev: stateDB.refund}) |
|
|
|
if gas > self.refund { |
|
|
|
if gas > stateDB.refund { |
|
|
|
panic("Refund counter below zero") |
|
|
|
panic("Refund counter below zero") |
|
|
|
} |
|
|
|
} |
|
|
|
self.refund -= gas |
|
|
|
stateDB.refund -= gas |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Exist reports whether the given account address exists in the state.
|
|
|
|
// Exist reports whether the given account address exists in the state.
|
|
|
|
// Notably this also returns true for suicided accounts.
|
|
|
|
// Notably this also returns true for suicided accounts.
|
|
|
|
func (self *StateDB) Exist(addr common.Address) bool { |
|
|
|
func (stateDB *StateDB) Exist(addr common.Address) bool { |
|
|
|
return self.getStateObject(addr) != nil |
|
|
|
return stateDB.getStateObject(addr) != nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Empty returns whether the state object is either non-existent
|
|
|
|
// Empty returns whether the state object is either non-existent
|
|
|
|
// or empty according to the EIP161 specification (balance = nonce = code = 0)
|
|
|
|
// or empty according to the EIP161 specification (balance = nonce = code = 0)
|
|
|
|
func (self *StateDB) Empty(addr common.Address) bool { |
|
|
|
func (stateDB *StateDB) Empty(addr common.Address) bool { |
|
|
|
so := self.getStateObject(addr) |
|
|
|
so := stateDB.getStateObject(addr) |
|
|
|
return so == nil || so.empty() |
|
|
|
return so == nil || so.empty() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve the balance from the given address or 0 if object not found
|
|
|
|
// GetBalance retrieves the balance from the given address or 0 if object not found
|
|
|
|
func (self *StateDB) GetBalance(addr common.Address) *big.Int { |
|
|
|
func (stateDB *StateDB) GetBalance(addr common.Address) *big.Int { |
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
return stateObject.Balance() |
|
|
|
return stateObject.Balance() |
|
|
|
} |
|
|
|
} |
|
|
|
return common.Big0 |
|
|
|
return common.Big0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) GetNonce(addr common.Address) uint64 { |
|
|
|
// GetNonce returns the nonce of the given address.
|
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
func (stateDB *StateDB) GetNonce(addr common.Address) uint64 { |
|
|
|
|
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
return stateObject.Nonce() |
|
|
|
return stateObject.Nonce() |
|
|
|
} |
|
|
|
} |
|
|
@ -224,31 +228,34 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 { |
|
|
|
return 0 |
|
|
|
return 0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) GetCode(addr common.Address) []byte { |
|
|
|
// GetCode returns code of a given address.
|
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
func (stateDB *StateDB) GetCode(addr common.Address) []byte { |
|
|
|
|
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
return stateObject.Code(self.db) |
|
|
|
return stateObject.Code(stateDB.db) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) GetCodeSize(addr common.Address) int { |
|
|
|
// GetCodeSize returns code size of a given address in stateDB.
|
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
func (stateDB *StateDB) GetCodeSize(addr common.Address) int { |
|
|
|
|
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject == nil { |
|
|
|
if stateObject == nil { |
|
|
|
return 0 |
|
|
|
return 0 |
|
|
|
} |
|
|
|
} |
|
|
|
if stateObject.code != nil { |
|
|
|
if stateObject.code != nil { |
|
|
|
return len(stateObject.code) |
|
|
|
return len(stateObject.code) |
|
|
|
} |
|
|
|
} |
|
|
|
size, err := self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash())) |
|
|
|
size, err := stateDB.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash())) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
self.setError(err) |
|
|
|
stateDB.setError(err) |
|
|
|
} |
|
|
|
} |
|
|
|
return size |
|
|
|
return size |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) GetCodeHash(addr common.Address) common.Hash { |
|
|
|
// GetCodeHash returns code hash of a given address.
|
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
func (stateDB *StateDB) GetCodeHash(addr common.Address) common.Hash { |
|
|
|
|
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject == nil { |
|
|
|
if stateObject == nil { |
|
|
|
return common.Hash{} |
|
|
|
return common.Hash{} |
|
|
|
} |
|
|
|
} |
|
|
@ -256,25 +263,25 @@ func (self *StateDB) GetCodeHash(addr common.Address) common.Hash { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetState retrieves a value from the given account's storage trie.
|
|
|
|
// GetState retrieves a value from the given account's storage trie.
|
|
|
|
func (self *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash { |
|
|
|
func (stateDB *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash { |
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
return stateObject.GetState(self.db, hash) |
|
|
|
return stateObject.GetState(stateDB.db, hash) |
|
|
|
} |
|
|
|
} |
|
|
|
return common.Hash{} |
|
|
|
return common.Hash{} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetProof returns the MerkleProof for a given Account
|
|
|
|
// GetProof returns the MerkleProof for a given Account
|
|
|
|
func (self *StateDB) GetProof(a common.Address) ([][]byte, error) { |
|
|
|
func (stateDB *StateDB) GetProof(a common.Address) ([][]byte, error) { |
|
|
|
var proof proofList |
|
|
|
var proof proofList |
|
|
|
err := self.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof) |
|
|
|
err := stateDB.trie.Prove(crypto.Keccak256(a.Bytes()), 0, &proof) |
|
|
|
return [][]byte(proof), err |
|
|
|
return [][]byte(proof), err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetProof returns the StorageProof for given key
|
|
|
|
// GetStorageProof returns the StorageProof for given key
|
|
|
|
func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) { |
|
|
|
func (stateDB *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byte, error) { |
|
|
|
var proof proofList |
|
|
|
var proof proofList |
|
|
|
trie := self.StorageTrie(a) |
|
|
|
trie := stateDB.StorageTrie(a) |
|
|
|
if trie == nil { |
|
|
|
if trie == nil { |
|
|
|
return proof, errors.New("storage trie for requested address does not exist") |
|
|
|
return proof, errors.New("storage trie for requested address does not exist") |
|
|
|
} |
|
|
|
} |
|
|
@ -283,32 +290,33 @@ func (self *StateDB) GetStorageProof(a common.Address, key common.Hash) ([][]byt |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetCommittedState retrieves a value from the given account's committed storage trie.
|
|
|
|
// GetCommittedState retrieves a value from the given account's committed storage trie.
|
|
|
|
func (self *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash { |
|
|
|
func (stateDB *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash { |
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
return stateObject.GetCommittedState(self.db, hash) |
|
|
|
return stateObject.GetCommittedState(stateDB.db, hash) |
|
|
|
} |
|
|
|
} |
|
|
|
return common.Hash{} |
|
|
|
return common.Hash{} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Database retrieves the low level database supporting the lower level trie ops.
|
|
|
|
// Database retrieves the low level database supporting the lower level trie ops.
|
|
|
|
func (self *StateDB) Database() Database { |
|
|
|
func (stateDB *StateDB) Database() Database { |
|
|
|
return self.db |
|
|
|
return stateDB.db |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// StorageTrie returns the storage trie of an account.
|
|
|
|
// StorageTrie returns the storage trie of an account.
|
|
|
|
// The return value is a copy and is nil for non-existent accounts.
|
|
|
|
// The return value is a copy and is nil for non-existent accounts.
|
|
|
|
func (self *StateDB) StorageTrie(addr common.Address) Trie { |
|
|
|
func (stateDB *StateDB) StorageTrie(addr common.Address) Trie { |
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject == nil { |
|
|
|
if stateObject == nil { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
cpy := stateObject.deepCopy(self) |
|
|
|
cpy := stateObject.deepCopy(stateDB) |
|
|
|
return cpy.updateTrie(self.db) |
|
|
|
return cpy.updateTrie(stateDB.db) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) HasSuicided(addr common.Address) bool { |
|
|
|
// HasSuicided checks if the state object of the given addr is suicided.
|
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
func (stateDB *StateDB) HasSuicided(addr common.Address) bool { |
|
|
|
|
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
return stateObject.suicided |
|
|
|
return stateObject.suicided |
|
|
|
} |
|
|
|
} |
|
|
@ -320,46 +328,50 @@ func (self *StateDB) HasSuicided(addr common.Address) bool { |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
// AddBalance adds amount to the account associated with addr.
|
|
|
|
// AddBalance adds amount to the account associated with addr.
|
|
|
|
func (self *StateDB) AddBalance(addr common.Address, amount *big.Int) { |
|
|
|
func (stateDB *StateDB) AddBalance(addr common.Address, amount *big.Int) { |
|
|
|
stateObject := self.GetOrNewStateObject(addr) |
|
|
|
stateObject := stateDB.GetOrNewStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
stateObject.AddBalance(amount) |
|
|
|
stateObject.AddBalance(amount) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// SubBalance subtracts amount from the account associated with addr.
|
|
|
|
// SubBalance subtracts amount from the account associated with addr.
|
|
|
|
func (self *StateDB) SubBalance(addr common.Address, amount *big.Int) { |
|
|
|
func (stateDB *StateDB) SubBalance(addr common.Address, amount *big.Int) { |
|
|
|
stateObject := self.GetOrNewStateObject(addr) |
|
|
|
stateObject := stateDB.GetOrNewStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
stateObject.SubBalance(amount) |
|
|
|
stateObject.SubBalance(amount) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) SetBalance(addr common.Address, amount *big.Int) { |
|
|
|
// SetBalance sets balance of an address.
|
|
|
|
stateObject := self.GetOrNewStateObject(addr) |
|
|
|
func (stateDB *StateDB) SetBalance(addr common.Address, amount *big.Int) { |
|
|
|
|
|
|
|
stateObject := stateDB.GetOrNewStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
stateObject.SetBalance(amount) |
|
|
|
stateObject.SetBalance(amount) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) SetNonce(addr common.Address, nonce uint64) { |
|
|
|
// SetNonce sets nonce of a given address.
|
|
|
|
stateObject := self.GetOrNewStateObject(addr) |
|
|
|
func (stateDB *StateDB) SetNonce(addr common.Address, nonce uint64) { |
|
|
|
|
|
|
|
stateObject := stateDB.GetOrNewStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
stateObject.SetNonce(nonce) |
|
|
|
stateObject.SetNonce(nonce) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) SetCode(addr common.Address, code []byte) { |
|
|
|
// SetCode sets code of a given address.
|
|
|
|
stateObject := self.GetOrNewStateObject(addr) |
|
|
|
func (stateDB *StateDB) SetCode(addr common.Address, code []byte) { |
|
|
|
|
|
|
|
stateObject := stateDB.GetOrNewStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
stateObject.SetCode(crypto.Keccak256Hash(code), code) |
|
|
|
stateObject.SetCode(crypto.Keccak256Hash(code), code) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) SetState(addr common.Address, key, value common.Hash) { |
|
|
|
// SetState sets hash value of a given address.
|
|
|
|
stateObject := self.GetOrNewStateObject(addr) |
|
|
|
func (stateDB *StateDB) SetState(addr common.Address, key, value common.Hash) { |
|
|
|
|
|
|
|
stateObject := stateDB.GetOrNewStateObject(addr) |
|
|
|
if stateObject != nil { |
|
|
|
if stateObject != nil { |
|
|
|
stateObject.SetState(self.db, key, value) |
|
|
|
stateObject.SetState(stateDB.db, key, value) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -368,12 +380,12 @@ func (self *StateDB) SetState(addr common.Address, key, value common.Hash) { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// The account's state object is still available until the state is committed,
|
|
|
|
// The account's state object is still available until the state is committed,
|
|
|
|
// getStateObject will return a non-nil account after Suicide.
|
|
|
|
// getStateObject will return a non-nil account after Suicide.
|
|
|
|
func (self *StateDB) Suicide(addr common.Address) bool { |
|
|
|
func (stateDB *StateDB) Suicide(addr common.Address) bool { |
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject == nil { |
|
|
|
if stateObject == nil { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
self.journal.append(suicideChange{ |
|
|
|
stateDB.journal.append(suicideChange{ |
|
|
|
account: &addr, |
|
|
|
account: &addr, |
|
|
|
prev: stateObject.suicided, |
|
|
|
prev: stateObject.suicided, |
|
|
|
prevbalance: new(big.Int).Set(stateObject.Balance()), |
|
|
|
prevbalance: new(big.Int).Set(stateObject.Balance()), |
|
|
@ -389,26 +401,26 @@ func (self *StateDB) Suicide(addr common.Address) bool { |
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
// updateStateObject writes the given object to the trie.
|
|
|
|
// updateStateObject writes the given object to the trie.
|
|
|
|
func (self *StateDB) updateStateObject(stateObject *stateObject) { |
|
|
|
func (stateDB *StateDB) updateStateObject(stateObject *stateObject) { |
|
|
|
addr := stateObject.Address() |
|
|
|
addr := stateObject.Address() |
|
|
|
data, err := rlp.EncodeToBytes(stateObject) |
|
|
|
data, err := rlp.EncodeToBytes(stateObject) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) |
|
|
|
panic(fmt.Errorf("can't encode object at %x: %v", addr[:], err)) |
|
|
|
} |
|
|
|
} |
|
|
|
self.setError(self.trie.TryUpdate(addr[:], data)) |
|
|
|
stateDB.setError(stateDB.trie.TryUpdate(addr[:], data)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// deleteStateObject removes the given object from the state trie.
|
|
|
|
// deleteStateObject removes the given object from the state trie.
|
|
|
|
func (self *StateDB) deleteStateObject(stateObject *stateObject) { |
|
|
|
func (stateDB *StateDB) deleteStateObject(stateObject *stateObject) { |
|
|
|
stateObject.deleted = true |
|
|
|
stateObject.deleted = true |
|
|
|
addr := stateObject.Address() |
|
|
|
addr := stateObject.Address() |
|
|
|
self.setError(self.trie.TryDelete(addr[:])) |
|
|
|
stateDB.setError(stateDB.trie.TryDelete(addr[:])) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve a state object given by the address. Returns nil if not found.
|
|
|
|
// Retrieve a state object given by the address. Returns nil if not found.
|
|
|
|
func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) { |
|
|
|
func (stateDB *StateDB) getStateObject(addr common.Address) (stateObject *stateObject) { |
|
|
|
// Prefer 'live' objects.
|
|
|
|
// Prefer 'live' objects.
|
|
|
|
if obj := self.stateObjects[addr]; obj != nil { |
|
|
|
if obj := stateDB.stateObjects[addr]; obj != nil { |
|
|
|
if obj.deleted { |
|
|
|
if obj.deleted { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
@ -416,9 +428,9 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Load the object from the database.
|
|
|
|
// Load the object from the database.
|
|
|
|
enc, err := self.trie.TryGet(addr[:]) |
|
|
|
enc, err := stateDB.trie.TryGet(addr[:]) |
|
|
|
if len(enc) == 0 { |
|
|
|
if len(enc) == 0 { |
|
|
|
self.setError(err) |
|
|
|
stateDB.setError(err) |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
var data Account |
|
|
|
var data Account |
|
|
@ -427,36 +439,36 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
// Insert into the live set.
|
|
|
|
// Insert into the live set.
|
|
|
|
obj := newObject(self, addr, data) |
|
|
|
obj := newObject(stateDB, addr, data) |
|
|
|
self.setStateObject(obj) |
|
|
|
stateDB.setStateObject(obj) |
|
|
|
return obj |
|
|
|
return obj |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) setStateObject(object *stateObject) { |
|
|
|
func (stateDB *StateDB) setStateObject(object *stateObject) { |
|
|
|
self.stateObjects[object.Address()] = object |
|
|
|
stateDB.stateObjects[object.Address()] = object |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Retrieve a state object or create a new state object if nil.
|
|
|
|
// GetOrNewStateObject retrieves a state object or create a new state object if nil.
|
|
|
|
func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject { |
|
|
|
func (stateDB *StateDB) GetOrNewStateObject(addr common.Address) *stateObject { |
|
|
|
stateObject := self.getStateObject(addr) |
|
|
|
stateObject := stateDB.getStateObject(addr) |
|
|
|
if stateObject == nil || stateObject.deleted { |
|
|
|
if stateObject == nil || stateObject.deleted { |
|
|
|
stateObject, _ = self.createObject(addr) |
|
|
|
stateObject, _ = stateDB.createObject(addr) |
|
|
|
} |
|
|
|
} |
|
|
|
return stateObject |
|
|
|
return stateObject |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// createObject creates a new state object. If there is an existing account with
|
|
|
|
// createObject creates a new state object. If there is an existing account with
|
|
|
|
// the given address, it is overwritten and returned as the second return value.
|
|
|
|
// the given address, it is overwritten and returned as the second return value.
|
|
|
|
func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) { |
|
|
|
func (stateDB *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) { |
|
|
|
prev = self.getStateObject(addr) |
|
|
|
prev = stateDB.getStateObject(addr) |
|
|
|
newobj = newObject(self, addr, Account{}) |
|
|
|
newobj = newObject(stateDB, addr, Account{}) |
|
|
|
newobj.setNonce(0) // sets the object to dirty
|
|
|
|
newobj.setNonce(0) // sets the object to dirty
|
|
|
|
if prev == nil { |
|
|
|
if prev == nil { |
|
|
|
self.journal.append(createObjectChange{account: &addr}) |
|
|
|
stateDB.journal.append(createObjectChange{account: &addr}) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
self.journal.append(resetObjectChange{prev: prev}) |
|
|
|
stateDB.journal.append(resetObjectChange{prev: prev}) |
|
|
|
} |
|
|
|
} |
|
|
|
self.setStateObject(newobj) |
|
|
|
stateDB.setStateObject(newobj) |
|
|
|
return newobj, prev |
|
|
|
return newobj, prev |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -470,21 +482,22 @@ func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObjec |
|
|
|
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
|
|
|
|
// 2. tx_create(sha(account ++ nonce)) (note that this gets the address of 1)
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Carrying over the balance ensures that Ether doesn't disappear.
|
|
|
|
// Carrying over the balance ensures that Ether doesn't disappear.
|
|
|
|
func (self *StateDB) CreateAccount(addr common.Address) { |
|
|
|
func (stateDB *StateDB) CreateAccount(addr common.Address) { |
|
|
|
new, prev := self.createObject(addr) |
|
|
|
new, prev := stateDB.createObject(addr) |
|
|
|
if prev != nil { |
|
|
|
if prev != nil { |
|
|
|
new.setBalance(prev.data.Balance) |
|
|
|
new.setBalance(prev.data.Balance) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) { |
|
|
|
// ForEachStorage runs a function on every item in state DB.
|
|
|
|
so := db.getStateObject(addr) |
|
|
|
func (stateDB *StateDB) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) { |
|
|
|
|
|
|
|
so := stateDB.getStateObject(addr) |
|
|
|
if so == nil { |
|
|
|
if so == nil { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil)) |
|
|
|
it := trie.NewIterator(so.getTrie(stateDB.db).NodeIterator(nil)) |
|
|
|
for it.Next() { |
|
|
|
for it.Next() { |
|
|
|
key := common.BytesToHash(db.trie.GetKey(it.Key)) |
|
|
|
key := common.BytesToHash(stateDB.trie.GetKey(it.Key)) |
|
|
|
if value, dirty := so.dirtyStorage[key]; dirty { |
|
|
|
if value, dirty := so.dirtyStorage[key]; dirty { |
|
|
|
cb(key, value) |
|
|
|
cb(key, value) |
|
|
|
continue |
|
|
|
continue |
|
|
@ -495,29 +508,29 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common |
|
|
|
|
|
|
|
|
|
|
|
// Copy creates a deep, independent copy of the state.
|
|
|
|
// Copy creates a deep, independent copy of the state.
|
|
|
|
// Snapshots of the copied state cannot be applied to the copy.
|
|
|
|
// Snapshots of the copied state cannot be applied to the copy.
|
|
|
|
func (self *StateDB) Copy() *StateDB { |
|
|
|
func (stateDB *StateDB) Copy() *StateDB { |
|
|
|
self.lock.Lock() |
|
|
|
stateDB.lock.Lock() |
|
|
|
defer self.lock.Unlock() |
|
|
|
defer stateDB.lock.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
// Copy all the basic fields, initialize the memory ones
|
|
|
|
// Copy all the basic fields, initialize the memory ones
|
|
|
|
state := &StateDB{ |
|
|
|
state := &StateDB{ |
|
|
|
db: self.db, |
|
|
|
db: stateDB.db, |
|
|
|
trie: self.db.CopyTrie(self.trie), |
|
|
|
trie: stateDB.db.CopyTrie(stateDB.trie), |
|
|
|
stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)), |
|
|
|
stateObjects: make(map[common.Address]*stateObject, len(stateDB.journal.dirties)), |
|
|
|
stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)), |
|
|
|
stateObjectsDirty: make(map[common.Address]struct{}, len(stateDB.journal.dirties)), |
|
|
|
refund: self.refund, |
|
|
|
refund: stateDB.refund, |
|
|
|
logs: make(map[common.Hash][]*types.Log, len(self.logs)), |
|
|
|
logs: make(map[common.Hash][]*types.Log, len(stateDB.logs)), |
|
|
|
logSize: self.logSize, |
|
|
|
logSize: stateDB.logSize, |
|
|
|
preimages: make(map[common.Hash][]byte), |
|
|
|
preimages: make(map[common.Hash][]byte), |
|
|
|
journal: newJournal(), |
|
|
|
journal: newJournal(), |
|
|
|
} |
|
|
|
} |
|
|
|
// Copy the dirty states, logs, and preimages
|
|
|
|
// Copy the dirty states, logs, and preimages
|
|
|
|
for addr := range self.journal.dirties { |
|
|
|
for addr := range stateDB.journal.dirties { |
|
|
|
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
|
|
|
|
// As documented [here](https://github.com/ethereum/go-ethereum/pull/16485#issuecomment-380438527),
|
|
|
|
// and in the Finalise-method, there is a case where an object is in the journal but not
|
|
|
|
// and in the Finalise-method, there is a case where an object is in the journal but not
|
|
|
|
// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
|
|
|
|
// in the stateObjects: OOG after touch on ripeMD prior to Byzantium. Thus, we need to check for
|
|
|
|
// nil
|
|
|
|
// nil
|
|
|
|
if object, exist := self.stateObjects[addr]; exist { |
|
|
|
if object, exist := stateDB.stateObjects[addr]; exist { |
|
|
|
state.stateObjects[addr] = object.deepCopy(state) |
|
|
|
state.stateObjects[addr] = object.deepCopy(state) |
|
|
|
state.stateObjectsDirty[addr] = struct{}{} |
|
|
|
state.stateObjectsDirty[addr] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
@ -525,13 +538,13 @@ func (self *StateDB) Copy() *StateDB { |
|
|
|
// Above, we don't copy the actual journal. This means that if the copy is copied, the
|
|
|
|
// Above, we don't copy the actual journal. This means that if the copy is copied, the
|
|
|
|
// loop above will be a no-op, since the copy's journal is empty.
|
|
|
|
// loop above will be a no-op, since the copy's journal is empty.
|
|
|
|
// Thus, here we iterate over stateObjects, to enable copies of copies
|
|
|
|
// Thus, here we iterate over stateObjects, to enable copies of copies
|
|
|
|
for addr := range self.stateObjectsDirty { |
|
|
|
for addr := range stateDB.stateObjectsDirty { |
|
|
|
if _, exist := state.stateObjects[addr]; !exist { |
|
|
|
if _, exist := state.stateObjects[addr]; !exist { |
|
|
|
state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state) |
|
|
|
state.stateObjects[addr] = stateDB.stateObjects[addr].deepCopy(state) |
|
|
|
state.stateObjectsDirty[addr] = struct{}{} |
|
|
|
state.stateObjectsDirty[addr] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
for hash, logs := range self.logs { |
|
|
|
for hash, logs := range stateDB.logs { |
|
|
|
cpy := make([]*types.Log, len(logs)) |
|
|
|
cpy := make([]*types.Log, len(logs)) |
|
|
|
for i, l := range logs { |
|
|
|
for i, l := range logs { |
|
|
|
cpy[i] = new(types.Log) |
|
|
|
cpy[i] = new(types.Log) |
|
|
@ -539,46 +552,46 @@ func (self *StateDB) Copy() *StateDB { |
|
|
|
} |
|
|
|
} |
|
|
|
state.logs[hash] = cpy |
|
|
|
state.logs[hash] = cpy |
|
|
|
} |
|
|
|
} |
|
|
|
for hash, preimage := range self.preimages { |
|
|
|
for hash, preimage := range stateDB.preimages { |
|
|
|
state.preimages[hash] = preimage |
|
|
|
state.preimages[hash] = preimage |
|
|
|
} |
|
|
|
} |
|
|
|
return state |
|
|
|
return state |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Snapshot returns an identifier for the current revision of the state.
|
|
|
|
// Snapshot returns an identifier for the current revision of the state.
|
|
|
|
func (self *StateDB) Snapshot() int { |
|
|
|
func (stateDB *StateDB) Snapshot() int { |
|
|
|
id := self.nextRevisionId |
|
|
|
id := stateDB.nextRevisionID |
|
|
|
self.nextRevisionId++ |
|
|
|
stateDB.nextRevisionID++ |
|
|
|
self.validRevisions = append(self.validRevisions, revision{id, self.journal.length()}) |
|
|
|
stateDB.validRevisions = append(stateDB.validRevisions, revision{id, stateDB.journal.length()}) |
|
|
|
return id |
|
|
|
return id |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// RevertToSnapshot reverts all state changes made since the given revision.
|
|
|
|
// RevertToSnapshot reverts all state changes made since the given revision.
|
|
|
|
func (self *StateDB) RevertToSnapshot(revid int) { |
|
|
|
func (stateDB *StateDB) RevertToSnapshot(revid int) { |
|
|
|
// Find the snapshot in the stack of valid snapshots.
|
|
|
|
// Find the snapshot in the stack of valid snapshots.
|
|
|
|
idx := sort.Search(len(self.validRevisions), func(i int) bool { |
|
|
|
idx := sort.Search(len(stateDB.validRevisions), func(i int) bool { |
|
|
|
return self.validRevisions[i].id >= revid |
|
|
|
return stateDB.validRevisions[i].id >= revid |
|
|
|
}) |
|
|
|
}) |
|
|
|
if idx == len(self.validRevisions) || self.validRevisions[idx].id != revid { |
|
|
|
if idx == len(stateDB.validRevisions) || stateDB.validRevisions[idx].id != revid { |
|
|
|
panic(fmt.Errorf("revision id %v cannot be reverted", revid)) |
|
|
|
panic(fmt.Errorf("revision id %v cannot be reverted", revid)) |
|
|
|
} |
|
|
|
} |
|
|
|
snapshot := self.validRevisions[idx].journalIndex |
|
|
|
snapshot := stateDB.validRevisions[idx].journalIndex |
|
|
|
|
|
|
|
|
|
|
|
// Replay the journal to undo changes and remove invalidated snapshots
|
|
|
|
// Replay the journal to undo changes and remove invalidated snapshots
|
|
|
|
self.journal.revert(self, snapshot) |
|
|
|
stateDB.journal.revert(stateDB, snapshot) |
|
|
|
self.validRevisions = self.validRevisions[:idx] |
|
|
|
stateDB.validRevisions = stateDB.validRevisions[:idx] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetRefund returns the current value of the refund counter.
|
|
|
|
// GetRefund returns the current value of the refund counter.
|
|
|
|
func (self *StateDB) GetRefund() uint64 { |
|
|
|
func (stateDB *StateDB) GetRefund() uint64 { |
|
|
|
return self.refund |
|
|
|
return stateDB.refund |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Finalise finalises the state by removing the self destructed objects
|
|
|
|
// Finalise finalises the state by removing the self destructed objects
|
|
|
|
// and clears the journal as well as the refunds.
|
|
|
|
// and clears the journal as well as the refunds.
|
|
|
|
func (s *StateDB) Finalise(deleteEmptyObjects bool) { |
|
|
|
func (stateDB *StateDB) Finalise(deleteEmptyObjects bool) { |
|
|
|
for addr := range s.journal.dirties { |
|
|
|
for addr := range stateDB.journal.dirties { |
|
|
|
stateObject, exist := s.stateObjects[addr] |
|
|
|
stateObject, exist := stateDB.stateObjects[addr] |
|
|
|
if !exist { |
|
|
|
if !exist { |
|
|
|
// ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
|
|
|
|
// ripeMD is 'touched' at block 1714175, in tx 0x1237f737031e40bcde4a8b7e717b2d15e3ecadfe49bb1bbc71ee9deb09c6fcf2
|
|
|
|
// That tx goes out of gas, and although the notion of 'touched' does not exist there, the
|
|
|
|
// That tx goes out of gas, and although the notion of 'touched' does not exist there, the
|
|
|
@ -590,81 +603,81 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { |
|
|
|
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { |
|
|
|
s.deleteStateObject(stateObject) |
|
|
|
stateDB.deleteStateObject(stateObject) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
stateObject.updateRoot(s.db) |
|
|
|
stateObject.updateRoot(stateDB.db) |
|
|
|
s.updateStateObject(stateObject) |
|
|
|
stateDB.updateStateObject(stateObject) |
|
|
|
} |
|
|
|
} |
|
|
|
s.stateObjectsDirty[addr] = struct{}{} |
|
|
|
stateDB.stateObjectsDirty[addr] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
// Invalidate journal because reverting across transactions is not allowed.
|
|
|
|
// Invalidate journal because reverting across transactions is not allowed.
|
|
|
|
s.clearJournalAndRefund() |
|
|
|
stateDB.clearJournalAndRefund() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// IntermediateRoot computes the current root hash of the state trie.
|
|
|
|
// IntermediateRoot computes the current root hash of the state trie.
|
|
|
|
// It is called in between transactions to get the root hash that
|
|
|
|
// It is called in between transactions to get the root hash that
|
|
|
|
// goes into transaction receipts.
|
|
|
|
// goes into transaction receipts.
|
|
|
|
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { |
|
|
|
func (stateDB *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { |
|
|
|
s.Finalise(deleteEmptyObjects) |
|
|
|
stateDB.Finalise(deleteEmptyObjects) |
|
|
|
return s.trie.Hash() |
|
|
|
return stateDB.trie.Hash() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Prepare sets the current transaction hash and index and block hash which is
|
|
|
|
// Prepare sets the current transaction hash and index and block hash which is
|
|
|
|
// used when the EVM emits new state logs.
|
|
|
|
// used when the EVM emits new state logs.
|
|
|
|
func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) { |
|
|
|
func (stateDB *StateDB) Prepare(thash, bhash common.Hash, ti int) { |
|
|
|
self.thash = thash |
|
|
|
stateDB.thash = thash |
|
|
|
self.bhash = bhash |
|
|
|
stateDB.bhash = bhash |
|
|
|
self.txIndex = ti |
|
|
|
stateDB.txIndex = ti |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *StateDB) clearJournalAndRefund() { |
|
|
|
func (stateDB *StateDB) clearJournalAndRefund() { |
|
|
|
s.journal = newJournal() |
|
|
|
stateDB.journal = newJournal() |
|
|
|
s.validRevisions = s.validRevisions[:0] |
|
|
|
stateDB.validRevisions = stateDB.validRevisions[:0] |
|
|
|
s.refund = 0 |
|
|
|
stateDB.refund = 0 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Commit writes the state to the underlying in-memory trie database.
|
|
|
|
// Commit writes the state to the underlying in-memory trie database.
|
|
|
|
func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) { |
|
|
|
func (stateDB *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) { |
|
|
|
defer s.clearJournalAndRefund() |
|
|
|
defer stateDB.clearJournalAndRefund() |
|
|
|
|
|
|
|
|
|
|
|
for addr := range s.journal.dirties { |
|
|
|
for addr := range stateDB.journal.dirties { |
|
|
|
s.stateObjectsDirty[addr] = struct{}{} |
|
|
|
stateDB.stateObjectsDirty[addr] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
// Commit objects to the trie.
|
|
|
|
// Commit objects to the trie.
|
|
|
|
for addr, stateObject := range s.stateObjects { |
|
|
|
for addr, stateObject := range stateDB.stateObjects { |
|
|
|
_, isDirty := s.stateObjectsDirty[addr] |
|
|
|
_, isDirty := stateDB.stateObjectsDirty[addr] |
|
|
|
switch { |
|
|
|
switch { |
|
|
|
case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()): |
|
|
|
case stateObject.suicided || (isDirty && deleteEmptyObjects && stateObject.empty()): |
|
|
|
// If the object has been removed, don't bother syncing it
|
|
|
|
// If the object has been removed, don't bother syncing it
|
|
|
|
// and just mark it for deletion in the trie.
|
|
|
|
// and just mark it for deletion in the trie.
|
|
|
|
s.deleteStateObject(stateObject) |
|
|
|
stateDB.deleteStateObject(stateObject) |
|
|
|
case isDirty: |
|
|
|
case isDirty: |
|
|
|
// Write any contract code associated with the state object
|
|
|
|
// Write any contract code associated with the state object
|
|
|
|
if stateObject.code != nil && stateObject.dirtyCode { |
|
|
|
if stateObject.code != nil && stateObject.dirtyCode { |
|
|
|
s.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code) |
|
|
|
stateDB.db.TrieDB().InsertBlob(common.BytesToHash(stateObject.CodeHash()), stateObject.code) |
|
|
|
stateObject.dirtyCode = false |
|
|
|
stateObject.dirtyCode = false |
|
|
|
} |
|
|
|
} |
|
|
|
// Write any storage changes in the state object to its storage trie.
|
|
|
|
// Write any storage changes in the state object to its storage trie.
|
|
|
|
if err := stateObject.CommitTrie(s.db); err != nil { |
|
|
|
if err := stateObject.CommitTrie(stateDB.db); err != nil { |
|
|
|
return common.Hash{}, err |
|
|
|
return common.Hash{}, err |
|
|
|
} |
|
|
|
} |
|
|
|
// Update the object in the main account trie.
|
|
|
|
// Update the object in the main account trie.
|
|
|
|
s.updateStateObject(stateObject) |
|
|
|
stateDB.updateStateObject(stateObject) |
|
|
|
} |
|
|
|
} |
|
|
|
delete(s.stateObjectsDirty, addr) |
|
|
|
delete(stateDB.stateObjectsDirty, addr) |
|
|
|
} |
|
|
|
} |
|
|
|
// Write trie changes.
|
|
|
|
// Write trie changes.
|
|
|
|
root, err = s.trie.Commit(func(leaf []byte, parent common.Hash) error { |
|
|
|
root, err = stateDB.trie.Commit(func(leaf []byte, parent common.Hash) error { |
|
|
|
var account Account |
|
|
|
var account Account |
|
|
|
if err := rlp.DecodeBytes(leaf, &account); err != nil { |
|
|
|
if err := rlp.DecodeBytes(leaf, &account); err != nil { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
if account.Root != emptyState { |
|
|
|
if account.Root != emptyState { |
|
|
|
s.db.TrieDB().Reference(account.Root, parent) |
|
|
|
stateDB.db.TrieDB().Reference(account.Root, parent) |
|
|
|
} |
|
|
|
} |
|
|
|
code := common.BytesToHash(account.CodeHash) |
|
|
|
code := common.BytesToHash(account.CodeHash) |
|
|
|
if code != emptyCode { |
|
|
|
if code != emptyCode { |
|
|
|
s.db.TrieDB().Reference(code, parent) |
|
|
|
stateDB.db.TrieDB().Reference(code, parent) |
|
|
|
} |
|
|
|
} |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
}) |
|
|
|
}) |
|
|
|