Skip to content

Commit

Permalink
Move peer latency constants to one place
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchengxu committed Oct 23, 2024
1 parent 1a26e35 commit e56c808
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
18 changes: 11 additions & 7 deletions crates/subcoin-network/src/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion crates/subcoin-network/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
8 changes: 2 additions & 6 deletions crates/subcoin-network/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down

0 comments on commit e56c808

Please sign in to comment.