diff options
Diffstat (limited to 'core/state/state_object.go')
-rw-r--r-- | core/state/state_object.go | 44 |
1 files changed, 34 insertions, 10 deletions
diff --git a/core/state/state_object.go b/core/state/state_object.go index 091d24184..f41ab0409 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -77,7 +77,7 @@ type stateObject struct { trie Trie // storage trie, which becomes non-nil on first access code Code // contract bytecode, which gets set when code is loaded - cachedStorage Storage // Storage entry cache to avoid duplicate reads + originStorage Storage // Storage cache of original entries to dedup rewrites dirtyStorage Storage // Storage entries that need to be flushed to disk // Cache flags. @@ -115,7 +115,7 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject { address: address, addrHash: crypto.Keccak256Hash(address[:]), data: data, - cachedStorage: make(Storage), + originStorage: make(Storage), dirtyStorage: make(Storage), } } @@ -159,13 +159,25 @@ func (c *stateObject) getTrie(db Database) Trie { return c.trie } -// GetState returns a value in account storage. +// GetState retrieves a value from the account storage trie. func (self *stateObject) GetState(db Database, key common.Hash) common.Hash { - value, exists := self.cachedStorage[key] - if exists { + // If we have a dirty value for this state entry, return it + value, dirty := self.dirtyStorage[key] + if dirty { return value } - // Load from DB in case it is missing. + // Otherwise return the entry's original value + return self.GetCommittedState(db, key) +} + +// GetCommittedState retrieves a value from the committed account storage trie. +func (self *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash { + // If we have the original value cached, return that + value, cached := self.originStorage[key] + if cached { + return value + } + // Otherwise load the value from the database enc, err := self.getTrie(db).TryGet(key[:]) if err != nil { self.setError(err) @@ -178,22 +190,27 @@ func (self *stateObject) GetState(db Database, key common.Hash) common.Hash { } value.SetBytes(content) } - self.cachedStorage[key] = value + self.originStorage[key] = value return value } // SetState updates a value in account storage. func (self *stateObject) SetState(db Database, key, value common.Hash) { + // If the new value is the same as old, don't set + prev := self.GetState(db, key) + if prev == value { + return + } + // New value is different, update and journal the change self.db.journal.append(storageChange{ account: &self.address, key: key, - prevalue: self.GetState(db, key), + prevalue: prev, }) self.setState(key, value) } func (self *stateObject) setState(key, value common.Hash) { - self.cachedStorage[key] = value self.dirtyStorage[key] = value } @@ -202,6 +219,13 @@ func (self *stateObject) updateTrie(db Database) Trie { tr := self.getTrie(db) for key, value := range self.dirtyStorage { delete(self.dirtyStorage, key) + + // Skip noop changes, persist actual changes + if value == self.originStorage[key] { + continue + } + self.originStorage[key] = value + if (value == common.Hash{}) { self.setError(tr.TryDelete(key[:])) continue @@ -279,7 +303,7 @@ func (self *stateObject) deepCopy(db *StateDB) *stateObject { } stateObject.code = self.code stateObject.dirtyStorage = self.dirtyStorage.Copy() - stateObject.cachedStorage = self.dirtyStorage.Copy() + stateObject.originStorage = self.originStorage.Copy() stateObject.suicided = self.suicided stateObject.dirtyCode = self.dirtyCode stateObject.deleted = self.deleted |