From 0ad8ac9520f7d27ccfa86f5feb5fc9a41d9d10bf Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Tue, 21 Nov 2023 02:05:48 -0500 Subject: [PATCH 1/4] QUIC-based LibP2p Swarm (replacing TCP) --- coordinator/Cargo.toml | 2 +- coordinator/src/p2p.rs | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/coordinator/Cargo.toml b/coordinator/Cargo.toml index 460e5d8bf..58b94e29e 100644 --- a/coordinator/Cargo.toml +++ b/coordinator/Cargo.toml @@ -48,7 +48,7 @@ env_logger = { version = "0.10", default-features = false, features = ["humantim futures = { version = "0.3", default-features = false, features = ["std"] } tokio = { version = "1", default-features = false, features = ["rt-multi-thread", "sync", "time", "macros"] } -libp2p = { version = "0.52", default-features = false, features = ["tokio", "tcp", "noise", "yamux", "gossipsub", "mdns", "macros"] } +libp2p = { version = "0.52", default-features = false, features = ["tokio", "quic", "gossipsub", "mdns", "macros"] } [dev-dependencies] futures = { version = "0.3", default-features = false, features = ["std"] } diff --git a/coordinator/src/p2p.rs b/coordinator/src/p2p.rs index aa392c52b..62e541097 100644 --- a/coordinator/src/p2p.rs +++ b/coordinator/src/p2p.rs @@ -22,8 +22,6 @@ use libp2p::{ futures::StreamExt, identity::Keypair, PeerId, - tcp::Config as TcpConfig, - noise, yamux, gossipsub::{ IdentTopic, FastMessageId, MessageId, MessageAuthenticity, ValidationMode, ConfigBuilder, IdentityTransform, AllowAllSubscriptionFilter, Event as GsEvent, PublishError, @@ -276,16 +274,7 @@ impl LibP2p { // TODO: Relay client? let mut swarm = SwarmBuilder::with_existing_identity(throwaway_key_pair) .with_tokio() - .with_tcp(TcpConfig::default().nodelay(true), noise::Config::new, || { - let mut config = yamux::Config::default(); - // 1 MiB default + max message size - config.set_max_buffer_size((1024 * 1024) + MAX_LIBP2P_MESSAGE_SIZE); - // 256 KiB default + max message size - config - .set_receive_window_size(((256 * 1024) + MAX_LIBP2P_MESSAGE_SIZE).try_into().unwrap()); - config - }) - .unwrap() + .with_quic() .with_behaviour(|_| behavior) .unwrap() .build(); From 8d54419ddcdfc16ced2d726fd26a89bb7bc42c0f Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 24 Nov 2023 21:59:33 -0500 Subject: [PATCH 2/4] Replace TCP port specifications with UDP --- coordinator/src/p2p.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coordinator/src/p2p.rs b/coordinator/src/p2p.rs index 62e541097..80de3da5d 100644 --- a/coordinator/src/p2p.rs +++ b/coordinator/src/p2p.rs @@ -279,7 +279,7 @@ impl LibP2p { .unwrap() .build(); const PORT: u16 = 30563; // 5132 ^ (('c' << 8) | 'o') - swarm.listen_on(format!("/ip4/0.0.0.0/tcp/{PORT}").parse().unwrap()).unwrap(); + swarm.listen_on(format!("/ip4/0.0.0.0/udp/{PORT}").parse().unwrap()).unwrap(); let (broadcast_send, mut broadcast_recv) = mpsc::unbounded_channel(); let (receive_send, receive_recv) = mpsc::unbounded_channel(); @@ -366,7 +366,7 @@ impl LibP2p { ))) => { for (peer, mut addr) in list { // Check the port is as expected to prevent trying to peer with Substrate nodes - if addr.pop() == Some(libp2p::multiaddr::Protocol::Tcp(PORT)) { + if addr.pop() == Some(libp2p::multiaddr::Protocol::Udp(PORT)) { log::info!("found peer via mdns"); swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer); } From 964284e7745403afb2f56fd010a52bbd65de4002 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sat, 25 Nov 2023 23:03:31 -0500 Subject: [PATCH 3/4] Add quic-v1 protocol ID to listen_on --- coordinator/src/p2p.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coordinator/src/p2p.rs b/coordinator/src/p2p.rs index 80de3da5d..ab1928fc7 100644 --- a/coordinator/src/p2p.rs +++ b/coordinator/src/p2p.rs @@ -279,7 +279,7 @@ impl LibP2p { .unwrap() .build(); const PORT: u16 = 30563; // 5132 ^ (('c' << 8) | 'o') - swarm.listen_on(format!("/ip4/0.0.0.0/udp/{PORT}").parse().unwrap()).unwrap(); + swarm.listen_on(format!("/ip4/0.0.0.0/udp/{PORT}/quic-v1").parse().unwrap()).unwrap(); let (broadcast_send, mut broadcast_recv) = mpsc::unbounded_channel(); let (receive_send, receive_recv) = mpsc::unbounded_channel(); From 01a2c98018167cebc09b3f44412bae73a65ce007 Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Sun, 26 Nov 2023 02:56:20 -0500 Subject: [PATCH 4/4] Correct mdns peer filtering from Udp to Quic Also adds commented with_quic_config. --- coordinator/src/p2p.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/coordinator/src/p2p.rs b/coordinator/src/p2p.rs index ab1928fc7..3b46ba854 100644 --- a/coordinator/src/p2p.rs +++ b/coordinator/src/p2p.rs @@ -275,6 +275,17 @@ impl LibP2p { let mut swarm = SwarmBuilder::with_existing_identity(throwaway_key_pair) .with_tokio() .with_quic() + /* + .with_quic_config(|mut config| { + config.max_idle_timeout = 85; + // We send KeepAlive after 80, so this isn't needed + config.keep_alive_interval = Duration::from_secs(config.max_idle_timeout.into() + 1); + // 1 MiB + max message size + config.max_stream_data = (1024 * 1024) + u32::try_from(MAX_LIBP2P_MESSAGE_SIZE).unwrap(); + // Support 10 maxed out streams + config.max_connection_data = 10 * config.max_stream_data; + }) + */ .with_behaviour(|_| behavior) .unwrap() .build(); @@ -365,8 +376,8 @@ impl LibP2p { libp2p::mdns::Event::Discovered(list), ))) => { for (peer, mut addr) in list { - // Check the port is as expected to prevent trying to peer with Substrate nodes - if addr.pop() == Some(libp2p::multiaddr::Protocol::Udp(PORT)) { + // Only peer with Quic, effectively preventing connecting to Substrate nodes + if addr.pop() == Some(libp2p::multiaddr::Protocol::QuicV1) { log::info!("found peer via mdns"); swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer); }