diff --git a/.github/workflows/release-dcutr-example.yml b/.github/workflows/release-dcutr-example.yml index 0d3c588..ba146bd 100644 --- a/.github/workflows/release-dcutr-example.yml +++ b/.github/workflows/release-dcutr-example.yml @@ -18,8 +18,7 @@ jobs: - name: Get version id: get_version - working-directory: examples/dcutr - run: echo "version=dcutr-example-$(cargo read-manifest | jq -r '.version')" >> $GITHUB_OUTPUT + run: echo "version=dcutr-example-$(cargo read-manifest --manifest-path examples/dcutr/Cargo.toml | jq -r '.version')" >> $GITHUB_OUTPUT - name: Check if release exists id: check_release @@ -44,20 +43,19 @@ jobs: - name: Checkout code uses: actions/checkout@v4 - - name: Install cross - working-directory: examples/dcutr - run: cargo install cross --version 0.2.5 + - name: Setup rust toolchain + run: rustup toolchain install stable --profile minimal + + - name: Setup rust cache + uses: Swatinem/rust-cache@v2 - name: Build for Intel Linux - working-directory: examples/dcutr - run: cargo build --release --target=x86_64-unknown-linux-gnu + run: cargo build -p dcutr-example --release --target=x86_64-unknown-linux-gnu - name: Build for Aarch Linux - working-directory: examples/dcutr - run: cross build --release --target=aarch64-unknown-linux-gnu + run: cross build -p dcutr-example --release --target=aarch64-unknown-linux-gnu - name: Create artifacts directory - working-directory: examples/dcutr run: | mkdir -p artifacts cp target/x86_64-unknown-linux-gnu/release/dcutr-example artifacts/dcutr-example-x86_64-unknown-linux @@ -68,6 +66,6 @@ jobs: with: tag_name: ${{ needs.metadata.outputs.version }} files: | - examples/dcutr/artifacts/* + artifacts/* env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/Cargo.lock b/Cargo.lock index deca40d..7c90fef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -587,8 +587,6 @@ dependencies = [ "camino", "clap", "eyre", - "futures", - "futures-timer", "libp2p", "multiaddr", "serde", diff --git a/examples/dcutr/Cargo.toml b/examples/dcutr/Cargo.toml index 8b93359..f8e4295 100644 --- a/examples/dcutr/Cargo.toml +++ b/examples/dcutr/Cargo.toml @@ -10,8 +10,6 @@ license = "MIT OR Apache-2.0" camino = "1.1.6" clap = { version = "4.5.4", features = ["derive", "env"] } eyre = "0.6.12" -futures = "0.3.30" -futures-timer = "3.0" libp2p = { version = "0.53.2", features = [ "dcutr", "dns", diff --git a/examples/dcutr/src/main.rs b/examples/dcutr/src/main.rs index 9636915..4768c61 100644 --- a/examples/dcutr/src/main.rs +++ b/examples/dcutr/src/main.rs @@ -2,7 +2,7 @@ use std::str::FromStr; use std::{error::Error, time::Duration}; use clap::Parser; -use futures::{executor::block_on, future::FutureExt, stream::StreamExt}; +use libp2p::futures::prelude::*; use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; use libp2p::{dcutr, identify, identity, noise, ping, relay, yamux, Multiaddr, PeerId}; use multiaddr::Protocol; @@ -101,60 +101,54 @@ async fn main() -> Result<(), Box> { .unwrap(); // Wait to listen on all interfaces. - block_on(async { - let mut delay = futures_timer::Delay::new(std::time::Duration::from_secs(1)).fuse(); - loop { - futures::select! { - event = swarm.next() => { - match event.unwrap() { - SwarmEvent::NewListenAddr { address, .. } => { - info!(%address, "Listening on address"); - } - event => panic!("{event:?}"), + loop { + tokio::select! { + Some(event) = swarm.next() => { + match event { + SwarmEvent::NewListenAddr { address, .. } => { + info!(%address, "Listening on address"); } + event => panic!("{event:?}"), } - _ = delay => { - // Likely listening on all interfaces now, thus continuing by breaking the loop. - break; - } + } + _ = tokio::time::sleep(Duration::from_secs(1)) => { + // Likely listening on all interfaces now, thus continuing by breaking the loop. + break; } } - }); + } // Connect to the relay server. Not for the reservation or relayed connection, but to (a) learn // our local public address and (b) enable a freshly started relay to learn its public address. swarm.dial(opt.relay_address.clone()).unwrap(); - block_on(async { - let mut learned_observed_addr = false; - let mut told_relay_observed_addr = false; - - loop { - match swarm.next().await.unwrap() { - SwarmEvent::NewListenAddr { .. } => {} - SwarmEvent::Dialing { .. } => {} - SwarmEvent::ConnectionEstablished { .. } => {} - SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {} - SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent { - .. - })) => { - info!("Told relay its public address"); - told_relay_observed_addr = true; - } - SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received { - info: identify::Info { observed_addr, .. }, - .. - })) => { - info!(address=%observed_addr, "Relay told us our observed address"); - learned_observed_addr = true; - } - event => panic!("{event:?}"), - } - if learned_observed_addr && told_relay_observed_addr { - break; + let mut learned_observed_addr = false; + let mut told_relay_observed_addr = false; + + loop { + match swarm.next().await.unwrap() { + SwarmEvent::NewListenAddr { .. } => {} + SwarmEvent::Dialing { .. } => {} + SwarmEvent::ConnectionEstablished { .. } => {} + SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {} + SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent { .. })) => { + info!("Told relay its public address"); + told_relay_observed_addr = true; + } + SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received { + info: identify::Info { observed_addr, .. }, + .. + })) => { + info!(address=%observed_addr, "Relay told us our observed address"); + learned_observed_addr = true; } + event => panic!("{event:?}"), } - }); + + if learned_observed_addr && told_relay_observed_addr { + break; + } + } match opt.mode { Mode::Dial => { @@ -173,63 +167,61 @@ async fn main() -> Result<(), Box> { } } - block_on(async { - let mut stdin = tokio::io::BufReader::new(tokio::io::stdin()).lines(); - - loop { - let event = tokio::select! { - Some(event) = swarm.next() => event, - Ok(Some(line)) = stdin.next_line() => { - match line.trim() { - "peers" => { - info!("Connected peers: {}", swarm.network_info().num_peers()); - for peer in swarm.connected_peers() { - info!(peer=%peer, "Connected peer"); - } + let mut stdin = tokio::io::BufReader::new(tokio::io::stdin()).lines(); + + loop { + let event = tokio::select! { + Some(event) = swarm.next() => event, + Ok(Some(line)) = stdin.next_line() => { + match line.trim() { + "peers" => { + info!("Connected peers: {}", swarm.network_info().num_peers()); + for peer in swarm.connected_peers() { + info!(peer=%peer, "Connected peer"); } - _ => info!("Unknown command"), } - continue; + _ => info!("Unknown command"), } - }; + continue; + } + }; - match event { - SwarmEvent::NewListenAddr { address, .. } => { - info!(%address, "Listening on address"); - } - SwarmEvent::Behaviour(BehaviourEvent::RelayClient( - relay::client::Event::ReservationReqAccepted { .. }, - )) => { - assert!(opt.mode == Mode::Listen); - info!("Relay accepted our reservation request"); - } - SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => { - info!(?event, "\x1b[33mrelay\x1b[0m"); - } - SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => { - info!(?event, "\x1b[32mdcutr\x1b[0m"); - } - SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => { - info!(?event, "\x1b[34midentify\x1b[0m"); - } - SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {} - SwarmEvent::ConnectionEstablished { - peer_id, endpoint, .. - } => { - info!(peer=%peer_id, ?endpoint, "Established new connection"); - } - SwarmEvent::ConnectionClosed { - peer_id, endpoint, .. - } => { - info!(peer=%peer_id, ?endpoint, "Closed connection"); - } - SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => { - info!(peer=?peer_id, "Outgoing connection failed: {error}"); - } - _ => {} + match event { + SwarmEvent::NewListenAddr { address, .. } => { + info!(%address, "Listening on address"); + } + SwarmEvent::Behaviour(BehaviourEvent::RelayClient( + relay::client::Event::ReservationReqAccepted { .. }, + )) => { + assert!(opt.mode == Mode::Listen); + info!("Relay accepted our reservation request"); } + SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => { + info!(?event, "\x1b[33mrelay\x1b[0m"); + } + SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => { + info!(?event, "\x1b[32mdcutr\x1b[0m"); + } + SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => { + info!(?event, "\x1b[34midentify\x1b[0m"); + } + SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {} + SwarmEvent::ConnectionEstablished { + peer_id, endpoint, .. + } => { + info!(peer=%peer_id, ?endpoint, "Established new connection"); + } + SwarmEvent::ConnectionClosed { + peer_id, endpoint, .. + } => { + info!(peer=%peer_id, ?endpoint, "Closed connection"); + } + SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => { + info!(peer=?peer_id, "Outgoing connection failed: {error}"); + } + _ => {} } - }) + } } fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {