Skip to content

Commit

Permalink
Clean up docs tx manager (#6682)
Browse files Browse the repository at this point in the history
  • Loading branch information
emhane authored Feb 20, 2024
1 parent 99d1d8d commit cb61334
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 61 deletions.
4 changes: 2 additions & 2 deletions crates/net/network/src/transactions/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST,
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
};
use derive_more::Constructor;
Expand Down Expand Up @@ -29,7 +29,7 @@ pub struct TransactionFetcherConfig {

impl Default for TransactionFetcherConfig {
fn default() -> Self {
Self { soft_limit_byte_size_pooled_transactions_response: SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE, soft_limit_byte_size_pooled_transactions_response_on_pack_request: DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST
Self { soft_limit_byte_size_pooled_transactions_response: SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE, soft_limit_byte_size_pooled_transactions_response_on_pack_request: DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ
}
}
}
146 changes: 94 additions & 52 deletions crates/net/network/src/transactions/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@

/// Soft limit for the number of hashes in a
/// [`NewPooledTransactionHashes`](reth_eth_wire::NewPooledTransactionHashes) broadcast message.
///
/// Spec'd at 4096 hashes.
///
/// <https://github.com/ethereum/devp2p/blob/master/caps/eth.md#newpooledtransactionhashes-0x08>
pub const SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE: usize = 4096;

/// Default soft limit for the byte size of a [`Transactions`](reth_eth_wire::Transactions)
/// broadcast message. Default is 128 KiB.
/// broadcast message.
///
/// Default is 128 KiB.
pub const DEFAULT_SOFT_LIMIT_BYTE_SIZE_TRANSACTIONS_BROADCAST_MESSAGE: usize = 128 * 1024;

/* ================ REQUEST-RESPONSE ================ */

/// Recommended soft limit for the number of hashes in a
/// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request. Spec'd at 256 hashes
/// (8 KiB).
/// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request.
///
/// Spec'd at 256 hashes (8 KiB).
///
/// <https://github.com/ethereum/devp2p/blob/master/caps/eth.md#getpooledtransactions-0x09>
pub const SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST: usize = 256;

/// Soft limit for the byte size of a [`PooledTransactions`](reth_eth_wire::PooledTransactions)
/// response on assembling a [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions)
/// request. Spec'd at 2 MiB.
/// request.
///
/// Spec'd at 2 MiB.
///
/// <https://github.com/ethereum/devp2p/blob/master/caps/eth.md#protocol-messages>.
pub const SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE: usize = 2 * 1024 * 1024;
Expand All @@ -31,20 +37,27 @@ pub mod tx_manager {
use super::SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE;

/// Default limit for number of transactions to keep track of for a single peer, for
/// transactions that the peer's pool and local pool have in common. Default is 10 KiB.
/// transactions that the peer's pool and local pool have in common.
///
/// Default is 10 KiB.
pub const DEFAULT_CAPACITY_CACHE_SEEN_BY_PEER_AND_IN_POOL: usize = 10 * 1024;

/// Default limit for the number of transactions to keep track of for a single peer, for
/// transactions that are in the peer's pool but maybe not in the local pool yet.
///
/// Default is 10 KiB.
pub const DEFAULT_CAPACITY_CACHE_SENT_BY_PEER_AND_MAYBE_IN_POOL: usize = 10 * 1024;

/// Default maximum pending pool imports to tolerate. Default is equivalent to the number of
/// hashes in one full announcement, which is spec'd at 4096 hashes, so 4096 pending pool
/// imports.
/// Default maximum pending pool imports to tolerate.
///
/// Default is equivalent to the number of hashes in one full announcement, which is spec'd at
/// 4096 hashes, so 4096 pending pool imports.
pub const DEFAULT_MAX_COUNT_PENDING_POOL_IMPORTS: usize =
SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE;

/// Default limit for number of bad imports to keep track of. Default is 10 KiB.
/// Default limit for number of bad imports to keep track of.
///
/// Default is 10 KiB.
pub const DEFAULT_CAPACITY_CACHE_BAD_IMPORTS: usize = 100 * 1024;
}

Expand All @@ -63,137 +76,166 @@ pub mod tx_fetcher {
/// [`PooledTransactions`](reth_eth_wire::PooledTransactions) response on assembling a
/// [`GetPooledTransactions`](reth_eth_wire::PooledTransactions) request. This defaults to less
/// than the [`SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE`], at 2 MiB, used when
/// assembling a [`PooledTransactions`](reth_eth_wire::PooledTransactions) response. Default
/// is 128 KiB.
pub const DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST: usize = 128 * 1024;
/// assembling a [`PooledTransactions`](reth_eth_wire::PooledTransactions) response.
///
/// Default is 128 KiB.
pub const DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ: usize = 128 * 1024;

/* ==================== RETRIES ==================== */

/// Default maximum request retires per [`TxHash`](reth_primitives::TxHash). Note, this is reset
/// should the [`TxHash`](reth_primitives::TxHash) re-appear in an announcement after it has
/// been evicted from the hashes pending fetch cache, i.e. the counter is restarted. If this
/// happens, it is likely a very popular transaction, that should and can indeed be fetched
/// hence this behaviour is favourable. Default is 2 retries.
/// Default maximum request retires per [`TxHash`](reth_primitives::TxHash). Note, this is
/// reset should the [`TxHash`](reth_primitives::TxHash) re-appear in an announcement after it
/// has been evicted from the hashes pending fetch cache, i.e. the counter is restarted. If
/// this happens, it is likely a very popular transaction, that should and can indeed be
/// fetched hence this behaviour is favourable.
///
/// Default is 2 retries.
pub const DEFAULT_MAX_RETRIES: u8 = 2;

/// Default number of alternative peers to keep track of for each transaction pending fetch. At
/// most [`DEFAULT_MAX_RETRIES`], which defaults to 2 peers, can ever be needed per peer.
/// Default is the sum of [`DEFAULT_MAX_RETRIES`] and
///
/// Default is the sum of [`DEFAULT_MAX_RETRIES`] an
/// [`DEFAULT_MARGINAL_COUNT_FALLBACK_PEERS`], which defaults to 1 peer, so 3 peers.
pub const DEFAULT_MAX_COUNT_FALLBACK_PEERS: u8 =
DEFAULT_MAX_RETRIES + DEFAULT_MARGINAL_COUNT_FALLBACK_PEERS;

/// Default marginal on fallback peers. This is the case, since a transaction is only requested
/// once from each individual peer. Default is 1 peer.
/// once from each individual peer.
///
/// Default is 1 peer.
const DEFAULT_MARGINAL_COUNT_FALLBACK_PEERS: u8 = 1;

/* ==================== CONCURRENCY ==================== */

/// Default maximum concurrent [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions)
/// requests. Default is the product of [`DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER`],
/// which defaults to 1 request, and the sum of [`DEFAULT_MAX_COUNT_PEERS_INBOUND`] and
/// requests.
///
/// Default is the product of [`DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER`], which
/// defaults to 1 request, and the sum of [`DEFAULT_MAX_COUNT_PEERS_INBOUND`] and
/// [`DEFAULT_MAX_COUNT_PEERS_OUTBOUND`], which default to 30 and 100 peers respectively, so
/// 130 requests.
pub const DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS: u32 =
DEFAULT_MAX_COUNT_PEERS_INBOUND + DEFAULT_MAX_COUNT_PEERS_OUTBOUND;

/// Default maximum number of concurrent
/// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions)s to allow per peer. This
/// number reflects concurrent requests for different hashes. Default is 1 request.
/// number reflects concurrent requests for different hashes.
///
/// Default is 1 request.
pub const DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER: u8 = 1;

/* =============== HASHES PENDING FETCH ================ */

/// Default limit for number of transactions waiting for an idle peer to be fetched from.
///
/// Default is 100 times the [`SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST`],
/// which defaults to 256 hashes, so 25 600 hashes.
pub const DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH: usize =
100 * SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST;

/// Default maximum number of hashes pending fetch to tolerate at any time. Default is half of
/// [`DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH`], which defaults to 25 600 hashes, so 12 800
/// hashes.
/// Default maximum number of hashes pending fetch to tolerate at any time.
///
/// Default is half of [`DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH`], which defaults to 25 600
/// hashes, so 12 800 hashes.
pub const DEFAULT_MAX_COUNT_PENDING_FETCH: usize = DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH / 2;

/* ====== LIMITED CAPACITY ON FETCH PENDING HASHES ====== */

/// Default budget for finding an idle fallback peer for any hash pending fetch, when said
/// search is budget constrained. Default is a sixth of [`DEFAULT_MAX_COUNT_PENDING_FETCH`],
/// which defaults to 12 800 hashes (the breadth of the search), divided by
/// [`DEFAULT_MAX_COUNT_FALLBACK_PEERS`], which defaults to 3 peers (the depth of the search),
/// so the 711 lru hashes in the pending hashes cache.
/// search is budget constrained.
///
/// Default is a sixth of [`DEFAULT_MAX_COUNT_PENDING_FETCH`], which defaults to 12 800 hashes
/// (the breadth of the search), divided by [`DEFAULT_MAX_COUNT_FALLBACK_PEERS`], which
/// defaults to 3 peers (the depth of the search), so the 711 lru hashes in the pending hashes
/// cache.
pub const DEFAULT_BUDGET_FIND_IDLE_FALLBACK_PEER: usize =
DEFAULT_MAX_COUNT_PENDING_FETCH / 6 / DEFAULT_MAX_COUNT_FALLBACK_PEERS as usize;

/// Default budget for finding hashes in the intersection of transactions announced by a peer
/// and in the cache of hashes pending fetch, when said search is budget constrained. Default
/// is a sixth of [`DEFAULT_MAX_COUNT_PENDING_FETCH`], which defaults to 12 800 hashes (the
/// breadth of the search), so 2133 lru hashes in the pending hashes cache.
/// and in the cache of hashes pending fetch, when said search is budget constrained.
///
/// Default is a sixth of [`DEFAULT_MAX_COUNT_PENDING_FETCH`], which defaults to 12 800 hashes
/// (the breadth of the search), so 2133 lru hashes in the pending hashes cache.
pub const DEFAULT_BUDGET_FIND_INTERSECTION_ANNOUNCED_BY_PEER_AND_PENDING_FETCH: usize =
DEFAULT_MAX_COUNT_PENDING_FETCH / 6;

/* ====== SCALARS FOR USE ON FETCH PENDING HASHES ====== */

/// Default soft limit for the number of hashes in a
/// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request, when it is filled
/// from hashes pending fetch. Default is half of the
/// [`SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST`] which by spec is 256
/// hashes, so 128 hashes.
/// from hashes pending fetch.
///
/// Default is half of the [`SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST`]
/// which by spec is 256 hashes, so 128 hashes.
pub const DEFAULT_SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST_ON_FETCH_PENDING_HASHES:
usize = SOFT_LIMIT_COUNT_HASHES_IN_GET_POOLED_TRANSACTIONS_REQUEST / 2;

/// Default soft limit for a [`PooledTransactions`](reth_eth_wire::PooledTransactions) response
/// when it's used as expected response in calibrating the filling of a
/// [`GetPooledTransactions`](reth_eth_wire::GetPooledTransactions) request, when the request
/// is filled from hashes pending fetch. Default is half of
/// [`DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST`],
/// is filled from hashes pending fetch.
///
/// Default is half of
/// [`DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ`],
/// which defaults to 128 KiB, so 64 KiB.
pub const DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_FETCH_PENDING_HASHES:
usize = DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST / 2;

/// Default max inflight request when fetching pending hashes. Default is half of
/// [`DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS`], which defaults to 130 requests, so 65 requests.
usize =
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ /
2;

/// Default max inflight request when fetching pending hashes.
///
/// Default is half of [`DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS`], which defaults to 130
/// requests, so 65 requests.
pub const DEFAULT_MAX_COUNT_INFLIGHT_REQUESTS_ON_FETCH_PENDING_HASHES: usize =
DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS as usize / 2;

/// Default divisor of the max inflight request when calculating search breadth of the search
/// for any idle peer to which to send a request filled with hashes pending fetch. The max
/// inflight requests is configured in
/// [`TransactionFetcherInfo`](crate::transactions::fetcher::TransactionFetcherInfo). Default
/// is 3 requests.
/// [`TransactionFetcherInfo`](crate::transactions::fetcher::TransactionFetcherInfo).
///
/// Default is 3 requests.
pub const DEFAULT_DIVISOR_MAX_COUNT_INFLIGHT_REQUESTS_ON_FIND_IDLE_PEER: usize = 3;

/// Default divisor of the max inflight request when calculating search breadth of the search
/// for the intersection of hashes announced by a peer and hashes pending fetch. The max
/// inflight requests is configured in
/// [`TransactionFetcherInfo`](crate::transactions::fetcher::TransactionFetcherInfo). Default
/// is 2 requests.
/// [`TransactionFetcherInfo`](crate::transactions::fetcher::TransactionFetcherInfo).
///
/// Default is 2 requests.
pub const DEFAULT_DIVISOR_MAX_COUNT_INFLIGHT_REQUESTS_ON_FIND_INTERSECTION: usize = 2;

// Default divisor to the max pending pool imports when calculating search breadth of the
/// search for any idle peer to which to send a request filled with hashes pending fetch.
/// The max pending pool imports is configured in
/// [`PendingPoolImportsInfo`](crate::transactions::PendingPoolImportsInfo). Default
/// is 4 requests.
/// [`PendingPoolImportsInfo`](crate::transactions::PendingPoolImportsInfo).
///
/// Default is 4 requests.
pub const DEFAULT_DIVISOR_MAX_COUNT_PENDING_POOL_IMPORTS_ON_FIND_IDLE_PEER: usize = 4;

/// Default divisor to the max pending pool imports when calculating search breadth of the
/// search for any idle peer to which to send a request filled with hashes pending fetch.
/// The max pending pool imports is configured in
/// [`PendingPoolImportsInfo`](crate::transactions::PendingPoolImportsInfo). Default
/// is 3 requests.
/// [`PendingPoolImportsInfo`](crate::transactions::PendingPoolImportsInfo).
///
/// Default is 3 requests.
pub const DEFAULT_DIVISOR_MAX_COUNT_PENDING_POOL_IMPORTS_ON_FIND_INTERSECTION: usize = 3;

/* ================== ROUGH MEASURES ================== */

/// Average byte size of an encoded transaction. Default is
/// [`SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE`], which defaults to 2 MiB,
/// Average byte size of an encoded transaction.
///
/// Default is [`SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE`], which defaults to 2 MiB,
/// divided by [`SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE`], which
/// is spec'd at 4096 hashes, so 521 bytes.
pub const AVERAGE_BYTE_SIZE_TX_ENCODED: usize =
SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE /
SOFT_LIMIT_COUNT_HASHES_IN_NEW_POOLED_TRANSACTIONS_BROADCAST_MESSAGE;

/// Median observed size in bytes of a small encoded legacy transaction. Default is 120 bytes.
/// Median observed size in bytes of a small encoded legacy transaction.
///
/// Default is 120 bytes.
pub const MEDIAN_BYTE_SIZE_SMALL_LEGACY_TX_ENCODED: usize = 120;
}
6 changes: 3 additions & 3 deletions crates/net/network/src/transactions/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ impl Default for TransactionFetcherInfo {
fn default() -> Self {
Self::new(
DEFAULT_MAX_COUNT_INFLIGHT_REQUESTS_ON_FETCH_PENDING_HASHES,
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST,
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE
)
}
Expand Down Expand Up @@ -1078,8 +1078,8 @@ mod test {
B256::from_slice(&[5; 32]),
];
let eth68_sizes = [
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST - MEDIAN_BYTE_SIZE_SMALL_LEGACY_TX_ENCODED - 1, // first will fit
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST, // second won't
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ - MEDIAN_BYTE_SIZE_SMALL_LEGACY_TX_ENCODED - 1, // first will fit
DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ, // second won't
2, // free space > `MEDIAN_BYTE_SIZE_SMALL_LEGACY_TX_ENCODED`, third will fit, no more after this
9,
0,
Expand Down
2 changes: 1 addition & 1 deletion crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub(crate) use fetcher::{FetchEvent, TransactionFetcher};
pub use validation::*;

pub use self::constants::{
tx_fetcher::DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE_ON_PACK_GET_POOLED_TRANSACTIONS_REQUEST,
tx_fetcher::DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
};
use self::constants::{tx_manager::*, DEFAULT_SOFT_LIMIT_BYTE_SIZE_TRANSACTIONS_BROADCAST_MESSAGE};
Expand Down
Loading

0 comments on commit cb61334

Please sign in to comment.