@@ -22,11 +22,13 @@ import (
2222 "fmt"
2323 "math/big"
2424 "sort"
25+ "time"
2526
2627 "github.com/ethereum/go-ethereum/common"
2728 "github.com/ethereum/go-ethereum/core/types"
2829 "github.com/ethereum/go-ethereum/crypto"
2930 "github.com/ethereum/go-ethereum/log"
31+ "github.com/ethereum/go-ethereum/metrics"
3032 "github.com/ethereum/go-ethereum/rlp"
3133 "github.com/ethereum/go-ethereum/trie"
3234)
@@ -86,6 +88,16 @@ type StateDB struct {
8688 journal * journal
8789 validRevisions []revision
8890 nextRevisionId int
91+
92+ // Measurements gathered during execution for debugging purposes
93+ AccountReads time.Duration
94+ AccountHashes time.Duration
95+ AccountUpdates time.Duration
96+ AccountCommits time.Duration
97+ StorageReads time.Duration
98+ StorageHashes time.Duration
99+ StorageUpdates time.Duration
100+ StorageCommits time.Duration
89101}
90102
91103// Create a new state from a given trie.
@@ -386,46 +398,61 @@ func (self *StateDB) Suicide(addr common.Address) bool {
386398//
387399
388400// updateStateObject writes the given object to the trie.
389- func (self * StateDB ) updateStateObject (stateObject * stateObject ) {
401+ func (s * StateDB ) updateStateObject (stateObject * stateObject ) {
402+ // Track the amount of time wasted on updating the account from the trie
403+ if metrics .EnabledExpensive {
404+ defer func (start time.Time ) { s .AccountUpdates += time .Since (start ) }(time .Now ())
405+ }
406+ // Encode the account and update the account trie
390407 addr := stateObject .Address ()
408+
391409 data , err := rlp .EncodeToBytes (stateObject )
392410 if err != nil {
393411 panic (fmt .Errorf ("can't encode object at %x: %v" , addr [:], err ))
394412 }
395- self .setError (self .trie .TryUpdate (addr [:], data ))
413+ s .setError (s .trie .TryUpdate (addr [:], data ))
396414}
397415
398416// deleteStateObject removes the given object from the state trie.
399- func (self * StateDB ) deleteStateObject (stateObject * stateObject ) {
417+ func (s * StateDB ) deleteStateObject (stateObject * stateObject ) {
418+ // Track the amount of time wasted on deleting the account from the trie
419+ if metrics .EnabledExpensive {
420+ defer func (start time.Time ) { s .AccountUpdates += time .Since (start ) }(time .Now ())
421+ }
422+ // Delete the account from the trie
400423 stateObject .deleted = true
424+
401425 addr := stateObject .Address ()
402- self .setError (self .trie .TryDelete (addr [:]))
426+ s .setError (s .trie .TryDelete (addr [:]))
403427}
404428
405429// Retrieve a state object given by the address. Returns nil if not found.
406- func (self * StateDB ) getStateObject (addr common.Address ) (stateObject * stateObject ) {
407- // Prefer ' live' objects.
408- if obj := self .stateObjects [addr ]; obj != nil {
430+ func (s * StateDB ) getStateObject (addr common.Address ) (stateObject * stateObject ) {
431+ // Prefer live objects
432+ if obj := s .stateObjects [addr ]; obj != nil {
409433 if obj .deleted {
410434 return nil
411435 }
412436 return obj
413437 }
414-
415- // Load the object from the database.
416- enc , err := self .trie .TryGet (addr [:])
438+ // Track the amount of time wasted on loading the object from the database
439+ if metrics .EnabledExpensive {
440+ defer func (start time.Time ) { s .AccountReads += time .Since (start ) }(time .Now ())
441+ }
442+ // Load the object from the database
443+ enc , err := s .trie .TryGet (addr [:])
417444 if len (enc ) == 0 {
418- self .setError (err )
445+ s .setError (err )
419446 return nil
420447 }
421448 var data Account
422449 if err := rlp .DecodeBytes (enc , & data ); err != nil {
423450 log .Error ("Failed to decode state object" , "addr" , addr , "err" , err )
424451 return nil
425452 }
426- // Insert into the live set.
427- obj := newObject (self , addr , data )
428- self .setStateObject (obj )
453+ // Insert into the live set
454+ obj := newObject (s , addr , data )
455+ s .setStateObject (obj )
429456 return obj
430457}
431458
@@ -600,6 +627,11 @@ func (s *StateDB) Finalise(deleteEmptyObjects bool) {
600627// goes into transaction receipts.
601628func (s * StateDB ) IntermediateRoot (deleteEmptyObjects bool ) common.Hash {
602629 s .Finalise (deleteEmptyObjects )
630+
631+ // Track the amount of time wasted on hashing the account trie
632+ if metrics .EnabledExpensive {
633+ defer func (start time.Time ) { s .AccountHashes += time .Since (start ) }(time .Now ())
634+ }
603635 return s .trie .Hash ()
604636}
605637
@@ -624,7 +656,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error)
624656 for addr := range s .journal .dirties {
625657 s .stateObjectsDirty [addr ] = struct {}{}
626658 }
627- // Commit objects to the trie.
659+ // Commit objects to the trie, measuring the elapsed time
628660 for addr , stateObject := range s .stateObjects {
629661 _ , isDirty := s .stateObjectsDirty [addr ]
630662 switch {
@@ -647,7 +679,10 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (root common.Hash, err error)
647679 }
648680 delete (s .stateObjectsDirty , addr )
649681 }
650- // Write trie changes.
682+ // Write the account trie changes, measuing the amount of wasted time
683+ if metrics .EnabledExpensive {
684+ defer func (start time.Time ) { s .AccountCommits += time .Since (start ) }(time .Now ())
685+ }
651686 root , err = s .trie .Commit (func (leaf []byte , parent common.Hash ) error {
652687 var account Account
653688 if err := rlp .DecodeBytes (leaf , & account ); err != nil {
0 commit comments