mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add tx propagation mode (#11594)
This commit is contained in:
@ -18,6 +18,9 @@ pub struct TransactionsManagerConfig {
|
||||
pub transaction_fetcher_config: TransactionFetcherConfig,
|
||||
/// Max number of seen transactions to store for each peer.
|
||||
pub max_transactions_seen_by_peer_history: u32,
|
||||
/// How new pending transactions are propagated.
|
||||
#[cfg_attr(feature = "serde", serde(default))]
|
||||
pub propagation_mode: TransactionPropagationMode,
|
||||
}
|
||||
|
||||
impl Default for TransactionsManagerConfig {
|
||||
@ -25,6 +28,31 @@ impl Default for TransactionsManagerConfig {
|
||||
Self {
|
||||
transaction_fetcher_config: TransactionFetcherConfig::default(),
|
||||
max_transactions_seen_by_peer_history: DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER,
|
||||
propagation_mode: TransactionPropagationMode::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Determines how new pending transactions are propagated to other peers in full.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub enum TransactionPropagationMode {
|
||||
/// Send full transactions to sqrt of current peers.
|
||||
#[default]
|
||||
Sqrt,
|
||||
/// Always send transactions in full.
|
||||
All,
|
||||
/// Send full transactions to a maximum number of peers
|
||||
Max(usize),
|
||||
}
|
||||
|
||||
impl TransactionPropagationMode {
|
||||
/// Returns the number of peers that should
|
||||
pub(crate) fn full_peer_count(&self, peer_count: usize) -> usize {
|
||||
match self {
|
||||
Self::Sqrt => (peer_count as f64).sqrt().round() as usize,
|
||||
Self::All => peer_count,
|
||||
Self::Max(max) => peer_count.min(*max),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@ pub use self::constants::{
|
||||
tx_fetcher::DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
|
||||
SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
|
||||
};
|
||||
pub use config::{TransactionFetcherConfig, TransactionsManagerConfig};
|
||||
pub use config::{TransactionFetcherConfig, TransactionPropagationMode, TransactionsManagerConfig};
|
||||
pub use validation::*;
|
||||
|
||||
pub(crate) use fetcher::{FetchEvent, TransactionFetcher};
|
||||
@ -246,8 +246,8 @@ pub struct TransactionsManager<Pool> {
|
||||
pending_transactions: ReceiverStream<TxHash>,
|
||||
/// Incoming events from the [`NetworkManager`](crate::NetworkManager).
|
||||
transaction_events: UnboundedMeteredReceiver<NetworkTransactionEvent>,
|
||||
/// Max number of seen transactions to store for each peer.
|
||||
max_transactions_seen_by_peer_history: u32,
|
||||
/// How the `TransactionsManager` is configured.
|
||||
config: TransactionsManagerConfig,
|
||||
/// `TransactionsManager` metrics
|
||||
metrics: TransactionsManagerMetrics,
|
||||
}
|
||||
@ -298,8 +298,7 @@ impl<Pool: TransactionPool> TransactionsManager<Pool> {
|
||||
from_network,
|
||||
NETWORK_POOL_TRANSACTIONS_SCOPE,
|
||||
),
|
||||
max_transactions_seen_by_peer_history: transactions_manager_config
|
||||
.max_transactions_seen_by_peer_history,
|
||||
config: transactions_manager_config,
|
||||
metrics,
|
||||
}
|
||||
}
|
||||
@ -424,9 +423,8 @@ where
|
||||
return propagated
|
||||
}
|
||||
|
||||
// send full transactions to a fraction of the connected peers (square root of the total
|
||||
// number of connected peers)
|
||||
let max_num_full = (self.peers.len() as f64).sqrt().round() as usize;
|
||||
// send full transactions to a set of the connected peers based on the configured mode
|
||||
let max_num_full = self.config.propagation_mode.full_peer_count(self.peers.len());
|
||||
|
||||
// Note: Assuming ~random~ order due to random state of the peers map hasher
|
||||
for (peer_idx, (peer_id, peer)) in self.peers.iter_mut().enumerate() {
|
||||
@ -914,7 +912,7 @@ where
|
||||
messages,
|
||||
version,
|
||||
client_version,
|
||||
self.max_transactions_seen_by_peer_history,
|
||||
self.config.max_transactions_seen_by_peer_history,
|
||||
);
|
||||
let peer = match self.peers.entry(peer_id) {
|
||||
Entry::Occupied(mut entry) => {
|
||||
|
||||
@ -226,6 +226,7 @@ impl NetworkArgs {
|
||||
self.max_capacity_cache_txns_pending_fetch,
|
||||
),
|
||||
max_transactions_seen_by_peer_history: self.max_seen_tx_history,
|
||||
propagation_mode: Default::default(),
|
||||
};
|
||||
|
||||
// Configure basic network stack
|
||||
|
||||
Reference in New Issue
Block a user