-
Notifications
You must be signed in to change notification settings - Fork 246
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
Simplify bootstrapping process #1699
Merged
shamil-gadelshin
merged 1 commit into
change_bootstrapping
from
change_bootstrapping-simplification
Jul 26, 2023
+67
−73
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,10 +16,11 @@ use crate::shared::{Command, CreatedSubscription, NewPeerInfo, Shared}; | |||||
use crate::utils::{ | ||||||
convert_multiaddresses, is_global_address_or_dns, PeerAddress, ResizableSemaphorePermit, | ||||||
}; | ||||||
use async_mutex::Mutex as AsyncMutex; | ||||||
use bytes::Bytes; | ||||||
use futures::channel::mpsc; | ||||||
use futures::future::Fuse; | ||||||
use futures::{FutureExt, SinkExt, StreamExt}; | ||||||
use futures::{FutureExt, StreamExt}; | ||||||
use libp2p::core::ConnectedPoint; | ||||||
use libp2p::gossipsub::{Event as GossipsubEvent, TopicHash}; | ||||||
use libp2p::identify::Event as IdentifyEvent; | ||||||
|
@@ -85,6 +86,14 @@ enum QueryResultSender { | |||||
}, | ||||||
} | ||||||
|
||||||
#[derive(Debug, Default)] | ||||||
enum BootstrapCommandState { | ||||||
#[default] | ||||||
NotStarted, | ||||||
InProgress(mpsc::UnboundedReceiver<()>), | ||||||
Finished, | ||||||
} | ||||||
|
||||||
/// Runner for the Node. | ||||||
#[must_use = "Node does not function properly unless its runner is driven forward"] | ||||||
pub struct NodeRunner<ProviderStorage> | ||||||
|
@@ -129,7 +138,7 @@ where | |||||
/// Addresses to bootstrap Kademlia network | ||||||
bootstrap_addresses: Vec<Multiaddr>, | ||||||
/// Ensures a single bootstrap on run() invocation. | ||||||
was_bootstrapped: bool, | ||||||
bootstrap_command_state: Arc<AsyncMutex<BootstrapCommandState>>, | ||||||
} | ||||||
|
||||||
// Helper struct for NodeRunner configuration (clippy requirement). | ||||||
|
@@ -196,15 +205,13 @@ where | |||||
special_connection_decision_handler, | ||||||
rng: StdRng::seed_from_u64(KADEMLIA_PEERS_ADDRESSES_BATCH_SIZE as u64), // any seed | ||||||
bootstrap_addresses, | ||||||
was_bootstrapped: false, | ||||||
bootstrap_command_state: Arc::new(AsyncMutex::new(BootstrapCommandState::default())), | ||||||
} | ||||||
} | ||||||
|
||||||
/// Drives the main networking future forward. | ||||||
pub async fn run(&mut self) { | ||||||
if !self.was_bootstrapped { | ||||||
self.bootstrap().await; | ||||||
} | ||||||
self.bootstrap().await; | ||||||
|
||||||
loop { | ||||||
futures::select! { | ||||||
|
@@ -226,7 +233,7 @@ where | |||||
}, | ||||||
command = self.command_receiver.next() => { | ||||||
if let Some(command) = command { | ||||||
self.handle_command(command).await; | ||||||
self.handle_command(command); | ||||||
} else { | ||||||
break; | ||||||
} | ||||||
|
@@ -246,14 +253,38 @@ where | |||||
|
||||||
/// Bootstraps Kademlia network | ||||||
async fn bootstrap(&mut self) { | ||||||
self.was_bootstrapped = true; | ||||||
let bootstrap_command_state = Arc::clone(&self.bootstrap_command_state); | ||||||
let mut bootstrap_command_state = bootstrap_command_state.lock().await; | ||||||
let bootstrap_command_receiver = match &mut *bootstrap_command_state { | ||||||
BootstrapCommandState::NotStarted => { | ||||||
error!("Bootstrap started."); | ||||||
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.
Suggested change
|
||||||
|
||||||
let (result_sender, mut result_receiver) = mpsc::unbounded(); | ||||||
let (bootstrap_command_sender, bootstrap_command_receiver) = mpsc::unbounded(); | ||||||
|
||||||
debug!("Bootstrap started."); | ||||||
self.handle_command(Command::Bootstrap { | ||||||
result_sender: bootstrap_command_sender, | ||||||
}); | ||||||
|
||||||
*bootstrap_command_state = | ||||||
BootstrapCommandState::InProgress(bootstrap_command_receiver); | ||||||
match &mut *bootstrap_command_state { | ||||||
BootstrapCommandState::InProgress(bootstrap_command_receiver) => { | ||||||
bootstrap_command_receiver | ||||||
} | ||||||
_ => { | ||||||
unreachable!("Was just set to that exact value"); | ||||||
} | ||||||
} | ||||||
} | ||||||
BootstrapCommandState::InProgress(bootstrap_command_receiver) => { | ||||||
bootstrap_command_receiver | ||||||
} | ||||||
BootstrapCommandState::Finished => { | ||||||
return; | ||||||
} | ||||||
}; | ||||||
|
||||||
self.handle_command(Command::Bootstrap { result_sender }) | ||||||
.await; | ||||||
debug!("Bootstrap started."); | ||||||
|
||||||
let mut bootstrap_step = 0; | ||||||
loop { | ||||||
|
@@ -266,7 +297,7 @@ where | |||||
break; | ||||||
} | ||||||
}, | ||||||
result = result_receiver.next() => { | ||||||
result = bootstrap_command_receiver.next() => { | ||||||
if result.is_some() { | ||||||
debug!(%bootstrap_step, "Kademlia bootstrapping..."); | ||||||
bootstrap_step += 1; | ||||||
|
@@ -278,6 +309,7 @@ where | |||||
} | ||||||
|
||||||
debug!("Bootstrap finished."); | ||||||
*bootstrap_command_state = BootstrapCommandState::Finished; | ||||||
} | ||||||
|
||||||
/// Handles periodical tasks. | ||||||
|
@@ -559,7 +591,7 @@ where | |||||
"Peer has different protocol version. Peer was banned.", | ||||||
); | ||||||
|
||||||
self.ban_peer(peer_id).await; | ||||||
self.ban_peer(peer_id); | ||||||
} | ||||||
|
||||||
if info.listen_addrs.len() > 30 { | ||||||
|
@@ -853,38 +885,20 @@ where | |||||
) || cancelled; | ||||||
} | ||||||
Err(error) => { | ||||||
debug!(?error, "Bootstrap query failed.",); | ||||||
|
||||||
self.set_bootstrap_finished(false); | ||||||
debug!(?error, "Bootstrap query failed."); | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if last || cancelled { | ||||||
// There will be no more progress | ||||||
self.query_id_receivers.remove(&id); | ||||||
|
||||||
if last { | ||||||
self.set_bootstrap_finished(true); | ||||||
} | ||||||
|
||||||
if cancelled { | ||||||
self.set_bootstrap_finished(false); | ||||||
} | ||||||
} | ||||||
} | ||||||
_ => {} | ||||||
} | ||||||
} | ||||||
|
||||||
fn set_bootstrap_finished(&mut self, success: bool) { | ||||||
if let Some(shared) = self.shared_weak.upgrade() { | ||||||
shared.bootstrap_finished.store(true, Ordering::SeqCst); | ||||||
|
||||||
debug!(%success, "Bootstrap finished.",); | ||||||
} | ||||||
} | ||||||
|
||||||
// Returns `true` if query was cancelled | ||||||
fn unbounded_send_and_cancel_on_error<T>( | ||||||
kademlia: &mut Kademlia<ProviderOnlyRecordStore<ProviderStorage>>, | ||||||
|
@@ -982,7 +996,7 @@ where | |||||
.add_peers_to_dial(&peers); | ||||||
} | ||||||
|
||||||
async fn handle_command(&mut self, command: Command) { | ||||||
fn handle_command(&mut self, command: Command) { | ||||||
match command { | ||||||
Command::GetValue { | ||||||
key, | ||||||
|
@@ -1234,7 +1248,7 @@ where | |||||
); | ||||||
} | ||||||
Command::BanPeer { peer_id } => { | ||||||
self.ban_peer(peer_id).await; | ||||||
self.ban_peer(peer_id); | ||||||
} | ||||||
Command::Dial { address } => { | ||||||
let _ = self.swarm.dial(address); | ||||||
|
@@ -1244,7 +1258,7 @@ where | |||||
|
||||||
let _ = result_sender.send(connected_peers); | ||||||
} | ||||||
Command::Bootstrap { mut result_sender } => { | ||||||
Command::Bootstrap { result_sender } => { | ||||||
let kademlia = &mut self.swarm.behaviour_mut().kademlia; | ||||||
|
||||||
for (peer_id, address) in convert_multiaddresses(self.bootstrap_addresses.clone()) { | ||||||
|
@@ -1262,17 +1276,13 @@ where | |||||
} | ||||||
Err(err) => { | ||||||
debug!(?err, "Bootstrap error."); | ||||||
|
||||||
let _ = result_sender.close().await; | ||||||
|
||||||
self.set_bootstrap_finished(false); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
async fn ban_peer(&mut self, peer_id: PeerId) { | ||||||
fn ban_peer(&mut self, peer_id: PeerId) { | ||||||
// Remove temporary ban if there is any before creating a permanent one | ||||||
self.temporary_bans.lock().remove(&peer_id); | ||||||
|
||||||
|
@@ -1281,8 +1291,7 @@ where | |||||
self.swarm.behaviour_mut().block_list.block_peer(peer_id); | ||||||
self.swarm.behaviour_mut().kademlia.remove_peer(&peer_id); | ||||||
self.networking_parameters_registry | ||||||
.remove_all_known_peer_addresses(peer_id) | ||||||
.await; | ||||||
.remove_all_known_peer_addresses(peer_id); | ||||||
} | ||||||
|
||||||
fn register_event_metrics<E: Debug>(&mut self, swarm_event: &SwarmEvent<Event, E>) { | ||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.