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(networking): allow disabling relay server #1856

Closed
wants to merge 1 commit into from
Closed
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
28 changes: 22 additions & 6 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use futures::future::Either;
use futures::StreamExt;
#[cfg(feature = "local-discovery")]
use libp2p::mdns;
use libp2p::Transport as _;
use libp2p::{swarm::behaviour::toggle::Toggle, Transport as _};
use libp2p::{core::muxing::StreamMuxerBox, relay};
use libp2p::{
identity::Keypair,
Expand Down Expand Up @@ -187,15 +187,15 @@ pub enum VerificationKind {
#[behaviour(to_swarm = "NodeEvent")]
pub(super) struct NodeBehaviour {
#[cfg(feature = "upnp")]
pub(super) upnp: libp2p::swarm::behaviour::toggle::Toggle<libp2p::upnp::tokio::Behaviour>,
pub(super) upnp: Toggle<libp2p::upnp::tokio::Behaviour>,
pub(super) request_response: request_response::cbor::Behaviour<Request, Response>,
pub(super) kademlia: kad::Behaviour<UnifiedRecordStore>,
#[cfg(feature = "local-discovery")]
pub(super) mdns: mdns::tokio::Behaviour,
pub(super) identify: libp2p::identify::Behaviour,
pub(super) dcutr: libp2p::dcutr::Behaviour,
pub(super) relay_client: libp2p::relay::client::Behaviour,
pub(super) relay_server: libp2p::relay::Behaviour,
pub(super) relay_server: Toggle<libp2p::relay::Behaviour>,
}

#[derive(Debug)]
Expand All @@ -215,6 +215,7 @@ pub struct NetworkBuilder {
metrics_server_port: Option<u16>,
#[cfg(feature = "upnp")]
upnp: bool,
relay_server: bool,
}

impl NetworkBuilder {
Expand All @@ -234,6 +235,7 @@ impl NetworkBuilder {
metrics_server_port: None,
#[cfg(feature = "upnp")]
upnp: false,
relay_server: true,
}
}

Expand Down Expand Up @@ -272,6 +274,10 @@ impl NetworkBuilder {
self.upnp = upnp;
}

pub fn relay_server(&mut self, enabled: bool) {
self.relay_server = enabled;
}

/// Creates a new `SwarmDriver` instance, along with a `Network` handle
/// for sending commands and an `mpsc::Receiver<NetworkEvent>` for receiving
/// network events. It initializes the swarm, sets up the transport, and
Expand Down Expand Up @@ -334,6 +340,7 @@ impl NetworkBuilder {
let listen_addr = self.listen_addr;
#[cfg(feature = "upnp")]
let upnp = self.upnp;
let relay_server = self.relay_server;

let (network, events_receiver, mut swarm_driver) = self.build(
kad_cfg,
Expand All @@ -343,6 +350,7 @@ impl NetworkBuilder {
IDENTIFY_NODE_VERSION_STR.to_string(),
#[cfg(feature = "upnp")]
upnp,
relay_server,
)?;

// Listen on the provided address
Expand Down Expand Up @@ -396,12 +404,14 @@ impl NetworkBuilder {
IDENTIFY_CLIENT_VERSION_STR.to_string(),
#[cfg(feature = "upnp")]
false,
false,
)?;

Ok((network, net_event_recv, driver))
}

/// Private helper to create the network components with the provided config and req/res behaviour
#[allow(clippy::too_many_arguments)]
fn build(
self,
kad_cfg: kad::Config,
Expand All @@ -410,6 +420,7 @@ impl NetworkBuilder {
req_res_protocol: ProtocolSupport,
identify_version: String,
#[cfg(feature = "upnp")] upnp: bool,
relay_server: bool,
) -> Result<(Network, mpsc::Receiver<NetworkEvent>, SwarmDriver)> {
let peer_id = PeerId::from(self.keypair.public());
// vdash metric (if modified please notify at https://github.com/happybeing/vdash/issues):
Expand Down Expand Up @@ -543,15 +554,20 @@ impl NetworkBuilder {
})
.boxed();

let relay_server = {
let relay_server = if relay_server {
debug!("Enabling relay server behavior");
let relay_server_cfg = relay::Config {
max_reservations: 1024, // the number of home nodes that we can support
max_circuits: 32_000, // total max number of relayed connections we can support
max_circuits_per_peer: 1024, // max number of relayed connections per peer
..Default::default()
};
libp2p::relay::Behaviour::new(peer_id, relay_server_cfg)
};
Some(libp2p::relay::Behaviour::new(peer_id, relay_server_cfg))
} else {
debug!("Disabling relay server behavior");
None
}
.into();

let behaviour = NodeBehaviour {
relay_client: relay_behaviour,
Expand Down
5 changes: 5 additions & 0 deletions sn_node/src/bin/safenode/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ struct Opt {
#[clap(long, default_value_t = false)]
upnp: bool,

/// Disable relay server functionality. Peers wil not be able to use this server as a relay.
#[clap(long, default_value_t = false)]
no_relay_server: bool,

/// Specify the logging output destination.
///
/// Valid values are "stdout", "data-dir", or a custom path.
Expand Down Expand Up @@ -220,6 +224,7 @@ fn main() -> Result<()> {
opt.owner.clone(),
#[cfg(feature = "upnp")]
opt.upnp,
!opt.no_relay_server,
);
node_builder.is_behind_home_network = opt.home_network;
#[cfg(feature = "open-metrics")]
Expand Down
6 changes: 5 additions & 1 deletion sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ pub struct NodeBuilder {
owner: Option<String>,
#[cfg(feature = "upnp")]
upnp: bool,
relay_server: bool,
}

impl NodeBuilder {
/// Instantiate the builder
#[allow(clippy::too_many_arguments)]
pub fn new(
keypair: Keypair,
addr: SocketAddr,
Expand All @@ -95,6 +97,7 @@ impl NodeBuilder {
root_dir: PathBuf,
owner: Option<String>,
#[cfg(feature = "upnp")] upnp: bool,
relay_server: bool,
Copy link
Member

@RolandSherwin RolandSherwin Jun 7, 2024

Choose a reason for hiding this comment

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

I think this need not be passed as an arg to NodeBuilder::new(). Since this is a builder, we can add a function inside NodeBuilder to set the value. And then call it if necessary?

Copy link
Member

Choose a reason for hiding this comment

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

The same should've been done for owner arg :( can we create a commit for that as well if possible?

) -> Self {
Self {
keypair,
Expand All @@ -108,6 +111,7 @@ impl NodeBuilder {
owner,
#[cfg(feature = "upnp")]
upnp,
relay_server,
}
}

Expand Down Expand Up @@ -162,9 +166,9 @@ impl NodeBuilder {
network_builder.metrics_server_port(self.metrics_server_port);
network_builder.initial_peers(self.initial_peers.clone());
network_builder.is_behind_home_network(self.is_behind_home_network);

#[cfg(feature = "upnp")]
network_builder.upnp(self.upnp);
network_builder.relay_server(self.relay_server);

let (network, network_event_receiver, swarm_driver) = network_builder.build_node()?;
let node_events_channel = NodeEventsChannel::default();
Expand Down
Loading