Skip to content

Commit 5d921fa

Browse files
committed
core, params: polish net gas metering PR a bit
1 parent caa2c23 commit 5d921fa

File tree

11 files changed

+165
-226
lines changed

11 files changed

+165
-226
lines changed

core/state/state_object.go

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ type stateObject struct {
7777
trie Trie // storage trie, which becomes non-nil on first access
7878
code Code // contract bytecode, which gets set when code is loaded
7979

80-
cachedStorage Storage // Storage entry cache to avoid duplicate reads
80+
originStorage Storage // Storage cache of original entries to dedup rewrites
8181
dirtyStorage Storage // Storage entries that need to be flushed to disk
82-
originalValue Storage // Map of original storage values, at the beginning of current call context
82+
8383
// Cache flags.
8484
// When an object is marked suicided it will be delete from the trie
8585
// during the "update" phase of the state transition.
@@ -115,9 +115,8 @@ func newObject(db *StateDB, address common.Address, data Account) *stateObject {
115115
address: address,
116116
addrHash: crypto.Keccak256Hash(address[:]),
117117
data: data,
118-
cachedStorage: make(Storage),
118+
originStorage: make(Storage),
119119
dirtyStorage: make(Storage),
120-
originalValue: make(Storage),
121120
}
122121
}
123122

@@ -160,13 +159,25 @@ func (c *stateObject) getTrie(db Database) Trie {
160159
return c.trie
161160
}
162161

163-
// GetState returns a value in account storage.
162+
// GetState retrieves a value from the account storage trie.
164163
func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
165-
value, exists := self.cachedStorage[key]
166-
if exists {
164+
// If we have a dirty value for this state entry, return it
165+
value, dirty := self.dirtyStorage[key]
166+
if dirty {
167+
return value
168+
}
169+
// Otherwise return the entry's original value
170+
return self.GetCommittedState(db, key)
171+
}
172+
173+
// GetCommittedState retrieves a value from the committed account storage trie.
174+
func (self *stateObject) GetCommittedState(db Database, key common.Hash) common.Hash {
175+
// If we have the original value cached, return that
176+
value, cached := self.originStorage[key]
177+
if cached {
167178
return value
168179
}
169-
// Load from DB in case it is missing.
180+
// Otherwise load the value from the database
170181
enc, err := self.getTrie(db).TryGet(key[:])
171182
if err != nil {
172183
self.setError(err)
@@ -179,37 +190,27 @@ func (self *stateObject) GetState(db Database, key common.Hash) common.Hash {
179190
}
180191
value.SetBytes(content)
181192
}
182-
self.cachedStorage[key] = value
193+
self.originStorage[key] = value
183194
return value
184195
}
185196

186-
// GetOriginalStateValue returns the state value that is currently in the Trie, that is, ignoring any
187-
// changes that have been made but not yet written to trie.
188-
func (self *stateObject) GetOriginalStateValue(db Database, key common.Hash) common.Hash{
189-
if original, exist:= self.originalValue[key]; exist {
190-
// original value has been set, return it
191-
return original
192-
}
193-
return self.GetState(db, key)
194-
}
195-
196197
// SetState updates a value in account storage.
197198
func (self *stateObject) SetState(db Database, key, value common.Hash) {
199+
// If the new value is the same as old, don't set
198200
prev := self.GetState(db, key)
201+
if prev == value {
202+
return
203+
}
204+
// New value is different, update and journal the change
199205
self.db.journal.append(storageChange{
200206
account: &self.address,
201207
key: key,
202208
prevalue: prev,
203209
})
204-
if _, isSet := self.originalValue[key]; !isSet {
205-
// original value has not been set, so set it now
206-
self.originalValue[key] = prev
207-
}
208210
self.setState(key, value)
209211
}
210212

211213
func (self *stateObject) setState(key, value common.Hash) {
212-
self.cachedStorage[key] = value
213214
self.dirtyStorage[key] = value
214215
}
215216

