Skip to content

Commit 37800ee

Browse files
committed
eth/protocols/snap, trie: address review concerns
1 parent 8648b65 commit 37800ee

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

eth/protocols/snap/sync.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,8 @@ func (s *Syncer) Sync(root common.Hash, cancel chan struct{}) error {
615615
}
616616
}()
617617
defer s.report(true)
618+
// commit any trie- and bytecode-healing data.
619+
defer s.commitHealer(true)
618620

619621
// Whether sync completed or not, disregard any future packets
620622
defer func() {
@@ -651,7 +653,6 @@ func (s *Syncer) Sync(root common.Hash, cancel chan struct{}) error {
651653
trienodeHealResps = make(chan *trienodeHealResponse)
652654
bytecodeHealResps = make(chan *bytecodeHealResponse)
653655
)
654-
defer s.commit(true)
655656
for {
656657
// Remove all completed tasks and terminate sync if everything's done
657658
s.cleanStorageTasks()
@@ -2155,7 +2156,7 @@ func (s *Syncer) processTrienodeHealResponse(res *trienodeHealResponse) {
21552156
log.Error("Invalid trienode processed", "hash", hash, "err", err)
21562157
}
21572158
}
2158-
s.commit(false)
2159+
s.commitHealer(false)
21592160

21602161
// Calculate the processing rate of one filled trie node
21612162
rate := float64(fills) / (float64(time.Since(start)) / float64(time.Second))
@@ -2202,7 +2203,7 @@ func (s *Syncer) processTrienodeHealResponse(res *trienodeHealResponse) {
22022203
}
22032204
}
22042205

2205-
func (s *Syncer) commit(force bool) {
2206+
func (s *Syncer) commitHealer(force bool) {
22062207
if !force && s.healer.scheduler.MemSize() < ethdb.IdealBatchSize {
22072208
return
22082209
}
@@ -2242,7 +2243,7 @@ func (s *Syncer) processBytecodeHealResponse(res *bytecodeHealResponse) {
22422243
log.Error("Invalid bytecode processed", "hash", hash, "err", err)
22432244
}
22442245
}
2245-
s.commit(false)
2246+
s.commitHealer(false)
22462247
}
22472248

22482249
// forwardAccountTask takes a filled account task and persists anything available

trie/sync.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ type syncMemBatch struct {
111111
nodes map[string][]byte // In-memory membatch of recently completed nodes
112112
hashes map[string]common.Hash // Hashes of recently completed nodes
113113
codes map[common.Hash][]byte // In-memory membatch of recently completed codes
114-
size uint64
114+
size uint64 // Estimated batch-size of in-memory data.
115115
}
116116

117117
// newSyncMemBatch allocates a new memory-buffer for not-yet persisted trie nodes.
@@ -339,6 +339,7 @@ func (s *Sync) Commit(dbw ethdb.Batch) error {
339339
return nil
340340
}
341341

342+
// MemSize returns an estimated size (in bytes) of the data held in the membatch.
342343
func (s *Sync) MemSize() uint64 {
343344
return s.membatch.size
344345
}
@@ -484,7 +485,10 @@ func (s *Sync) commitNodeRequest(req *nodeRequest) error {
484485
// Write the node content to the membatch
485486
s.membatch.nodes[string(req.path)] = req.data
486487
s.membatch.hashes[string(req.path)] = req.hash
487-
s.membatch.size += 2*uint64(len(req.path)) + 32 + uint64(len(req.data))
488+
// The size tracking refers to the db-batch, not the in-memory data.
489+
// Therefore, we ignore the req.path, and account only for the hash+data
490+
// which eventually is written to db.
491+
s.membatch.size += common.HashLength + uint64(len(req.data))
488492
delete(s.nodeReqs, string(req.path))
489493
s.fetches[len(req.path)]--
490494

@@ -506,7 +510,7 @@ func (s *Sync) commitNodeRequest(req *nodeRequest) error {
506510
func (s *Sync) commitCodeRequest(req *codeRequest) error {
507511
// Write the node content to the membatch
508512
s.membatch.codes[req.hash] = req.data
509-
s.membatch.size += uint64(32 + len(req.data))
513+
s.membatch.size += common.HashLength + uint64(len(req.data))
510514
delete(s.codeReqs, req.hash)
511515
s.fetches[len(req.path)]--
512516

0 commit comments

Comments
 (0)