Skip to content

Commit 8b15c6e

Browse files
committed
ref: add invalid block range error + test
1 parent 14d3e9f commit 8b15c6e

File tree

7 files changed

+106
-26
lines changed

7 files changed

+106
-26
lines changed

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub enum ScannerError {
2727

2828
#[error("Event count must be greater than 0")]
2929
InvalidEventCount,
30+
31+
#[error("Max block range must be greater than 0")]
32+
InvalidMaxBlockRange,
3033
}
3134

3235
impl From<RobustProviderError> for ScannerError {

src/event_scanner/scanner/historic.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ impl EventScannerBuilder<Historic> {
2828
/// # Errors
2929
///
3030
/// Returns an error if:
31-
/// - The provider connection fails
32-
/// - The specified block range exceeds the latest block on the chain
31+
/// * The provider connection fails
32+
/// * The specified block range exceeds the latest block on the chain
33+
/// * The max block range is zero
3334
pub async fn connect<N: Network>(
3435
self,
3536
provider: impl IntoRobustProvider<N>,
@@ -93,7 +94,11 @@ impl<N: Network> EventScanner<Historic, N> {
9394
#[cfg(test)]
9495
mod tests {
9596
use super::*;
96-
use alloy::providers::{Provider, ProviderBuilder};
97+
use alloy::{
98+
network::Ethereum,
99+
providers::{Provider, ProviderBuilder, RootProvider, mock::Asserter},
100+
rpc::client::RpcClient,
101+
};
97102
use alloy_node_bindings::Anvil;
98103

99104
#[test]
@@ -197,4 +202,15 @@ mod tests {
197202
_ => panic!("Expected BlockExceedsLatest error"),
198203
}
199204
}
205+
206+
#[tokio::test]
207+
async fn test_historic_returns_error_with_zero_max_block_range() {
208+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
209+
let result = EventScannerBuilder::historic().max_block_range(0).connect(provider).await;
210+
211+
match result {
212+
Err(ScannerError::InvalidMaxBlockRange) => {}
213+
_ => panic!("Expected InvalidMaxBlockRange error"),
214+
}
215+
}
200216
}

src/event_scanner/scanner/latest.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ impl EventScannerBuilder<LatestEvents> {
3333
/// Returns an error if:
3434
/// * The provider connection fails
3535
/// * The event count is zero
36+
/// * The max block range is zero
3637
pub async fn connect<N: Network>(
3738
self,
3839
provider: impl IntoRobustProvider<N>,
@@ -146,4 +147,15 @@ mod tests {
146147
_ => panic!("Expected InvalidEventCount error"),
147148
}
148149
}
150+
151+
#[tokio::test]
152+
async fn test_latest_returns_error_with_zero_max_block_range() {
153+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
154+
let result = EventScannerBuilder::latest(10).max_block_range(0).connect(provider).await;
155+
156+
match result {
157+
Err(ScannerError::InvalidMaxBlockRange) => {}
158+
_ => panic!("Expected InvalidMaxBlockRange error"),
159+
}
160+
}
149161
}

src/event_scanner/scanner/live.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ impl EventScannerBuilder<Live> {
1818
///
1919
/// # Errors
2020
///
21-
/// Returns an error if the provider connection fails.
21+
/// Returns an error if:
22+
/// * The provider connection fails
23+
/// * The max block range is zero
2224
pub async fn connect<N: Network>(
2325
self,
2426
provider: impl IntoRobustProvider<N>,
@@ -58,6 +60,12 @@ impl<N: Network> EventScanner<Live, N> {
5860

5961
#[cfg(test)]
6062
mod tests {
63+
use alloy::{
64+
network::Ethereum,
65+
providers::{RootProvider, mock::Asserter},
66+
rpc::client::RpcClient,
67+
};
68+
6169
use super::*;
6270

6371
#[test]
@@ -89,4 +97,15 @@ mod tests {
8997
assert_eq!(builder.block_range_scanner.max_block_range, 105);
9098
assert_eq!(builder.config.block_confirmations, 8);
9199
}
100+
101+
#[tokio::test]
102+
async fn test_live_returns_error_with_zero_max_block_range() {
103+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
104+
let result = EventScannerBuilder::live().max_block_range(0).connect(provider).await;
105+
106+
match result {
107+
Err(ScannerError::InvalidMaxBlockRange) => {}
108+
_ => panic!("Expected InvalidMaxBlockRange error"),
109+
}
110+
}
92111
}

src/event_scanner/scanner/mod.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,7 @@ impl<M> EventScannerBuilder<M> {
373373
///
374374
/// # Arguments
375375
///
376-
/// * `max_block_range` - Maximum number of blocks to process per batch.
377-
///
378-
/// # Panics
379-
///
380-
/// If `max_block_range` is zero
376+
/// * `max_block_range` - Maximum number of blocks to process per batch (must be greater than 0)
381377
///
382378
/// # Example
383379
///
@@ -388,7 +384,6 @@ impl<M> EventScannerBuilder<M> {
388384
/// - Batch 4: blocks 1090–1099 (10 blocks)
389385
#[must_use]
390386
pub fn max_block_range(mut self, max_block_range: u64) -> Self {
391-
assert!(max_block_range != 0, "max block range should be greater than zero");
392387
self.block_range_scanner.max_block_range = max_block_range;
393388
self
394389
}
@@ -400,6 +395,9 @@ impl<M> EventScannerBuilder<M> {
400395
self,
401396
provider: impl IntoRobustProvider<N>,
402397
) -> Result<EventScanner<M, N>, ScannerError> {
398+
if self.block_range_scanner.max_block_range == 0 {
399+
return Err(ScannerError::InvalidMaxBlockRange);
400+
}
403401
let block_range_scanner = self.block_range_scanner.connect::<N>(provider).await?;
404402
Ok(EventScanner { config: self.config, block_range_scanner, listeners: Vec::new() })
405403
}
@@ -488,21 +486,17 @@ mod tests {
488486

489487
#[tokio::test]
490488
async fn test_latest_returns_error_with_zero_count() {
491-
use alloy::providers::{RootProvider, mock::Asserter};
492-
use alloy::rpc::client::RpcClient;
493-
489+
use alloy::{
490+
providers::{RootProvider, mock::Asserter},
491+
rpc::client::RpcClient,
492+
};
493+
494494
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
495495
let result = EventScannerBuilder::latest(0).connect(provider).await;
496-
496+
497497
match result {
498498
Err(ScannerError::InvalidEventCount) => {}
499499
_ => panic!("Expected InvalidEventCount error"),
500500
}
501501
}
502-
503-
#[test]
504-
#[should_panic(expected = "max block range should be greater than zero")]
505-
fn test_max_block_range_panics_with_zero() {
506-
let _ = EventScannerBuilder::historic().max_block_range(0);
507-
}
508502
}

src/event_scanner/scanner/sync/from_block.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ impl EventScannerBuilder<SyncFromBlock> {
2020
///
2121
/// # Errors
2222
///
23-
/// Returns an error if the provider connection fails.
23+
/// Returns an error if:
24+
/// * The provider connection fails
25+
/// * The max block range is zero
2426
pub async fn connect<N: Network>(
2527
self,
2628
provider: impl IntoRobustProvider<N>,
@@ -61,7 +63,12 @@ impl<N: Network> EventScanner<SyncFromBlock, N> {
6163

6264
#[cfg(test)]
6365
mod tests {
64-
use alloy::eips::BlockNumberOrTag;
66+
use alloy::{
67+
eips::BlockNumberOrTag,
68+
network::Ethereum,
69+
providers::{RootProvider, mock::Asserter},
70+
rpc::client::RpcClient,
71+
};
6572

6673
use super::*;
6774

@@ -111,4 +118,16 @@ mod tests {
111118
assert!(matches!(builder.config.from_block, BlockNumberOrTag::Number(2)));
112119
assert_eq!(builder.config.block_confirmations, 7);
113120
}
121+
122+
#[tokio::test]
123+
async fn test_sync_from_block_returns_error_with_zero_max_block_range() {
124+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
125+
let result =
126+
EventScannerBuilder::sync().from_block(100).max_block_range(0).connect(provider).await;
127+
128+
match result {
129+
Err(ScannerError::InvalidMaxBlockRange) => {}
130+
_ => panic!("Expected InvalidMaxBlockRange error"),
131+
}
132+
}
114133
}

src/event_scanner/scanner/sync/from_latest.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ impl EventScannerBuilder<SyncFromLatestEvents> {
3333
/// # Errors
3434
///
3535
/// Returns an error if:
36-
/// - The provider connection fails
37-
/// - The event count is zero
36+
/// * The provider connection fails
37+
/// * The event count is zero
38+
/// * The max block range is zero
3839
pub async fn connect<N: Network>(
3940
self,
4041
provider: impl IntoRobustProvider<N>,
@@ -124,18 +125,34 @@ impl<N: Network> EventScanner<SyncFromLatestEvents, N> {
124125

125126
#[cfg(test)]
126127
mod tests {
127-
use alloy::{network::Ethereum, providers::{RootProvider, mock::Asserter}, rpc::client::RpcClient};
128+
use alloy::{
129+
network::Ethereum,
130+
providers::{RootProvider, mock::Asserter},
131+
rpc::client::RpcClient,
132+
};
128133

129134
use super::*;
130135

131136
#[tokio::test]
132137
async fn test_sync_from_latest_returns_error_with_zero_count() {
133138
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
134139
let result = EventScannerBuilder::sync().from_latest(0).connect(provider).await;
135-
140+
136141
match result {
137142
Err(ScannerError::InvalidEventCount) => {}
138143
_ => panic!("Expected InvalidEventCount error"),
139144
}
140145
}
146+
147+
#[tokio::test]
148+
async fn test_sync_from_latest_returns_error_with_zero_max_block_range() {
149+
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
150+
let result =
151+
EventScannerBuilder::sync().from_latest(10).max_block_range(0).connect(provider).await;
152+
153+
match result {
154+
Err(ScannerError::InvalidMaxBlockRange) => {}
155+
_ => panic!("Expected InvalidMaxBlockRange error"),
156+
}
157+
}
141158
}

0 commit comments

Comments
 (0)