Skip to main content

reth_network/transactions/
constants.rs

1/* ==================== BROADCAST ==================== */
2
3pub use reth_eth_wire_types::SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE;
4
5/// Default soft limit for the byte size of a [`Transactions`](reth_eth_wire::Transactions)
6/// broadcast message.
7///
8/// Default is 128 KiB.
9pub const DEFAULT_SOFT_LIMIT_BYTE_SIZE_TRANSACTIONS_BROADCAST_MESSAGE: usize = 128 * 1024;
10
11/* ================ REQUEST-RESPONSE ================ */
12
13/// Recommended soft limit for the number of hashes in a
14/// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request.
15///
16/// Spec'd at 256 hashes (8 KiB).
17///
18/// <https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09>
19pub const SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST: usize = 256;
20
21/// Soft limit for the byte size of a [`PooledTransactions`](reth_eth_wire::PooledTransactions)
22/// response on assembling a [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions)
23/// request.
24///
25/// Spec'd at 2 MiB.
26///
27/// <https://github.com/ethereum/devp2p/blob/master/caps/eth.md#protocol-messages>.
28pub const SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE: usize = 2 * 1024 * 1024;
29
30/// Constants used by [`TransactionsManager`](super::TransactionsManager).
31pub mod tx_manager {
32    use super::SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE;
33
34    /// Default limit for number of transactions to keep track of for a single peer.
35    ///
36    /// Default is 320 transaction hashes.
37    pub const DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER: u32 = 10 * 1024 / 32;
38
39    /// Default maximum pending pool imports to tolerate.
40    ///
41    /// Default is equivalent to the number of hashes in one full announcement, which is spec'd at
42    /// 4096 hashes, so 4096 pending pool imports.
43    pub const DEFAULT_MAX_COUNT_PENDING_POOL_IMPORTS: usize =
44        SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE;
45
46    /// Default limit for number of bad imports to keep track of.
47    ///
48    /// Default is 100 KiB, i.e. 3 200 transaction hashes.
49    pub const DEFAULT_MAX_COUNT_BAD_IMPORTS: u32 = 100 * 1024 / 32;
50
51    /// Default memory limit (in bytes) for the channel between
52    /// [`NetworkManager`](crate::NetworkManager) and
53    /// [`TransactionsManager`](crate::transactions::TransactionsManager).
54    ///
55    /// Caps the total in-flight bytes of `NetworkTransactionEvent`s buffered between the two
56    /// tasks. When the budget is exhausted, new events are dropped (see metric
57    /// `total_dropped_tx_events_at_full_capacity`).
58    pub const DEFAULT_TX_MANAGER_CHANNEL_MEMORY_LIMIT_BYTES: usize = 1024 * 1024 * 1024;
59}
60
61/// Constants used by [`TransactionFetcher`](super::TransactionFetcher).
62pub mod tx_fetcher {
63    use reth_network_types::peers::config::{
64        DEFAULT_MAX_COUNT_PEERS_INBOUND, DEFAULT_MAX_COUNT_PEERS_OUTBOUND,
65    };
66
67    use super::{
68        SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
69        SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST,
70        SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE,
71    };
72
73    /* ============== SCALARS OF MESSAGES ============== */
74
75    /// Default soft limit for the byte size of a
76    /// [`PooledTransactions`](reth_eth_wire::PooledTransactions) response on assembling a
77    /// [`GetPooledTransactions`](reth_eth_wire::PooledTransactions) request. This defaults to less
78    /// than the [`SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE`], at 2 MiB, used when
79    /// assembling a [`PooledTransactions`](reth_eth_wire::PooledTransactions) response.
80    ///
81    /// Default is 128 KiB.
82    pub const DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ: usize = 128 * 1024;
83
84    /* ==================== RETRIES ==================== */
85
86    /// Default maximum request retires per [`TxHash`](alloy_primitives::TxHash). Note, this is
87    /// reset should the [`TxHash`](alloy_primitives::TxHash) re-appear in an announcement after it
88    /// has been evicted from the hashes pending fetch cache, i.e. the counter is restarted. If
89    /// this happens, it is likely a very popular transaction, that should and can indeed be
90    /// fetched hence this behaviour is favourable.
91    ///
92    /// Default is 2 retries.
93    pub const DEFAULT_MAX_RETRIES: u8 = 2;
94
95    /// Default number of alternative peers to keep track of for each transaction pending fetch. At
96    /// most [`DEFAULT_MAX_RETRIES`], which defaults to 2 peers, can ever be needed per peer.
97    ///
98    /// Default is the sum of [`DEFAULT_MAX_RETRIES`] an
99    /// [`DEFAULT_MARGINAL_COUNT_FALLBACK_PEERS`], which defaults to 1 peer, so 3 peers.
100    pub const DEFAULT_MAX_COUNT_FALLBACK_PEERS: u8 =
101        DEFAULT_MAX_RETRIES + DEFAULT_MARGINAL_COUNT_FALLBACK_PEERS;
102
103    /// Default marginal on fallback peers. This is the case, since a transaction is only requested
104    /// once from each individual peer.
105    ///
106    /// Default is 1 peer.
107    pub const DEFAULT_MARGINAL_COUNT_FALLBACK_PEERS: u8 = 1;
108
109    /* ==================== CONCURRENCY ==================== */
110
111    /// Default maximum concurrent [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions)
112    /// requests.
113    ///
114    /// Default is the product of [`DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER`], which
115    /// defaults to 1 request, and the sum of [`DEFAULT_MAX_COUNT_PEERS_INBOUND`] and
116    /// [`DEFAULT_MAX_COUNT_PEERS_OUTBOUND`], which default to 30 and 100 peers respectively, so
117    /// 130 requests.
118    pub const DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS: u32 =
119        DEFAULT_MAX_COUNT_PEERS_INBOUND + DEFAULT_MAX_COUNT_PEERS_OUTBOUND;
120
121    /// Default maximum number of concurrent
122    /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions)s to allow per peer. This
123    /// number reflects concurrent requests for different hashes.
124    ///
125    /// Default is 1 request.
126    pub const DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER: u8 = 1;
127
128    /* =============== HASHES PENDING FETCH ================ */
129
130    /// Default limit for number of transactions waiting for an idle peer to be fetched from.
131    ///
132    /// Default is 100 times the [`SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST`],
133    /// which defaults to 256 hashes, so 25 600 hashes.
134    pub const DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH: u32 =
135        100 * SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST as u32;
136
137    /// Default max size for cache of inflight and pending transactions fetch.
138    ///
139    /// Default is [`DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH`] +
140    /// [`DEFAULT_MAX_COUNT_INFLIGHT_REQUESTS_ON_FETCH_PENDING_HASHES`], which is 25600 hashes and
141    /// 65 requests, so it is 25665 hashes.
142    pub const DEFAULT_MAX_CAPACITY_CACHE_INFLIGHT_AND_PENDING_FETCH: u32 =
143        DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH +
144            DEFAULT_MAX_COUNT_INFLIGHT_REQUESTS_ON_FETCH_PENDING_HASHES as u32;
145
146    /// Default maximum number of hashes pending fetch to tolerate at any time.
147    ///
148    /// Default is half of [`DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH`], which defaults to 25 600
149    /// hashes, so 12 800 hashes.
150    pub const DEFAULT_MAX_COUNT_PENDING_FETCH: usize =
151        DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH as usize / 2;
152
153    /* ====== LIMITED CAPACITY ON FETCH PENDING HASHES ====== */
154
155    /// Default budget for finding an idle fallback peer for any hash pending fetch, when said
156    /// search is budget constrained.
157    ///
158    /// Default is a sixth of [`DEFAULT_MAX_COUNT_PENDING_FETCH`], which defaults to 12 800 hashes
159    /// (the ideal max number of hashes pending fetch), divided by
160    /// [`DEFAULT_MAX_COUNT_FALLBACK_PEERS`], which defaults to 3 peers (the depth of the search),
161    /// so a search breadth of 711 lru hashes in the pending hashes cache.
162    pub const DEFAULT_BUDGET_FIND_IDLE_FALLBACK_PEER: usize =
163        DEFAULT_MAX_COUNT_PENDING_FETCH / 6 / DEFAULT_MAX_COUNT_FALLBACK_PEERS as usize;
164
165    /// Default budget for finding hashes in the intersection of transactions announced by a peer
166    /// and in the cache of hashes pending fetch, when said search is budget constrained.
167    ///
168    /// Default is an eight of [`DEFAULT_MAX_COUNT_PENDING_FETCH`], which defaults to 12 800 hashes
169    /// (the ideal max number of hashes pending fetch), so a search breadth of 1 600 lru hashes in
170    /// the pending hashes cache.
171    pub const DEFAULT_BUDGET_FIND_INTERSECTION_ANNOUNCED_BY_PEER_AND_PENDING_FETCH: usize =
172        DEFAULT_MAX_COUNT_PENDING_FETCH / 8;
173
174    /* ====== SCALARS FOR USE ON FETCH PENDING HASHES ====== */
175
176    /// Default soft limit for the number of hashes in a
177    /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request, when it is filled
178    /// from hashes pending fetch.
179    ///
180    /// Default is half of the [`SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST`]
181    /// which by spec is 256 hashes, so 128 hashes.
182    pub const DEFAULT_SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST_ON_FETCH_PENDING_HASHES:
183    usize = SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST / 2;
184
185    /// Default soft limit for a [`PooledTransactions`](reth_eth_wire::PooledTransactions) response
186    /// when it's used as expected response in calibrating the filling of a
187    /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request, when the request
188    /// is filled from hashes pending fetch.
189    ///
190    /// Default is half of
191    /// [`DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ`],
192    /// which defaults to 128 KiB, so 64 KiB.
193    pub const DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_FETCH_PENDING_HASHES:
194        usize =
195        DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ /
196            2;
197
198    /// Default max inflight request when fetching pending hashes.
199    ///
200    /// Default is half of [`DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS`], which defaults to 130
201    /// requests, so 65 requests.
202    pub const DEFAULT_MAX_COUNT_INFLIGHT_REQUESTS_ON_FETCH_PENDING_HASHES: usize =
203        DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS as usize / 2;
204
205    /// Default divisor of the max inflight request when calculating search breadth of the search
206    /// for any idle peer to which to send a request filled with hashes pending fetch. The max
207    /// inflight requests is configured in
208    /// [`TransactionFetcherInfo`](crate::transactions::fetcher::TransactionFetcherInfo).
209    ///
210    /// Default is 3 requests.
211    pub const DEFAULT_DIVISOR_MAX_COUNT_INFLIGHT_REQUESTS_ON_FIND_IDLE_PEER: usize = 3;
212
213    /// Default divisor of the max inflight request when calculating search breadth of the search
214    /// for the intersection of hashes announced by a peer and hashes pending fetch. The max
215    /// inflight requests is configured in
216    /// [`TransactionFetcherInfo`](crate::transactions::fetcher::TransactionFetcherInfo).
217    ///
218    /// Default is 3 requests.
219    pub const DEFAULT_DIVISOR_MAX_COUNT_INFLIGHT_REQUESTS_ON_FIND_INTERSECTION: usize = 3;
220
221    // Default divisor to the max pending pool imports when calculating search breadth of the
222    /// search for any idle peer to which to send a request filled with hashes pending fetch.
223    /// The max pending pool imports is configured in
224    /// [`PendingPoolImportsInfo`](crate::transactions::PendingPoolImportsInfo).
225    ///
226    /// Default is 4 requests.
227    pub const DEFAULT_DIVISOR_MAX_COUNT_PENDING_POOL_IMPORTS_ON_FIND_IDLE_PEER: usize = 4;
228
229    /// Default divisor to the max pending pool imports when calculating search breadth of the
230    /// search for any idle peer to which to send a request filled with hashes pending fetch.
231    /// The max pending pool imports is configured in
232    /// [`PendingPoolImportsInfo`](crate::transactions::PendingPoolImportsInfo).
233    ///
234    /// Default is 4 requests.
235    pub const DEFAULT_DIVISOR_MAX_COUNT_PENDING_POOL_IMPORTS_ON_FIND_INTERSECTION: usize = 4;
236
237    /* ================== ROUGH MEASURES ================== */
238
239    /// Average byte size of an encoded transaction.
240    ///
241    /// Default is [`SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE`], which defaults to 2 MiB,
242    /// divided by [`SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE`], which
243    /// is spec'd at 4096 hashes, so 521 bytes.
244    pub const AVERAGE_BYTE_SIZE_TX_ENCODED: usize =
245        SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE /
246            SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE;
247
248    /// Median observed size in bytes of a small encoded legacy transaction.
249    ///
250    /// Default is 120 bytes.
251    pub const MEDIAN_BYTE_SIZE_SMALL_LEGACY_TX_ENCODED: usize = 120;
252
253    /// Marginal on the number of hashes to preallocate memory for in a
254    /// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request, when packed
255    /// according to the [`Eth68`](reth_eth_wire::EthVersion::Eth68) protocol version. To make
256    /// sure enough memory is preallocated in most cases, it's sensible to use a margin. This,
257    /// since the capacity is calculated based on median value
258    /// [`MEDIAN_BYTE_SIZE_SMALL_LEGACY_TX_ENCODED`]. There may otherwise be a noteworthy number of
259    /// cases where just 1 or 2 bytes too little memory is preallocated.
260    ///
261    /// Default is 8 hashes.
262    pub const DEFAULT_MARGINAL_COUNT_HASHES_GET_POOLED_TRANSACTIONS_REQUEST: usize = 8;
263}