|
|
@@ -76,7 +76,7 @@ type StateDB struct {
|
|
|
|
|
|
// Journal of state modifications. This is the backbone of
|
|
|
// Snapshot and RevertToSnapshot.
|
|
|
- journal journal
|
|
|
+ journal *journal
|
|
|
validRevisions []revision
|
|
|
nextRevisionId int
|
|
|
|
|
|
@@ -96,6 +96,7 @@ func New(root common.Hash, db Database) (*StateDB, error) {
|
|
|
stateObjectsDirty: make(map[common.Address]struct{}),
|
|
|
logs: make(map[common.Hash][]*types.Log),
|
|
|
preimages: make(map[common.Hash][]byte),
|
|
|
+ journal: newJournal(),
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
@@ -131,7 +132,7 @@ func (self *StateDB) Reset(root common.Hash) error {
|
|
|
}
|
|
|
|
|
|
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.BlockHash = self.bhash
|
|
|
@@ -156,7 +157,7 @@ func (self *StateDB) Logs() []*types.Log {
|
|
|
// AddPreimage records a SHA3 preimage seen by the VM.
|
|
|
func (self *StateDB) AddPreimage(hash common.Hash, preimage []byte) {
|
|
|
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))
|
|
|
copy(pi, preimage)
|
|
|
self.preimages[hash] = pi
|
|
|
@@ -169,7 +170,7 @@ func (self *StateDB) Preimages() map[common.Hash][]byte {
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
@@ -255,7 +256,7 @@ func (self *StateDB) StorageTrie(addr common.Address) Trie {
|
|
|
if stateObject == nil {
|
|
|
return nil
|
|
|
}
|
|
|
- cpy := stateObject.deepCopy(self, nil)
|
|
|
+ cpy := stateObject.deepCopy(self)
|
|
|
return cpy.updateTrie(self.db)
|
|
|
}
|
|
|
|
|
|
@@ -325,7 +326,7 @@ func (self *StateDB) Suicide(addr common.Address) bool {
|
|
|
if stateObject == nil {
|
|
|
return false
|
|
|
}
|
|
|
- self.journal = append(self.journal, suicideChange{
|
|
|
+ self.journal.append(suicideChange{
|
|
|
account: &addr,
|
|
|
prev: stateObject.suicided,
|
|
|
prevbalance: new(big.Int).Set(stateObject.Balance()),
|
|
|
@@ -379,7 +380,7 @@ func (self *StateDB) getStateObject(addr common.Address) (stateObject *stateObje
|
|
|
return nil
|
|
|
}
|
|
|
// Insert into the live set.
|
|
|
- obj := newObject(self, addr, data, self.MarkStateObjectDirty)
|
|
|
+ obj := newObject(self, addr, data)
|
|
|
self.setStateObject(obj)
|
|
|
return obj
|
|
|
}
|
|
|
@@ -397,22 +398,16 @@ func (self *StateDB) GetOrNewStateObject(addr common.Address) *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
|
|
|
// the given address, it is overwritten and returned as the second return value.
|
|
|
func (self *StateDB) createObject(addr common.Address) (newobj, prev *stateObject) {
|
|
|
prev = self.getStateObject(addr)
|
|
|
- newobj = newObject(self, addr, Account{}, self.MarkStateObjectDirty)
|
|
|
+ newobj = newObject(self, addr, Account{})
|
|
|
newobj.setNonce(0) // sets the object to dirty
|
|
|
if prev == nil {
|
|
|
- self.journal = append(self.journal, createObjectChange{account: &addr})
|
|
|
+ self.journal.append(createObjectChange{account: &addr})
|
|
|
} else {
|
|
|
- self.journal = append(self.journal, resetObjectChange{prev: prev})
|
|
|
+ self.journal.append(resetObjectChange{prev: prev})
|
|
|
}
|
|
|
self.setStateObject(newobj)
|
|
|
return newobj, prev
|
|
|
@@ -466,16 +461,17 @@ func (self *StateDB) Copy() *StateDB {
|
|
|
state := &StateDB{
|
|
|
db: self.db,
|
|
|
trie: self.db.CopyTrie(self.trie),
|
|
|
- stateObjects: make(map[common.Address]*stateObject, len(self.stateObjectsDirty)),
|
|
|
- stateObjectsDirty: make(map[common.Address]struct{}, len(self.stateObjectsDirty)),
|
|
|
+ stateObjects: make(map[common.Address]*stateObject, len(self.journal.dirties)),
|
|
|
+ stateObjectsDirty: make(map[common.Address]struct{}, len(self.journal.dirties)),
|
|
|
refund: self.refund,
|
|
|
logs: make(map[common.Hash][]*types.Log, len(self.logs)),
|
|
|
logSize: self.logSize,
|
|
|
preimages: make(map[common.Hash][]byte),
|
|
|
+ journal: newJournal(),
|
|
|
}
|
|
|
// Copy the dirty states, logs, and preimages
|
|
|
- for addr := range self.stateObjectsDirty {
|
|
|
- state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state, state.MarkStateObjectDirty)
|
|
|
+ for addr := range self.journal.dirties {
|
|
|
+ state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state)
|
|
|
state.stateObjectsDirty[addr] = struct{}{}
|
|
|
}
|
|
|
for hash, logs := range self.logs {
|
|
|
@@ -492,7 +488,7 @@ func (self *StateDB) Copy() *StateDB {
|
|
|
func (self *StateDB) Snapshot() int {
|
|
|
id := 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
|
|
|
}
|
|
|
|
|
|
@@ -507,13 +503,8 @@ func (self *StateDB) RevertToSnapshot(revid int) {
|
|
|
}
|
|
|
snapshot := self.validRevisions[idx].journalIndex
|
|
|
|
|
|
- // Replay the journal to undo changes.
|
|
|
- for i := len(self.journal) - 1; i >= snapshot; i-- {
|
|
|
- self.journal[i].undo(self)
|
|
|
- }
|
|
|
- self.journal = self.journal[:snapshot]
|
|
|
-
|
|
|
- // Remove invalidated snapshots from the stack.
|
|
|
+ // Replay the journal to undo changes and remove invalidated snapshots
|
|
|
+ self.journal.revert(self, snapshot)
|
|
|
self.validRevisions = self.validRevisions[:idx]
|
|
|
}
|
|
|
|
|
|
@@ -525,14 +516,25 @@ func (self *StateDB) GetRefund() uint64 {
|
|
|
// Finalise finalises the state by removing the self destructed objects
|
|
|
// and clears the journal as well as the refunds.
|
|
|
func (s *StateDB) Finalise(deleteEmptyObjects bool) {
|
|
|
- for addr := range s.stateObjectsDirty {
|
|
|
- stateObject := s.stateObjects[addr]
|
|
|
+ for addr := range s.journal.dirties {
|
|
|
+ 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()) {
|
|
|
s.deleteStateObject(stateObject)
|
|
|
} else {
|
|
|
stateObject.updateRoot(s.db)
|
|
|
s.updateStateObject(stateObject)
|
|
|
}
|
|
|
+ s.stateObjectsDirty[addr] = struct{}{}
|
|
|
}
|
|
|
// Invalidate journal because reverting across transactions is not allowed.
|
|
|
s.clearJournalAndRefund()
|
|
|
@@ -576,7 +578,7 @@ func (s *StateDB) DeleteSuicides() {
|
|
|
}
|
|
|
|
|
|
func (s *StateDB) clearJournalAndRefund() {
|
|
|
- s.journal = nil
|
|
|
+ s.journal = newJournal()
|
|
|
s.validRevisions = s.validRevisions[:0]
|
|
|
s.refund = 0
|
|
|
}
|
|
|
@@ -585,6 +587,9 @@ func (s *StateDB) clearJournalAndRefund() {
|
|
|
func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error) {
|
|
|
defer s.clearJournalAndRefund()
|
|
|
|
|
|
+ for addr := range s.journal.dirties {
|
|
|
+ s.stateObjectsDirty[addr] = struct{}{}
|
|
|
+ }
|
|
|
// Commit objects to the trie.
|
|
|
for addr, stateObject := range s.stateObjects {
|
|
|
_, isDirty := s.stateObjectsDirty[addr]
|