Skip to content

Commit

Permalink
chore(sn_networking): enable_gossip via the builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Nov 21, 2023
1 parent 73e3d78 commit 4ab1def
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
13 changes: 7 additions & 6 deletions sn_client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Client {
pub async fn new(
signer: SecretKey,
peers: Option<Vec<Multiaddr>>,
joins_gossip: bool,
enable_gossip: bool,
connection_timeout: Option<Duration>,
) -> Result<Self> {
// If any of our contact peers has a global address, we'll assume we're in a global network.
Expand All @@ -71,16 +71,17 @@ impl Client {
info!("Startup a client with peers {peers:?} and local {local:?} flag");
info!("Starting Kad swarm in client mode...");

let network_builder =
let mut network_builder =
NetworkBuilder::new(Keypair::generate_ed25519(), local, std::env::temp_dir());

#[cfg(feature = "open-metrics")]
let mut network_builder = network_builder;
if enable_gossip {
network_builder.enable_gossip();
}

#[cfg(feature = "open-metrics")]
network_builder.metrics_registry(Registry::default());

let (network, mut network_event_receiver, swarm_driver) =
network_builder.build_client(joins_gossip)?;
let (network, mut network_event_receiver, swarm_driver) = network_builder.build_client()?;
info!("Client constructed network and swarm_driver");
let events_channel = ClientEventsChannel::default();

Expand Down
17 changes: 9 additions & 8 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ pub struct NetworkBuilder {
local: bool,
root_dir: PathBuf,
listen_addr: Option<SocketAddr>,
enable_gossip: bool,
request_timeout: Option<Duration>,
concurrency_limit: Option<usize>,
#[cfg(feature = "open-metrics")]
Expand All @@ -145,6 +146,7 @@ impl NetworkBuilder {
local,
root_dir,
listen_addr: None,
enable_gossip: false,
request_timeout: None,
concurrency_limit: None,
#[cfg(feature = "open-metrics")]
Expand All @@ -158,6 +160,11 @@ impl NetworkBuilder {
self.listen_addr = Some(listen_addr);
}

/// Enable gossip for the network
pub fn enable_gossip(&mut self) {
self.enable_gossip = true;
}

pub fn request_timeout(&mut self, request_timeout: Duration) {
self.request_timeout = Some(request_timeout);
}
Expand Down Expand Up @@ -239,7 +246,6 @@ impl NetworkBuilder {
kad_cfg,
Some(store_cfg),
false,
false,
ProtocolSupport::Full,
truncate_patch_version(SN_NODE_VERSION_STR).to_string(),
)?;
Expand All @@ -263,10 +269,7 @@ impl NetworkBuilder {
}

/// Same as `build_node` API but creates the network components in client mode
pub fn build_client(
self,
is_gossip_client: bool,
) -> Result<(Network, mpsc::Receiver<NetworkEvent>, SwarmDriver)> {
pub fn build_client(self) -> Result<(Network, mpsc::Receiver<NetworkEvent>, SwarmDriver)> {
// Create a Kademlia behaviour for client mode, i.e. set req/resp protocol
// to outbound-only mode and don't listen on any address
let mut kad_cfg = kad::Config::default(); // default query timeout is 60 secs
Expand All @@ -285,7 +288,6 @@ impl NetworkBuilder {
kad_cfg,
None,
true,
is_gossip_client,
ProtocolSupport::Outbound,
truncate_patch_version(IDENTIFY_CLIENT_VERSION_STR).to_string(),
)?;
Expand All @@ -299,7 +301,6 @@ impl NetworkBuilder {
kad_cfg: kad::Config,
record_store_cfg: Option<NodeRecordStoreConfig>,
is_client: bool,
is_gossip_client: bool,
req_res_protocol: ProtocolSupport,
identify_version: String,
) -> Result<(Network, mpsc::Receiver<NetworkEvent>, SwarmDriver)> {
Expand Down Expand Up @@ -397,7 +398,7 @@ impl NetworkBuilder {
.map(|(peer_id, muxer), _| (peer_id, StreamMuxerBox::new(muxer)))
.boxed();

let gossipsub = if !is_client || is_gossip_client {
let gossipsub = if self.enable_gossip {
// Gossipsub behaviour
let gossipsub_config = libp2p::gossipsub::ConfigBuilder::default()
// we don't currently require source peer id and/or signing
Expand Down
2 changes: 1 addition & 1 deletion sn_networking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ mod tests {
fn test_network_sign_verify() -> eyre::Result<()> {
let (network, _, _) =
NetworkBuilder::new(Keypair::generate_ed25519(), false, std::env::temp_dir())
.build_client(false)?;
.build_client()?;
let msg = b"test message";
let sig = network.sign(msg)?;
assert!(network.verify(msg, &sig));
Expand Down
2 changes: 2 additions & 0 deletions sn_node/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ impl NodeBuilder {
};

let mut network_builder = NetworkBuilder::new(self.keypair, self.local, self.root_dir);

network_builder.enable_gossip();
network_builder.listen_addr(self.addr);
#[cfg(feature = "open-metrics")]
network_builder.metrics_registry(metrics_registry);
Expand Down

0 comments on commit 4ab1def

Please sign in to comment.