diff --git a/crates/subcoin-network/src/peer_manager.rs b/crates/subcoin-network/src/peer_manager.rs index 9adaa5c..4733d84 100644 --- a/crates/subcoin-network/src/peer_manager.rs +++ b/crates/subcoin-network/src/peer_manager.rs @@ -29,8 +29,12 @@ pub const PROTOCOL_VERSION: u32 = 70016; /// This version includes support for the `sendheaders` feature. pub const MIN_PROTOCOL_VERSION: u32 = 70012; -/// Peer is considered as a slow one if the average ping latency is higher than 5 seconds. -const SLOW_PEER_LATENCY: Latency = 5_000; +/// Peer is considered as a slow one if the average ping latency is higher than 500ms. +const SLOW_PEER_LATENCY: Latency = 500; + +/// Threshold for peer latency in milliseconds, the default is 1000ms. +/// If a peer's latency exceeds this value, it will be considered a slow peer and may be evicted. +pub const LATENCY_THRESHOLD: Latency = 1000; /// Interval for evicting the slowest peer, 10 minutes. /// @@ -167,8 +171,8 @@ pub enum PingState { } impl PingState { - const PING_INTERVAL: Duration = Duration::from_secs(120); const PING_TIMEOUT: Duration = Duration::from_secs(30); + const PING_INTERVAL: Duration = Duration::from_secs(120); fn should_ping(&self) -> bool { match self { @@ -366,15 +370,15 @@ where self.connected_peers .iter() .filter_map(|(peer_id, peer_info)| { - let average_latency = peer_info.ping_latency.average(); + let avg_latency = peer_info.ping_latency.average(); - if average_latency > SLOW_PEER_LATENCY { - Some((peer_id, average_latency)) + if avg_latency > SLOW_PEER_LATENCY { + Some((peer_id, avg_latency)) } else { None } }) - .max_by_key(|(_peer_id, average_latency)| *average_latency) + .max_by_key(|(_peer_id, avg_latency)| *avg_latency) .map(|(peer_id, peer_latency)| SlowPeer { peer_id: *peer_id, peer_latency, diff --git a/crates/subcoin-network/src/sync.rs b/crates/subcoin-network/src/sync.rs index cec6e28..16055fd 100644 --- a/crates/subcoin-network/src/sync.rs +++ b/crates/subcoin-network/src/sync.rs @@ -474,7 +474,7 @@ where pub(super) fn switch_to_idle(&mut self) { tracing::debug!( best_number = self.client.best_number(), - "Blocks-First sync completed, switching to Idle" + "Blocks-First sync completed, switching to Syncing::Idle" ); self.update_syncing_state(Syncing::Idle); } @@ -520,6 +520,9 @@ where if let Inventory::Block(block_hash) = inventories[0] { if !self.inflight_announced_blocks.contains(&block_hash) { // A new block maybe broadcasted via `inv` message. + tracing::trace!( + "Requesting a new block {block_hash} announced from {from:?}" + ); return self.announced_blocks_request(vec![block_hash], from); } } diff --git a/crates/subcoin-network/src/worker.rs b/crates/subcoin-network/src/worker.rs index d388c25..11f6024 100644 --- a/crates/subcoin-network/src/worker.rs +++ b/crates/subcoin-network/src/worker.rs @@ -3,11 +3,11 @@ use crate::metrics::Metrics; use crate::network::{ IncomingTransaction, NetworkStatus, NetworkWorkerMessage, SendTransactionResult, }; -use crate::peer_manager::{Config, NewPeer, PeerManager, SlowPeer}; +use crate::peer_manager::{Config, NewPeer, PeerManager, SlowPeer, LATENCY_THRESHOLD}; use crate::peer_store::PeerStore; use crate::sync::{ChainSync, LocatorRequest, SyncAction, SyncRequest}; use crate::transaction_manager::TransactionManager; -use crate::{Bandwidth, Error, Latency, PeerId, SyncStrategy}; +use crate::{Bandwidth, Error, PeerId, SyncStrategy}; use bitcoin::p2p::message::{NetworkMessage, MAX_INV_SIZE}; use bitcoin::p2p::message_blockdata::{GetBlocksMessage, GetHeadersMessage, Inventory}; use futures::stream::FusedStream; @@ -26,10 +26,6 @@ use tokio::time::MissedTickBehavior; /// Interval at which we perform time based maintenance const TICK_TIMEOUT: Duration = Duration::from_millis(1100); -/// Threshold for peer latency in milliseconds, the default is 10 seconds. -/// If a peer's latency exceeds this value, it will be considered a slow peer and may be evicted. -const LATENCY_THRESHOLD: Latency = 1000; - /// Network event. #[derive(Debug)] pub enum Event {