reth_network/transactions/
config.rs

1use super::{
2    DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER,
3    DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
4    SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
5};
6use crate::transactions::constants::tx_fetcher::{
7    DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH, DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS,
8    DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER,
9};
10use derive_more::Constructor;
11
12/// Configuration for managing transactions within the network.
13#[derive(Debug, Clone)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct TransactionsManagerConfig {
16    /// Configuration for fetching transactions.
17    pub transaction_fetcher_config: TransactionFetcherConfig,
18    /// Max number of seen transactions to store for each peer.
19    pub max_transactions_seen_by_peer_history: u32,
20    /// How new pending transactions are propagated.
21    #[cfg_attr(feature = "serde", serde(default))]
22    pub propagation_mode: TransactionPropagationMode,
23}
24
25impl Default for TransactionsManagerConfig {
26    fn default() -> Self {
27        Self {
28            transaction_fetcher_config: TransactionFetcherConfig::default(),
29            max_transactions_seen_by_peer_history: DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER,
30            propagation_mode: TransactionPropagationMode::default(),
31        }
32    }
33}
34
35/// Determines how new pending transactions are propagated to other peers in full.
36#[derive(Debug, Clone, Default)]
37#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
38pub enum TransactionPropagationMode {
39    /// Send full transactions to sqrt of current peers.
40    #[default]
41    Sqrt,
42    /// Always send transactions in full.
43    All,
44    /// Send full transactions to a maximum number of peers
45    Max(usize),
46}
47
48impl TransactionPropagationMode {
49    /// Returns the number of peers full transactions should be propagated to.
50    pub(crate) fn full_peer_count(&self, peer_count: usize) -> usize {
51        match self {
52            Self::Sqrt => (peer_count as f64).sqrt().round() as usize,
53            Self::All => peer_count,
54            Self::Max(max) => peer_count.min(*max),
55        }
56    }
57}
58
59/// Configuration for fetching transactions.
60#[derive(Debug, Constructor, Clone)]
61#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
62pub struct TransactionFetcherConfig {
63    /// Max inflight [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) requests.
64    pub max_inflight_requests: u32,
65    /// Max inflight [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) requests per
66    /// peer.
67    pub max_inflight_requests_per_peer: u8,
68    /// Soft limit for the byte size of a
69    /// [`PooledTransactions`](reth_eth_wire::PooledTransactions) response on assembling a
70    /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request. Spec'd at 2
71    /// MiB.
72    pub soft_limit_byte_size_pooled_transactions_response: usize,
73    /// Soft limit for the byte size of the expected
74    /// [`PooledTransactions`](reth_eth_wire::PooledTransactions) response on packing a
75    /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request with hashes.
76    pub soft_limit_byte_size_pooled_transactions_response_on_pack_request: usize,
77    /// Max capacity of the cache of transaction hashes, for transactions that weren't yet fetched.
78    /// A transaction is pending fetch if its hash didn't fit into a
79    /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) yet, or it wasn't returned
80    /// upon request to peers.
81    pub max_capacity_cache_txns_pending_fetch: u32,
82}
83
84impl Default for TransactionFetcherConfig {
85    fn default() -> Self {
86        Self {
87            max_inflight_requests: DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS,
88            max_inflight_requests_per_peer: DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER,
89            soft_limit_byte_size_pooled_transactions_response:
90                SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
91            soft_limit_byte_size_pooled_transactions_response_on_pack_request:
92                DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
93                max_capacity_cache_txns_pending_fetch: DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH,
94        }
95    }
96}