From 74d9df9b340743224f0bd8bc0df506127c1af2f1 Mon Sep 17 00:00:00 2001 From: Max Revitt Date: Thu, 26 Sep 2024 14:42:21 +0100 Subject: [PATCH] fix(stage_batches): only unwind if the stream block is ahead of the db block --- zk/stages/stage_batches.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/zk/stages/stage_batches.go b/zk/stages/stage_batches.go index 97a0ed739c5..2409178db41 100644 --- a/zk/stages/stage_batches.go +++ b/zk/stages/stage_batches.go @@ -353,8 +353,8 @@ LOOP: return err } - if entry.BatchNumber != dbBatchNum { - // if the bath number mismatches, it means that we need to trigger an unwinding of blocks + if entry.BatchNumber > dbBatchNum { + // if the batch number is higher than the one we know about, it means that we need to trigger an unwinding of blocks log.Warn(fmt.Sprintf("[%s] Batch number mismatch detected. Triggering unwind...", logPrefix), "block", entry.L2BlockNumber, "ds batch", entry.BatchNumber, "db batch", dbBatchNum) if err := rollback(logPrefix, eriDb, hermezDb, dsQueryClient, entry.L2BlockNumber, tx, u); err != nil { @@ -400,7 +400,13 @@ LOOP: // skip if we already have this block if entry.L2BlockNumber < lastBlockHeight+1 { - log.Warn(fmt.Sprintf("[%s] Unwinding to block %d", logPrefix, entry.L2BlockNumber)) + log.Warn(fmt.Sprintf("[%s] Skipping block %d, already processed", logPrefix, entry.L2BlockNumber)) + continue + } + + // check for sequential block numbers + if entry.L2BlockNumber > lastBlockHeight+1 { + log.Warn(fmt.Sprintf("[%s] Stream skipped ahead, unwinding to block %d", logPrefix, entry.L2BlockNumber)) badBlock, err := eriDb.ReadCanonicalHash(entry.L2BlockNumber) if err != nil { return fmt.Errorf("failed to get bad block: %v", err) @@ -409,11 +415,6 @@ LOOP: return nil } - // check for sequential block numbers - if entry.L2BlockNumber != lastBlockHeight+1 { - return fmt.Errorf("block number is not sequential, expected %d, got %d", lastBlockHeight+1, entry.L2BlockNumber) - } - // batch boundary - record the highest hashable block number (last block in last full batch) if entry.BatchNumber > highestSeenBatchNo { highestHashableL2BlockNo = entry.L2BlockNumber - 1