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

Add Kademlia mode override. #1736

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/subspace-networking/src/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use libp2p::multiaddr::Protocol;
use libp2p::swarm::SwarmBuilder;
use libp2p::yamux::Config as YamuxConfig;
use libp2p::{identity, Multiaddr, PeerId, StreamProtocol, TransportError};
use libp2p_kad::Mode;
use parking_lot::Mutex;
use std::borrow::Cow;
use std::iter::Empty;
Expand Down Expand Up @@ -224,6 +225,8 @@ pub struct Config<ProviderStorage> {
pub special_target_connections: u32,
/// Addresses to bootstrap Kademlia network
pub bootstrap_addresses: Vec<Multiaddr>,
/// Optionally overrides the default Kademlia server mode.
pub kademlia_mode_override: Option<Option<Mode>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just Option<Mode> and default it to Some(Mode::Client)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better yet, just Mode

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None is a valid Kademlia mode (automatic mode). This way I can set three modes as well as use the default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need automatic mode? I thought the whole point was to make it explicit

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially, I set the Server mode to upgrade the libp2p with preserving the current behavior. However, It would be great to use the automatic mode, otherwise, we should add basically the same code as they did. On the other hand, when we know that we must not get to the routing table (as for temporary clients now or for nodes in the future) it's convenient to set Client mode explicitly. I believe that having all options available is good.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still looks very strange. If we remove outer Option we just don't set anything on None (libp2p default) and if it is Some then we do set it to whatever was specified. I don't think Some(None) is useful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I change the semantics then.

}

impl<ProviderStorage> fmt::Debug for Config<ProviderStorage> {
Expand Down Expand Up @@ -337,6 +340,7 @@ where
general_target_connections: SWARM_TARGET_CONNECTION_NUMBER,
special_target_connections: SWARM_TARGET_CONNECTION_NUMBER,
bootstrap_addresses: Vec::new(),
kademlia_mode_override: None,
}
}
}
Expand Down Expand Up @@ -400,6 +404,7 @@ where
general_target_connections,
special_target_connections,
bootstrap_addresses,
kademlia_mode_override,
} = config;
let local_peer_id = peer_id(&keypair);

Expand Down Expand Up @@ -512,6 +517,7 @@ where
general_connection_decision_handler,
special_connection_decision_handler,
bootstrap_addresses,
kademlia_mode_override,
});

Ok((node, node_runner))
Expand Down
14 changes: 10 additions & 4 deletions crates/subspace-networking/src/node_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ where
bootstrap_addresses: Vec<Multiaddr>,
/// Ensures a single bootstrap on run() invocation.
bootstrap_command_state: Arc<AsyncMutex<BootstrapCommandState>>,
/// Optionally overrides the default Kademlia server mode.
kademlia_mode_override: Option<Option<Mode>>,
}

// Helper struct for NodeRunner configuration (clippy requirement).
Expand All @@ -159,6 +161,7 @@ where
pub(crate) general_connection_decision_handler: Option<ConnectedPeersHandler>,
pub(crate) special_connection_decision_handler: Option<ConnectedPeersHandler>,
pub(crate) bootstrap_addresses: Vec<Multiaddr>,
pub(crate) kademlia_mode_override: Option<Option<Mode>>,
}

impl<ProviderStorage> NodeRunner<ProviderStorage>
Expand All @@ -180,6 +183,7 @@ where
general_connection_decision_handler,
special_connection_decision_handler,
bootstrap_addresses,
kademlia_mode_override,
}: NodeRunnerConfig<ProviderStorage>,
) -> Self {
Self {
Expand All @@ -206,6 +210,7 @@ where
rng: StdRng::seed_from_u64(KADEMLIA_PEERS_ADDRESSES_BATCH_SIZE as u64), // any seed
bootstrap_addresses,
bootstrap_command_state: Arc::new(AsyncMutex::new(BootstrapCommandState::default())),
kademlia_mode_override,
}
}

Expand Down Expand Up @@ -286,10 +291,11 @@ where

debug!("Bootstrap started.");

self.swarm
.behaviour_mut()
.kademlia
.set_mode(Some(Mode::Server));
// Set the Kademlia mode. The default is Server unless overridden.
let mode = self.kademlia_mode_override.unwrap_or(Some(Mode::Server));
self.swarm.behaviour_mut().kademlia.set_mode(mode);

debug!("Kademlia mode set: {:?}.", mode);

let mut bootstrap_step = 0;
loop {
Expand Down