|
|
@ -76,7 +76,7 @@ type StateDB struct { |
|
|
|
|
|
|
|
|
|
|
|
// Journal of state modifications. This is the backbone of
|
|
|
|
// Journal of state modifications. This is the backbone of
|
|
|
|
// Snapshot and RevertToSnapshot.
|
|
|
|
// Snapshot and RevertToSnapshot.
|
|
|
|
journal journal |
|
|
|
journal *journal |
|
|
|
validRevisions []revision |
|
|
|
validRevisions []revision |
|
|
|
nextRevisionId int |
|
|
|
nextRevisionId int |
|
|
|
|
|
|
|
|
|
|
@ -96,6 +96,7 @@ func New(root common.Hash, db Database) (*StateDB, error) { |
|
|
|
stateObjectsDirty: make(map[common.Address]struct{}), |
|
|
|
stateObjectsDirty: make(map[common.Address]struct{}), |
|
|
|
logs: make(map[common.Hash][]*types.Log), |
|
|
|
logs: make(map[common.Hash][]*types.Log), |
|
|
|
preimages: make(map[common.Hash][]byte), |
|
|
|
preimages: make(map[common.Hash][]byte), |
|
|
|
|
|
|
|
journal: newJournal(), |
|
|
|
}, nil |
|
|
|
}, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -131,7 +132,7 @@ func (self *StateDB) Reset(root common.Hash) error { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) AddLog(log *types.Log) { |
|
|
|
func (self *StateDB) AddLog(log *types.Log) { |
|
|
|
self.journal = append(self.journal, addLogChange{txhash: self.thash}) |
|
|
|
self.journal.append(addLogChange{txhash: self.thash}) |
|
|
|
|
|
|
|
|
|
|
|
log.TxHash = self.thash |
|
|
|
log.TxHash = self.thash |
|
|
|
log.BlockHash = self.bhash |
|
|
|
log.BlockHash = self.bhash |
|
|
@ -156,7 +157,7 @@ func (self *StateDB) Logs() []*types.Log { |
|
|
|
// 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 (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) { |
|
|
|
if _, ok := self.preimages[hash]; !ok { |
|
|
|
if _, ok := self.preimages[hash]; !ok { |
|
|
|
self.journal = append(self.journal, addPreimageChange{hash: hash}) |
|
|
|
self.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 |
|
|
|
self.preimages[hash] = pi |
|
|
@ -169,7 +170,7 @@ func (self *StateDB) Preimages() map[common.Hash][]byte { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *StateDB) AddRefund(gas uint64) { |
|
|
|
func (self *StateDB) AddRefund(gas uint64) { |
|
|
|
self.journal = append(self.journal, refundChange{prev: self.refund}) |
|
|
|
self.journal.append(refundChange{prev: self.refund}) |
|
|
|
self.refund += gas |
|
|
|
self.refund += gas |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -255,7 +256,7 @@ func (self *StateDB) StorageTrie(addr common.Address) Trie { |
|
|
|
if stateObject == nil { |
|
|
|
if stateObject == nil { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
cpy := stateObject.deepCopy(self, nil) |
|
|
|
cpy := stateObject.deepCopy(self) |
|
|
|
return cpy.updateTrie(self.db) |
|
|
|
return cpy.updateTrie(self.db) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -325,7 +326,7 @@ func (self *StateDB) Suicide(addr common.Address) bool { |
|
|
|
if stateObject == nil { |
|
|
|
if stateObject == nil { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
self.journal = append(self.journal, suicideChange{ |
|
|
|
self.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()), |
|
|
@ -379,7 +380,7 @@ 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, self.MarkStateObjectDirty) |
|
|
|
obj := newObject(self, addr, data) |
|
|
|
self.setStateObject(obj) |
|
|
|
self.setStateObject(obj) |
|
|
|
return obj |
|
|
|
return obj |
|
|
|
} |
|
|
|
} |
|
|
@ -397,22 +398,16 @@ func (self *StateDB) GetOrNewStateObject(addr common.Address) *stateObject { |
|
|
|
return stateObject |
|
|
|
return stateObject |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// MarkStateObjectDirty adds the specified object to the dirty map to avoid costly
|
|
|
|
|
|
|
|
// state object cache iteration to find a handful of modified ones.
|
|
|
|
|
|
|
|
func (self *StateDB) MarkStateObjectDirty(addr common.Address) { |
|
|
|
|
|
|
|
self.stateObjectsDirty[addr] = struct{}{} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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 (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) { |
|
|
|
prev = self.getStateObject(addr) |
|
|
|
prev = self.getStateObject(addr) |
|
|
|
newobj = newObject(self, addr, Account{}, self.MarkStateObjectDirty) |
|
|
|
newobj = newObject(self, 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(self.journal, createObjectChange{account: &addr}) |
|
|
|
self.journal.append(createObjectChange{account: &addr}) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
self.journal = append(self.journal, resetObjectChange{prev: prev}) |
|
|
|
self.journal.append(resetObjectChange{prev: prev}) |
|
|
|
} |
|
|
|
} |
|
|
|
self.setStateObject(newobj) |
|
|
|
self.setStateObject(newobj) |
|
|
|
return newobj, prev |
|
|
|
return newobj, prev |
|
|
@ -466,16 +461,17 @@ func (self *StateDB) Copy() *StateDB { |
|
|
|
state := &StateDB{ |
|
|
|
state := &StateDB{ |
|
|
|
db: self.db, |
|
|
|
db: self.db, |
|
|
|
trie: self.db.CopyTrie(self.trie), |
|
|
|
trie: self.db.CopyTrie(self.trie), |
|
|
|
stateObjects: make(map[common.Address]*stateObject, len(self.stateObjectsDirty)), |
|
|
|
stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)), |
|
|
|
stateObjectsDirty: make(map[common.Address]struct{}, len(self.stateObjectsDirty)), |
|
|
|
stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)), |
|
|
|
refund: self.refund, |
|
|
|
refund: self.refund, |
|
|
|
logs: make(map[common.Hash][]*types.Log, len(self.logs)), |
|
|
|
logs: make(map[common.Hash][]*types.Log, len(self.logs)), |
|
|
|
logSize: self.logSize, |
|
|
|
logSize: self.logSize, |
|
|
|
preimages: make(map[common.Hash][]byte), |
|
|
|
preimages: make(map[common.Hash][]byte), |
|
|
|
|
|
|
|
journal: newJournal(), |
|
|
|
} |
|
|
|
} |
|
|
|
// Copy the dirty states, logs, and preimages
|
|
|
|
// Copy the dirty states, logs, and preimages
|
|
|
|
for addr := range self.stateObjectsDirty { |
|
|
|
for addr := range self.journal.dirties { |
|
|
|
state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state, state.MarkStateObjectDirty) |
|
|
|
state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state) |
|
|
|
state.stateObjectsDirty[addr] = struct{}{} |
|
|
|
state.stateObjectsDirty[addr] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
for hash, logs := range self.logs { |
|
|
|
for hash, logs := range self.logs { |
|
|
@ -492,7 +488,7 @@ func (self *StateDB) Copy() *StateDB { |
|
|
|
func (self *StateDB) Snapshot() int { |
|
|
|
func (self *StateDB) Snapshot() int { |
|
|
|
id := self.nextRevisionId |
|
|
|
id := self.nextRevisionId |
|
|
|
self.nextRevisionId++ |
|
|
|
self.nextRevisionId++ |
|
|
|
self.validRevisions = append(self.validRevisions, revision{id, len(self.journal)}) |
|
|
|
self.validRevisions = append(self.validRevisions, revision{id, self.journal.length()}) |
|
|
|
return id |
|
|
|
return id |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -507,13 +503,8 @@ func (self *StateDB) RevertToSnapshot(revid int) { |
|
|
|
} |
|
|
|
} |
|
|
|
snapshot := self.validRevisions[idx].journalIndex |
|
|
|
snapshot := self.validRevisions[idx].journalIndex |
|
|
|
|
|
|
|
|
|
|
|
// Replay the journal to undo changes.
|
|
|
|
// Replay the journal to undo changes and remove invalidated snapshots
|
|
|
|
for i := len(self.journal) - 1; i >= snapshot; i-- { |
|
|
|
self.journal.revert(self, snapshot) |
|
|
|
self.journal[i].undo(self) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
self.journal = self.journal[:snapshot] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove invalidated snapshots from the stack.
|
|
|
|
|
|
|
|
self.validRevisions = self.validRevisions[:idx] |
|
|
|
self.validRevisions = self.validRevisions[:idx] |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -525,14 +516,25 @@ func (self *StateDB) GetRefund() uint64 { |
|
|
|
// 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 (s *StateDB) Finalise(deleteEmptyObjects bool) { |
|
|
|
for addr := range s.stateObjectsDirty { |
|
|
|
for addr := range s.journal.dirties { |
|
|
|
stateObject := s.stateObjects[addr] |
|
|
|
stateObject, exist := s.stateObjects[addr] |
|
|
|
|
|
|
|
if !exist { |
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
// touch-event will still be recorded in the journal. Since ripeMD is a special snowflake,
|
|
|
|
|
|
|
|
// it will persist in the journal even though the journal is reverted. In this special circumstance,
|
|
|
|
|
|
|
|
// it may exist in `s.journal.dirties` but not in `s.stateObjects`.
|
|
|
|
|
|
|
|
// Thus, we can safely ignore it here
|
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { |
|
|
|
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { |
|
|
|
s.deleteStateObject(stateObject) |
|
|
|
s.deleteStateObject(stateObject) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
stateObject.updateRoot(s.db) |
|
|
|
stateObject.updateRoot(s.db) |
|
|
|
s.updateStateObject(stateObject) |
|
|
|
s.updateStateObject(stateObject) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
s.stateObjectsDirty[addr] = struct{}{} |
|
|
|
} |
|
|
|
} |
|
|
|
// Invalidate journal because reverting across transactions is not allowed.
|
|
|
|
// Invalidate journal because reverting across transactions is not allowed.
|
|
|
|
s.clearJournalAndRefund() |
|
|
|
s.clearJournalAndRefund() |
|
|
@ -576,7 +578,7 @@ func (s *StateDB) DeleteSuicides() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *StateDB) clearJournalAndRefund() { |
|
|
|
func (s *StateDB) clearJournalAndRefund() { |
|
|
|
s.journal = nil |
|
|
|
s.journal = newJournal() |
|
|
|
s.validRevisions = s.validRevisions[:0] |
|
|
|
s.validRevisions = s.validRevisions[:0] |
|
|
|
s.refund = 0 |
|
|
|
s.refund = 0 |
|
|
|
} |
|
|
|
} |
|
|
@ -585,6 +587,9 @@ func (s *StateDB) clearJournalAndRefund() { |
|
|
|
func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) { |
|
|
|
func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) { |
|
|
|
defer s.clearJournalAndRefund() |
|
|
|
defer s.clearJournalAndRefund() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for addr := range s.journal.dirties { |
|
|
|
|
|
|
|
s.stateObjectsDirty[addr] = struct{}{} |
|
|
|
|
|
|
|
} |
|
|
|
// Commit objects to the trie.
|
|
|
|
// Commit objects to the trie.
|
|
|
|
for addr, stateObject := range s.stateObjects { |
|
|
|
for addr, stateObject := range s.stateObjects { |
|
|
|
_, isDirty := s.stateObjectsDirty[addr] |
|
|
|
_, isDirty := s.stateObjectsDirty[addr] |
|
|
|