core/state: fix flawed meter on storage reads

This commit is contained in:
Martin Holst Swende 2020-02-06 09:57:07 +01:00
parent cf82fca583
commit 31e89911b1
No known key found for this signature in database
GPG key ID: 683B438C05A5DDF0

View file

@ -199,10 +199,22 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
var ( var (
enc []byte enc []byte
err error err error
meter *time.Duration
) )
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 s.db.snap != nil {
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { s.db.SnapshotStorageReads += time.Since(start) }(time.Now()) meter = &s.db.SnapshotStorageReads
} }
// If the object was destructed in *this* block (and potentially resurrected), // If the object was destructed in *this* block (and potentially resurrected),
// the storage has been cleared out, and we should *not* consult the previous // the storage has been cleared out, and we should *not* consult the previous
@ -217,8 +229,14 @@ func (s *stateObject) GetCommittedState(db Database, key common.Hash) common.Has
} }
// If snapshot unavailable or reading from it failed, load from the database // If snapshot unavailable or reading from it failed, load from the database
if s.db.snap == nil || err != nil { 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()
}
if metrics.EnabledExpensive { if metrics.EnabledExpensive {
defer func(start time.Time) { s.db.StorageReads += time.Since(start) }(time.Now()) meter = &s.db.StorageReads
} }
if enc, err = s.getTrie(db).TryGet(key[:]); err != nil { if enc, err = s.getTrie(db).TryGet(key[:]); err != nil {
s.setError(err) s.setError(err)