From e59846bbd74bf58f4d0f7dacb6a5c168aea66ae4 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 13 May 2024 20:22:14 -0700 Subject: [PATCH 1/4] core/state: unused prefetcher error from trie method --- core/state/state_object.go | 20 +++++++------------- core/state/statedb.go | 6 +++--- core/state/trie_prefetcher.go | 6 +++--- core/state/trie_prefetcher_test.go | 4 ++-- 4 files changed, 15 insertions(+), 21 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 3239be368cb2..f28bc6bf7a8b 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -150,11 +150,11 @@ func (s *stateObject) getTrie() (Trie, error) { // trie in the state object. The caller might want to do that, but it's cleaner // to break the hidden interdependency between retrieving tries from the db or // from the prefetcher. -func (s *stateObject) getPrefetchedTrie() (Trie, error) { +func (s *stateObject) getPrefetchedTrie() Trie { // If there's nothing to meaningfully return, let the user figure it out by // pulling the trie from disk. if s.data.Root == types.EmptyRootHash || s.db.prefetcher == nil { - return nil, nil + return nil } // Attempt to retrieve the trie from the pretecher return s.db.prefetcher.trie(s.addrHash, s.data.Root) @@ -313,25 +313,19 @@ func (s *stateObject) updateTrie() (Trie, error) { return s.trie, nil } // Retrieve a pretecher populated trie, or fall back to the database - tr, err := s.getPrefetchedTrie() - switch { - case err != nil: - // Fetcher retrieval failed, something's very wrong, abort - s.db.setError(err) - return nil, err - - case tr == nil: + tr := s.getPrefetchedTrie() + if tr == nil { // Fetcher not running or empty trie, fallback to the database trie - tr, err = s.getTrie() + _, err := s.getTrie() if err != nil { s.db.setError(err) return nil, err } - - default: + } else { // Prefetcher returned a live trie, swap it out for the current one s.trie = tr } + // The snapshot storage map for the object var ( storage map[common.Hash][]byte diff --git a/core/state/statedb.go b/core/state/statedb.go index 0ef52a88f6ea..ccc7ca4ba236 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -878,9 +878,9 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { start = time.Now() if s.prefetcher != nil { - if trie, err := s.prefetcher.trie(common.Hash{}, s.originalRoot); err != nil { - log.Error("Failed to retrieve account pre-fetcher trie", "err", err) - } else if trie != nil { + if trie := s.prefetcher.trie(common.Hash{}, s.originalRoot); trie == nil { + log.Error("Failed to retrieve account pre-fetcher trie") + } else { s.trie = trie } } diff --git a/core/state/trie_prefetcher.go b/core/state/trie_prefetcher.go index 7e08964e41bf..2e524a15488b 100644 --- a/core/state/trie_prefetcher.go +++ b/core/state/trie_prefetcher.go @@ -145,16 +145,16 @@ func (p *triePrefetcher) prefetch(owner common.Hash, root common.Hash, addr comm // trie returns the trie matching the root hash, blocking until the fetcher of // the given trie terminates. If no fetcher exists for the request, nil will be // returned. -func (p *triePrefetcher) trie(owner common.Hash, root common.Hash) (Trie, error) { +func (p *triePrefetcher) trie(owner common.Hash, root common.Hash) Trie { // Bail if no trie was prefetched for this root fetcher := p.fetchers[p.trieID(owner, root)] if fetcher == nil { log.Error("Prefetcher missed to load trie", "owner", owner, "root", root) p.deliveryMissMeter.Mark(1) - return nil, nil + return nil } // Subfetcher exists, retrieve its trie - return fetcher.peek(), nil + return fetcher.peek() } // used marks a batch of state items used to allow creating statistics as to diff --git a/core/state/trie_prefetcher_test.go b/core/state/trie_prefetcher_test.go index d6788fad9936..478407dfbb04 100644 --- a/core/state/trie_prefetcher_test.go +++ b/core/state/trie_prefetcher_test.go @@ -58,7 +58,7 @@ func TestUseAfterTerminate(t *testing.T) { if err := prefetcher.prefetch(common.Hash{}, db.originalRoot, common.Address{}, [][]byte{skey.Bytes()}); err == nil { t.Errorf("Prefetch succeeded after terminate: %v", err) } - if _, err := prefetcher.trie(common.Hash{}, db.originalRoot); err != nil { - t.Errorf("Trie retrieval failed after terminate: %v", err) + if tr := prefetcher.trie(common.Hash{}, db.originalRoot); tr == nil { + t.Errorf("Prefetcher returned nil trie after terminate") } } From b01d3a5ddbc1527c7f4bd583e9264ae101617588 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 13 May 2024 20:37:15 -0700 Subject: [PATCH 2/4] fix --- core/state/state_object.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index f28bc6bf7a8b..8eea609f03da 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -315,16 +315,15 @@ func (s *stateObject) updateTrie() (Trie, error) { // Retrieve a pretecher populated trie, or fall back to the database tr := s.getPrefetchedTrie() if tr == nil { + var err error // Fetcher not running or empty trie, fallback to the database trie - _, err := s.getTrie() + tr, err = s.getTrie() if err != nil { s.db.setError(err) return nil, err } - } else { - // Prefetcher returned a live trie, swap it out for the current one - s.trie = tr } + s.trie = tr // The snapshot storage map for the object var ( From d9e3bceb92f2b48db164e27e5dd2edaac989ce9c Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Mon, 13 May 2024 20:54:04 -0700 Subject: [PATCH 3/4] apply suggestion > Co-authored-by: Gary Rong --- core/state/state_object.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 8eea609f03da..8725eafa3ee8 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -314,9 +314,12 @@ func (s *stateObject) updateTrie() (Trie, error) { } // Retrieve a pretecher populated trie, or fall back to the database tr := s.getPrefetchedTrie() - if tr == nil { - var err error + if tr != nil { + // Prefetcher returned a live trie, swap it out for the current one + s.trie = tr + } else { // Fetcher not running or empty trie, fallback to the database trie + var err error tr, err = s.getTrie() if err != nil { s.db.setError(err) From 65077a5af37f07195161aaf2a1a203990550b615 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Tue, 14 May 2024 06:09:00 -0700 Subject: [PATCH 4/4] core/state: remove redundant setting of stateObject trie --- core/state/state_object.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index 8725eafa3ee8..fe5ac50b9874 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -326,7 +326,6 @@ func (s *stateObject) updateTrie() (Trie, error) { return nil, err } } - s.trie = tr // The snapshot storage map for the object var (