From 91ecef628a93ab63605cb69532bfb1da0073a5a5 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Thu, 22 Sep 2016 21:12:51 +0200 Subject: [PATCH] core/state: keeping track of written state --- core/state/state_object.go | 8 ++++++++ core/state/state_write.go | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 305c66f9d8..a8a67d09ba 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -80,6 +80,7 @@ type StateObject struct { remove bool deleted bool dirty bool + written bool } func NewStateObject(address common.Address, db trie.Database) *StateObject { @@ -98,6 +99,7 @@ func NewStateObject(address common.Address, db trie.Database) *StateObject { func (self *StateObject) MarkForDeletion() { self.remove = true self.dirty = true + self.written = false if glog.V(logger.Core) { glog.Infof("%x: #%d %v X\n", self.Address(), self.nonce, self.balance) @@ -138,6 +140,7 @@ func (self *StateObject) GetState(key common.Hash) common.Hash { func (self *StateObject) SetState(key, value common.Hash) { self.storage[key] = value self.dirty = true + self.written = false } // Update updates the current cached storage to the trie @@ -157,6 +160,7 @@ func (c *StateObject) AddBalance(amount *big.Int) { if glog.V(logger.Core) { glog.Infof("%x: #%d %v (+ %v)\n", c.Address(), c.nonce, c.balance, amount) } + c.written = false } func (c *StateObject) SubBalance(amount *big.Int) { @@ -165,11 +169,13 @@ func (c *StateObject) SubBalance(amount *big.Int) { if glog.V(logger.Core) { glog.Infof("%x: #%d %v (- %v)\n", c.Address(), c.nonce, c.balance, amount) } + c.written = false } func (c *StateObject) SetBalance(amount *big.Int) { c.balance = amount c.dirty = true + c.written = false } func (c *StateObject) St() Storage { @@ -223,11 +229,13 @@ func (self *StateObject) SetCode(code []byte) { self.code = code self.codeHash = crypto.Keccak256(code) self.dirty = true + self.written = false } func (self *StateObject) SetNonce(nonce uint64) { self.nonce = nonce self.dirty = true + self.written = false } func (self *StateObject) Nonce() uint64 { diff --git a/core/state/state_write.go b/core/state/state_write.go index 60cbb7a6e3..73faf06b94 100644 --- a/core/state/state_write.go +++ b/core/state/state_write.go @@ -27,13 +27,13 @@ func CommitBatch(state *State) (common.Hash, ethdb.Batch) { func stateCommit(state *State, db trie.DatabaseWriter) (common.Hash, error) { // make sure the state is flattened before committing state = Flatten(state) - for address, stateObject := range state.StateObjects { + for _, stateObject := range state.StateObjects { if stateObject.remove { // If the object has been removed, don't bother syncing it // and just mark it for deletion in the trie. stateObject.deleted = true state.Trie.Delete(stateObject.Address().Bytes()[:]) - } else if state.ownedStateObjects[address] { + } else if !stateObject.written { // Write any contract code associated with the state object if len(stateObject.code) > 0 { if err := db.Put(stateObject.codeHash, stateObject.code); err != nil { @@ -60,6 +60,7 @@ func stateCommit(state *State, db trie.DatabaseWriter) (common.Hash, error) { } stateObject.dirty = false + stateObject.written = true } return state.Trie.CommitTo(db) }