Skip to content
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

feat(kad): add config option for bootstrap interval #4730

Closed
mxinden opened this issue Oct 26, 2023 · 4 comments · Fixed by #4838
Closed

feat(kad): add config option for bootstrap interval #4730

mxinden opened this issue Oct 26, 2023 · 4 comments · Fixed by #4838
Assignees
Labels
difficulty:easy getting-started Issues that can be tackled if you don't know the internals of libp2p very well help wanted

Comments

@mxinden
Copy link
Member

mxinden commented Oct 26, 2023

When using libp2p-kad it is recommended to regularly call Kademlia::bootstrap to ensure a healthy routing table.

/// Bootstraps the local node to join the DHT.
///
/// Bootstrapping is a multi-step operation that starts with a lookup of the local node's
/// own ID in the DHT. This introduces the local node to the other nodes
/// in the DHT and populates its routing table with the closest neighbours.
///
/// Subsequently, all buckets farther from the bucket of the closest neighbour are
/// refreshed by initiating an additional bootstrapping query for each such
/// bucket with random keys.
///
/// Returns `Ok` if bootstrapping has been initiated with a self-lookup, providing the
/// `QueryId` for the entire bootstrapping process. The progress of bootstrapping is
/// reported via [`Event::OutboundQueryProgressed{QueryResult::Bootstrap}`] events,
/// with one such event per bootstrapping query.
///
/// Returns `Err` if bootstrapping is impossible due an empty routing table.
///
/// > **Note**: Bootstrapping requires at least one node of the DHT to be known.
/// > See [`Behaviour::add_address`].
pub fn bootstrap(&mut self) -> Result<QueryId, NoKnownPeers> {

See e.g. libp2p-server calling Kademlia::bootstrap on an interval.

const BOOTSTRAP_INTERVAL: Duration = Duration::from_secs(5 * 60);

Instead of requiring each user to setup their own interval on which to call Kademlia::bootstrap, provide a new option in libp2p_kad::Config. Something along the lines of:

  /// The configuration for the `Kademlia` behaviour.
  ///
  /// The configuration is consumed by [`Behaviour::new`].
  #[derive(Debug, Clone)]
  pub struct Config {
+    bootstrap_interval(Option<Duration>), 
@mxinden mxinden added difficulty:easy help wanted getting-started Issues that can be tackled if you don't know the internals of libp2p very well labels Oct 26, 2023
@thomaseizinger
Copy link
Contributor

thomaseizinger commented Oct 26, 2023

Is "bootstrap" a kademlia term (from the paper)? Maybe we should call it "refresh"?

@PanGan21
Copy link
Contributor

Hi, I was looking at this issue. Since a new refresh_interval will be added in the Config of Kademlia, isn't it required to move the following logic from the server in the kademlia behaviour?

if let Poll::Ready(()) = futures::poll!(&mut bootstrap_timer) {
            bootstrap_timer.reset(BOOTSTRAP_INTERVAL);
            let _ = swarm.behaviour_mut().kademlia.as_mut().map(|k| {
                k.bootstrap();
            });
        }

Otherwise the interval should be exposed from kademlia in a way to the client code I think

@thomaseizinger
Copy link
Contributor

Yes correct. We will have to have a timer within the libp2p_kad::Behaviour struct that is polled as part of Behaviour::poll (and calls self.bootstrap() when the timer fires). Does that make sense?

@PanGan21
Copy link
Contributor

Makes sense yes. Can I give it a try?

@mergify mergify bot closed this as completed in #4838 Mar 8, 2024
mergify bot pushed a commit that referenced this issue Mar 8, 2024
Previously, users were responsible for calling `bootstrap` on an interval. This was documented but hard to discover for people new to the library. To maintain healthy routing tables, it is advised to regularly call `bootstrap`. By default, we will now do this automatically every 5 minutes and once we add a peer to our routing table, assuming we didn't bootstrap yet. This is especially useful as part of nodes starting up and connecting to bootstrap nodes.

Closes: #4730.

Pull-Request: #4838.

Co-authored-by: stormshield-frb <[email protected]>
guillaumemichel pushed a commit that referenced this issue Mar 28, 2024
Previously, users were responsible for calling `bootstrap` on an interval. This was documented but hard to discover for people new to the library. To maintain healthy routing tables, it is advised to regularly call `bootstrap`. By default, we will now do this automatically every 5 minutes and once we add a peer to our routing table, assuming we didn't bootstrap yet. This is especially useful as part of nodes starting up and connecting to bootstrap nodes.

Closes: #4730.

Pull-Request: #4838.

Co-authored-by: stormshield-frb <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
difficulty:easy getting-started Issues that can be tackled if you don't know the internals of libp2p very well help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants