Skip to content

Commit

Permalink
core/state: fix read-meters + simplify code (ethereum#24304)
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman authored and qinglin89 committed Aug 18, 2022
1 parent 11d2bae commit 788b771
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
38 changes: 13 additions & 25 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,10 @@ func (s *StateObject) GetCommittedState(db Database, key common.Hash) common.Has
}
// If no live objects are available, attempt to use snapshots
var (
enc []byte
err error
meter *time.Duration
enc []byte
err error
)
readStart := time.Now()
if metrics.EnabledExpensive {
// If the snap is 'under construction', the first lookup may fail. If that
// happens, we don't want to double-count the time elapsed. Thus this
// dance with the metering.
defer func() {
if meter != nil {
*meter += time.Since(readStart)
}
}()
}
if s.db.snap != nil {
if metrics.EnabledExpensive {
meter = &s.db.SnapshotStorageReads
}
// If the object was destructed in *this* block (and potentially resurrected),
// the storage has been cleared out, and we should *not* consult the previous
// snapshot about any storage values. The only possible alternatives are:
Expand All @@ -266,21 +251,24 @@ func (s *StateObject) GetCommittedState(db Database, key common.Hash) common.Has
if _, destructed := s.db.snapDestructs[s.address]; destructed {
return common.Hash{}
}
start := time.Now()
enc, err = s.db.snap.Storage(s.addrHash, crypto.Keccak256Hash(key.Bytes()))
if metrics.EnabledExpensive {
s.db.SnapshotStorageReads += time.Since(start)
}
}

// If snapshot unavailable or reading from it failed, load from the database
if s.db.snap == nil || err != nil {
if meter != nil {
// If we already spent time checking the snapshot, account for it
// and reset the readStart
*meter += time.Since(readStart)
readStart = time.Now()
}
start := time.Now()
// if metrics.EnabledExpensive {
// meter = &s.db.StorageReads
// }
enc, err = s.getTrie(db).TryGet(key.Bytes())
if metrics.EnabledExpensive {
meter = &s.db.StorageReads
s.db.StorageReads += time.Since(start)
}
if enc, err = s.getTrie(db).TryGet(key.Bytes()); err != nil {
if err != nil {
s.setError(err)
return common.Hash{}
}
Expand Down
19 changes: 9 additions & 10 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -657,16 +657,14 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *StateObject {
return obj
}
// If no live objects are available, attempt to use snapshots
var (
data *types.StateAccount
err error
)
var data *types.StateAccount
if s.snap != nil {
start := time.Now()
acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes()))
if metrics.EnabledExpensive {
defer func(start time.Time) { s.SnapshotAccountReads += time.Since(start) }(time.Now())
s.SnapshotAccountReads += time.Since(start)
}
var acc *snapshot.Account
if acc, err = s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())); err == nil {
if err == nil {
if acc == nil {
return nil
}
Expand All @@ -686,7 +684,7 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *StateObject {
}

// If snapshot unavailable or reading from it failed, load from the database
if s.snap == nil || err != nil {
if data == nil {
if s.trie == nil {
tr, err := s.db.OpenTrie(s.originalRoot)
if err != nil {
Expand All @@ -695,10 +693,11 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *StateObject {
}
s.trie = tr
}
start := time.Now()
enc, err := s.trie.TryGet(addr.Bytes())
if metrics.EnabledExpensive {
defer func(start time.Time) { s.AccountReads += time.Since(start) }(time.Now())
s.AccountReads += time.Since(start)
}
enc, err := s.trie.TryGet(addr.Bytes())
if err != nil {
s.setError(fmt.Errorf("getDeleteStateObject (%x) error: %v", addr.Bytes(), err))
return nil
Expand Down

0 comments on commit 788b771

Please sign in to comment.