Skip to content

Commit

Permalink
chore: tidy up wasm32 as target arch rather than a feat
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuef committed Jan 24, 2024
1 parent 6e96353 commit 903092b
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions sn_client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ local-discovery=["sn_networking/local-discovery"]
open-metrics = ["sn_networking/open-metrics", "prometheus-client"]
# required to pass on flag to node builds
quic = ["sn_networking/quic"]
webtransport-websys = ["sn_networking/webtransport-websys"]

[dependencies]
async-trait = "0.1"
Expand Down Expand Up @@ -60,4 +59,5 @@ workspace = true


[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.12", features = ["js"] }
getrandom = { version = "0.2.12", features = ["js"] }
sn_networking = { path = "../sn_networking", version = "0.12.32" }
6 changes: 4 additions & 2 deletions sn_networking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ repository = "https://github.com/maidsafe/safe_network"
version = "0.12.35"

[features]
default=[]
default=["quic"]
local-discovery=["libp2p/mdns"]
quic=["libp2p/quic"]
webtransport-websys=["libp2p/webtransport-websys"]
tcp=["libp2p/tcp"]
open-metrics=["libp2p/metrics", "prometheus-client", "hyper", "sysinfo"]

[dependencies]
Expand Down Expand Up @@ -54,3 +54,5 @@ workspace = true

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.12", features = ["js"] }
libp2p = { version="0.53", features = ["tokio", "dns", "kad", "macros", "request-response", "cbor","identify", "autonat", "noise", "tcp", "yamux", "gossipsub", "webtransport-websys"] }

64 changes: 33 additions & 31 deletions sn_networking/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,17 @@ use crate::{
Network, CLOSE_GROUP_SIZE,
};
use futures::StreamExt;
#[cfg(any(feature = "quic", feature = "webtransport-websys"))]
#[cfg(not(feature = "tcp"))]
use libp2p::core::muxing::StreamMuxerBox;
#[cfg(feature = "local-discovery")]
use libp2p::mdns;
#[cfg(feature = "quic")]
use libp2p::quic;
#[cfg(feature = "webtransport-websys")]
use libp2p::webtransport_websys;
// default transports
#[cfg(all(not(feature = "tcp"), not(target_arch = "wasm32")))]
use libp2p::quic::{tokio::Transport as TokioTransport, Config as TransportConfig};
#[cfg(feature = "tcp")]
use libp2p::tcp::{tokio::Transport as TokioTransport, Config as TransportConfig};
#[cfg(target_arch = "wasm32")]
use libp2p::webtransport_websys::{Config as TransportConfig, Transport as TokioTransport};
use libp2p::{
autonat,
identity::Keypair,
Expand Down Expand Up @@ -319,20 +322,24 @@ impl NetworkBuilder {
)?;

// Listen on the provided address
let listen_addr = listen_addr.ok_or(Error::ListenAddressNotProvided)?;
#[cfg(not(any(feature = "quic", feature = "webtransport-websys")))]
let listen_addr = Multiaddr::from(listen_addr.ip()).with(Protocol::Tcp(listen_addr.port()));
let listen_socket_addr = listen_addr.ok_or(Error::ListenAddressNotProvided)?;

// Flesh out the multiaddress
let listen_addr = Multiaddr::from(listen_socket_addr.ip());

#[cfg(feature = "quic")]
let listen_addr = Multiaddr::from(listen_addr.ip())
.with(Protocol::Udp(listen_addr.port()))
let listen_addr = listen_addr
.with(Protocol::Udp(listen_socket_addr.port()))
.with(Protocol::QuicV1);

#[cfg(feature = "webtransport-websys")]
let listen_addr = Multiaddr::from(listen_addr.ip())
.with(Protocol::Udp(listen_addr.port()))
#[cfg(target_arch = "wasm32")]
let listen_addr = listen_addr
.with(Protocol::Udp(listen_socket_addr.port()))
.with(Protocol::WebTransport);

#[cfg(feature = "tcp")]
Multiaddr::from(listen_socket_addr.ip()).with(Protocol::Tcp(listen_socket_addr.port()));

let _listener_id = swarm_driver
.swarm
.listen_on(listen_addr)
Expand Down Expand Up @@ -458,9 +465,13 @@ impl NetworkBuilder {
libp2p::identify::Behaviour::new(cfg)
};

// Transport
#[cfg(not(any(feature = "quic", feature = "webtransport-websys")))]
let mut transport = libp2p::tcp::tokio::Transport::new(libp2p::tcp::Config::default())
#[cfg(not(feature = "tcp"))]
let main_transport = TokioTransport::new(TransportConfig::new(&self.keypair))
.map(|(peer_id, muxer), _| (peer_id, StreamMuxerBox::new(muxer)))
.boxed();

#[cfg(feature = "tcp")]
let main_transport = TokioTransport::new(TransportConfig::default())
.upgrade(libp2p::core::upgrade::Version::V1)
.authenticate(
libp2p::noise::Config::new(&self.keypair)
Expand All @@ -469,17 +480,6 @@ impl NetworkBuilder {
.multiplex(libp2p::yamux::Config::default())
.boxed();

#[cfg(feature = "quic")]
let mut transport = libp2p::quic::tokio::Transport::new(quic::Config::new(&self.keypair))
.map(|(peer_id, muxer), _| (peer_id, StreamMuxerBox::new(muxer)))
.boxed();

#[cfg(feature = "webtransport-websys")]
let mut transport =
webtransport_websys::Transport::new(webtransport_websys::Config::new(&self.keypair))
.map(|(peer_id, conn), _| (peer_id, StreamMuxerBox::new(conn)))
.boxed();

let gossipsub = if self.enable_gossip {
// Gossipsub behaviour
let gossipsub_config = libp2p::gossipsub::ConfigBuilder::default()
Expand Down Expand Up @@ -516,11 +516,13 @@ impl NetworkBuilder {

let gossipsub = Toggle::from(gossipsub);

if !self.local {
let transport = if !self.local {
debug!("Preventing non-global dials");
// Wrap TCP or UDP in a transport that prevents dialing local addresses.
transport = libp2p::core::transport::global_only::Transport::new(transport).boxed();
}
// Wrap upper in a transport that prevents dialing local addresses.
libp2p::core::transport::global_only::Transport::new(main_transport).boxed()
} else {
main_transport
};

// Disable AutoNAT if we are either running locally or a client.
let autonat = if !self.local && !is_client {
Expand Down
2 changes: 1 addition & 1 deletion sn_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ color-eyre = "0.6.2"
[dev-dependencies]
tempfile = "3.6.0"
reqwest = { version="0.11.18", default-features=false, features = ["rustls"] }
sn_protocol = { path = "../sn_protocol", version = "0.10.14", features = ["test-utils"]}
sn_protocol = { path = "../sn_protocol", version = "0.10.14", features = ["test-utils", "rpc"]}

[lints]
workspace = true
11 changes: 6 additions & 5 deletions sn_peers_acquisition/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,18 @@ async fn get_network_contacts(args: &PeersArgs) -> Result<Vec<Multiaddr>> {
pub fn parse_peer_addr(addr: &str) -> Result<Multiaddr> {
// Parse valid IPv4 socket address, e.g. `1.2.3.4:1234`.
if let Ok(addr) = addr.parse::<std::net::SocketAddrV4>() {
#[cfg(not(feature = "quic, webtransport-websys"))]
let multiaddr = Multiaddr::from(*addr.ip());
#[cfg(feature = "tcp")]
// Turn the address into a `/ip4/<ip>/tcp/<port>` multiaddr.
let multiaddr = Multiaddr::from(*addr.ip()).with(Protocol::Tcp(addr.port()));
let multiaddr = multiaddr.with(Protocol::Tcp(addr.port()));
#[cfg(feature = "quic")]
// Turn the address into a `/ip4/<ip>/udp/<port>/quic-v1` multiaddr.
let multiaddr = Multiaddr::from(*addr.ip())
let multiaddr = multiaddr
.with(Protocol::Udp(addr.port()))
.with(Protocol::QuicV1);
#[cfg(feature = "webtransport-websys")]
#[cfg(target_arch = "wasm32")]
// Turn the address into a `/ip4/<ip>/udp/<port>/webtransport-websys-v1` multiaddr.
let multiaddr = Multiaddr::from(*addr.ip())
let multiaddr = multiaddr
.with(Protocol::Udp(addr.port()))
.with(Protocol::WebTransport);
return Ok(multiaddr);
Expand Down

0 comments on commit 903092b

Please sign in to comment.