Skip to content

Commit 1cbf9ec

Browse files
committed
chore: cleanup futures, adjust release workflow for the rust workspace
1 parent 378ca10 commit 1cbf9ec

File tree

4 files changed

+96
-110
lines changed

4 files changed

+96
-110
lines changed

.github/workflows/release-dcutr-example.yml

+9-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ jobs:
1818

1919
- name: Get version
2020
id: get_version
21-
working-directory: examples/dcutr
22-
run: echo "version=dcutr-example-$(cargo read-manifest | jq -r '.version')" >> $GITHUB_OUTPUT
21+
run: echo "version=dcutr-example-$(cargo read-manifest --manifest-path examples/dcutr/Cargo.toml | jq -r '.version')" >> $GITHUB_OUTPUT
2322

2423
- name: Check if release exists
2524
id: check_release
@@ -44,20 +43,19 @@ jobs:
4443
- name: Checkout code
4544
uses: actions/checkout@v4
4645

47-
- name: Install cross
48-
working-directory: examples/dcutr
49-
run: cargo install cross --version 0.2.5
46+
- name: Setup rust toolchain
47+
run: rustup toolchain install stable --profile minimal
48+
49+
- name: Setup rust cache
50+
uses: Swatinem/rust-cache@v2
5051

5152
- name: Build for Intel Linux
52-
working-directory: examples/dcutr
53-
run: cargo build --release --target=x86_64-unknown-linux-gnu
53+
run: cargo build -p dcutr-example --release --target=x86_64-unknown-linux-gnu
5454

5555
- name: Build for Aarch Linux
56-
working-directory: examples/dcutr
57-
run: cross build --release --target=aarch64-unknown-linux-gnu
56+
run: cross build -p dcutr-example --release --target=aarch64-unknown-linux-gnu
5857

