Skip to content

Commit 51e7968

Browse files
authored
core/state: fix read-meters + simplify code (#24304)
1 parent fb3a652 commit 51e7968

File tree

2 files changed

+19
-35
lines changed

2 files changed

+19
-35
lines changed

core/state/state_object.go

+10-25
Original file line numberDiff line numberDiff line change
@@ -198,25 +198,10 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
198198
}
199199
// If no live objects are available, attempt to use snapshots
200200
var (
201-
enc []byte
202-
err error
203-
meter *time.Duration
201+
enc []byte
202+
err error
204203
)
205-
readStart := time.Now()
206-
if metrics.EnabledExpensive {
207-
// If the snap is 'under construction', the first lookup may fail. If that
208-
// happens, we don't want to double-count the time elapsed. Thus this
209-
// dance with the metering.
210-
defer func() {
211-
if meter != nil {
212-
*meter += time.Since(readStart)
213-
}
214-
}()
215-
}
216204
if s.db.snap != nil {
217-
if metrics.EnabledExpensive {
218-
meter = &s.db.SnapshotStorageReads
219-
}
220205
// If the object was destructed in *this* block (and potentially resurrected),
221206
// the storage has been cleared out, and we should *not* consult the previous
222207
// snapshot about any storage values. The only possible alternatives are:
@@ -226,20 +211,20 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
226211
if _, destructed := s.db.snapDestructs[s.addrHash]; destructed {
227212
return common.Hash{}
228213
}
214+
start := time.Now()
229215
enc, err = s.db.snap.Storage(s.addrHash, crypto.Keccak256Hash(key.Bytes()))
216+
if metrics.EnabledExpensive {
217+
s.db.SnapshotStorageReads += time.Since(start)
218+
}
230219
}
231220
// If the snapshot is unavailable or reading from it fails, load from the database.
232221
if s.db.snap == nil || err != nil {
233-
if meter != nil {
234-
// If we already spent time checking the snapshot, account for it
235-
// and reset the readStart
236-
*meter += time.Since(readStart)
237-
readStart = time.Now()
238-
}
222+
start := time.Now()
223+
enc, err = s.getTrie(db).TryGet(key.Bytes())
239224
if metrics.EnabledExpensive {
240-
meter = &s.db.StorageReads
225+
s.db.StorageReads += time.Since(start)
241226
}
242-
if enc, err = s.getTrie(db).TryGet(key.Bytes()); err != nil {
227+
if err != nil {
243228
s.setError(err)
244229
return common.Hash{}
245230
}

core/state/statedb.go

+9-10
Original file line numberDiff line numberDiff line change
@@ -506,16 +506,14 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
506506
return obj
507507
}
508508
// If no live objects are available, attempt to use snapshots
509-
var (
510-
data *types.StateAccount
511-
err error
512-
)
509+
var data *types.StateAccount
513510
if s.snap != nil {
511+
start := time.Now()
512+
acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes()))
514513
if metrics.EnabledExpensive {
515-
defer func(start time.Time) { s.SnapshotAccountReads += time.Since(start) }(time.Now())
514+
s.SnapshotAccountReads += time.Since(start)
516515
}
517-
var acc *snapshot.Account
518-
if acc, err = s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())); err == nil {
516+
if err == nil {
519517
if acc == nil {
520518
return nil
521519
}
@@ -534,11 +532,12 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *stateObject {
534532
}
535533
}
536534
// If snapshot unavailable or reading from it failed, load from the database
537-
if s.snap == nil || err != nil {
535+
if data == nil {
536+
start := time.Now()
537+
enc, err := s.trie.TryGet(addr.Bytes())
538538
if metrics.EnabledExpensive {
539-
defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
539+
s.AccountReads += time.Since(start)
540540
}
541-
enc, err := s.trie.TryGet(addr.Bytes())
542541
if err != nil {
543542
s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr.Bytes(), err))
544543
return nil

0 commit comments

Comments
 (0)