|
@@ -77,9 +77,9 @@ type stateObject struct {
|
|
|
trie Trie // storage trie, which becomes non-nil on first access
|
|
trie Trie // storage trie, which becomes non-nil on first access
|
|
|
code Code // contract bytecode, which gets set when code is loaded
|
|
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
|
|
dirtyStorage Storage // Storage entries that need to be flushed to disk
|
|
|
- originalValue Storage // Map of original storage values, at the beginning of current call context
|
|
|
|
|
|
|
+
|
|
|
// Cache flags.
|
|
// Cache flags.
|
|
|
// When an object is marked suicided it will be delete from the trie
|
|
// When an object is marked suicided it will be delete from the trie
|
|
|
// during the "update" phase of the state transition.
|
|
// during the "update" phase of the state transition.
|
|
@@ -115,9 +115,8 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject {
|
|
|
address: address,
|
|
address: address,
|
|
|
addrHash: crypto.Keccak256Hash(address[:]),
|
|
addrHash: crypto.Keccak256Hash(address[:]),
|
|
|
data: data,
|
|
data: data,
|
|
|
- cachedStorage: make(Storage),
|
|
|
|
|
|
|
+ originStorage: make(Storage),
|
|
|
dirtyStorage: make(Storage),
|
|
dirtyStorage: make(Storage),
|
|
|
- originalValue: make(Storage),
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -160,13 +159,25 @@ func (c *stateObject) getTrie(db Database) Trie {
|
|
|
return c.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 {
|
|
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
|
|
|
|
|
+ }
|
|
|
|
|
+ // 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
|
|
return value
|
|
|
}
|
|
}
|
|
|
- // Load from DB in case it is missing.
|
|
|
|
|
|
|
+ // Otherwise load the value from the database
|
|
|
enc, err := self.getTrie(db).TryGet(key[:])
|
|
enc, err := self.getTrie(db).TryGet(key[:])
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
self.setError(err)
|
|
self.setError(err)
|
|
@@ -179,37 +190,27 @@ func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
|
|
|
}
|
|
}
|
|
|
value.SetBytes(content)
|
|
value.SetBytes(content)
|
|
|
}
|
|
}
|
|
|
- self.cachedStorage[key] = value
|
|
|
|
|
|
|
+ self.originStorage[key] = value
|
|
|
return value
|
|
return value
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// GetOriginalStateValue returns the state value that is currently in the Trie, that is, ignoring any
|
|
|
|
|
-// changes that have been made but not yet written to trie.
|
|
|
|
|
-func (self *stateObject) GetOriginalStateValue(db Database, key common.Hash) common.Hash{
|
|
|
|
|
- if original, exist:= self.originalValue[key]; exist {
|
|
|
|
|
- // original value has been set, return it
|
|
|
|
|
- return original
|
|
|
|
|
- }
|
|
|
|
|
- return self.GetState(db, key)
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
// SetState updates a value in account storage.
|
|
// SetState updates a value in account storage.
|
|
|
func (self *stateObject) SetState(db Database, key, value common.Hash) {
|
|
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)
|
|
prev := self.GetState(db, key)
|
|
|
|
|
+ if prev == value {
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ // New value is different, update and journal the change
|
|
|
self.db.journal.append(storageChange{
|
|
self.db.journal.append(storageChange{
|
|
|
account: &self.address,
|
|
account: &self.address,
|
|
|
key: key,
|
|
key: key,
|
|
|
prevalue: prev,
|
|
prevalue: prev,
|
|
|
})
|
|
})
|
|
|
- if _, isSet := self.originalValue[key]; !isSet {
|
|
|
|
|
- // original value has not been set, so set it now
|
|
|
|
|
- self.originalValue[key] = prev
|
|
|
|
|
- }
|
|
|
|
|
self.setState(key, value)
|
|
self.setState(key, value)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (self *stateObject) setState(key, value common.Hash) {
|
|
func (self *stateObject) setState(key, value common.Hash) {
|
|
|
- self.cachedStorage[key] = value
|
|
|
|
|
self.dirtyStorage[key] = value
|
|
self.dirtyStorage[key] = value
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -218,6 +219,13 @@ func (self *stateObject) updateTrie(db Database) Trie {
|
|
|
tr := self.getTrie(db)
|
|
tr := self.getTrie(db)
|
|
|
for key, value := range self.dirtyStorage {
|
|
for key, value := range self.dirtyStorage {
|
|
|
delete(self.dirtyStorage, key)
|
|
delete(self.dirtyStorage, key)
|
|
|
|
|
+
|
|
|
|
|
+ // Skip noop changes, persist actual changes
|
|
|
|
|
+ if value == self.originStorage[key] {
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+ self.originStorage[key] = value
|
|
|
|
|
+
|
|
|
if (value == common.Hash{}) {
|
|
if (value == common.Hash{}) {
|
|
|
self.setError(tr.TryDelete(key[:]))
|
|
self.setError(tr.TryDelete(key[:]))
|
|
|
continue
|
|
continue
|
|
@@ -226,10 +234,6 @@ func (self *stateObject) updateTrie(db Database) Trie {
|
|
|
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
|
|
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
|
|
|
self.setError(tr.TryUpdate(key[:], v))
|
|
self.setError(tr.TryUpdate(key[:], v))
|
|
|
}
|
|
}
|
|
|
- // Clean the map containing 'original' value of storage entries
|
|
|
|
|
- for k, _ := range self.originalValue {
|
|
|
|
|
- delete(self.originalValue, k)
|
|
|
|
|
- }
|
|
|
|
|
return tr
|
|
return tr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -299,8 +303,7 @@ func (self *stateObject) deepCopy(db *StateDB) *stateObject {
|
|
|
}
|
|
}
|
|
|
stateObject.code = self.code
|
|
stateObject.code = self.code
|
|
|
stateObject.dirtyStorage = self.dirtyStorage.Copy()
|
|
stateObject.dirtyStorage = self.dirtyStorage.Copy()
|
|
|
- stateObject.cachedStorage = self.dirtyStorage.Copy()
|
|
|
|
|
- stateObject.originalValue = self.originalValue.Copy()
|
|
|
|
|
|
|
+ stateObject.originStorage = self.originStorage.Copy()
|
|
|
stateObject.suicided = self.suicided
|
|
stateObject.suicided = self.suicided
|
|
|
stateObject.dirtyCode = self.dirtyCode
|
|
stateObject.dirtyCode = self.dirtyCode
|
|
|
stateObject.deleted = self.deleted
|
|
stateObject.deleted = self.deleted
|