use alloy::network::Network;
use crate::{
ScannerError,
event_scanner::{
EventScanner, StartProof,
block_range_handler::{BlockRangeHandler, StreamHandler},
builder::{EventScannerBuilder, Live},
},
};
use robust_provider::IntoRobustProvider;
impl EventScannerBuilder<Live> {
#[must_use]
pub fn block_confirmations(mut self, confirmations: u64) -> Self {
self.config.block_confirmations = confirmations;
self
}
#[must_use]
pub fn max_concurrent_fetches(mut self, max_concurrent_fetches: usize) -> Self {
self.config.max_concurrent_fetches = max_concurrent_fetches;
self
}
pub async fn connect<N: Network>(
self,
provider: impl IntoRobustProvider<N>,
) -> Result<EventScanner<Live, N>, ScannerError> {
if self.config.max_concurrent_fetches == 0 {
return Err(ScannerError::InvalidMaxConcurrentFetches);
}
self.build(provider).await
}
}
impl<N: Network> EventScanner<Live, N> {
pub async fn start(self) -> Result<StartProof, ScannerError> {
info!(
block_confirmations = self.config.block_confirmations,
listener_count = self.listeners.len(),
"Starting EventScanner in Live mode"
);
let stream = self.block_range_scanner.stream_live(self.config.block_confirmations).await?;
let broadcast_channel_capacity = self.buffer_capacity();
let handler = StreamHandler::new(
self.block_range_scanner.provider().clone(),
self.listeners,
self.config.max_concurrent_fetches,
broadcast_channel_capacity,
);
tokio::spawn(async move {
handler.handle(stream).await;
});
Ok(StartProof::new())
}
}
#[cfg(test)]
mod tests {
use alloy::{
network::Ethereum,
node_bindings::Anvil,
providers::{ProviderBuilder, RootProvider, mock::Asserter},
rpc::client::RpcClient,
};
use crate::{
block_range_scanner::{
DEFAULT_BLOCK_CONFIRMATIONS, DEFAULT_MAX_BLOCK_RANGE, DEFAULT_STREAM_BUFFER_CAPACITY,
},
event_scanner::builder::DEFAULT_MAX_CONCURRENT_FETCHES,
};
use super::*;
#[test]
fn test_live_scanner_builder_pattern() {
let builder = EventScannerBuilder::live()
.max_block_range(25)
.block_confirmations(5)
.max_concurrent_fetches(10)
.buffer_capacity(33);
assert_eq!(builder.block_range_scanner.max_block_range, 25);
assert_eq!(builder.config.block_confirmations, 5);
assert_eq!(builder.config.max_concurrent_fetches, 10);
assert_eq!(builder.block_range_scanner.buffer_capacity, 33);
}
#[test]
fn test_historic_scanner_builder_default_values() {
let builder = EventScannerBuilder::live();
assert_eq!(builder.config.block_confirmations, DEFAULT_BLOCK_CONFIRMATIONS);
assert_eq!(builder.config.max_concurrent_fetches, DEFAULT_MAX_CONCURRENT_FETCHES);
assert_eq!(builder.block_range_scanner.max_block_range, DEFAULT_MAX_BLOCK_RANGE);
assert_eq!(builder.block_range_scanner.buffer_capacity, DEFAULT_STREAM_BUFFER_CAPACITY);
}
#[test]
fn test_live_scanner_builder_last_call_wins() {
let builder = EventScannerBuilder::live()
.max_block_range(25)
.max_block_range(55)
.max_block_range(105)
.block_confirmations(2)
.block_confirmations(4)
.block_confirmations(8)
.max_concurrent_fetches(10)
.max_concurrent_fetches(20)
.buffer_capacity(20)
.buffer_capacity(40);
assert_eq!(builder.block_range_scanner.max_block_range, 105);
assert_eq!(builder.config.block_confirmations, 8);
assert_eq!(builder.config.max_concurrent_fetches, 20);
assert_eq!(builder.block_range_scanner.buffer_capacity, 40);
}
#[tokio::test]
async fn accepts_zero_confirmations() -> anyhow::Result<()> {
let anvil = Anvil::new().try_spawn().unwrap();
let provider = ProviderBuilder::new().connect_http(anvil.endpoint_url());
let scanner = EventScannerBuilder::live().block_confirmations(0).connect(provider).await?;
assert_eq!(scanner.config.block_confirmations, 0);
Ok(())
}
#[tokio::test]
async fn test_live_returns_error_with_zero_max_block_range() {
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
let result = EventScannerBuilder::live().max_block_range(0).connect(provider).await;
match result {
Err(ScannerError::InvalidMaxBlockRange) => {}
_ => panic!("Expected InvalidMaxBlockRange error"),
}
}
#[tokio::test]
async fn returns_error_with_zero_max_concurrent_fetches() {
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
let result = EventScannerBuilder::live().max_concurrent_fetches(0).connect(provider).await;
assert!(matches!(result, Err(ScannerError::InvalidMaxConcurrentFetches)));
}
#[tokio::test]
async fn returns_error_with_zero_buffer_capacity() {
let provider = RootProvider::<Ethereum>::new(RpcClient::mocked(Asserter::new()));
let result = EventScannerBuilder::live().buffer_capacity(0).connect(provider).await;
assert!(matches!(result, Err(ScannerError::InvalidBufferCapacity)));
}
}