Skip to content

Commit

Permalink
litep2p: Periodically perform kademlia queries instead if under number
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Jul 16, 2024
1 parent 24bafe3 commit b6c9901
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 41 deletions.
26 changes: 3 additions & 23 deletions substrate/client/network/src/litep2p/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use std::{
cmp,
collections::{HashMap, HashSet, VecDeque},
pin::Pin,
sync::{atomic::AtomicUsize, Arc},
sync::Arc,
task::{Context, Poll},
time::{Duration, Instant},
};
Expand Down Expand Up @@ -204,12 +204,6 @@ pub struct Discovery {

/// Delay to next `FIND_NODE` query.
duration_to_next_find_query: Duration,

/// Number of connected peers as reported by the blocks announcement protocol.
num_connected_peers: Arc<AtomicUsize>,

/// Number of active connections over which we interrupt the discovery process.
discovery_only_if_under_num: usize,
}

/// Legacy (fallback) Kademlia protocol name based on `protocol_id`.
Expand Down Expand Up @@ -244,8 +238,6 @@ impl Discovery {
protocol_id: &ProtocolId,
known_peers: HashMap<PeerId, Vec<Multiaddr>>,
listen_addresses: Arc<RwLock<HashSet<Multiaddr>>>,
num_connected_peers: Arc<AtomicUsize>,
discovery_only_if_under_num: usize,
_peerstore_handle: Arc<dyn PeerStoreProvider>,
) -> (Self, PingConfig, IdentifyConfig, KademliaConfig, Option<MdnsConfig>) {
let (ping_config, ping_event_stream) = PingConfig::default();
Expand Down Expand Up @@ -299,8 +291,6 @@ impl Discovery {
genesis_hash,
fork_id,
)]),
num_connected_peers,
discovery_only_if_under_num,
},
ping_config,
identify_config,
Expand All @@ -309,11 +299,6 @@ impl Discovery {
)
}

/// Get number of connected peers.
fn num_connected_peers(&self) -> usize {
self.num_connected_peers.load(std::sync::atomic::Ordering::Relaxed)
}

/// Add known peer to `Kademlia`.
#[allow(unused)]
pub async fn add_known_peer(&mut self, peer: PeerId, addresses: Vec<Multiaddr>) {
Expand Down Expand Up @@ -473,10 +458,7 @@ impl Stream for Discovery {
if let Some(mut delay) = this.next_kad_query.take() {
match delay.poll_unpin(cx) {
Poll::Ready(()) => {
let num_peers = this.num_connected_peers();
if num_peers < this.discovery_only_if_under_num &&
this.find_node_queries.len() < MAX_INFLIGHT_FIND_NODE_QUERIES
{
if this.find_node_queries.len() < MAX_INFLIGHT_FIND_NODE_QUERIES {
let peer = PeerId::random();
log::debug!(target: LOG_TARGET, "start next kademlia query for {peer:?}");

Expand All @@ -495,9 +477,7 @@ impl Stream for Discovery {
} else {
log::debug!(
target: LOG_TARGET,
"discovery is paused: {num_peers}/{} connected peers and in flight queries: {}/{MAX_INFLIGHT_FIND_NODE_QUERIES}",
this.discovery_only_if_under_num,
this.find_node_queries.len(),
"discovery is paused; too many in-flight queries",
);
}

Expand Down
19 changes: 1 addition & 18 deletions substrate/client/network/src/litep2p/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ use std::{
future::Future,
iter,
pin::Pin,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
sync::{atomic::Ordering, Arc},
time::{Duration, Instant},
};

Expand Down Expand Up @@ -166,11 +163,6 @@ pub struct Litep2pNetworkBackend {
/// Discovery.
discovery: Discovery,

/// Number of uniquely connected peers.
///
/// This is used to instruct the discovery about the number of connected peers.
num_uniquely_connected: Arc<AtomicUsize>,

/// Connected peers.
peers: HashMap<litep2p::PeerId, ConnectionContext>,

Expand Down Expand Up @@ -441,9 +433,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
let peer_store_handle = params.network_config.peer_store_handle();
let executor = Arc::new(Litep2pExecutor { executor: params.executor });

let limit_discovery_under =
params.network_config.network_config.default_peers_set.out_peers as usize + 15;

let FullNetworkConfiguration {
notification_protocols,
request_response_protocols,
Expand Down Expand Up @@ -547,7 +536,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac

// enable ipfs ping, identify and kademlia, and potentially mdns if user enabled it
let listen_addresses = Arc::new(Default::default());
let num_uniquely_connected = Arc::new(Default::default());

let (discovery, ping_config, identify_config, kademlia_config, maybe_mdns_config) =
Discovery::new(
Expand All @@ -557,8 +545,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
&params.protocol_id,
known_addresses.clone(),
Arc::clone(&listen_addresses),
Arc::clone(&num_uniquely_connected),
limit_discovery_under,
Arc::clone(&peer_store_handle),
);

Expand Down Expand Up @@ -614,7 +600,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
cmd_rx,
metrics,
peerset_handles: notif_protocols,
num_uniquely_connected,
discovery,
pending_put_values: HashMap::new(),
pending_get_values: HashMap::new(),
Expand Down Expand Up @@ -691,8 +676,6 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac
log::debug!(target: LOG_TARGET, "starting litep2p network backend");

loop {
self.num_uniquely_connected.store(self.peers.len(), Ordering::Relaxed);

tokio::select! {
command = self.cmd_rx.next() => match command {
None => return,
Expand Down

0 comments on commit b6c9901

Please sign in to comment.