Skip to content
Open
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
eb22c19
feat: implement autorelay behaviour
dariusc93 Sep 2, 2025
b1c1cdc
chore: update cargo.toml and add changelog entry
dariusc93 Sep 2, 2025
cb2fbab
chore: rename function
dariusc93 Sep 2, 2025
40a0582
chore: remofe let/else condition
dariusc93 Sep 2, 2025
eb7a6c7
chore: remove redundant trait members
dariusc93 Sep 2, 2025
60b6931
chore: remove unused imports
dariusc93 Sep 2, 2025
8ed974e
chore: mark status accordingly when listener errors, closes or expires.
dariusc93 Sep 2, 2025
ed64813
chore: fmt
dariusc93 Sep 2, 2025
bc76458
chore: fmt
dariusc93 Sep 2, 2025
bb02c38
chore: remove pending target
dariusc93 Sep 3, 2025
c0e50c0
chore: remove pending target and add functions to check relay status …
dariusc93 Sep 10, 2025
dbbe11e
Merge branch 'master' into feat/autorelay
dariusc93 Sep 11, 2025
709a6f6
Update protocols/relay/CHANGELOG.md
dariusc93 Sep 12, 2025
d81bc41
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 Sep 12, 2025
3a89490
Merge branch 'master' into feat/autorelay
dariusc93 May 11, 2026
43f37f8
chore: overhaul existing code; redo reservation checks
dariusc93 May 14, 2026
1c6091f
chore: add example
dariusc93 May 16, 2026
414222c
chore: misc changes
dariusc93 May 16, 2026
94a8dcd
chore: fmt
dariusc93 May 16, 2026
f0bdcc0
chore: add a backoff to prevent using the relay for a specific durati…
dariusc93 May 16, 2026
1f83b52
chore: fmt
dariusc93 May 16, 2026
b9912e4
Merge branch 'master' into feat/autorelay
dariusc93 May 16, 2026
0554051
chore: fmt
dariusc93 May 16, 2026
adab099
chore: update changeling
dariusc93 May 17, 2026
0ce7695
chore: add function to enable/disable autorelay
dariusc93 May 17, 2026
4163c10
chore: add support for static relays and add status change event
dariusc93 May 20, 2026
d721741
chore: notify swarm about relay availability
dariusc93 May 20, 2026
d2a2719
chore: perform a check on empty external addr events
dariusc93 May 20, 2026
2ae8654
chore: allow overriding autorelay with status
dariusc93 May 21, 2026
e2feef5
Merge branch 'master' into feat/autorelay
dariusc93 May 22, 2026
31be754
chore: cleanup
dariusc93 May 23, 2026
f384982
chore: record previous successful relays
dariusc93 May 23, 2026
252b2f5
chore: add backoff for static relay
dariusc93 May 24, 2026
e436da5
chore: add backoff on failure
dariusc93 May 24, 2026
05f55d9
chore: update test
dariusc93 May 24, 2026
e3aa8a5
chore: cleanup
dariusc93 May 25, 2026
4a22b25
chore: prioritize static relays
dariusc93 May 26, 2026
efa2768
chore: cleanup test
dariusc93 May 26, 2026
0b9109a
chore: misc cleanup and linting
dariusc93 May 26, 2026
79c88f7
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 May 26, 2026
0f3545b
chore: update example
dariusc93 May 26, 2026
3f127a4
Merge branch 'master' into feat/autorelay
dariusc93 May 26, 2026
081806b
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 May 26, 2026
bdc57d5
Merge branch 'master' into feat/autorelay
dariusc93 May 27, 2026
710cb13
Merge branch 'master' into feat/autorelay
dariusc93 May 30, 2026
8906802
chore: support multiple addresses for a static peer
dariusc93 May 31, 2026
db97c82
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 May 31, 2026
aa31f50
chore: use `ConnectionEvent::RemoteProtocolChange` only
dariusc93 Jun 4, 2026
f984fb3
chore: mark peer connection as not supported if peer id is diff
dariusc93 Jun 4, 2026
3d73abb
chore: return early if connection doesnt exist
dariusc93 Jun 4, 2026
2615a51
chore: cleanup failures
dariusc93 Jun 4, 2026
81c9889
chore: determinate candidates selection through lowest connection per…
dariusc93 Jun 4, 2026
2252585
chore: use web_time::Instant
dariusc93 Jun 5, 2026
bbc8412
chore: use SystemTime for previous relays
dariusc93 Jun 5, 2026
b843545
chore: fmt
dariusc93 Jun 9, 2026
6884c65
chore: remove allow(deprecated)
dariusc93 Jun 15, 2026
b778f2e
chore: perform reservation cool down against peer instead of connection
dariusc93 Jun 15, 2026
5b7509c
chore: add logging
dariusc93 Jun 19, 2026
cbcfe37
chore: switch relay_peer_id from trait member to fn
dariusc93 Jun 19, 2026
0c1a579
chore: fmt
dariusc93 Jun 19, 2026
ca2817d
chore: fmt
dariusc93 Jun 19, 2026
afdc276
chore: linting
dariusc93 Jun 19, 2026
0c1e399
Merge branch 'master' into feat/autorelay
dariusc93 Jul 30, 2026
9a4e7fe
Merge branch 'master' into feat/autorelay
dariusc93 Sep 3, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"examples/ipfs-private",
"examples/metrics",
"examples/ping",
"examples/relay-client",
"examples/relay-server",
"examples/rendezvous",
"examples/stream",
Expand Down
20 changes: 20 additions & 0 deletions examples/relay-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "relay-client-example"
version = "0.1.0"
edition.workspace = true
publish = false
license = "MIT"

