Skip to content

Commit fd64cdb

Browse files
committed
fix: check if end reached before fetching next start block
1 parent c24f742 commit fd64cdb

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

src/block_range_scanner.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,8 @@ impl<N: Network> Service<N> {
578578

579579
self.next_start_block = BlockHashAndNumber::from_header::<N>(start.header());
580580

581+
// must be <= to include the edge case when start == end (i.e. return the single block
582+
// range)
581583
while self.next_start_block.number <= end.header().number() {
582584
self.ensure_current_not_reorged().await?;
583585

@@ -592,18 +594,29 @@ impl<N: Network> Service<N> {
592594
))
593595
.await;
594596

595-
let next_start_block_number = (batch_end_block_number + 1).into();
596-
self.next_start_block = self
597-
.provider
598-
.get_block_by_number(next_start_block_number)
599-
.await?
600-
.map(|block| BlockHashAndNumber::from_header::<N>(block.header()))
601-
.ok_or(BlockRangeScannerError::BlockNotFound(next_start_block_number))?;
602-
603597
batch_count += 1;
604598
if batch_count % 10 == 0 {
605599
debug!(batch_count = batch_count, "Processed historical batches");
606600
}
601+
602+
if batch_end_block_number == end.header().number() {
603+
break;
604+
}
605+
606+
let next_start_block_number = (batch_end_block_number + 1).into();
607+
let next_start_block =
608+
match self.provider.get_block_by_number(next_start_block_number).await {
609+
Ok(block) => {
610+
block.expect("block number is less than 'end', so it should exist")
611+
}
612+
Err(e) => {
613+
error!(error = %e, "Failed to get block by number");
614+
let e: BlockRangeScannerError = e.into();
615+
self.send_to_subscriber(BlockRangeMessage::Error(e.clone())).await;
616+
return Err(e);
617+
}
618+
};
619+
self.next_start_block = BlockHashAndNumber::from_header::<N>(next_start_block.header());
607620
}
608621

609622
info!(batch_count = batch_count, "Historical sync completed");

tests/historic_to_live/basic.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use event_scanner::{
1111
};
1212
use tokio::{
1313
sync::Mutex,
14-
time::{Duration, sleep, timeout},
14+
time::{Duration, timeout},
1515
};
1616
use tokio_stream::StreamExt;
1717

@@ -47,8 +47,6 @@ async fn replays_historical_then_switches_to_live() -> anyhow::Result<()> {
4747
client.start_scanner(BlockNumberOrTag::Number(first_historical_block), None).await
4848
});
4949

50-
sleep(Duration::from_millis(200)).await;
51-
5250
for _ in 0..live_events {
5351
contract.increase().send().await?.watch().await?;
5452
}

0 commit comments

Comments
 (0)