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 all commits
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>,
/// Kademlia mode. None means "automatic mode".
pub kademlia_mode: Option<Mode>,
}

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: Some(Mode::Server),
Copy link
Member

Choose a reason for hiding this comment

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

Hm... I recall we wanted the default to be client, not server. Why this change? If we want to keep libp2p's default then we need to leave None.

}
}
}
Expand Down Expand Up @@ -400,6 +404,7 @@ where
general_target_connections,
special_target_connections,
bootstrap_addresses,
kademlia_mode,
} = 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,
});

Ok((node, node_runner))
Expand Down
8 changes: 7 additions & 1 deletion 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>>,
/// Kademlia mode. None means "automatic mode".
kademlia_mode: 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: 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,
}: 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,
}
}

Expand Down Expand Up @@ -289,7 +294,8 @@ where
self.swarm
.behaviour_mut()
.kademlia
.set_mode(Some(Mode::Server));
.set_mode(self.kademlia_mode);
debug!("Kademlia mode set: {:?}.", self.kademlia_mode);

let mut bootstrap_step = 0;
loop {
Expand Down