Skip to main content

event_scanner/block_range_scanner/
builder.rs

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