From 7e520c43104fd0447acd7372a63ba1daa5c05929 Mon Sep 17 00:00:00 2001 From: daixiheguu Date: Sat, 8 Aug 2026 16:24:04 +0800 Subject: [PATCH] eth/protocols/snap: advance catch-up pivot after batch commit (#35463) --- eth/protocols/snap/syncv2.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/eth/protocols/snap/syncv2.go b/eth/protocols/snap/syncv2.go index 01d09c073e..29607a2c83 100644 --- a/eth/protocols/snap/syncv2.go +++ b/eth/protocols/snap/syncv2.go @@ -918,16 +918,18 @@ func (s *syncerV2) catchUp(target *types.Header, cancel chan struct{}) error { } // Persist incremental progress so a crash mid-catchUp can resume - // from the next unapplied block. - s.lock.Lock() - s.pivot = headers[hash] - s.lock.Unlock() - s.saveSyncStatusWithDB(batch) + // from the next unapplied block. Serialize the next pivot without + // advancing the in-memory pivot until the batch has committed. + nextPivot := headers[hash] + s.saveSyncStatusWith(batch, nextPivot) // Commit the state transition alongside the sync progress atomically. if err := batch.Write(); err != nil { return err } + s.lock.Lock() + s.pivot = nextPivot + s.lock.Unlock() } log.Info("BAL catch-up progress", "applied", end, "target", to, "remaining", to-end) } @@ -1414,11 +1416,12 @@ func (s *syncerV2) resetSyncState() { // saveSyncStatus marshals the remaining sync tasks into db. func (s *syncerV2) saveSyncStatus() { - s.saveSyncStatusWithDB(s.db) + s.saveSyncStatusWith(s.db, s.pivot) } -// saveSyncStatusWithDB marshals the remaining sync tasks into the given database. -func (s *syncerV2) saveSyncStatusWithDB(db ethdb.KeyValueWriter) { +// saveSyncStatusWith marshals the remaining sync tasks alongside the provided +// pivot header into the database. +func (s *syncerV2) saveSyncStatusWith(db ethdb.KeyValueWriter, pivot *types.Header) { // Serialize any partial progress to disk before spinning down for _, task := range s.tasks { // Save the account hashes of completed storage. @@ -1432,7 +1435,7 @@ func (s *syncerV2) saveSyncStatusWithDB(db ethdb.KeyValueWriter) { } // Store the actual progress markers. progress := &syncProgressV2{ - Pivot: s.pivot, + Pivot: pivot, Tasks: s.tasks, Phase: s.getPhase(), AccountSynced: s.accountSynced,