Skip to content

Commit

Permalink
track if there was a successfull bootstrap and the query id of the au…
Browse files Browse the repository at this point in the history
…tomated bootstrap
  • Loading branch information
PanGan21 committed Nov 30, 2023
1 parent f72a8a1 commit f2aaad2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
42 changes: 24 additions & 18 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ pub struct Behaviour<TStore> {
/// The timer used to poll [`Behaviour::bootstrap`].
bootstrap_timer: Option<Delay>,

/// Tracks if the latest bootstrap was successfull.
initial_bootstrap: Option<InitialBootstrap>,
/// Tracks if the there was a successfull bootstrap.
any_bootstrap_successful: bool,

/// Tracks the `QueryId` of the automated bootstrap.
current_automated_bootstrap: Option<QueryId>,
}

/// The configurable strategies for the insertion of peers
Expand Down Expand Up @@ -477,7 +480,8 @@ where
no_events_waker: None,
bootstrap_interval: config.bootstrap_interval,
bootstrap_timer,
initial_bootstrap: None,
any_bootstrap_successful: false,
current_automated_bootstrap: None,
}
}

Expand Down Expand Up @@ -579,7 +583,9 @@ where
};
match entry.insert(addresses.clone(), status) {
kbucket::InsertResult::Inserted => {
self.handle_bootstrap();
if !self.any_bootstrap_successful {
self.maybe_bootstrap();
}
self.queued_events.push_back(ToSwarm::GenerateEvent(
Event::RoutingUpdated {
peer: *peer,
Expand Down Expand Up @@ -915,8 +921,7 @@ where
Err(NoKnownPeers())
} else {
let inner = QueryInner::new(info);
let query_id = self.queries.add_iter_closest(local_key, peers, inner);
Ok(query_id)
Ok(self.queries.add_iter_closest(local_key, peers, inner))
}
}

Expand Down Expand Up @@ -1310,7 +1315,9 @@ where
let addresses = Addresses::new(a);
match entry.insert(addresses.clone(), new_status) {
kbucket::InsertResult::Inserted => {
self.handle_bootstrap();
if !self.any_bootstrap_successful {
self.maybe_bootstrap();
}
let event = Event::RoutingUpdated {
peer,
is_new_peer: true,
Expand Down Expand Up @@ -2074,11 +2081,14 @@ where
}
}

fn handle_bootstrap(&mut self) {
if self.initial_bootstrap.is_none() {
fn maybe_bootstrap(&mut self) {
if self.current_automated_bootstrap.is_none() {
match self.bootstrap() {
Ok(query_id) => self.initial_bootstrap = Some(InitialBootstrap::Active(query_id)),
Err(_) => self.initial_bootstrap = None,
Ok(query_id) => {
self.current_automated_bootstrap = Some(query_id);
self.any_bootstrap_successful = true;
}
Err(_) => self.current_automated_bootstrap = None,
}
}
}
Expand Down Expand Up @@ -2509,7 +2519,9 @@ where
if let Poll::Ready(()) = Pin::new(&mut bootstrap_timer).poll(cx) {
if let Some(interval) = self.bootstrap_interval {
bootstrap_timer.reset(interval);
self.handle_bootstrap();
if !self.any_bootstrap_successful {
self.maybe_bootstrap();
}
}
}
self.bootstrap_timer = Some(bootstrap_timer);
Expand Down Expand Up @@ -3396,9 +3408,3 @@ where
.collect::<Vec<_>>()
.join(", ")
}

#[derive(PartialEq)]
pub enum InitialBootstrap {
Active(QueryId),
Completed,
}
6 changes: 3 additions & 3 deletions protocols/kad/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ pub use behaviour::{
AddProviderContext, AddProviderError, AddProviderOk, AddProviderPhase, AddProviderResult,
BootstrapError, BootstrapOk, BootstrapResult, GetClosestPeersError, GetClosestPeersOk,
GetClosestPeersResult, GetProvidersError, GetProvidersOk, GetProvidersResult, GetRecordError,
GetRecordOk, GetRecordResult, InboundRequest, InitialBootstrap, Mode, NoKnownPeers, PeerRecord,
PutRecordContext, PutRecordError, PutRecordOk, PutRecordPhase, PutRecordResult, QueryInfo,
QueryMut, QueryRef, QueryResult, QueryStats, RoutingUpdate,
GetRecordOk, GetRecordResult, InboundRequest, Mode, NoKnownPeers, PeerRecord, PutRecordContext,
PutRecordError, PutRecordOk, PutRecordPhase, PutRecordResult, QueryInfo, QueryMut, QueryRef,
QueryResult, QueryStats, RoutingUpdate,
};
pub use behaviour::{
Behaviour, BucketInserts, Caching, Config, Event, ProgressStep, Quorum, StoreInserts,
Expand Down

0 comments on commit f2aaad2

Please sign in to comment.