-
Notifications
You must be signed in to change notification settings - Fork 695
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
litep2p/kad: Configure periodic network bootstrap for better throughput #4942
base: master
Are you sure you want to change the base?
Changes from 12 commits
a126147
d9b57ee
3b2c5ac
b61e973
837f196
5610b95
f5aee82
6ac1bff
4983b89
32c9257
2a6d231
c71fa9e
24bafe3
b6c9901
7bf91eb
5d09d4a
f676446
0124309
a8e1cae
fa3ce93
2ab14ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -166,8 +166,15 @@ pub struct Litep2pNetworkBackend { | |
/// Discovery. | ||
discovery: Discovery, | ||
|
||
/// Number of connected peers. | ||
num_connected: Arc<AtomicUsize>, | ||
/// Number of connected peers over the block announce protocol. | ||
/// | ||
/// This is used to update metrics and network status. | ||
num_sync_connected: Arc<AtomicUsize>, | ||
|
||
/// 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>, | ||
|
@@ -259,6 +266,17 @@ impl Litep2pNetworkBackend { | |
Ok((local_identity, local_peer_id)) | ||
} | ||
|
||
/// Fetch the number of connected peers from the peerset handle and update | ||
/// the atomic `num_connected` shared between the network backend. | ||
fn fetch_sync_connected_peers(&self) -> usize { | ||
lexnv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let num_sync_connected = self | ||
.peerset_handles | ||
.get(&self.block_announce_protocol) | ||
lexnv marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.map_or(0usize, |handle| handle.connected_peers.load(Ordering::Relaxed)); | ||
self.num_sync_connected.store(num_sync_connected, Ordering::Relaxed); | ||
num_sync_connected | ||
} | ||
|
||
/// Configure transport protocols for `Litep2pNetworkBackend`. | ||
fn configure_transport<B: BlockT + 'static, H: ExHashT>( | ||
config: &FullNetworkConfiguration<B, H, Self>, | ||
|
@@ -439,6 +457,9 @@ 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if Do you think the discovery should be guided by the number of sync peers connected, and not by the number of known peers? Also, I would make sure we do a random walk at least once per minute as it was before, even if we have enough peers connected (make |
||
|
||
let FullNetworkConfiguration { | ||
notification_protocols, | ||
request_response_protocols, | ||
|
@@ -541,6 +562,8 @@ 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( | ||
&network_config, | ||
|
@@ -549,6 +572,8 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac | |
¶ms.protocol_id, | ||
known_addresses.clone(), | ||
Arc::clone(&listen_addresses), | ||
Arc::clone(&num_uniquely_connected), | ||
limit_discovery_under, | ||
Arc::clone(&peer_store_handle), | ||
); | ||
|
||
|
@@ -592,20 +617,21 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac | |
)); | ||
|
||
// register rest of the metrics now that `Litep2p` has been created | ||
let num_connected = Arc::new(Default::default()); | ||
let bandwidth: Arc<dyn BandwidthSink> = | ||
Arc::new(Litep2pBandwidthSink { sink: litep2p.bandwidth_sink() }); | ||
|
||
let num_sync_connected = Arc::new(Default::default()); | ||
if let Some(registry) = ¶ms.metrics_registry { | ||
MetricSources::register(registry, bandwidth, Arc::clone(&num_connected))?; | ||
MetricSources::register(registry, bandwidth, Arc::clone(&num_sync_connected))?; | ||
} | ||
|
||
Ok(Self { | ||
network_service, | ||
cmd_rx, | ||
metrics, | ||
peerset_handles: notif_protocols, | ||
num_connected, | ||
num_sync_connected, | ||
num_uniquely_connected, | ||
discovery, | ||
pending_put_values: HashMap::new(), | ||
pending_get_values: HashMap::new(), | ||
|
@@ -682,11 +708,8 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac | |
log::debug!(target: LOG_TARGET, "starting litep2p network backend"); | ||
|
||
loop { | ||
let num_connected_peers = self | ||
.peerset_handles | ||
.get(&self.block_announce_protocol) | ||
.map_or(0usize, |handle| handle.connected_peers.load(Ordering::Relaxed)); | ||
self.num_connected.store(num_connected_peers, Ordering::Relaxed); | ||
self.fetch_sync_connected_peers(); | ||
self.num_uniquely_connected.store(self.peers.len(), Ordering::Relaxed); | ||
|
||
tokio::select! { | ||
command = self.cmd_rx.next() => match command { | ||
|
@@ -707,11 +730,10 @@ impl<B: BlockT + 'static, H: ExHashT> NetworkBackend<B, H> for Litep2pNetworkBac | |
self.event_streams.push(tx); | ||
} | ||
NetworkServiceCommand::Status { tx } => { | ||
let num_connected_peers = self.fetch_sync_connected_peers(); | ||
|
||
let _ = tx.send(NetworkStatus { | ||
num_connected_peers: self | ||
.peerset_handles | ||
.get(&self.block_announce_protocol) | ||
.map_or(0usize, |handle| handle.connected_peers.load(Ordering::Relaxed)), | ||
num_connected_peers, | ||
total_bytes_inbound: self.litep2p.bandwidth_sink().inbound() as u64, | ||
total_bytes_outbound: self.litep2p.bandwidth_sink().outbound() as u64, | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a metric that should be moved to the sync crate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep that sounds good, have created an issue for this:
Since this was a bit involved for the peerstore metrics :D