Skip to content

Commit

Permalink
eth, trie: address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 committed Oct 20, 2023
1 parent 10e4305 commit 573676a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
4 changes: 4 additions & 0 deletions eth/protocols/snap/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ var (
// largeStorageGauge is the metric to track how many storages are large enough
// to retrieved concurrently.
largeStorageGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/storage/large", nil)

// skipStorageHealingGauge is the metric to track how many storages are retrieved
// in multiple requests but healing is not necessary.
skipStorageHealingGauge = metrics.NewRegisteredGauge("eth/protocols/snap/sync/storage/noheal", nil)
)
21 changes: 14 additions & 7 deletions eth/protocols/snap/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ func (s *Syncer) loadSyncStatus() {
})
// Skip the left boundary if it's not the first range.
// Skip the right boundary if it's not the last range.
options = options.SkipBoundary(task.Next != (common.Hash{}), task.Last != common.MaxHash, boundaryAccountNodesGauge)
options = options.WithSkipBoundary(task.Next != (common.Hash{}), task.Last != common.MaxHash, boundaryAccountNodesGauge)
}
task.genTrie = trie.NewStackTrie(options)
for accountHash, subtasks := range task.SubTasks {
Expand All @@ -791,7 +791,7 @@ func (s *Syncer) loadSyncStatus() {
})
// Skip the left boundary if it's not the first range.
// Skip the right boundary if it's not the last range.
options = options.SkipBoundary(subtask.Next != common.Hash{}, subtask.Last != common.MaxHash, boundaryStorageNodesGauge)
options = options.WithSkipBoundary(subtask.Next != common.Hash{}, subtask.Last != common.MaxHash, boundaryStorageNodesGauge)
}
subtask.genTrie = trie.NewStackTrie(options)
}
Expand Down Expand Up @@ -858,7 +858,7 @@ func (s *Syncer) loadSyncStatus() {
})
// Skip the left boundary if it's not the first range.
// Skip the right boundary if it's not the last range.
options = options.SkipBoundary(next != common.Hash{}, last != common.MaxHash, boundaryAccountNodesGauge)
options = options.WithSkipBoundary(next != common.Hash{}, last != common.MaxHash, boundaryAccountNodesGauge)
}
s.tasks = append(s.tasks, &accountTask{
Next: next,
Expand Down Expand Up @@ -2073,7 +2073,7 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
})
// Keep the left boundary as it's the first range.
// Skip the right boundary if it's not the last range.
options.SkipBoundary(false, r.End() != common.MaxHash, boundaryStorageNodesGauge)
options = options.WithSkipBoundary(false, r.End() != common.MaxHash, boundaryStorageNodesGauge)
}
tasks = append(tasks, &storageTask{
Next: common.Hash{},
Expand Down Expand Up @@ -2102,7 +2102,7 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
})
// Skip the left boundary as it's not the first range
// Skip the right boundary if it's not the last range.
options.SkipBoundary(true, r.End() != common.MaxHash, boundaryStorageNodesGauge)
options = options.WithSkipBoundary(true, r.End() != common.MaxHash, boundaryStorageNodesGauge)
}
tasks = append(tasks, &storageTask{
Next: r.Start(),
Expand Down Expand Up @@ -2193,17 +2193,24 @@ func (s *Syncer) processStorageResponse(res *storageResponse) {
if res.subTask != nil {
if res.subTask.done {
root := res.subTask.genTrie.Commit()
if err := res.subTask.genBatch.Write(); err != nil {
log.Error("Failed to persist stack slots", "err", err)
}
res.subTask.genBatch.Reset()

// If the chunk's root is an overflown but full delivery,
// clear the heal request.
accountHash := res.accounts[len(res.accounts)-1]
if root == res.subTask.root && rawdb.HasStorageTrieNode(s.db, accountHash, nil, root) {
// If the chunk's root is an overflown but full delivery, clear the heal request
for i, account := range res.mainTask.res.hashes {
if account == accountHash {
res.mainTask.needHeal[i] = false
skipStorageHealingGauge.Inc(1)
}
}
}
}
if res.subTask.genBatch.ValueSize() > ethdb.IdealBatchSize || res.subTask.done {
if res.subTask.genBatch.ValueSize() > ethdb.IdealBatchSize {
if err := res.subTask.genBatch.Write(); err != nil {
log.Error("Failed to persist stack slots", "err", err)
}
Expand Down
8 changes: 4 additions & 4 deletions trie/stacktrie.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ func (o *StackTrieOptions) WithCleaner(cleaner func(path []byte)) *StackTrieOpti
return o
}

// 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, gauge metrics.Gauge) *StackTrieOptions {
// WithSkipBoundary configures whether the left and right boundary nodes are
// filtered for committing, along with a gauge metrics to track how many
// boundary nodes are met.
func (o *StackTrieOptions) WithSkipBoundary(skipLeft, skipRight bool, gauge metrics.Gauge) *StackTrieOptions {
o.SkipLeftBoundary = skipLeft
o.SkipRightBoundary = skipRight
o.boundaryGauge = gauge
Expand Down
2 changes: 1 addition & 1 deletion trie/stacktrie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func buildPartialTree(entries []*kv, t *testing.T) map[string]common.Hash {
noRight = true
}
}
options = options.SkipBoundary(noLeft, noRight, nil)
options = options.WithSkipBoundary(noLeft, noRight, nil)
options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) {
nodes[string(path)] = hash
})
Expand Down

0 comments on commit 573676a

Please sign in to comment.