Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use std::{
sync::{
Arc,
atomic::{AtomicUsize, Ordering},
},
time::Duration,
};

use alloy::{network::Ethereum, providers::ProviderBuilder, rpc::types::Log, sol};
use alloy_node_bindings::{Anvil, AnvilInstance};
use async_trait::async_trait;
use event_scanner::EventCallback;
use tokio::time::sleep;

// Shared test contract used across integration tests
sol! {
#[allow(missing_docs)]
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220b846b706f79f5ae1fc4a4238319e723a092f47ce4051404186424739164ab02264736f6c634300081e0033")]
contract TestCounter {
uint256 public count;

event CountIncreased(uint256 newCount);
event CountDecreased(uint256 newCount);

function increase() public {
count += 1;
emit CountIncreased(count);
}

function decrease() public {
require(count > 0, "Count cannot be negative");
count -= 1;
emit CountDecreased(count);
}

function getCount() public view returns (uint256) {
return count;
}
}
}

pub struct EventCounter {
pub count: Arc<AtomicUsize>,
}

#[async_trait]
impl EventCallback for EventCounter {
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
self.count.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}

pub struct SlowProcessor {
pub delay_ms: u64,
pub processed: Arc<AtomicUsize>,
}

#[async_trait]
impl EventCallback for SlowProcessor {
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
sleep(Duration::from_millis(self.delay_ms)).await;
self.processed.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}

pub fn spawn_anvil(block_time_secs: u64) -> anyhow::Result<AnvilInstance> {
Ok(Anvil::new().block_time(block_time_secs).try_spawn()?)
}

pub async fn build_provider(
anvil: &AnvilInstance,
) -> anyhow::Result<impl alloy::providers::Provider<Ethereum> + Clone> {
let wallet = anvil.wallet().expect("anvil should return a default wallet");
let provider = ProviderBuilder::new().wallet(wallet).connect(anvil.endpoint().as_str()).await?;
Ok(provider)
}

pub async fn deploy_counter<P>(provider: P) -> anyhow::Result<TestCounter::TestCounterInstance<P>>
where
P: alloy::providers::Provider<Ethereum> + Clone,
{
let contract = TestCounter::deploy(provider).await?;
Ok(contract)
}
95 changes: 18 additions & 77 deletions tests/live_scanning_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,27 @@ use std::{
time::Duration,
};

use alloy::{providers::ProviderBuilder, rpc::types::Log, sol, sol_types::SolEvent};
use alloy_node_bindings::Anvil;
use async_trait::async_trait;
use event_scanner::{EventCallback, EventFilter, ScannerBuilder};
mod common;
use alloy::sol_types::SolEvent;
use common::{TestCounter, build_provider, deploy_counter, spawn_anvil};
use event_scanner::{EventFilter, ScannerBuilder};
use tokio::time::sleep;

sol! {
#[allow(missing_docs)]
#[sol(rpc, bytecode="608080604052346015576101b0908161001a8239f35b5f80fdfe6080806040526004361015610012575f80fd5b5f3560e01c90816306661abd1461016157508063a87d942c14610145578063d732d955146100ad5763e8927fbc14610048575f80fd5b346100a9575f3660031901126100a9575f5460018101809111610095576020817f7ca2ca9527391044455246730762df008a6b47bbdb5d37a890ef78394535c040925f55604051908152a1005b634e487b7160e01b5f52601160045260245ffd5b5f80fd5b346100a9575f3660031901126100a9575f548015610100575f198101908111610095576020817f53a71f16f53e57416424d0d18ccbd98504d42a6f98fe47b09772d8f357c620ce925f55604051908152a1005b60405162461bcd60e51b815260206004820152601860248201527f436f756e742063616e6e6f74206265206e6567617469766500000000000000006044820152606490fd5b346100a9575f3660031901126100a95760205f54604051908152f35b346100a9575f3660031901126100a9576020905f548152f3fea2646970667358221220b846b706f79f5ae1fc4a4238319e723a092f47ce4051404186424739164ab02264736f6c634300081e0033")]
contract LiveTestCounter {
uint256 public count;

event CountIncreased(uint256 newCount);
event CountDecreased(uint256 newCount);

function increase() public {
count += 1;
emit CountIncreased(count);
}

function decrease() public {
require(count > 0, "Count cannot be negative");
count -= 1;
emit CountDecreased(count);
}

function getCount() public view returns (uint256) {
return count;
}
}
}

