Skip to main content

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