mirror of
https://github.com/paradigmxyz/reth.git
synced 2026-01-29 17:18:08 -05:00
feat: add test chain event subscriptions (#1928)
This commit is contained in:
33
crates/interfaces/src/test_utils/events.rs
Normal file
33
crates/interfaces/src/test_utils/events.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use crate::events::{ChainEventSubscriptions, NewBlockNotification, NewBlockNotifications};
|
||||
use async_trait::async_trait;
|
||||
use reth_primitives::{Header, H256};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender};
|
||||
|
||||
/// A test ChainEventSubscriptions
|
||||
#[derive(Clone, Default)]
|
||||
pub struct TestChainEventSubscriptions {
|
||||
new_blocks_txs: Arc<Mutex<Vec<UnboundedSender<NewBlockNotification>>>>,
|
||||
}
|
||||
|
||||
impl TestChainEventSubscriptions {
|
||||
/// Adds new block to the queue that can be consumed with
|
||||
/// [`TestChainEventSubscriptions::subscribe_new_blocks`]
|
||||
pub fn add_new_block(&mut self, hash: H256, header: Header) {
|
||||
let header = Arc::new(header);
|
||||
self.new_blocks_txs
|
||||
.lock()
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.retain(|tx| tx.send(NewBlockNotification { hash, header: header.clone() }).is_ok())
|
||||
}
|
||||
}
|
||||
|
||||
impl ChainEventSubscriptions for TestChainEventSubscriptions {
|
||||
fn subscribe_new_blocks(&self) -> NewBlockNotifications {
|
||||
let (new_blocks_tx, new_blocks_rx) = unbounded_channel();
|
||||
self.new_blocks_txs.lock().as_mut().unwrap().push(new_blocks_tx);
|
||||
|
||||
new_blocks_rx
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
#![allow(unused)]
|
||||
|
||||
mod bodies;
|
||||
mod events;
|
||||
mod headers;
|
||||
|
||||
/// Generators for different data structures like block headers, block bodies and ranges of those.
|
||||
pub mod generators;
|
||||
|
||||
pub use bodies::*;
|
||||
pub use events::*;
|
||||
pub use headers::*;
|
||||
|
||||
Reference in New Issue
Block a user