state: track code dirtyness and only write if changed

This commit is contained in:
Péter Szilágyi 2016-09-23 08:57:51 +03:00
parent 8fbdde5491
commit 3c26ba4d30
2 changed files with 8 additions and 5 deletions

View file

@ -83,6 +83,7 @@ type StateObject struct {
// When an object is marked for deletion it will be delete from the trie // When an object is marked for deletion it will be delete from the trie
// during the "update" phase of the state transition // during the "update" phase of the state transition
dirty bool // true if anything has changed dirty bool // true if anything has changed
dirtyCode bool // true if the code was updated
remove bool remove bool
deleted bool deleted bool
} }
@ -238,6 +239,7 @@ func (self *StateObject) Copy(db trie.Database) *StateObject {
stateObject.storage = self.storage.Copy() stateObject.storage = self.storage.Copy()
stateObject.remove = self.remove stateObject.remove = self.remove
stateObject.dirty = self.dirty stateObject.dirty = self.dirty
stateObject.dirtyCode = self.dirtyCode
stateObject.deleted = self.deleted stateObject.deleted = self.deleted
return stateObject return stateObject
} }
@ -270,7 +272,7 @@ func (self *StateObject) Code(db trie.Database) []byte {
func (self *StateObject) SetCode(code []byte) { func (self *StateObject) SetCode(code []byte) {
self.code = code self.code = code
self.data.CodeHash = crypto.Keccak256(code) self.data.CodeHash = crypto.Keccak256(code)
self.dirty = true self.dirty, self.dirtyCode = true, true
} }
func (self *StateObject) SetNonce(nonce uint64) { func (self *StateObject) SetNonce(nonce uint64) {

View file

@ -447,10 +447,11 @@ func (s *StateDB) commit(dbw trie.DatabaseWriter) (root common.Hash, err error)
delete(s.all, addr) delete(s.all, addr)
} else if stateObject.dirty { } else if stateObject.dirty {
// Write any contract code associated with the state object // Write any contract code associated with the state object
if stateObject.code != nil { if stateObject.code != nil && stateObject.dirtyCode {
if err := dbw.Put(stateObject.CodeHash(), stateObject.code); err != nil { if err := dbw.Put(stateObject.CodeHash(), stateObject.code); err != nil {
return common.Hash{}, err return common.Hash{}, err
} }
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, dbw); err != nil { if err := stateObject.CommitTrie(s.db, dbw); err != nil {