[package.metadata.release]
release = false

[dependencies]
clap = { version = "4.6.1", features = ["derive"] }
futures = { workspace = true }
libp2p = { path = "../../libp2p", features = ["dns", "identify", "macros", "noise", "ping", "quic", "relay", "tcp", "tokio", "yamux"] }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "signal"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter"] }

[lints]
workspace = true
17 changes: 17 additions & 0 deletions examples/relay-client/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Description

A small relay client that demonstrates the autorelay.

## Run the client

In another terminal, pointing it at the relay:

```
cargo run \
--secret-key-seed 1 \
--relay /ip4/$RELAY_IP/tcp/$PORT/p2p/$RELAY_PEERID \
--max-reservations 2
```

Provide `relay` multiple times to point at additional relays; autorelay will
pick among them up to `max-reservations`.
144 changes: 144 additions & 0 deletions examples/relay-client/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
use std::{error::Error, num::NonZeroU8};
Comment thread
dariusc93 marked this conversation as resolved.

use clap::Parser;
use futures::stream::StreamExt;
use libp2p::{
core::multiaddr::Multiaddr,
identify,
identity::Keypair,
noise, ping,
relay::{self, autorelay},
swarm::{NetworkBehaviour, SwarmEvent},
tcp, yamux,
};
use tracing_subscriber::EnvFilter;

#[derive(Debug, Parser)]
#[command(name = "libp2p relay client")]
struct Opts {
/// Fixed value used to derive a deterministic peer id.
#[arg(long)]
secret_key_seed: Option<u8>,

/// List of relay addresses
#[arg(long = "relay", required = true)]
relays: Vec<Multiaddr>,

/// Maximum number of relay reservations autorelay should maintain.
#[arg(long, default_value_t = 2)]
max_reservations: u8,
}

