core, params: polish net gas metering PR a bit

This commit is contained in:
Péter Szilágyi
2018-09-18 16:24:35 +03:00
parent caa2c23a38
commit 5d921fa3a0
11 changed files with 167 additions and 228 deletions

View File

@ -77,9 +77,9 @@ 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
originalValue Storage // Map of original storage values, at the beginning of current call context
// Cache flags.
// When an object is marked suicided it will be delete from the trie
// during the "update" phase of the state transition.
@ -115,9 +115,8 @@ 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),
originalValue: make(Storage),
}
}
@ -160,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)
@ -179,37 +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
}
// 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.
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: 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)
}
func (self *stateObject) setState(key, value common.Hash) {
self.cachedStorage[key] = value
self.dirtyStorage[key] = value
}
@ -218,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
@ -226,10 +234,6 @@ func (self *stateObject) updateTrie(db Database) Trie {
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
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
}
@ -299,8 +303,7 @@ func (self *stateObject) deepCopy(db *StateDB) *stateObject {
}
stateObject.code = self.code
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.dirtyCode = self.dirtyCode
stateObject.deleted = self.deleted