reth_network/transactions/
constants.rs

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