Skip to content

Commit

Permalink
fix: e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
goran-ethernal committed Jan 21, 2025
1 parent 3e323f3 commit b232918
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 24 deletions.
2 changes: 2 additions & 0 deletions l1infotreesync/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ func (p *processor) Reorg(ctx context.Context, firstReorgedBlock uint64) error {
p.halted = false
p.haltedReason = ""
}

shouldRollback = false

return nil
}

Expand Down
20 changes: 6 additions & 14 deletions sync/evmdownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func NewEVMDownloader(
func (d *EVMDownloader) Download(ctx context.Context, fromBlock uint64, downloadedCh chan EVMBlock) {
lastBlock := d.WaitForNewBlocks(ctx, 0)
toBlock := fromBlock
automaticToBlockResize := true

for {
select {
Expand All @@ -87,15 +86,11 @@ func (d *EVMDownloader) Download(ctx context.Context, fromBlock uint64, download
default:
}

if automaticToBlockResize {
toBlock = fromBlock + d.syncBlockChunkSize
if toBlock > lastBlock {
toBlock = lastBlock
}
toBlock = fromBlock + d.syncBlockChunkSize
if toBlock > lastBlock {
toBlock = lastBlock
}

automaticToBlockResize = true // reset the flag

if fromBlock > toBlock {
d.log.Debugf(
"waiting for new blocks, last block processed %d, last block seen on L1 %d",
Expand Down Expand Up @@ -140,9 +135,8 @@ func (d *EVMDownloader) Download(ctx context.Context, fromBlock uint64, download
// we have no events, keep increasing the block range until we hit a log
if lastFinalizedBlockNumber > toBlock {
// we might be behind a lot, so go until last finalized block
toBlock = lastFinalizedBlockNumber + 1
} else {
toBlock++
toBlock = lastFinalizedBlockNumber
lastBlock = lastFinalizedBlockNumber
}

if lastFinalizedBlockNumber-fromBlock >= d.syncBlockChunkSize {
Expand All @@ -153,15 +147,13 @@ func (d *EVMDownloader) Download(ctx context.Context, fromBlock uint64, download
fromBlock = lastFinalizedBlockNumber
}

automaticToBlockResize = false

continue
} else if blocks[blocks.Len()-1].Num <= lastFinalizedBlockNumber {
// if the last block we have logs for is less than or equal to the last finalized block,
// report all of the blocks without the need to report the last empty block, since it is finalized
// and we do not need to track it in the reorg detector
reportBlocksFn(blocks.Len())
fromBlock = lastFinalizedBlockNumber + 1
fromBlock = toBlock + 1
} 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
Expand Down
34 changes: 25 additions & 9 deletions sync/evmdownloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,12 @@ func TestDownload(t *testing.T) {
d.On("GetEventsByBlockRange", mock.Anything, uint64(0), uint64(1)).
Return(EVMBlocks{}, false).Once()

// iteration 1: block 2 has events
// iteration 1: we have a new block, so increase to block
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(2)}, nil).Once()
d.On("GetEventsByBlockRange", mock.Anything, uint64(0), uint64(1)).
Return(EVMBlocks{}, false).Once()

// iteration 2: block 2 has events
b2 := EVMBlock{
EVMBlockHeader: EVMBlockHeader{
Num: 2,
Expand All @@ -231,12 +236,12 @@ func TestDownload(t *testing.T) {
d.On("GetEventsByBlockRange", mock.Anything, uint64(0), uint64(2)).
Return(EVMBlocks{b2}, false).Once()

// iteration 2: wait for next block to be created (jump to block 8)
// iteration 3: wait for next block to be created (jump to block 8)
d.On("WaitForNewBlocks", mock.Anything, uint64(2)).
After(time.Millisecond * 100).
Return(uint64(8)).Once()

// iteration 3: blocks 6 and 7 have events, but last finalized block is 5
// iteration 4: blocks 6 and 7 have events, but last finalized block is 5
b6 := EVMBlock{
EVMBlockHeader: EVMBlockHeader{
Num: 6,
Expand Down Expand Up @@ -271,7 +276,7 @@ func TestDownload(t *testing.T) {

// iteration 7: last finalized block is now 20, no events, report empty block
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(20)}, nil).Once()
d.On("GetEventsByBlockRange", mock.Anything, uint64(9), uint64(20)).
d.On("GetEventsByBlockRange", mock.Anything, uint64(9), uint64(19)).
Return(EVMBlocks{}, false)

b20 := EVMBlock{
Expand All @@ -283,16 +288,27 @@ func TestDownload(t *testing.T) {
expectedBlocks = append(expectedBlocks, b20)
d.On("GetBlockHeader", mock.Anything, uint64(20)).Return(b20.EVMBlockHeader, false) // reporting empty finalized block

// iteration 8, 9 and 10: last finalized block is still 20, no events
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(20)}, nil).Times(3)
// iteration 8: last finalized block is 21, no events
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(21)}, nil).Once()
d.On("GetEventsByBlockRange", mock.Anything, uint64(20), uint64(20)).
Return(EVMBlocks{}, false)

// iteration 9: last finalized block is 22, no events
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(22)}, nil).Once()
d.On("GetEventsByBlockRange", mock.Anything, uint64(20), uint64(21)).
Return(EVMBlocks{}, false)

// iteration 10: last finalized block is 23, no events
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(23)}, nil).Once()
d.On("GetEventsByBlockRange", mock.Anything, uint64(20), uint64(22)).
Return(EVMBlocks{}, false)

// iteration 11: last finalized block is still 23, no events
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(23)}, nil).Once()
d.On("GetEventsByBlockRange", mock.Anything, uint64(20), uint64(23)).
Return(EVMBlocks{}, false)

// iteration 11: from block 20 to 24, events on last block
// iteration 12: finalized block is 24, has events
b24 := EVMBlock{
EVMBlockHeader: EVMBlockHeader{
Num: 24,
Expand All @@ -301,11 +317,11 @@ func TestDownload(t *testing.T) {
Events: []interface{}{testEvent(common.HexToHash("24"))},
}
expectedBlocks = append(expectedBlocks, b24)
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(20)}, nil).Once()
d.On("GetLastFinalizedBlock", mock.Anything).Return(&types.Header{Number: big.NewInt(24)}, nil).Twice()
d.On("GetEventsByBlockRange", mock.Anything, uint64(20), uint64(24)).
Return(EVMBlocks{b24}, false)

// iteration 12: closing the downloader
// iteration 13: closing the downloader
d.On("WaitForNewBlocks", mock.Anything, uint64(24)).Return(uint64(25)).After(time.Millisecond * 100).Once()

go dwnldr.Download(ctx1, 0, downloadCh)
Expand Down
2 changes: 1 addition & 1 deletion sync/evmdriver.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ reset:
cancel()
return
case b, ok := <-downloadCh:
d.log.Debugf("handleNewBlock, blockNum: %d, blockHash: %s", b.Num, b.Hash)
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.log.Debugf("handleNewBlock, blockNum: %d, blockHash: %s", b.Num, b.Hash)
d.handleNewBlock(ctx, cancel, b)
}
case firstReorgedBlock := <-d.reorgSub.ReorgedBlock:
Expand Down

0 comments on commit b232918

Please sign in to comment.