#[derive(NetworkBehaviour)]
struct Behaviour {
relay_client: relay::client::Behaviour,
autorelay: autorelay::Behaviour,
identify: identify::Behaviour,
ping: ping::Behaviour,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.try_init();

let opts = Opts::parse();

let max_reservations = NonZeroU8::new(opts.max_reservations)
.ok_or("--max-reservations must be greater than zero")?;
let autorelay_config = autorelay::Config::default().set_max_reservations(max_reservations);

let mut swarm =
libp2p::SwarmBuilder::with_existing_identity(generate_ed25519(opts.secret_key_seed))
.with_tokio()
.with_tcp(
tcp::Config::default().nodelay(true),
noise::Config::new,
yamux::Config::default,
)?
.with_quic()
.with_dns()?
.with_relay_client(noise::Config::new, yamux::Config::default)?
.with_behaviour(|keypair, relay_behaviour| Behaviour {
relay_client: relay_behaviour,
autorelay: autorelay::Behaviour::new_with_config(autorelay_config),
identify: identify::Behaviour::new(identify::Config::new(
"/autorelay-example/0.1.0".to_owned(),
keypair.public(),
)),
ping: ping::Behaviour::new(ping::Config::new()),
})?
.build();

swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
swarm.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse()?)?;

let local_peer_id = *swarm.local_peer_id();
tracing::info!(%local_peer_id, "Local peer id");

for addr in &opts.relays {
tracing::info!(%addr, "Dialing relay");
swarm.dial(addr.clone())?;
}

loop {
tokio::select! {
event = swarm.select_next_some() => match event {
SwarmEvent::NewListenAddr { address, .. } => {
tracing::info!(%address, "Listening");
}
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
tracing::info!(peer=%peer_id, address=%endpoint.get_remote_address(), "Connected");
}
SwarmEvent::ConnectionClosed { peer_id, .. } => {
tracing::info!(peer=%peer_id, "Disconnected");
}
SwarmEvent::ExternalAddrConfirmed { address } => {
tracing::info!(%address, "External address confirmed");
}
SwarmEvent::ExternalAddrExpired { address } => {
tracing::info!(%address, "External address expired");
}
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(
relay::client::Event::ReservationReqAccepted {
relay_peer_id, renewal, ..
},
)) => {
if renewal {
tracing::info!(%relay_peer_id, "Reservation renewed");
} else {
tracing::info!(%relay_peer_id, "Reservation accepted");
}
}
SwarmEvent::Behaviour(BehaviourEvent::RelayClient(event)) => {
tracing::debug!(?event, "Relay client event");
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify::Event::Received {
peer_id, info, ..
})) => {
tracing::debug!(peer=%peer_id, protocols=?info.protocols, "Identify received");
}
SwarmEvent::Behaviour(BehaviourEvent::Identify(_)) => {}
SwarmEvent::Behaviour(BehaviourEvent::Ping(_)) => {}
SwarmEvent::Behaviour(BehaviourEvent::Autorelay(e)) => {
tracing::debug!(?e, "Autorelay event");
}
_ => {}
},
}
}
}

fn generate_ed25519(secret_key_seed: Option<u8>) -> Keypair {
match secret_key_seed {
Some(secret_key_seed) => {
let mut bytes = [0u8; 32];
bytes[0] = secret_key_seed;
Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length")
}
None => Keypair::generate_ed25519(),
}
}
2 changes: 2 additions & 0 deletions protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
See [PR 6154](https://github.com/libp2p/rust-libp2p/pull/6154).
- Expire external address when a relay listener is closed without a replacement reservation.
See [PR 6285](https://github.com/libp2p/rust-libp2p/pull/6285).
- Implements autorelay that would make a reservation as soon as a connection reports supporting HOP protocol.
See [PR 6156](https://github.com/libp2p/rust-libp2p/pull/6156)

## 0.21.1
- reduce allocations by replacing `get_or_insert` with `get_or_insert_with`
Expand Down
1 change: 1 addition & 0 deletions protocols/relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ thiserror = { workspace = true }
tracing = { workspace = true }

[dev-dependencies]
libp2p-identify = { workspace = true }
libp2p-identity = { workspace = true, features = ["rand"] }
libp2p-ping = { workspace = true }
libp2p-plaintext = { workspace = true }
Expand Down
Loading
Loading