struct EventCounter {
pub count: Arc<AtomicUsize>,
}

#[async_trait]
impl EventCallback for EventCounter {
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
self.count.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}

struct SlowProcessor {
pub delay_ms: u64,
pub processed: Arc<AtomicUsize>,
}

#[async_trait]
impl EventCallback for SlowProcessor {
async fn on_event(&self, _log: &Log) -> anyhow::Result<()> {
sleep(Duration::from_millis(self.delay_ms)).await;
self.processed.fetch_add(1, Ordering::SeqCst);
Ok(())
}
}
use crate::common::{EventCounter, SlowProcessor};

#[tokio::test]
async fn test_live_scanning_basic() -> anyhow::Result<()> {
let anvil = Anvil::new().block_time(1).try_spawn()?;
let wallet = anvil.wallet();
let provider =
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;

let contract = LiveTestCounter::deploy(provider.clone()).await?;
let anvil = spawn_anvil(1)?;
let provider = build_provider(&anvil).await?;
let contract = deploy_counter(provider.clone()).await?;
let contract_address = *contract.address();

let event_count = Arc::new(AtomicUsize::new(0));
let callback = Arc::new(EventCounter { count: event_count.clone() });

let filter = EventFilter {
contract_address,
event: LiveTestCounter::CountIncreased::SIGNATURE.to_owned(),
event: TestCounter::CountIncreased::SIGNATURE.to_owned(),
callback,
};

Expand All @@ -103,12 +50,9 @@ async fn test_live_scanning_basic() -> anyhow::Result<()> {

#[tokio::test]
async fn test_live_scanning_multiple_events() -> anyhow::Result<()> {
let anvil = Anvil::new().block_time(1).try_spawn()?;
let wallet = anvil.wallet();
let provider =
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;

let contract = LiveTestCounter::deploy(provider.clone()).await?;
let anvil = spawn_anvil(1)?;
let provider = build_provider(&anvil).await?;
let contract = deploy_counter(provider).await?;
let contract_address = *contract.address();

let increase_count = Arc::new(AtomicUsize::new(0));
Expand All @@ -120,13 +64,13 @@ async fn test_live_scanning_multiple_events() -> anyhow::Result<()> {

let increase_filter = EventFilter {
contract_address,
event: LiveTestCounter::CountIncreased::SIGNATURE.to_owned(),
event: TestCounter::CountIncreased::SIGNATURE.to_owned(),
callback: increase_callback,
};

let decrease_filter = EventFilter {
contract_address,
event: LiveTestCounter::CountDecreased::SIGNATURE.to_owned(),
event: TestCounter::CountDecreased::SIGNATURE.to_owned(),
callback: decrease_callback,
};

Expand Down Expand Up @@ -157,20 +101,17 @@ async fn test_live_scanning_multiple_events() -> anyhow::Result<()> {

#[tokio::test]
async fn test_live_scanning_with_slow_processor() -> anyhow::Result<()> {
let anvil = Anvil::new().block_time(1).try_spawn()?;
let wallet = anvil.wallet();
let provider =
ProviderBuilder::new().wallet(wallet.unwrap()).connect(anvil.endpoint().as_str()).await?;

let contract = LiveTestCounter::deploy(provider.clone()).await?;
let anvil = spawn_anvil(1)?;
let provider = build_provider(&anvil).await?;
let contract = deploy_counter(provider.clone()).await?;
let contract_address = *contract.address();

let processed = Arc::new(AtomicUsize::new(0));
let callback = Arc::new(SlowProcessor { delay_ms: 100, processed: processed.clone() });

let filter = EventFilter {
contract_address,
event: LiveTestCounter::CountIncreased::SIGNATURE.to_owned(),
event: TestCounter::CountIncreased::SIGNATURE.to_owned(),
callback,
};

Expand Down