diff --git a/eth/protocols/snap/metrics.go b/eth/protocols/snap/metrics.go index c04bea4258..8123b6c57f 100644 --- a/eth/protocols/snap/metrics.go +++ b/eth/protocols/snap/metrics.go @@ -35,6 +35,11 @@ var ( // performed to determine if node needs to be deleted. lookupGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/lookup", nil) - // boundaryNodesGauge is the metric to track how many boundary trie node are met. - boundaryNodesGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/boundary", nil) + // boundaryAccountNodesGauge is the metric to track how many boundary trie + // nodes in account trie are met. + boundaryAccountNodesGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/boundary/account", nil) + + // boundaryAccountNodesGauge is the metric to track how many boundary trie + // nodes in storage tries are met. + boundaryStorageNodesGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/boundary/storage", nil) ) diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index 025a83adc5..b36372976e 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -762,9 +762,7 @@ func (s *Syncer) loadSyncStatus() { options = options.WithCleaner(func(path []byte) { s.cleanPath(task.genBatch, common.Hash{}, path) }) - options = options.SkipBoundary(true, true, func(path []byte, hash common.Hash, blob []byte) { - boundaryNodesGauge.Inc(1) - }) + options = options.SkipBoundary(true, true, boundaryAccountNodesGauge) } task.genTrie = trie.NewStackTrie(options) for accountHash, subtasks := range task.SubTasks { @@ -789,9 +787,7 @@ func (s *Syncer) loadSyncStatus() { options = options.WithCleaner(func(path []byte) { s.cleanPath(subtask.genBatch, owner, path) }) - options = options.SkipBoundary(true, true, func(path []byte, hash common.Hash, blob []byte) { - boundaryNodesGauge.Inc(1) - }) + options = options.SkipBoundary(true, true, boundaryStorageNodesGauge) } subtask.genTrie = trie.NewStackTrie(options) } @@ -856,9 +852,7 @@ func (s *Syncer) loadSyncStatus() { options = options.WithCleaner(func(path []byte) { s.cleanPath(batch, common.Hash{}, path) }) - options = options.SkipBoundary(true, true, func(path []byte, hash common.Hash, blob []byte) { - boundaryNodesGauge.Inc(1) - }) + options = options.SkipBoundary(true, true, boundaryAccountNodesGauge) } s.tasks = append(s.tasks, &accountTask{ Next: next, @@ -2066,9 +2060,7 @@ func (s *Syncer) processStorageResponse(res *storageResponse) { options = options.WithCleaner(func(path []byte) { s.cleanPath(batch, owner, path) }) - options.SkipBoundary(true, true, func(path []byte, hash common.Hash, blob []byte) { - boundaryNodesGauge.Inc(1) - }) + options.SkipBoundary(true, true, boundaryStorageNodesGauge) } tasks = append(tasks, &storageTask{ Next: common.Hash{}, @@ -2095,9 +2087,7 @@ func (s *Syncer) processStorageResponse(res *storageResponse) { options = options.WithCleaner(func(path []byte) { s.cleanPath(batch, owner, path) }) - options.SkipBoundary(true, true, func(path []byte, hash common.Hash, blob []byte) { - boundaryNodesGauge.Inc(1) - }) + options.SkipBoundary(true, true, boundaryStorageNodesGauge) } tasks = append(tasks, &storageTask{ Next: r.Start(), diff --git a/trie/stacktrie.go b/trie/stacktrie.go index 6ad30e8322..c86caa6bcf 100644 --- a/trie/stacktrie.go +++ b/trie/stacktrie.go @@ -23,6 +23,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/metrics" ) var ( @@ -35,9 +36,9 @@ type StackTrieOptions struct { Writer func(path []byte, hash common.Hash, blob []byte) // The function to commit the dirty nodes Cleaner func(path []byte) // The function to clean up dangling nodes - SkipLeftBoundary bool // Flag whether the nodes on the left boundary are skipped for committing - SkipRightBoundary bool // Flag whether the nodes on the right boundary are skipped for committing - OnBoundary func(path []byte, hash common.Hash, blob []byte) // Callback invoked when the node is skipped committing + SkipLeftBoundary bool // Flag whether the nodes on the left boundary are skipped for committing + SkipRightBoundary bool // Flag whether the nodes on the right boundary are skipped for committing + boundaryGauge metrics.Gauge // Gauge to track how many boundary nodes are met } // NewStackTrieOptions initializes an empty options for stackTrie. @@ -58,10 +59,10 @@ func (o *StackTrieOptions) WithCleaner(cleaner func(path []byte)) *StackTrieOpti // SkipBoundary configures whether the left and right boundary nodes are filtered // for committing, along with a onBoundary callback invoked whenever the boundary // nodes are met. -func (o *StackTrieOptions) SkipBoundary(skipLeft, skipRight bool, onBoundary func(path []byte, hash common.Hash, blob []byte)) *StackTrieOptions { +func (o *StackTrieOptions) SkipBoundary(skipLeft, skipRight bool, gauge metrics.Gauge) *StackTrieOptions { o.SkipLeftBoundary = skipLeft o.SkipRightBoundary = skipRight - o.OnBoundary = onBoundary + o.boundaryGauge = gauge return o } @@ -429,21 +430,19 @@ func (t *StackTrie) hash(st *stNode, path []byte) { if t.options.Writer == nil { return } - hash := common.BytesToHash(st.val) - // Skip committing if the node is on the left boundary and stackTrie is // configured to filter the boundary. if t.options.SkipLeftBoundary && bytes.HasPrefix(t.first, path) { - if t.options.OnBoundary != nil { - t.options.OnBoundary(path, hash, blob) + if t.options.boundaryGauge != nil { + t.options.boundaryGauge.Inc(1) } return } // Skip committing if the node is on the right boundary and stackTrie is // configured to filter the boundary. if t.options.SkipRightBoundary && bytes.HasPrefix(t.last, path) { - if t.options.OnBoundary != nil { - t.options.OnBoundary(path, hash, blob) + if t.options.boundaryGauge != nil { + t.options.boundaryGauge.Inc(1) } return } diff --git a/trie/stacktrie_test.go b/trie/stacktrie_test.go index 573a3f9cf5..6cdf19d3db 100644 --- a/trie/stacktrie_test.go +++ b/trie/stacktrie_test.go @@ -382,9 +382,8 @@ func TestStacktrieNotModifyValues(t *testing.T) { func buildPartialTree(entries []*kv, t *testing.T) map[string]common.Hash { var ( - options = NewStackTrieOptions() - nodes = make(map[string]common.Hash) - boundary = make(map[string]common.Hash) + options = NewStackTrieOptions() + nodes = make(map[string]common.Hash) ) var ( first int @@ -409,9 +408,7 @@ func buildPartialTree(entries []*kv, t *testing.T) map[string]common.Hash { noRight = true } } - options = options.SkipBoundary(noLeft, noRight, func(path []byte, hash common.Hash, blob []byte) { - boundary[string(path)] = hash - }) + options = options.SkipBoundary(noLeft, noRight, nil) options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) { nodes[string(path)] = hash }) @@ -421,19 +418,6 @@ func buildPartialTree(entries []*kv, t *testing.T) map[string]common.Hash { tr.MustUpdate(entries[i].k, entries[i].v) } tr.Commit() - - for path := range boundary { - var expect bool - if noLeft && bytes.HasPrefix(keybytesToHex(entries[first].k), []byte(path)) { - expect = true - } - if noRight && bytes.HasPrefix(keybytesToHex(entries[last].k), []byte(path)) { - expect = true - } - if !expect { - t.Fatalf("Unexpected boundary node, %v", []byte(path)) - } - } return nodes }