5958
- name: Create artifacts directory
60-
working-directory: examples/dcutr
6159
run: |
6260
mkdir -p artifacts
6361
cp target/x86_64-unknown-linux-gnu/release/dcutr-example artifacts/dcutr-example-x86_64-unknown-linux
@@ -68,6 +66,6 @@ jobs:
6866
with:
6967
tag_name: ${{ needs.metadata.outputs.version }}
7068
files: |
71-
examples/dcutr/artifacts/*
69+
artifacts/*
7270
env:
7371
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Cargo.lock

-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/dcutr/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ license = "MIT OR Apache-2.0"
1010
camino = "1.1.6"
1111
clap = { version = "4.5.4", features = ["derive", "env"] }
1212
eyre = "0.6.12"
13-
futures = "0.3.30"
14-
futures-timer = "3.0"
1513
libp2p = { version = "0.53.2", features = [
1614
"dcutr",
1715
"dns",

examples/dcutr/src/main.rs

+87-95
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::str::FromStr;
22
use std::{error::Error, time::Duration};
33

44
use clap::Parser;
5-
use futures::{executor::block_on, future::FutureExt, stream::StreamExt};
5+
use libp2p::futures::prelude::*;
66
use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
77
use libp2p::{dcutr, identify, identity, noise, ping, relay, yamux, Multiaddr, PeerId};
88
use multiaddr::Protocol;
@@ -101,60 +101,54 @@ async fn main() -> Result<(), Box<dyn Error>> {
101101
.unwrap();
102102

103103
// Wait to listen on all interfaces.
104-
block_on(async {
105-
let mut delay = futures_timer::Delay::new(std::time::Duration::from_secs(1)).fuse();
106-
loop {
107-
futures::select! {
108-
event = swarm.next() => {
109-
match event.unwrap() {
110-
SwarmEvent::NewListenAddr { address, .. } => {
111-
info!(%address, "Listening on address");
112-
}
113-
event => panic!("{event:?}"),
104+
loop {
105+
tokio::select! {
106+
Some(event) = swarm.next() => {
107+
match event {
108+
SwarmEvent::NewListenAddr { address, .. } => {
109+
info!(%address, "Listening on address");
114110
}
111+
event => panic!("{event:?}"),
115112
}
116-
_ = delay => {
117-
// Likely listening on all interfaces now, thus continuing by breaking the loop.
118-
break;
119-
}
113+
}
114+
_ = tokio::time::sleep(Duration::from_secs(1)) => {
115+
// Likely listening on all interfaces now, thus continuing by breaking the loop.
116+
break;
120117
}
121118
}
122-
});
119+
}
123120

124121
// Connect to the relay server. Not for the reservation or relayed connection, but to (a) learn
125122
// our local public address and (b) enable a freshly started relay to learn its public address.
126123
swarm.dial(opt.relay_address.clone()).unwrap();
127-
block_on(async {
128-
let mut learned_observed_addr = false;
129-
let mut told_relay_observed_addr = false;
130-
131-
loop {
132-
match swarm.next().await.unwrap() {
133-
SwarmEvent::NewListenAddr { .. } => {}
134-
SwarmEvent::Dialing { .. } => {}
135-
SwarmEvent::ConnectionEstablished { .. } => {}
136-
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
137-
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent {
138-
..
139-
})) => {
140-
info!("Told relay its public address");
141-
told_relay_observed_addr = true;
142-
}
143-
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
144-
info: identify::Info { observed_addr, .. },
145-
..
146-
})) => {
147-
info!(address=%observed_addr, "Relay told us our observed address");
148-
learned_observed_addr = true;
149-
}
150-
event => panic!("{event:?}"),
151-
}
152124

153-
if learned_observed_addr && told_relay_observed_addr {
154-
break;
125+
let mut learned_observed_addr = false;
126+
let mut told_relay_observed_addr = false;
127+
128+
loop {
129+
match swarm.next().await.unwrap() {
130+
SwarmEvent::NewListenAddr { .. } => {}
131+
SwarmEvent::Dialing { .. } => {}
132+
SwarmEvent::ConnectionEstablished { .. } => {}
133+
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
134+
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Sent { .. })) => {
135+
info!("Told relay its public address");
136+
told_relay_observed_addr = true;
137+
}
138+
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
139+
info: identify::Info { observed_addr, .. },
140+
..
141+
})) => {
142+
info!(address=%observed_addr, "Relay told us our observed address");
143+
learned_observed_addr = true;
155144
}
145+
event => panic!("{event:?}"),
156146
}
157-
});
147+
148+
if learned_observed_addr && told_relay_observed_addr {
149+
break;
150+
}
151+
}
158152

159153
match opt.mode {
160154
Mode::Dial => {
@@ -173,63 +167,61 @@ async fn main() -> Result<(), Box<dyn Error>> {
173167
}
174168
}
175169

176-
block_on(async {
177-
let mut stdin = tokio::io::BufReader::new(tokio::io::stdin()).lines();
178-
179-
loop {
180-
let event = tokio::select! {
181-
Some(event) = swarm.next() => event,
182-
Ok(Some(line)) = stdin.next_line() => {
183-
match line.trim() {
184-
"peers" => {
185-
info!("Connected peers: {}", swarm.network_info().num_peers());
186-
for peer in swarm.connected_peers() {
187-
info!(peer=%peer, "Connected peer");
188-
}
170+
let mut stdin = tokio::io::BufReader::new(tokio::io::stdin()).lines();
171+
172+
loop {
173+
let event = tokio::select! {
174+
Some(event) = swarm.next() => event,
175+
Ok(Some(line)) = stdin.next_line() => {
176+
match line.trim() {
177+
"peers" => {
178+
info!("Connected peers: {}", swarm.network_info().num_peers());
179+
for peer in swarm.connected_peers() {
180+
info!(peer=%peer, "Connected peer");
189181
}
190-
_ => info!("Unknown command"),
191182
}
192-
continue;
183+
_ => info!("Unknown command"),
193184
}
194-
};
185+
continue;
186+
}
187+
};
195188

196-
match event {
197-
SwarmEvent::NewListenAddr { address, .. } => {
198-
info!(%address, "Listening on address");
199-
}
200-
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
201-
relay::client::Event::ReservationReqAccepted { .. },
202-
)) => {
203-
assert!(opt.mode == Mode::Listen);
204-
info!("Relay accepted our reservation request");
205-
}
206-
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
207-
info!(?event, "\x1b[33mrelay\x1b[0m");
208-
}
209-
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
210-
info!(?event, "\x1b[32mdcutr\x1b[0m");
211-
}
212-
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
213-
info!(?event, "\x1b[34midentify\x1b[0m");
214-
}
215-
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
216-
SwarmEvent::ConnectionEstablished {
217-
peer_id, endpoint, ..
218-
} => {
219-
info!(peer=%peer_id, ?endpoint, "Established new connection");
220-
}
221-
SwarmEvent::ConnectionClosed {
222-
peer_id, endpoint, ..
223-
} => {
224-
info!(peer=%peer_id, ?endpoint, "Closed connection");
225-
}
226-
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
227-
info!(peer=?peer_id, "Outgoing connection failed: {error}");
228-
}
229-
_ => {}
189+
match event {
190+
SwarmEvent::NewListenAddr { address, .. } => {
191+
info!(%address, "Listening on address");
192+
}
193+
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
194+
relay::client::Event::ReservationReqAccepted { .. },
195+
)) => {
196+
assert!(opt.mode == Mode::Listen);
197+
info!("Relay accepted our reservation request");
230198
}
199+
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
200+
info!(?event, "\x1b[33mrelay\x1b[0m");
201+
}
202+
SwarmEvent::Behaviour(BehaviourEvent::Dcutr(event)) => {
203+
info!(?event, "\x1b[32mdcutr\x1b[0m");
204+
}
205+
SwarmEvent::Behaviour(BehaviourEvent::Identify(event)) => {
206+
info!(?event, "\x1b[34midentify\x1b[0m");
207+
}
208+
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
209+
SwarmEvent::ConnectionEstablished {
210+
peer_id, endpoint, ..
211+
} => {
212+
info!(peer=%peer_id, ?endpoint, "Established new connection");
213+
}
214+
SwarmEvent::ConnectionClosed {
215+
peer_id, endpoint, ..
216+
} => {
217+
info!(peer=%peer_id, ?endpoint, "Closed connection");
218+
}
219+
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
220+
info!(peer=?peer_id, "Outgoing connection failed: {error}");
221+
}
222+
_ => {}
231223
}
232-
})
224+
}
233225
}
234226

235227
fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {

0 commit comments

Comments
 (0)