Skip to content

Commit

Permalink
fix: driver handling closing download channel
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Jan 20, 2025
1 parent 00b33d0 commit 9447a00
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
10 changes: 4 additions & 6 deletions l1infotreesync/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,6 @@ func TestWithReorgs(t *testing.T) {
// Block 4, 5, 6 after the fork
commitBlocks(t, client, 3, time.Millisecond*500)

// Make sure syncer is up to date
waitForSyncerToCatchUp(ctx, t, syncer, client)

// Assert rollup exit root after the fork - should be zero since there are no events in the block after the fork
expectedRollupExitRoot, err = verifySC.GetRollupExitRoot(&bind.CallOpts{Pending: false})
require.NoError(t, err)
Expand All @@ -244,11 +241,12 @@ func TestWithReorgs(t *testing.T) {
require.NoError(t, err)
time.Sleep(time.Millisecond * 500)

commitBlocks(t, client, 1, time.Millisecond*100)

// create some events and update the trees
updateL1InfoTreeAndRollupExitTree(2, 1)

// Block 4, 5, 6, 7 after the fork
commitBlocks(t, client, 4, time.Millisecond*100)
commitBlocks(t, client, 1, time.Millisecond*100)

// Make sure syncer is up to date
waitForSyncerToCatchUp(ctx, t, syncer, client)
Expand Down Expand Up @@ -360,7 +358,7 @@ func waitForSyncerToCatchUp(ctx context.Context, t *testing.T, syncer *l1infotre
require.NoError(t, err)
lb, err := client.Client().BlockNumber(ctx)
require.NoError(t, err)
if lpb == lb {
if lpb == lb-1 || lpb == lb {
syncerUpToDate = true
break
}
Expand Down
3 changes: 2 additions & 1 deletion sync/evmdownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ func (d *EVMDownloader) Download(ctx context.Context, fromBlock uint64, download
// and we do not need to track it in the reorg detector
reportBlocksFn(blocks.Len())
fromBlock = lastFinalizedBlockNumber + 1
} else if blocks[len(blocks)-1].Num < toBlock {
} else if blocks[blocks.Len()-1].Num < toBlock {
// if we have logs in some of the blocks, and they are not all finalized,
// check if we have finalized blocks in gotten range, report them and
// set the from block from the last finalized block and keep increasing the range
// if not keep getting that range to protect us from possible mishandling of block hashes
lastFinalizedBlock, index, exists := blocks.LastFinalizedBlock(lastFinalizedBlockNumber)
if exists {
reportBlocksFn(index + 1) // num of blocks to report is index + 1 since index is zero based
Expand Down
10 changes: 7 additions & 3 deletions sync/evmdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ reset:
cancellableCtx, cancel := context.WithCancel(ctx)
defer cancel()

log.Info("Starting sync...", " lastProcessedBlock", lastProcessedBlock)
log.Info("Starting sync...", " lastProcessedBlock ", lastProcessedBlock)
// start downloading
downloadCh := make(chan EVMBlock, d.downloadBufferSize)
go d.downloader.Download(cancellableCtx, lastProcessedBlock+1, downloadCh)
Expand All @@ -92,9 +92,13 @@ reset:
d.log.Info("sync stopped due to context done")
cancel()
return
case b := <-downloadCh:
case b, ok := <-downloadCh:
d.log.Debugf("handleNewBlock, blockNum: %d, blockHash: %s", b.Num, b.Hash)
d.handleNewBlock(ctx, cancel, b)
if ok {
// when channel is closing, it is sending an empty block with num = 0, and empty hash
// because it is not passing object by reference, but by value, so do not handle that since it is closing
d.handleNewBlock(ctx, cancel, b)
}
case firstReorgedBlock := <-d.reorgSub.ReorgedBlock:
d.log.Info("handleReorg from block: ", firstReorgedBlock)
d.handleReorg(ctx, cancel, firstReorgedBlock)
Expand Down

0 comments on commit 9447a00

Please sign in to comment.