From 369b4a400deb50c542dab37dd36a793b7ed13689 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Mon, 9 Sep 2024 22:48:49 -0600 Subject: [PATCH 1/3] refactor: use tokio instead of async-std in the relay-server example --- Cargo.lock | 3 +-- examples/relay-server/Cargo.toml | 5 ++--- examples/relay-server/src/main.rs | 7 ++++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c12e6fb984..87d81035471 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4970,11 +4970,10 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" name = "relay-server-example" version = "0.1.0" dependencies = [ - "async-std", - "async-trait", "clap", "futures", "libp2p", + "tokio", "tracing", "tracing-subscriber", ] diff --git a/examples/relay-server/Cargo.toml b/examples/relay-server/Cargo.toml index 12d3e2467ce..7385cf6c033 100644 --- a/examples/relay-server/Cargo.toml +++ b/examples/relay-server/Cargo.toml @@ -10,10 +10,9 @@ release = false [dependencies] clap = { version = "4.5.6", features = ["derive"] } -async-std = { version = "1.12", features = ["attributes"] } -async-trait = "0.1" +tokio = { version = "1.37.0", features = ["full"] } futures = { workspace = true } -libp2p = { path = "../../libp2p", features = [ "async-std", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] } +libp2p = { path = "../../libp2p", features = ["tokio", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay", "quic"] } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter"] } diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index bf5817454f8..4f3ad0c8e11 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -23,7 +23,7 @@ use clap::Parser; use futures::executor::block_on; -use futures::stream::StreamExt; +use futures::StreamExt; use libp2p::{ core::multiaddr::Protocol, core::Multiaddr, @@ -35,7 +35,8 @@ use std::error::Error; use std::net::{Ipv4Addr, Ipv6Addr}; use tracing_subscriber::EnvFilter; -fn main() -> Result<(), Box> { +#[tokio::main] +async fn main() -> Result<(), Box> { let _ = tracing_subscriber::fmt() .with_env_filter(EnvFilter::from_default_env()) .try_init(); @@ -46,7 +47,7 @@ fn main() -> Result<(), Box> { let local_key: identity::Keypair = generate_ed25519(opt.secret_key_seed); let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_key) - .with_async_std() + .with_tokio() .with_tcp( tcp::Config::default(), noise::Config::new, From 2a57702255953b1282809d2b12a1b7f73638fd76 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Thu, 12 Sep 2024 14:06:51 -0600 Subject: [PATCH 2/3] remove the block_on from futures::executor --- examples/relay-server/src/main.rs | 37 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index 4f3ad0c8e11..2fc3e765cc9 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -22,7 +22,6 @@ #![doc = include_str!("../README.md")] use clap::Parser; -use futures::executor::block_on; use futures::StreamExt; use libp2p::{ core::multiaddr::Protocol, @@ -82,27 +81,27 @@ async fn main() -> Result<(), Box> { .with(Protocol::QuicV1); swarm.listen_on(listen_addr_quic)?; - block_on(async { - loop { - match swarm.next().await.expect("Infinite Stream.") { - SwarmEvent::Behaviour(event) => { - if let BehaviourEvent::Identify(identify::Event::Received { - info: identify::Info { observed_addr, .. }, - .. - }) = &event - { - swarm.add_external_address(observed_addr.clone()); - } - - println!("{event:?}") - } - SwarmEvent::NewListenAddr { address, .. } => { - println!("Listening on {address:?}"); + + loop { + match swarm.next().await.expect("Infinite Stream.") { + SwarmEvent::Behaviour(event) => { + if let BehaviourEvent::Identify(identify::Event::Received { + info: identify::Info { observed_addr, .. }, + .. + }) = &event + { + swarm.add_external_address(observed_addr.clone()); } - _ => {} + + println!("{event:?}") } + SwarmEvent::NewListenAddr { address, .. } => { + println!("Listening on {address:?}"); + } + _ => {} } - }) + } + } #[derive(NetworkBehaviour)] From 503af7ba64a80d5bb1c41a16c2813f41f9ea5697 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Wed, 18 Sep 2024 22:52:13 -0600 Subject: [PATCH 3/3] adding format --- examples/relay-server/src/main.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/relay-server/src/main.rs b/examples/relay-server/src/main.rs index 2fc3e765cc9..46a122d0717 100644 --- a/examples/relay-server/src/main.rs +++ b/examples/relay-server/src/main.rs @@ -81,7 +81,6 @@ async fn main() -> Result<(), Box> { .with(Protocol::QuicV1); swarm.listen_on(listen_addr_quic)?; - loop { match swarm.next().await.expect("Infinite Stream.") { SwarmEvent::Behaviour(event) => { @@ -101,7 +100,6 @@ async fn main() -> Result<(), Box> { _ => {} } } - } #[derive(NetworkBehaviour)]