diff --git a/protocol/flows/src/flowcontext/transactions.rs b/protocol/flows/src/flowcontext/transactions.rs index 0b4eadfb7..9baf6e1e7 100644 --- a/protocol/flows/src/flowcontext/transactions.rs +++ b/protocol/flows/src/flowcontext/transactions.rs @@ -97,7 +97,9 @@ impl TransactionsSpread { async fn broadcast(&self, msg: KaspadMessage, should_throttle: bool) { if should_throttle { - self.hub.broadcast_some(msg, 0.5).await + // Broadcast to only half of the peers + // TODO: Figure out the better percentage + self.hub.broadcast_to_some_peers(msg, 0.5).await } else { self.hub.broadcast(msg).await } diff --git a/protocol/flows/src/v5/txrelay/flow.rs b/protocol/flows/src/v5/txrelay/flow.rs index ea10f3dcc..88fb7b531 100644 --- a/protocol/flows/src/v5/txrelay/flow.rs +++ b/protocol/flows/src/v5/txrelay/flow.rs @@ -99,10 +99,10 @@ impl RelayTransactionsFlow { if snapshot_delta.low_priority_tx_counts > 0 { let tps = snapshot_delta.low_priority_tx_counts / 10; - if tps > MAX_TPS_THRESHOLD { + if !should_throttle && tps > MAX_TPS_THRESHOLD { warn!("P2P tx relay threshold exceeded. Throttling relay. Current: {}, Max: {}", tps, MAX_TPS_THRESHOLD); should_throttle = true; - } else if tps < MAX_TPS_THRESHOLD / 2 && should_throttle { + } else if should_throttle && tps < MAX_TPS_THRESHOLD / 2 { warn!("P2P tx relay threshold back to normal. Current: {}, Max: {}", tps, MAX_TPS_THRESHOLD); should_throttle = false; } @@ -143,7 +143,7 @@ impl RelayTransactionsFlow { // To reduce the P2P TPS to below the threshold, we need to request up to a max of // whatever the balances overage. If MAX_TPS_THRESHOLD is 3000 and the current TPS is 4000, // then we can only request up to 2000 (MAX - (4000 - 3000)) to average out into the threshold. - let curr_p2p_tps = snapshot_delta.low_priority_tx_counts / (snapshot_delta.elapsed_time.as_millis().max(1) as u64); + let curr_p2p_tps = 1000 * snapshot_delta.low_priority_tx_counts / (snapshot_delta.elapsed_time.as_millis().max(1) as u64); let overage = if should_throttle && curr_p2p_tps > MAX_TPS_THRESHOLD { curr_p2p_tps - MAX_TPS_THRESHOLD } else { 0 }; let limit = MAX_TPS_THRESHOLD - overage; diff --git a/protocol/p2p/src/core/hub.rs b/protocol/p2p/src/core/hub.rs index 2183b04f0..0861e9b8b 100644 --- a/protocol/p2p/src/core/hub.rs +++ b/protocol/p2p/src/core/hub.rs @@ -105,7 +105,7 @@ impl Hub { } /// Broadcast a message to some peers given a percentage - pub async fn broadcast_some(&self, msg: KaspadMessage, percentage: f64) { + pub async fn broadcast_to_some_peers(&self, msg: KaspadMessage, percentage: f64) { let percentage = percentage.clamp(0.0, 1.0); let peers = self.peers.read().values().cloned().collect::>();