Skip to content

Commit 26ee1ca

Browse files
authored
Rename BlockScanner to a More Accurate BlockRangeScanner (#36)
1 parent 2be6742 commit 26ee1ca

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Event Scanner is a Rust library for monitoring EVM-based smart contract events.
4141
The library exposes two primary layers:
4242

4343
- `EventScannerBuilder` / `EventScanner` – the main module the application will interact with.
44-
- `BlockScanner` – lower-level component that streams block ranges, handles reorg, batching, and provider subscriptions. This is exposed to the user but has many edge cases which will be documented in the future. For now interact with this via the `EventScanner`
44+
- `BlockRangeScanner` – lower-level component that streams block ranges, handles reorg, batching, and provider subscriptions.
4545

4646
Callbacks implement the `EventCallback` trait. They are executed through a `CallbackStrategy` that performs retries when necessary before reporting failures.
4747

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
//!
33
//! ```rust,no_run
44
//! use alloy::{eips::BlockNumberOrTag, network::Ethereum, primitives::BlockNumber};
5-
//! use event_scanner::block_scanner::BlockScannerError;
5+
//! use event_scanner::block_range_scanner::BlockScannerError;
66
//! use std::ops::Range;
77
//! use tokio_stream::{StreamExt, wrappers::ReceiverStream};
88
//!
99
//! use alloy::transports::http::reqwest::Url;
10-
//! use event_scanner::block_scanner::{BlockScanner, BlockScannerClient};
10+
//! use event_scanner::block_range_scanner::{BlockRangeScanner, BlockScannerClient};
1111
//! use tokio::time::Duration;
1212
//! use tracing::{error, info};
1313
//!
@@ -17,7 +17,7 @@
1717
//! tracing_subscriber::fmt::init();
1818
//!
1919
//! // Configuration
20-
//! let block_scanner = BlockScanner::new()
20+
//! let block_range_scanner = BlockRangeScanner::new()
2121
//! .with_blocks_read_per_epoch(1000)
2222
//! .with_reorg_rewind_depth(5)
2323
//! .with_retry_interval(Duration::from_secs(12))
@@ -26,7 +26,7 @@
2626
//! .await?;
2727
//!
2828
//! // Create client to send subscribe command to block scanner
29-
//! let subscription_client: BlockScannerClient = block_scanner.run()?;
29+
//! let subscription_client: BlockScannerClient = block_range_scanner.run()?;
3030
//!
3131
//! let mut receiver: ReceiverStream<Result<Range<BlockNumber>, BlockScannerError>> =
3232
//! subscription_client
@@ -187,20 +187,20 @@ struct Config {
187187
block_confirmations: u64,
188188
}
189189

190-
pub struct BlockScanner {
190+
pub struct BlockRangeScanner {
191191
blocks_read_per_epoch: usize,
192192
reorg_rewind_depth: u64,
193193
retry_interval: Duration,
194194
block_confirmations: u64,
195195
}
196196

197-
impl Default for BlockScanner {
197+
impl Default for BlockRangeScanner {
198198
fn default() -> Self {
199199
Self::new()
200200
}
201201
}
202202

203-
impl BlockScanner {
203+
impl BlockRangeScanner {
204204
#[must_use]
205205
pub fn new() -> Self {
206206
Self {
@@ -778,7 +778,7 @@ mod tests {
778778
async fn live_mode_processes_all_blocks() -> anyhow::Result<()> {
779779
let anvil = Anvil::new().block_time_f64(0.01).try_spawn()?;
780780

781-
let sub_client = BlockScanner::new()
781+
let client = BlockRangeScanner::new()
782782
.with_blocks_read_per_epoch(3)
783783
.with_reorg_rewind_depth(5)
784784
.with_retry_interval(Duration::from_secs(1))
@@ -790,7 +790,7 @@ mod tests {
790790
let expected_blocks = 10;
791791

792792
let mut receiver =
793-
sub_client.subscribe(BlockNumberOrTag::Latest, None).await?.take(expected_blocks);
793+
client.subscribe(BlockNumberOrTag::Latest, None).await?.take(expected_blocks);
794794

795795
let mut block_range_start = 0;
796796

src/event_scanner.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, sync::Arc, time::Duration};
22

33
use crate::{
4-
block_scanner::{BlockScanner, BlockScannerError, ConnectedBlockScanner},
4+
block_range_scanner::{BlockRangeScanner, BlockScannerError, ConnectedBlockScanner},
55
callback::strategy::{CallbackStrategy, StateSyncAwareStrategy},
66
types::EventFilter,
77
};
@@ -18,7 +18,7 @@ use tokio_stream::StreamExt;
1818
use tracing::{error, info, warn};
1919

2020
pub struct EventScannerBuilder {
21-
block_scanner: BlockScanner,
21+
block_range_scanner: BlockRangeScanner,
2222
tracked_events: Vec<EventFilter>,
2323
callback_strategy: Arc<dyn CallbackStrategy>,
2424
}
@@ -34,7 +34,7 @@ impl EventScannerBuilder {
3434
/// Creates a new builder with default block scanner and callback strategy.
3535
pub fn new() -> Self {
3636
Self {
37-
block_scanner: BlockScanner::new(),
37+
block_range_scanner: BlockRangeScanner::new(),
3838
tracked_events: Vec::new(),
3939
callback_strategy: Self::get_default_callback_strategy(),
4040
}
@@ -64,28 +64,31 @@ impl EventScannerBuilder {
6464
/// Configures how many blocks are read per epoch during a historical sync.
6565
#[must_use]
6666
pub fn with_blocks_read_per_epoch(mut self, blocks_read_per_epoch: usize) -> Self {
67-
self.block_scanner = self.block_scanner.with_blocks_read_per_epoch(blocks_read_per_epoch);
67+
self.block_range_scanner =
68+
self.block_range_scanner.with_blocks_read_per_epoch(blocks_read_per_epoch);
6869
self
6970
}
7071

7172
/// Sets the depth to rewind when a reorg is detected.
7273
#[must_use]
7374
pub fn with_reorg_rewind_depth(mut self, reorg_rewind_depth: u64) -> Self {
74-
self.block_scanner = self.block_scanner.with_reorg_rewind_depth(reorg_rewind_depth);
75+
self.block_range_scanner =
76+
self.block_range_scanner.with_reorg_rewind_depth(reorg_rewind_depth);
7577
self
7678
}
7779

7880
/// Adjusts the retry interval when reconnecting to the provider.
7981
#[must_use]
8082
pub fn with_retry_interval(mut self, retry_interval: Duration) -> Self {
81-
self.block_scanner = self.block_scanner.with_retry_interval(retry_interval);
83+
self.block_range_scanner = self.block_range_scanner.with_retry_interval(retry_interval);
8284
self
8385
}
8486

8587
/// Configures how many confirmations are required before processing a block (used for reorgs).
8688
#[must_use]
8789
pub fn with_block_confirmations(mut self, block_confirmations: u64) -> Self {
88-
self.block_scanner = self.block_scanner.with_block_confirmations(block_confirmations);
90+
self.block_range_scanner =
91+
self.block_range_scanner.with_block_confirmations(block_confirmations);
8992
self
9093
}
9194

@@ -98,9 +101,9 @@ impl EventScannerBuilder {
98101
self,
99102
ws_url: Url,
100103
) -> Result<EventScanner<N>, BlockScannerError> {
101-
let block_scanner = self.block_scanner.connect_ws(ws_url).await?;
104+
let block_range_scanner = self.block_range_scanner.connect_ws(ws_url).await?;
102105
Ok(EventScanner {
103-
block_scanner,
106+
block_range_scanner,
104107
tracked_events: self.tracked_events,
105108
callback_strategy: self.callback_strategy,
106109
})
@@ -115,9 +118,9 @@ impl EventScannerBuilder {
115118
self,
116119
ipc_path: impl Into<String>,
117120
) -> Result<EventScanner<N>, BlockScannerError> {
118-
let block_scanner = self.block_scanner.connect_ipc(ipc_path.into()).await?;
121+
let block_range_scanner = self.block_range_scanner.connect_ipc(ipc_path.into()).await?;
119122
Ok(EventScanner {
120-
block_scanner,
123+
block_range_scanner,
121124
tracked_events: self.tracked_events,
122125
callback_strategy: self.callback_strategy,
123126
})
@@ -131,7 +134,7 @@ impl EventScannerBuilder {
131134
}
132135

133136
pub struct EventScanner<N: Network> {
134-
block_scanner: ConnectedBlockScanner<N>,
137+
block_range_scanner: ConnectedBlockScanner<N>,
135138
tracked_events: Vec<EventFilter>,
136139
callback_strategy: Arc<dyn CallbackStrategy>,
137140
}
@@ -180,7 +183,7 @@ impl<N: Network> EventScanner<N> {
180183
event_channels.insert(unique_event, sender);
181184
}
182185

183-
let client = self.block_scanner.run()?;
186+
let client = self.block_range_scanner.run()?;
184187
let mut stream = client.subscribe(start_height, end_height).await?;
185188

186189
while let Some(range) = stream.next().await {
@@ -235,7 +238,7 @@ impl<N: Network> EventScanner<N> {
235238
.from_block(from_block)
236239
.to_block(to_block);
237240

238-
match self.block_scanner.provider().get_logs(&filter).await {
241+
match self.block_range_scanner.provider().get_logs(&filter).await {
239242
Ok(logs) => {
240243
if logs.is_empty() {
241244
continue;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pub mod block_scanner;
1+
pub mod block_range_scanner;
22
pub mod callback;
33
pub mod event_scanner;
44
pub mod types;

0 commit comments

Comments
 (0)