Skip to content

Commit

Permalink
chore: cleanup futures, adjust release workflow for the rust workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
fbozic committed May 9, 2024
1 parent 378ca10 commit 1cbf9ec
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 110 deletions.
20 changes: 9 additions & 11 deletions .github/workflows/release-dcutr-example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -68,6 +66,6 @@ jobs:
with:
tag_name: ${{ needs.metadata.outputs.version }}
files: |
examples/dcutr/artifacts/*
artifacts/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions examples/dcutr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
182 changes: 87 additions & 95 deletions examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -101,60 +101,54 @@ async fn main() -> Result<(), Box<dyn Error>> {
.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 => {
Expand All @@ -173,63 +167,61 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}

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 {
Expand Down

0 comments on commit 1cbf9ec

Please sign in to comment.