1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use alloy::network::Network;
use robust_provider::IntoRobustProvider;
use crate::{
ScannerError,
block_range_scanner::{
DEFAULT_MAX_BLOCK_RANGE, DEFAULT_STREAM_BUFFER_CAPACITY, RingBufferCapacity,
scanner::BlockRangeScanner,
},
};
/// Builder/configuration for the block-range streaming service.
#[derive(Clone, Debug)]
pub struct BlockRangeScannerBuilder {
/// Maximum number of blocks per streamed range.
pub max_block_range: u64,
/// How many past block hashes to keep in memory for reorg detection.
///
/// If set to `RingBufferCapacity::Limited(0)`, reorg detection is disabled.
pub past_blocks_storage_capacity: RingBufferCapacity,
pub buffer_capacity: usize,
}
impl Default for BlockRangeScannerBuilder {
fn default() -> Self {
Self::new()
}
}
impl BlockRangeScannerBuilder {
/// Creates a scanner with default configuration.
#[must_use]
pub fn new() -> Self {
Self {
max_block_range: DEFAULT_MAX_BLOCK_RANGE,
past_blocks_storage_capacity: RingBufferCapacity::Limited(10),
buffer_capacity: DEFAULT_STREAM_BUFFER_CAPACITY,
}
}
/// Sets the maximum number of blocks per streamed range.
///
/// This controls batching for historical scans and for catch-up in live/sync scanners.
///
/// Must be greater than 0.
#[must_use]
pub fn max_block_range(mut self, max_block_range: u64) -> Self {
self.max_block_range = max_block_range;
self
}
/// Sets how many past block hashes to keep in memory for reorg detection.
///
/// If set to `RingBufferCapacity::Limited(0)`, reorg detection is disabled.
#[must_use]
pub fn past_blocks_storage_capacity(
mut self,
past_blocks_storage_capacity: RingBufferCapacity,
) -> Self {
self.past_blocks_storage_capacity = past_blocks_storage_capacity;
self
}
/// Sets the stream buffer capacity.
///
/// Controls the maximum number of messages that can be buffered in the stream
/// before backpressure is applied.
///
/// # Arguments
///
/// * `buffer_capacity` - Maximum number of messages to buffer (must be greater than 0)
#[must_use]
pub fn buffer_capacity(mut self, buffer_capacity: usize) -> Self {
self.buffer_capacity = buffer_capacity;
self
}
/// Connects to an existing provider
///
/// # Errors
///
/// Returns an error if the provider connection fails.
pub async fn connect<N: Network>(
self,
provider: impl IntoRobustProvider<N>,
) -> Result<BlockRangeScanner<N>, ScannerError> {
if self.max_block_range == 0 {
return Err(ScannerError::InvalidMaxBlockRange);
}
if self.buffer_capacity == 0 {
return Err(ScannerError::InvalidBufferCapacity);
}
let provider = provider.into_robust_provider().await?;
Ok(BlockRangeScanner::new(
provider,
self.max_block_range,
self.past_blocks_storage_capacity,
self.buffer_capacity,
))
}
}