reth_network/
builder.rs

1//! Builder support for configuring the entire setup.
2
3use crate::{
4    eth_requests::EthRequestHandler,
5    transactions::{
6        config::{
7            AnnouncementFilteringPolicy, StrictEthAnnouncementFilter, TransactionPropagationKind,
8        },
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<TransactionsManager<Pool, N>, Eth, N> {
81        self.transactions_with_policy(
82            pool,
83            transactions_manager_config,
84            TransactionPropagationKind::default(),
85        )
86    }
87
88    /// Creates a new [`TransactionsManager`] and wires it to the network.
89    ///
90    /// Uses the default [`StrictEthAnnouncementFilter`] for announcement filtering.
91    pub fn transactions_with_policy<Pool: TransactionPool, P: TransactionPropagationPolicy<N>>(
92        self,
93        pool: Pool,
94        transactions_manager_config: TransactionsManagerConfig,
95        propagation_policy: P,
96    ) -> NetworkBuilder<TransactionsManager<Pool, N>, Eth, N> {
97        self.transactions_with_policies(
98            pool,
99            transactions_manager_config,
100            propagation_policy,
101            StrictEthAnnouncementFilter::default(),
102        )
103    }
104
105    /// Creates a new [`TransactionsManager`] with custom propagation and announcement policies.
106    ///
107    /// This allows chains with custom transaction types (like CATX) to configure
108    /// the announcement filter to accept their transaction types.
109    pub fn transactions_with_policies<
110        Pool: TransactionPool,
111        P: TransactionPropagationPolicy<N>,
112        A: AnnouncementFilteringPolicy<N>,
113    >(
114        self,
115        pool: Pool,
116        transactions_manager_config: TransactionsManagerConfig,
117        propagation_policy: P,
118        announcement_policy: A,
119    ) -> NetworkBuilder<TransactionsManager<Pool, N>, Eth, N> {
120        let Self { mut network, request_handler, .. } = self;
121        let (tx, rx) = mpsc::unbounded_channel();
122        network.set_transactions(tx);
123        let handle = network.handle().clone();
124        let policies = NetworkPolicies::new(propagation_policy, announcement_policy);
125
126        let transactions = TransactionsManager::with_policy(
127            handle,
128            pool,
129            rx,
130            transactions_manager_config,
131            policies,
132        );
133        NetworkBuilder { network, request_handler, transactions }
134    }
135}