use crate::{
eth_requests::EthRequestHandler,
transactions::{TransactionsManager, TransactionsManagerConfig},
NetworkHandle, NetworkManager,
};
use reth_eth_wire::{EthNetworkPrimitives, NetworkPrimitives};
use reth_network_api::test_utils::PeersHandleProvider;
use reth_transaction_pool::TransactionPool;
use tokio::sync::mpsc;
pub(crate) const ETH_REQUEST_CHANNEL_CAPACITY: usize = 256;
#[allow(missing_debug_implementations)]
pub struct NetworkBuilder<Tx, Eth, N: NetworkPrimitives = EthNetworkPrimitives> {
pub(crate) network: NetworkManager<N>,
pub(crate) transactions: Tx,
pub(crate) request_handler: Eth,
}
impl<Tx, Eth, N: NetworkPrimitives> NetworkBuilder<Tx, Eth, N> {
pub fn split(self) -> (NetworkManager<N>, Tx, Eth) {
let Self { network, transactions, request_handler } = self;
(network, transactions, request_handler)
}
pub const fn network(&self) -> &NetworkManager<N> {
&self.network
}
pub fn network_mut(&mut self) -> &mut NetworkManager<N> {
&mut self.network
}
pub fn handle(&self) -> NetworkHandle<N> {
self.network.handle().clone()
}
pub fn split_with_handle(self) -> (NetworkHandle<N>, NetworkManager<N>, Tx, Eth) {
let Self { network, transactions, request_handler } = self;
let handle = network.handle().clone();
(handle, network, transactions, request_handler)
}
pub fn request_handler<Client>(
self,
client: Client,
) -> NetworkBuilder<Tx, EthRequestHandler<Client, N>, N> {
let Self { mut network, transactions, .. } = self;
let (tx, rx) = mpsc::channel(ETH_REQUEST_CHANNEL_CAPACITY);
network.set_eth_request_handler(tx);
let peers = network.handle().peers_handle().clone();
let request_handler = EthRequestHandler::new(client, peers, rx);
NetworkBuilder { network, request_handler, transactions }
}
pub fn transactions<Pool: TransactionPool>(
self,
pool: Pool,
transactions_manager_config: TransactionsManagerConfig,
) -> NetworkBuilder<TransactionsManager<Pool, N>, Eth, N> {
let Self { mut network, request_handler, .. } = self;
let (tx, rx) = mpsc::unbounded_channel();
network.set_transactions(tx);
let handle = network.handle().clone();
let transactions = TransactionsManager::new(handle, pool, rx, transactions_manager_config);
NetworkBuilder { network, request_handler, transactions }
}
}