eth, trie: remove onBoundary callback

This commit is contained in:
Gary Rong 2023-10-18 16:01:15 +08:00
parent c884b4dfcc
commit a43ea13491
4 changed files with 25 additions and 47 deletions

View file

@ -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)
)

View file

@ -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(),

View file

@ -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 (
@ -37,7 +38,7 @@ type StackTrieOptions struct {
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
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
}

View file

@ -384,7 +384,6 @@ 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)
)
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
}