@@ -145,16 +145,15 @@ func (s *stateObject) GetState(key common.Hash) common.Hash {
145145 return value
146146}
147147
148- // getState retrieves a value from the account storage trie and also returns if
149- // the slot is already dirty or not .
150- func (s * stateObject ) getState (key common.Hash ) (common.Hash , bool ) {
151- // If we have a dirty value for this state entry, return it
148+ // getState retrieves a value associated with the given storage key, along with
149+ // it's original value .
150+ func (s * stateObject ) getState (key common.Hash ) (common.Hash , common. Hash ) {
151+ origin := s . GetCommittedState ( key )
152152 value , dirty := s .dirtyStorage [key ]
153- if dirty {
154- return value , true
153+ if ! dirty {
154+ value = origin
155155 }
156- // Otherwise return the entry's original value
157- return s .GetCommittedState (key ), false
156+ return value , origin
158157}
159158
160159// GetCommittedState retrieves a value from the committed account storage trie.
@@ -219,36 +218,32 @@ func (s *stateObject) GetCommittedState(key common.Hash) common.Hash {
219218func (s * stateObject ) SetState (key , value common.Hash ) {
220219 // If the new value is the same as old, don't set. Otherwise, track only the
221220 // dirty changes, supporting reverting all of it back to no change.
222- prev , dirty := s .getState (key )
221+ prev , origin := s .getState (key )
223222 if prev == value {
224223 return
225224 }
226- var prevvalue * common.Hash
227- if dirty {
228- prevvalue = & prev
229- }
230225 // New value is different, update and journal the change
231226 s .db .journal .append (storageChange {
232227 account : & s .address ,
233228 key : key ,
234- prevvalue : prevvalue ,
229+ prevvalue : prev ,
230+ origvalue : origin ,
235231 })
236232 if s .db .logger != nil && s .db .logger .OnStorageChange != nil {
237233 s .db .logger .OnStorageChange (s .address , key , prev , value )
238234 }
239- s .setState (key , & value )
235+ s .setState (key , value , origin )
240236}
241237
242- // setState updates a value in account dirty storage. If the value being set is
243- // nil (assuming journal revert), the dirtyness is removed .
244- func (s * stateObject ) setState (key common.Hash , value * common.Hash ) {
245- // If the first set is being reverted , undo the dirty marker
246- if value == nil {
238+ // setState updates a value in account dirty storage. The dirtiness will be
239+ // removed if the value being set equals to the original value .
240+ func (s * stateObject ) setState (key common.Hash , value common. Hash , origin common.Hash ) {
241+ // Storage slot is set back to its original value , undo the dirty marker
242+ if value == origin {
247243 delete (s .dirtyStorage , key )
248244 return
249245 }
250- // Otherwise restore the previous value
251- s .dirtyStorage [key ] = * value
246+ s .dirtyStorage [key ] = value
252247}
253248
254249// finalise moves all dirty storage slots into the pending area to be hashed or
@@ -264,7 +259,7 @@ func (s *stateObject) finalise(prefetch bool) {
264259 slotsToPrefetch = append (slotsToPrefetch , common .CopyBytes (key [:])) // Copy needed for closure
265260 } else {
266261 // Otherwise, the slot was reverted to its original value, remove it
267- // from the pending area to avoid thrashing the data strutures .
262+ // from the pending area to avoid thrashing the data structure .
268263 delete (s .pendingStorage , key )
269264 }
270265 }
0 commit comments