Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add tx propagation mode #11594

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions crates/net/network/src/transactions/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,41 @@ 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 {
fn default() -> Self {
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),
}
}
}
Expand Down
16 changes: 7 additions & 9 deletions crates/net/network/src/transactions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -913,7 +911,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) => {
Expand Down
1 change: 1 addition & 0 deletions crates/node/core/src/args/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading