reth_network/
builder.rs

1//! Builder support for configuring the entire setup.
2
3use std::fmt::Debug;
4
5use crate::{
6    eth_requests::EthRequestHandler,
7    transactions::{
8        config::{StrictEthAnnouncementFilter, TransactionPropagationKind},
9        policy::NetworkPolicies,
10        TransactionPropagationPolicy, TransactionsManager, TransactionsManagerConfig,
11    },
12    NetworkHandle, NetworkManager,
13};
14use reth_eth_wire::{EthNetworkPrimitives, NetworkPrimitives};
15use reth_network_api::test_utils::PeersHandleProvider;
16use reth_transaction_pool::TransactionPool;
17use tokio::sync::mpsc;
18
19/// We set the max channel capacity of the `EthRequestHandler` to 256
20/// 256 requests with malicious 10MB body requests is 2.6GB which can be absorbed by the node.
21pub(crate) const ETH_REQUEST_CHANNEL_CAPACITY: usize = 256;
22
23/// A builder that can configure all components of the network.
24#[expect(missing_debug_implementations)]
25pub struct NetworkBuilder<Tx, Eth, N: NetworkPrimitives = EthNetworkPrimitives> {
26    pub(crate) network: NetworkManager<N>,
27    pub(crate) transactions: Tx,
28    pub(crate) request_handler: Eth,
29}
30
31// === impl NetworkBuilder ===
32
33impl<Tx, Eth, N: NetworkPrimitives> NetworkBuilder<Tx, Eth, N> {
34    /// Consumes the type and returns all fields.
35    pub fn split(self) -> (NetworkManager<N>, Tx, Eth) {
36        let Self { network, transactions, request_handler } = self;
37        (network, transactions, request_handler)
38    }
39
40    /// Returns the network manager.
41    pub const fn network(&self) -> &NetworkManager<N> {
42        &self.network
43    }
44
45    /// Returns the mutable network manager.
46    pub const fn network_mut(&mut self) -> &mut NetworkManager<N> {
47        &mut self.network
48    }
49
50    /// Returns the handle to the network.
51    pub fn handle(&self) -> NetworkHandle<N> {
52        self.network.handle().clone()
53    }
54
55    /// Consumes the type and returns all fields and also return a [`NetworkHandle`].
56    pub fn split_with_handle(self) -> (NetworkHandle<N>, NetworkManager<N>, Tx, Eth) {
57        let Self { network, transactions, request_handler } = self;
58        let handle = network.handle().clone();
59        (handle, network, transactions, request_handler)
60    }
61
62    /// Creates a new [`EthRequestHandler`] and wires it to the network.
63    pub fn request_handler<Client>(
64        self,
65        client: Client,
66    ) -> NetworkBuilder<Tx, EthRequestHandler<Client, N>, N> {
67        let Self { mut network, transactions, .. } = self;
68        let (tx, rx) = mpsc::channel(ETH_REQUEST_CHANNEL_CAPACITY);
69        network.set_eth_request_handler(tx);
70        let peers = network.handle().peers_handle().clone();
71        let request_handler = EthRequestHandler::new(client, peers, rx);
72        NetworkBuilder { network, request_handler, transactions }
73    }
74
75    /// Creates a new [`TransactionsManager`] and wires it to the network.
76    pub fn transactions<Pool: TransactionPool>(
77        self,
78        pool: Pool,
79        transactions_manager_config: TransactionsManagerConfig,
80    ) -> NetworkBuilder<
81        TransactionsManager<
82            Pool,
83            N,
84            NetworkPolicies<TransactionPropagationKind, StrictEthAnnouncementFilter>,
85        >,
86        Eth,
87        N,
88    > {
89        self.transactions_with_policy(
90            pool,
91            transactions_manager_config,
92            TransactionPropagationKind::default(),
93        )
94    }
95
96    /// Creates a new [`TransactionsManager`] and wires it to the network.
97    pub fn transactions_with_policy<
98        Pool: TransactionPool,
99        P: TransactionPropagationPolicy + Debug,
100    >(
101        self,
102        pool: Pool,
103        transactions_manager_config: TransactionsManagerConfig,
104        propagation_policy: P,
105    ) -> NetworkBuilder<
106        TransactionsManager<Pool, N, NetworkPolicies<P, StrictEthAnnouncementFilter>>,
107        Eth,
108        N,
109    > {
110        let Self { mut network, request_handler, .. } = self;
111        let (tx, rx) = mpsc::unbounded_channel();
112        network.set_transactions(tx);
113        let handle = network.handle().clone();
114        let announcement_policy = StrictEthAnnouncementFilter::default();
115        let policies = NetworkPolicies::new(propagation_policy, announcement_policy);
116
117        let transactions = TransactionsManager::with_policy(
118            handle,
119            pool,
120            rx,
121            transactions_manager_config,
122            policies,
123        );
124        NetworkBuilder { network, request_handler, transactions }
125    }
126}