core/state: revert metro suicide map addition (#15024)

This commit is contained in:
Péter Szilágyi 2017-08-24 13:42:00 +03:00 committed by Felix Lange
parent 20b818d206
commit ff9a868232
4 changed files with 28 additions and 49 deletions

View File

@ -91,11 +91,6 @@ func (ch suicideChange) undo(s *StateDB) {
if obj != nil { if obj != nil {
obj.suicided = ch.prev obj.suicided = ch.prev
obj.setBalance(ch.prevbalance) obj.setBalance(ch.prevbalance)
// if the object wasn't suicided before, remove
// it from the list of destructed objects as well.
if !obj.suicided {
delete(s.stateObjectsDestructed, *ch.account)
}
} }
} }

View File

@ -46,9 +46,8 @@ type StateDB struct {
trie Trie trie Trie
// This map holds 'live' objects, which will get modified while processing a state transition. // This map holds 'live' objects, which will get modified while processing a state transition.
stateObjects map[common.Address]*stateObject stateObjects map[common.Address]*stateObject
stateObjectsDirty map[common.Address]struct{} stateObjectsDirty map[common.Address]struct{}
stateObjectsDestructed map[common.Address]struct{}
// DB error. // DB error.
// State objects are used by the consensus core and VM which are // State objects are used by the consensus core and VM which are
@ -83,14 +82,13 @@ func New(root common.Hash, db Database) (*StateDB, error) {
return nil, err return nil, err
} }
return &StateDB{ return &StateDB{
db: db, db: db,
trie: tr, trie: tr,
stateObjects: make(map[common.Address]*stateObject), stateObjects: make(map[common.Address]*stateObject),
stateObjectsDirty: make(map[common.Address]struct{}), stateObjectsDirty: make(map[common.Address]struct{}),
stateObjectsDestructed: make(map[common.Address]struct{}), refund: new(big.Int),
refund: new(big.Int), 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),
}, nil }, nil
} }
@ -115,7 +113,6 @@ func (self *StateDB) Reset(root common.Hash) error {
self.trie = tr self.trie = tr
self.stateObjects = make(map[common.Address]*stateObject) self.stateObjects = make(map[common.Address]*stateObject)
self.stateObjectsDirty = make(map[common.Address]struct{}) self.stateObjectsDirty = make(map[common.Address]struct{})
self.stateObjectsDestructed = make(map[common.Address]struct{})
self.thash = common.Hash{} self.thash = common.Hash{}
self.bhash = common.Hash{} self.bhash = common.Hash{}
self.txIndex = 0 self.txIndex = 0
@ -323,7 +320,6 @@ func (self *StateDB) Suicide(addr common.Address) bool {
}) })
stateObject.markSuicided() stateObject.markSuicided()
stateObject.data.Balance = new(big.Int) stateObject.data.Balance = new(big.Int)
self.stateObjectsDestructed[addr] = struct{}{}
return true return true
} }
@ -456,23 +452,19 @@ func (self *StateDB) Copy() *StateDB {
// 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: self.db,
trie: self.trie, trie: self.trie,
stateObjects: make(map[common.Address]*stateObject, len(self.stateObjectsDirty)), stateObjects: make(map[common.Address]*stateObject, len(self.stateObjectsDirty)),
stateObjectsDirty: make(map[common.Address]struct{}, len(self.stateObjectsDirty)), stateObjectsDirty: make(map[common.Address]struct{}, len(self.stateObjectsDirty)),
stateObjectsDestructed: make(map[common.Address]struct{}, len(self.stateObjectsDestructed)), refund: new(big.Int).Set(self.refund),
refund: new(big.Int).Set(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),
} }
// Copy the dirty states, logs, and preimages // Copy the dirty states, logs, and preimages
for addr := range self.stateObjectsDirty { for addr := range self.stateObjectsDirty {
state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state, state.MarkStateObjectDirty) state.stateObjects[addr] = self.stateObjects[addr].deepCopy(state, state.MarkStateObjectDirty)
state.stateObjectsDirty[addr] = struct{}{} state.stateObjectsDirty[addr] = struct{}{}
if self.stateObjects[addr].suicided {
state.stateObjectsDestructed[addr] = struct{}{}
}
} }
for hash, logs := range self.logs { for hash, logs := range self.logs {
state.logs[hash] = make([]*types.Log, len(logs)) state.logs[hash] = make([]*types.Log, len(logs))
@ -520,10 +512,9 @@ func (self *StateDB) GetRefund() *big.Int {
return self.refund return self.refund
} }
// IntermediateRoot computes the current root hash of the state trie. // Finalise finalises the state by removing the self destructed objects
// It is called in between transactions to get the root hash that // and clears the journal as well as the refunds.
// goes into transaction receipts. func (s *StateDB) Finalise(deleteEmptyObjects bool) {
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
for addr := range s.stateObjectsDirty { for addr := range s.stateObjectsDirty {
stateObject := s.stateObjects[addr] stateObject := s.stateObjects[addr]
if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) { if stateObject.suicided || (deleteEmptyObjects && stateObject.empty()) {
@ -535,6 +526,13 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
} }
// Invalidate journal because reverting across transactions is not allowed. // Invalidate journal because reverting across transactions is not allowed.
s.clearJournalAndRefund() s.clearJournalAndRefund()
}
// IntermediateRoot computes the current root hash of the state trie.
// It is called in between transactions to get the root hash that
// goes into transaction receipts.
func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash {
s.Finalise(deleteEmptyObjects)
return s.trie.Hash() return s.trie.Hash()
} }
@ -546,19 +544,6 @@ func (self *StateDB) Prepare(thash, bhash common.Hash, ti int) {
self.txIndex = ti self.txIndex = ti
} }
// Finalise finalises the state by removing the self destructed objects
// in the current stateObjectsDestructed buffer and clears the journal
// as well as the refunds.
//
// Please note that Finalise is used by EIP#98 and is used instead of
// IntermediateRoot.
func (s *StateDB) Finalise() {
for addr := range s.stateObjectsDestructed {
s.deleteStateObject(s.stateObjects[addr])
}
s.clearJournalAndRefund()
}
// DeleteSuicides flags the suicided objects for deletion so that it // DeleteSuicides flags the suicided objects for deletion so that it
// won't be referenced again when called / queried up on. // won't be referenced again when called / queried up on.
// //

View File

@ -106,7 +106,7 @@ func ApplyTransaction(config *params.ChainConfig, bc *BlockChain, author *common
// Update the state with pending changes // Update the state with pending changes
var root []byte var root []byte
if config.IsMetropolis(header.Number) { if config.IsMetropolis(header.Number) {
statedb.Finalise() statedb.Finalise(true)
} else { } else {
root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes() root = statedb.IntermediateRoot(config.IsEIP158(header.Number)).Bytes()
} }

View File

@ -37,7 +37,6 @@ func TestBlockchain(t *testing.T) {
// Still failing tests // Still failing tests
bt.skipLoad(`^bcWalletTest.*_Byzantium$`) bt.skipLoad(`^bcWalletTest.*_Byzantium$`)
bt.skipLoad(`^bcStateTests/suicideCoinbase.json.*_Byzantium$`)
bt.walk(t, blockTestDir, func(t *testing.T, name string, test *BlockTest) { bt.walk(t, blockTestDir, func(t *testing.T, name string, test *BlockTest) {
if err := bt.checkFailure(t, name, test.Run()); err != nil { if err := bt.checkFailure(t, name, test.Run()); err != nil {