-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmod.rs
More file actions
566 lines (524 loc) · 21.6 KB
/
Copy pathmod.rs
File metadata and controls
566 lines (524 loc) · 21.6 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
use alloy::{
eips::{BlockId, BlockNumberOrTag},
network::{Ethereum, Network},
};
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use crate::{
EventFilter, ScannerError,
block_range_scanner::{
BlockRangeScanner, ConnectedBlockRangeScanner, DEFAULT_BLOCK_CONFIRMATIONS,
MAX_BUFFERED_MESSAGES, RingBufferCapacity,
},
event_scanner::{EventScannerResult, listener::EventListener},
robust_provider::IntoRobustProvider,
};
mod common;
mod historic;
mod latest;
mod live;
mod sync;
/// Default number of maximum concurrent fetches for each scanner mode.
pub const DEFAULT_MAX_CONCURRENT_FETCHES: usize = 24;
#[derive(Default)]
pub struct Unspecified;
pub struct Historic {
pub(crate) from_block: BlockId,
pub(crate) to_block: BlockId,
/// Controls how many log-fetching RPC requests can run in parallel during the scan.
pub(crate) max_concurrent_fetches: usize,
}
pub struct Live {
pub(crate) block_confirmations: u64,
/// Controls how many log-fetching RPC requests can run in parallel during the scan.
pub(crate) max_concurrent_fetches: usize,
}
pub struct LatestEvents {
pub(crate) count: usize,
pub(crate) from_block: BlockId,
pub(crate) to_block: BlockId,
/// Controls how many log-fetching RPC requests can run in parallel during the scan.
pub(crate) max_concurrent_fetches: usize,
}
#[derive(Default)]
pub struct Synchronize;
pub struct SyncFromLatestEvents {
pub(crate) count: usize,
pub(crate) block_confirmations: u64,
/// Controls how many log-fetching RPC requests can run in parallel during the scan.
pub(crate) max_concurrent_fetches: usize,
}
pub struct SyncFromBlock {
pub(crate) from_block: BlockId,
pub(crate) block_confirmations: u64,
/// Controls how many log-fetching RPC requests can run in parallel during the scan.
pub(crate) max_concurrent_fetches: usize,
}
impl Default for Historic {
fn default() -> Self {
Self {
from_block: BlockNumberOrTag::Earliest.into(),
to_block: BlockNumberOrTag::Latest.into(),
max_concurrent_fetches: DEFAULT_MAX_CONCURRENT_FETCHES,
}
}
}
impl Default for Live {
fn default() -> Self {
Self {
block_confirmations: DEFAULT_BLOCK_CONFIRMATIONS,
max_concurrent_fetches: DEFAULT_MAX_CONCURRENT_FETCHES,
}
}
}
pub struct EventScanner<M = Unspecified, N: Network = Ethereum> {
config: M,
block_range_scanner: ConnectedBlockRangeScanner<N>,
listeners: Vec<EventListener>,
}
#[derive(Default)]
pub struct EventScannerBuilder<M> {
pub(crate) config: M,
pub(crate) block_range_scanner: BlockRangeScanner,
}
impl EventScannerBuilder<Unspecified> {
/// Streams events from a historical block range.
///
/// # Example
///
/// ```no_run
/// # use alloy::{network::Ethereum, providers::{Provider, ProviderBuilder}};
/// # use event_scanner::{EventFilter, EventScannerBuilder, Message, robust_provider::RobustProviderBuilder};
/// # use tokio_stream::StreamExt;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let contract_address = alloy::primitives::address!("0xd8dA6BF26964af9d7eed9e03e53415d37aa96045");
/// // Stream all events from genesis to latest block
/// let provider = ProviderBuilder::new().connect("ws://localhost:8545").await?;
/// let robust_provider = RobustProviderBuilder::new(provider).build().await?;
/// let mut scanner = EventScannerBuilder::historic().connect(robust_provider).await?;
///
/// let filter = EventFilter::new().contract_address(contract_address);
/// let mut stream = scanner.subscribe(filter);
///
/// scanner.start().await?;
///
/// while let Some(Ok(Message::Data(logs))) = stream.next().await {
/// println!("Received {} logs", logs.len());
/// }
/// # Ok(())
/// # }
/// ```
///
/// Specifying a custom block range:
///
/// ```no_run
/// # use alloy::{network::Ethereum, providers::{Provider, ProviderBuilder}};
/// # use event_scanner::{EventScannerBuilder, robust_provider::RobustProviderBuilder};
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Stream events between blocks [1_000_000, 2_000_000]
/// let provider = ProviderBuilder::new().connect("ws://localhost:8545").await?;
/// let robust_provider = RobustProviderBuilder::new(provider).build().await?;
/// let mut scanner = EventScannerBuilder::historic()
/// .from_block(1_000_000)
/// .to_block(2_000_000)
/// .connect(robust_provider)
/// .await?;
/// # Ok(())
/// # }
/// ```
///
/// # How it works
///
/// The scanner streams events in chronological order (oldest to newest) within the specified
/// block range. Events are delivered in batches as they are fetched from the provider, with
/// batch sizes controlled by the [`max_block_range`][max_block_range] configuration.
///
/// # Key behaviors
///
/// * **Continuous streaming**: Events are delivered in multiple messages as they are fetched
/// * **Chronological order**: Events are always delivered oldest to newest
/// * **Concurrent log fetching**: Logs are fetched concurrently to reduce the execution time.
/// The maximum number of concurrent RPC calls is controlled by
/// [`max_concurrent_fetches`][max_concurrent_fetches]
/// * **Default range**: By default, scans from `Earliest` to `Latest` block
/// * **Batch control**: Use [`max_block_range`][max_block_range] to control how many blocks are
/// queried per RPC call
/// * **Reorg handling**: Performs reorg checks when streaming events from non-finalized blocks;
/// if a reorg is detected, streams events from the reorged blocks
/// * **Completion**: The scanner completes when the entire range has been processed.
///
/// [max_block_range]: crate::EventScannerBuilder::max_block_range
/// [max_concurrent_fetches]: crate::EventScannerBuilder::max_concurrent_fetches
#[must_use]
pub fn historic() -> EventScannerBuilder<Historic> {
EventScannerBuilder::default()
}
/// Streams new events as blocks are produced on-chain.
///
/// # Example
///
/// ```no_run
/// # use alloy::{network::Ethereum, providers::{Provider, ProviderBuilder}};
/// # use event_scanner::{EventFilter, EventScannerBuilder, Message, robust_provider::RobustProviderBuilder};
/// # use tokio_stream::StreamExt;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let contract_address = alloy::primitives::address!("0xd8dA6BF26964af9d7eed9e03e53415d37aa96045");
/// // Stream new events as they arrive
/// let provider = ProviderBuilder::new().connect("ws://localhost:8545").await?;
/// let robust_provider = RobustProviderBuilder::new(provider).build().await?;
/// let mut scanner = EventScannerBuilder::live()
/// .block_confirmations(20)
/// .connect(robust_provider)
/// .await?;
///
/// let filter = EventFilter::new().contract_address(contract_address);
/// let mut stream = scanner.subscribe(filter);
///
/// scanner.start().await?;
///
/// while let Some(msg) = stream.next().await {
/// match msg {
/// Ok(Message::Data(logs)) => {
/// println!("Received {} new events", logs.len());
/// }
/// Ok(Message::Notification(notification)) => {
/// println!("Notification received: {:?}", notification);
/// }
/// Err(e) => {
/// eprintln!("Error: {}", e);
/// }
/// }
/// }
/// # Ok(())
/// # }
/// ```
///
/// # How it works
///
/// The scanner subscribes to new blocks via WebSocket and streams events from confirmed
/// blocks. The `block_confirmations` setting determines how many blocks to wait before
/// considering a block confirmed, providing protection against chain reorganizations.
///
/// # Key behaviors
///
/// * **Real-time streaming**: Events are delivered as new blocks are confirmed
/// * **Reorg protection**: Waits for configured confirmations before emitting events
/// * **Continuous operation**: Runs indefinitely until the scanner is dropped or encounters an
/// error
/// * **Default confirmations**: By default, waits for 12 block confirmations
///
/// # Reorg behavior
///
/// When a reorg is detected:
/// 1. Emits [`Notification::ReorgDetected`][reorg] to all listeners
/// 2. Adjusts the next confirmed range using `block_confirmations`
/// 3. Re-emits events from the corrected confirmed block range
/// 4. Continues streaming from the new chain state
///
/// [reorg]: crate::types::Notification::ReorgDetected
#[must_use]
pub fn live() -> EventScannerBuilder<Live> {
EventScannerBuilder::default()
}
/// Creates a builder for sync mode scanners that combine historical catch-up with live
/// streaming.
///
/// This method returns a builder that must be further narrowed down:
/// ```rust,no_run
/// # use event_scanner::EventScannerBuilder;
/// // Sync from block mode
/// EventScannerBuilder::sync().from_block(1_000_000);
/// // Sync from latest events mode
/// EventScannerBuilder::sync().from_latest(10);
/// ```
///
/// See [`from_block`](crate::EventScannerBuilder#method.from_block-2) and
/// [`from_latest`](crate::EventScannerBuilder#method.from_latest) for details on each mode.
#[must_use]
pub fn sync() -> EventScannerBuilder<Synchronize> {
EventScannerBuilder::default()
}
/// Streams the latest `count` matching events per registered listener.
///
/// # Example
///
/// ```no_run
/// # use alloy::{network::Ethereum, providers::{Provider, ProviderBuilder}};
/// # use event_scanner::{EventFilter, EventScannerBuilder, Message, robust_provider::RobustProviderBuilder};
/// # use tokio_stream::StreamExt;
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// # let contract_address = alloy::primitives::address!("0xd8dA6BF26964af9d7eed9e03e53415d37aa96045");
/// // Collect the latest 10 events across Earliest..=Latest
/// let provider = ProviderBuilder::new().connect("ws://localhost:8545").await?;
/// let robust_provider = RobustProviderBuilder::new(provider).build().await?;
/// let mut scanner = EventScannerBuilder::latest(10).connect(robust_provider).await?;
///
/// let filter = EventFilter::new().contract_address(contract_address);
/// let mut stream = scanner.subscribe(filter);
///
/// scanner.start().await?;
///
/// // Expect a single message with up to 10 logs, then the stream ends
/// while let Some(Ok(Message::Data(logs))) = stream.next().await {
/// println!("Latest logs: {}", logs.len());
/// }
/// # Ok(())
/// # }
/// ```
///
/// Restricting to a specific block range:
///
/// ```no_run
/// # use alloy::{network::Ethereum, providers::{Provider, ProviderBuilder}};
/// # use event_scanner::{EventScannerBuilder, robust_provider::RobustProviderBuilder};
/// #
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Collect the latest 5 events between blocks [1_000_000, 1_100_000]
/// let provider = ProviderBuilder::new().connect("ws://localhost:8545").await?;
/// let robust_provider = RobustProviderBuilder::new(provider).build().await?;
/// let mut scanner = EventScannerBuilder::latest(5)
/// .from_block(1_000_000)
/// .to_block(1_100_000)
/// .connect(robust_provider);
/// # Ok(())
/// # }
/// ```
///
/// # How it works
///
/// The scanner performs a reverse-ordered scan (newest to oldest) within the specified block
/// range, collecting up to `count` events per registered listener. Once the target count is
/// reached or the range is exhausted, it delivers the events in chronological order (oldest to
/// newest) and completes.
///
/// When using a custom block range, the scanner automatically normalizes the range boundaries.
/// This means you can specify `from_block` and `to_block` in any order - the scanner will
/// always scan from the higher block number down to the lower one, regardless of which
/// parameter holds which value.
///
/// # Key behaviors
///
/// * **Single delivery**: Each registered stream receives at most `count` logs in a single
/// message, chronologically ordered
/// * **One-shot operation**: The scanner completes after delivering messages; it does not
/// continue streaming
/// * **Concurrent log fetching**: Logs are fetched concurrently to reduce the execution time.
/// The maximum number of concurrent RPC calls is controlled by
/// [`max_concurrent_fetches`][max_concurrent_fetches]
/// * **Flexible count**: If fewer than `count` events exist in the range, returns all available
/// events
/// * **Default range**: By default, scans from `Earliest` to `Latest` block
/// * **Reorg handling**: Periodically checks the tip to detect reorgs during the scan
///
/// # Notifications
///
/// The scanner can emit the following notifications:
///
/// * [`Notification::NoPastLogsFound`][no_logs]: Emitted when no matching logs are found in the
/// scanned range.
/// * [`Notification::ReorgDetected`][reorg]: Emitted when a reorg is detected during the scan.
///
/// # Arguments
///
/// * `count` - Maximum number of recent events to collect per listener (must be greater than 0)
///
/// # Reorg behavior
///
/// The scanner can detect reorgs during the scan by periodically checking that the range tip
/// has not changed. This is done only when the specified range tip is not a finalized
/// block.
///
/// On reorg detection:
/// 1. Emits [`Notification::ReorgDetected`][reorg] to all listeners
/// 2. Resets to the updated tip
/// 3. Reloads logs from the block range affected by the reorg
/// 4. Continues until `count` events are collected
///
/// Final delivery to log listeners preserves chronological order regardless of reorgs.
///
/// # Notes
///
/// For continuous streaming after collecting latest events, use
/// [`EventScannerBuilder::sync().from_latest(count)`][sync_from_latest] instead
///
/// [subscribe]: EventScanner::subscribe
/// [start]: EventScanner::start
/// [sync_from_latest]: EventScannerBuilder::from_latest
/// [reorg]: crate::Notification::ReorgDetected
/// [no_logs]: crate::Notification::NoPastLogsFound
/// [max_concurrent_fetches]: crate::EventScannerBuilder#method.max_concurrent_fetches-1
#[must_use]
pub fn latest(count: usize) -> EventScannerBuilder<LatestEvents> {
EventScannerBuilder::<LatestEvents>::new(count)
}
}
impl EventScannerBuilder<LatestEvents> {
#[must_use]
pub fn new(count: usize) -> Self {
Self {
config: LatestEvents {
count,
from_block: BlockNumberOrTag::Latest.into(),
to_block: BlockNumberOrTag::Earliest.into(),
max_concurrent_fetches: DEFAULT_MAX_CONCURRENT_FETCHES,
},
block_range_scanner: BlockRangeScanner::default(),
}
}
}
impl EventScannerBuilder<SyncFromLatestEvents> {
#[must_use]
pub fn new(count: usize) -> Self {
Self {
config: SyncFromLatestEvents {
count,
block_confirmations: DEFAULT_BLOCK_CONFIRMATIONS,
max_concurrent_fetches: DEFAULT_MAX_CONCURRENT_FETCHES,
},
block_range_scanner: BlockRangeScanner::default(),
}
}
}
impl EventScannerBuilder<SyncFromBlock> {
#[must_use]
pub fn new(from_block: impl Into<BlockId>) -> Self {
Self {
config: SyncFromBlock {
from_block: from_block.into(),
block_confirmations: DEFAULT_BLOCK_CONFIRMATIONS,
max_concurrent_fetches: DEFAULT_MAX_CONCURRENT_FETCHES,
},
block_range_scanner: BlockRangeScanner::default(),
}
}
}
impl<M> EventScannerBuilder<M> {
/// Sets the maximum block range per event batch.
///
/// Controls how the scanner splits a large block range into smaller batches for processing.
/// Each batch corresponds to a single RPC call to fetch logs. This prevents timeouts and
/// respects rate limits imposed by node providers.
///
/// # Arguments
///
/// * `max_block_range` - Maximum number of blocks to process per batch (must be greater than 0)
///
/// # Example
///
/// If scanning events from blocks 1000–1099 (100 blocks total) with `max_block_range(30)`:
/// * Batch 1: blocks 1000–1029 (30 blocks)
/// * Batch 2: blocks 1030–1059 (30 blocks)
/// * Batch 3: blocks 1060–1089 (30 blocks)
/// * Batch 4: blocks 1090–1099 (10 blocks)
#[must_use]
pub fn max_block_range(mut self, max_block_range: u64) -> Self {
self.block_range_scanner.max_block_range = max_block_range;
self
}
/// Sets how many of past blocks to keep in memory for reorg detection.
///
/// IMPORTANT: If zero, reorg detection is disabled.
///
/// # Arguments
///
/// * `past_blocks_storage_capacity` - Maximum number of blocks to keep in memory.
#[must_use]
pub fn past_blocks_storage_capacity(
mut self,
past_blocks_storage_capacity: RingBufferCapacity,
) -> Self {
self.block_range_scanner.past_blocks_storage_capacity = past_blocks_storage_capacity;
self
}
/// Builds the scanner by connecting to an existing provider.
///
/// This is a shared method used internally by scanner-specific `connect()` methods.
async fn build<N: Network>(
self,
provider: impl IntoRobustProvider<N>,
) -> Result<EventScanner<M, N>, ScannerError> {
if self.block_range_scanner.max_block_range == 0 {
return Err(ScannerError::InvalidMaxBlockRange);
}
let block_range_scanner = self.block_range_scanner.connect::<N>(provider).await?;
Ok(EventScanner { config: self.config, block_range_scanner, listeners: Vec::new() })
}
}
impl<M, N: Network> EventScanner<M, N> {
#[must_use]
pub fn subscribe(&mut self, filter: EventFilter) -> ReceiverStream<EventScannerResult> {
let (sender, receiver) = mpsc::channel::<EventScannerResult>(MAX_BUFFERED_MESSAGES);
self.listeners.push(EventListener { filter, sender });
ReceiverStream::new(receiver)
}
}
#[cfg(test)]
mod tests {
use alloy::{
providers::{RootProvider, mock::Asserter},
rpc::client::RpcClient,
};
use super::*;
#[test]
fn test_historic_scanner_config_defaults() {
let builder = EventScannerBuilder::<Historic>::default();
assert_eq!(builder.config.from_block, BlockNumberOrTag::Earliest.into());
assert_eq!(builder.config.to_block, BlockNumberOrTag::Latest.into());
}
#[test]
fn test_live_scanner_config_defaults() {
let builder = EventScannerBuilder::<Live>::default();
assert_eq!(builder.config.block_confirmations, DEFAULT_BLOCK_CONFIRMATIONS);
}
#[test]
fn test_latest_scanner_config_defaults() {
let builder = EventScannerBuilder::<LatestEvents>::new(10);
assert_eq!(builder.config.count, 10);
assert_eq!(builder.config.from_block, BlockNumberOrTag::Latest.into());
assert_eq!(builder.config.to_block, BlockNumberOrTag::Earliest.into());
}
#[test]
fn sync_scanner_config_defaults() {
let builder = EventScannerBuilder::<SyncFromBlock>::new(BlockNumberOrTag::Earliest);
assert_eq!(builder.config.from_block, BlockNumberOrTag::Earliest.into());
assert_eq!(builder.config.block_confirmations, DEFAULT_BLOCK_CONFIRMATIONS);
}
#[tokio::test]
async fn test_historic_event_stream_listeners_vector_updates() -> anyhow::Result<()> {
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
let mut scanner = EventScannerBuilder::historic().build(provider).await?;
assert!(scanner.listeners.is_empty());
let _stream1 = scanner.subscribe(EventFilter::new());
assert_eq!(scanner.listeners.len(), 1);
let _stream2 = scanner.subscribe(EventFilter::new());
let _stream3 = scanner.subscribe(EventFilter::new());
assert_eq!(scanner.listeners.len(), 3);
Ok(())
}
#[tokio::test]
async fn test_historic_event_stream_channel_capacity() -> anyhow::Result<()> {
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
let mut scanner = EventScannerBuilder::historic().build(provider).await?;
let _ = scanner.subscribe(EventFilter::new());
let sender = &scanner.listeners[0].sender;
assert_eq!(sender.capacity(), MAX_BUFFERED_MESSAGES);
Ok(())
}
#[tokio::test]
async fn test_latest_returns_error_with_zero_count() {
use alloy::{
providers::{RootProvider, mock::Asserter},
rpc::client::RpcClient,
};
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
let result = EventScannerBuilder::latest(0).connect(provider).await;
match result {
Err(ScannerError::InvalidEventCount) => {}
_ => panic!("Expected InvalidEventCount error"),
}
}
}