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

P2p/configure features #276

Merged
merged 6 commits into from
Nov 1, 2021
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
11 changes: 11 additions & 0 deletions .changes/p2p-feature-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"stronghold-p2p": patch
"iota-stronghold": patch
---

[[PR 276](https://github.com/iotaledger/stronghold.rs/pull/276)]
- Remove `relay` and `mdns` features.
- In the `StrongholdP2p` Interface enable / disable mdns and relay functionality on init via config flags in the `StrongholdP2pBuilder`.
Per default, both are enabled.
- In the `Stronghold` client interface enable / disable mdns and relay in the `NetworkConfig` when spawning a new p2p-network actor.
Per default, both are disabled.
40 changes: 32 additions & 8 deletions client/src/actors/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ impl NetworkActor {
let (firewall_tx, _) = mpsc::channel(0);
let (inbound_request_tx, inbound_request_rx) = EventChannel::new(10, ChannelSinkConfig::BufferLatest);
let firewall_config = FirewallConfiguration::new(Some(firewall_rule), Some(Rule::AllowAll));
let mut builder =
StrongholdP2pBuilder::new(firewall_tx, inbound_request_tx, None).with_firewall_config(firewall_config);
let mut builder = StrongholdP2pBuilder::new(firewall_tx, inbound_request_tx, None)
.with_firewall_config(firewall_config)
.with_mdns_support(network_config.enable_mdns)
.with_relay_support(network_config.enable_relay);
if let Some(keypair) = network_config.keypair {
builder = builder.with_keys(keypair)
}
Expand All @@ -123,6 +125,7 @@ impl NetworkActor {
if let Some(limit) = network_config.connections_limit {
builder = builder.with_connections_limit(limit)
}

let network = builder.build().await?;
let actor = Self {
network,
Expand Down Expand Up @@ -189,7 +192,7 @@ where

impl_handler!(GetSwarmInfo => SwarmInfo, |network, _msg| {
let listeners = network.get_listeners().await;
let local_peer_id = network.get_peer_id();
let local_peer_id = network.peer_id();
let connections = network.get_connections().await;
SwarmInfo { local_peer_id, listeners, connections}
});
Expand All @@ -211,7 +214,7 @@ impl_handler!(StopListeningAddr => (), |network, msg| {
network.stop_listening_addr(msg.address).await
});

impl_handler!(StopListeningRelay => (), |network, msg| {
impl_handler!(StopListeningRelay => bool, |network, msg| {
network.stop_listening_relay(msg.relay).await
});

Expand Down Expand Up @@ -256,10 +259,10 @@ impl_handler!(RemovePeerAddr => (), |network, msg| {
});

impl_handler!(AddDialingRelay => Option<Multiaddr>, |network, msg| {
network.add_dialing_relay(msg.relay, None).await
network.add_dialing_relay(msg.relay, None).await.unwrap()
});

impl_handler!(RemoveDialingRelay => (), |network, msg| {
impl_handler!(RemoveDialingRelay => bool, |network, msg| {
network.remove_dialing_relay(msg.relay).await
});

Expand All @@ -268,11 +271,16 @@ impl_handler!(RemoveDialingRelay => (), |network, msg| {
/// - A new keypair is created and used, from which the [`PeerId`] of the local peer is derived.
/// - No limit for simultaneous connections.
/// - Request-timeout and Connection-timeout are 10s.
/// - [`Mdns`][`libp2p::mdns`] protocol is disabled. **Note**: Enabling mdns will broadcast our own address and id to
/// the local network.
/// - [`Relay`][`libp2p::relay`] functionality is disabled.
pub struct NetworkConfig {
keypair: Option<InitKeypair>,
request_timeout: Option<Duration>,
connection_timeout: Option<Duration>,
connections_limit: Option<ConnectionLimits>,
enable_mdns: bool,
enable_relay: bool,
}

impl NetworkConfig {
Expand Down Expand Up @@ -303,6 +311,20 @@ impl NetworkConfig {
self.connection_timeout = Some(t);
self
}

/// Enable / Disable [`Mdns`][`libp2p::mdns`] protocol.
/// **Note**: Enabling mdns will broadcast our own address and id to the local network.
pub fn with_mdns_enabled(mut self, is_enabled: bool) -> Self {
self.enable_mdns = is_enabled;
self
}

/// Enable / Disable [`Relay`][`libp2p::relay`] functionality.
/// This also means that other peers can use us as relay/
pub fn with_relay_enabled(mut self, is_enabled: bool) -> Self {
self.enable_relay = is_enabled;
self
}
}

impl Default for NetworkConfig {
Expand All @@ -312,6 +334,8 @@ impl Default for NetworkConfig {
request_timeout: None,
connection_timeout: None,
connections_limit: None,
enable_mdns: false,
enable_relay: false,
}
}
}
Expand Down Expand Up @@ -382,7 +406,7 @@ pub mod messages {
}

#[derive(Message)]
#[rtype(result = "()")]
#[rtype(result = "bool")]
pub struct StopListeningRelay {
pub relay: PeerId,
}
Expand Down Expand Up @@ -458,7 +482,7 @@ pub mod messages {
}

#[derive(Message)]
#[rtype(result = "()")]
#[rtype(result = "bool")]
pub struct RemoveDialingRelay {
pub relay: PeerId,
}
Expand Down
10 changes: 5 additions & 5 deletions p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ homepage = "https://stronghold.docs.iota.org"
name = "p2p"

[dependencies]
either = "1.6"
futures = "0.3"
libp2p = { version = "0.40.0-rc.2", default-features = false, features = ["noise", "yamux"] }
libp2p = { version = "0.40.0-rc.3", default-features = false, features = ["noise", "yamux", "mdns", "relay"] }
serde = { version = "1.0", default-features = false, features = [ "alloc", "derive" ] }
serde_json = { version = "1.0", default-features = false, features = [ "alloc" ] }
smallvec = "1.6.1"
stronghold-derive = { path = "../derive", version = "0.2" }
thiserror = "1.0.30"
tokio = { version = "1.10", default-features = false, features = ["rt", "sync"] }
wasm-timer = "0.2.5"

[features]
default = [ "mdns", "relay", "tcp-transport"]
mdns = ["libp2p/mdns"]
relay = ["libp2p/relay"]
default = [ "tcp-transport"]
tcp-transport = ["libp2p/tcp-tokio", "libp2p/dns-tokio", "libp2p/websocket"]

[dev-dependencies]
stronghold-utils = { path = "../utils", version = "0.3" }
tokio = {version = "1.10", features = ["time", "macros"]}
libp2p = { version = "0.40.0-rc.2", default-features = false, features = ["tcp-tokio"] }
libp2p = { version = "0.40.0-rc.3", default-features = false, features = ["tcp-tokio"] }
Loading