@@ -218,6 +219,13 @@ func (self *stateObject) updateTrie(db Database) Trie {
218219
tr := self.getTrie(db)
219220
for key, value := range self.dirtyStorage {
220221
delete(self.dirtyStorage, key)
222+
223+
// Skip noop changes, persist actual changes
224+
if value == self.originStorage[key] {
225+
continue
226+
}
227+
self.originStorage[key] = value
228+
221229
if (value == common.Hash{}) {
222230
self.setError(tr.TryDelete(key[:]))
223231
continue
@@ -226,10 +234,6 @@ func (self *stateObject) updateTrie(db Database) Trie {
226234
v, _ := rlp.EncodeToBytes(bytes.TrimLeft(value[:], "\x00"))
227235
self.setError(tr.TryUpdate(key[:], v))
228236
}
229-
// Clean the map containing 'original' value of storage entries
230-
for k, _ := range self.originalValue {
231-
delete(self.originalValue, k)
232-
}
233237
return tr
234238
}
235239

@@ -299,8 +303,7 @@ func (self *stateObject) deepCopy(db *StateDB) *stateObject {
299303
}
300304
stateObject.code = self.code
301305
stateObject.dirtyStorage = self.dirtyStorage.Copy()
302-
stateObject.cachedStorage = self.dirtyStorage.Copy()
303-
stateObject.originalValue = self.originalValue.Copy()
306+
stateObject.originStorage = self.originStorage.Copy()
304307
stateObject.suicided = self.suicided
305308
stateObject.dirtyCode = self.dirtyCode
306309
stateObject.deleted = self.deleted

core/state/state_test.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,15 @@ func (s *StateSuite) TestNull(c *checker.C) {
9696
s.state.CreateAccount(address)
9797
//value := common.FromHex("0x823140710bf13990e4500136726d8b55")
9898
var value common.Hash
99+
99100
s.state.SetState(address, common.Hash{}, value)
100101
s.state.Commit(false)
101-
value = s.state.GetState(address, common.Hash{})
102-
if value != (common.Hash{}) {
103-
c.Errorf("expected empty hash. got %x", value)
102+
103+
if value := s.state.GetState(address, common.Hash{}); value != (common.Hash{}) {
104+
c.Errorf("expected empty current value, got %x", value)
105+
}
106+
if value := s.state.GetCommittedState(address, common.Hash{}); value != (common.Hash{}) {
107+
c.Errorf("expected empty committed value, got %x", value)
104108
}
105109
}
106110

@@ -110,20 +114,24 @@ func (s *StateSuite) TestSnapshot(c *checker.C) {
110114
data1 := common.BytesToHash([]byte{42})
111115
data2 := common.BytesToHash([]byte{43})
112116

117+
// snapshot the genesis state
118+
genesis := s.state.Snapshot()
119+
113120
// set initial state object value
114121
s.state.SetState(stateobjaddr, storageaddr, data1)
115-
// get snapshot of current state
116122
snapshot := s.state.Snapshot()
117123

118-
// set new state object value
124+
// set a new state object value, revert it and ensure correct content
119125
s.state.SetState(stateobjaddr, storageaddr, data2)
120-
// restore snapshot
121126
s.state.RevertToSnapshot(snapshot)
122127

123-
// get state storage value
124-
res := s.state.GetState(stateobjaddr, storageaddr)
128+
c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, data1)
129+
c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
125130

126-
c.Assert(data1, checker.DeepEquals, res)
131+
// revert up to the genesis state and ensure correct content
132+
s.state.RevertToSnapshot(genesis)
133+
c.Assert(s.state.GetState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
134+
c.Assert(s.state.GetCommittedState(stateobjaddr, storageaddr), checker.DeepEquals, common.Hash{})
127135
}
128136

129137
func (s *StateSuite) TestSnapshotEmpty(c *checker.C) {
@@ -208,24 +216,30 @@ func compareStateObjects(so0, so1 *stateObject, t *testing.T) {
208216
t.Fatalf("Code mismatch: have %v, want %v", so0.code, so1.code)
209217
}
210218

211-
if len(so1.cachedStorage) != len(so0.cachedStorage) {
212-
t.Errorf("Storage size mismatch: have %d, want %d", len(so1.cachedStorage), len(so0.cachedStorage))
219+
if len(so1.dirtyStorage) != len(so0.dirtyStorage) {
220+
t.Errorf("Dirty storage size mismatch: have %d, want %d", len(so1.dirtyStorage), len(so0.dirtyStorage))
213221
}
214-
for k, v := range so1.cachedStorage {
215-
if so0.cachedStorage[k] != v {
216-
t.Errorf("Storage key %x mismatch: have %v, want %v", k, so0.cachedStorage[k], v)
222+
for k, v := range so1.dirtyStorage {
223+
if so0.dirtyStorage[k] != v {
224+
t.Errorf("Dirty storage key %x mismatch: have %v, want %v", k, so0.dirtyStorage[k], v)
217225
}
218226
}
219-
for k, v := range so0.cachedStorage {
220-
if so1.cachedStorage[k] != v {
221-
t.Errorf("Storage key %x mismatch: have %v, want none.", k, v)
227+
for k, v := range so0.dirtyStorage {
228+
if so1.dirtyStorage[k] != v {
229+
t.Errorf("Dirty storage key %x mismatch: have %v, want none.", k, v)
222230
}
223231
}
224-
225-
if so0.suicided != so1.suicided {
226-
t.Fatalf("suicided mismatch: have %v, want %v", so0.suicided, so1.suicided)
232+
if len(so1.originStorage) != len(so0.originStorage) {
233+
t.Errorf("Origin storage size mismatch: have %d, want %d", len(so1.originStorage), len(so0.originStorage))
234+
}
235+
for k, v := range so1.originStorage {
236+
if so0.originStorage[k] != v {
237+
t.Errorf("Origin storage key %x mismatch: have %v, want %v", k, so0.originStorage[k], v)
238+
}
227239
}
228-
if so0.deleted != so1.deleted {
229-
t.Fatalf("Deleted mismatch: have %v, want %v", so0.deleted, so1.deleted)
240+
for k, v := range so0.originStorage {
241+
if so1.originStorage[k] != v {
242+
t.Errorf("Origin storage key %x mismatch: have %v, want none.", k, v)
243+
}
230244
}
231245
}

core/state/statedb.go

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -247,18 +247,20 @@ func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
247247
return common.BytesToHash(stateObject.CodeHash())
248248
}
249249

250-
func (self *StateDB) GetState(addr common.Address, bhash common.Hash) common.Hash {
250+
// GetState retrieves a value from the given account's storage trie.
251+
func (self *StateDB) GetState(addr common.Address, hash common.Hash) common.Hash {
251252
stateObject := self.getStateObject(addr)
252253
if stateObject != nil {
253-
return stateObject.GetState(self.db, bhash)
254+
return stateObject.GetState(self.db, hash)
254255
}
255256
return common.Hash{}
256257
}
257258

258-
func (self *StateDB) GetStateOriginal(addr common.Address, bhash common.Hash) common.Hash {
259+
// GetCommittedState retrieves a value from the given account's committed storage trie.
260+
func (self *StateDB) GetCommittedState(addr common.Address, hash common.Hash) common.Hash {
259261
stateObject := self.getStateObject(addr)
260262
if stateObject != nil {
261-
return stateObject.GetOriginalStateValue(self.db, bhash)
263+
return stateObject.GetCommittedState(self.db, hash)
262264
}
263265
return common.Hash{}
264266
}
@@ -454,19 +456,14 @@ func (db *StateDB) ForEachStorage(addr common.Address, cb func(key, value common
454456
if so == nil {
455457
return
456458
}
457-
458-
// When iterating over the storage check the cache first
459-
for h, value := range so.cachedStorage {
460-
cb(h, value)
461-
}
462-
463459
it := trie.NewIterator(so.getTrie(db.db).NodeIterator(nil))
464460
for it.Next() {
465-
// ignore cached values
466461
key := common.BytesToHash(db.trie.GetKey(it.Key))
467-
if _, ok := so.cachedStorage[key]; !ok {
468-
cb(key, common.BytesToHash(it.Value))
462+
if value, dirty := so.dirtyStorage[key]; dirty {
463+
cb(key, value)
464+
continue
469465
}
466+
cb(key, common.BytesToHash(it.Value))
470467
}
471468
}
472469

core/state/statedb_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error {
381381
checkeq("GetCodeSize", state.GetCodeSize(addr), checkstate.GetCodeSize(addr))
382382
// Check storage.
383383
if obj := state.getStateObject(addr); obj != nil {
384-
state.ForEachStorage(addr, func(key, val common.Hash) bool {
385-
return checkeq("GetState("+key.Hex()+")", val, checkstate.GetState(addr, key))
384+
state.ForEachStorage(addr, func(key, value common.Hash) bool {
385+
return checkeq("GetState("+key.Hex()+")", checkstate.GetState(addr, key), value)
386386
})
387-
checkstate.ForEachStorage(addr, func(key, checkval common.Hash) bool {
388-
return checkeq("GetState("+key.Hex()+")", state.GetState(addr, key), checkval)
387+
checkstate.ForEachStorage(addr, func(key, value common.Hash) bool {
388+
return checkeq("GetState("+key.Hex()+")", checkstate.GetState(addr, key), value)
389389
})
390390
}
391391
if err != nil {

0 commit comments

Comments
 (0)