-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(relay): implement autorelay #6156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dariusc93
wants to merge
64
commits into
libp2p:master
Choose a base branch
from
dariusc93:feat/autorelay
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 b1c1cdc
chore: update cargo.toml and add changelog entry
dariusc93 cb2fbab
chore: rename function
dariusc93 40a0582
chore: remofe let/else condition
dariusc93 eb7a6c7
chore: remove redundant trait members
dariusc93 60b6931
chore: remove unused imports
dariusc93 8ed974e
chore: mark status accordingly when listener errors, closes or expires.
dariusc93 ed64813
chore: fmt
dariusc93 bc76458
chore: fmt
dariusc93 bb02c38
chore: remove pending target
dariusc93 c0e50c0
chore: remove pending target and add functions to check relay status …
dariusc93 dbbe11e
Merge branch 'master' into feat/autorelay
dariusc93 709a6f6
Update protocols/relay/CHANGELOG.md
dariusc93 d81bc41
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 3a89490
Merge branch 'master' into feat/autorelay
dariusc93 43f37f8
chore: overhaul existing code; redo reservation checks
dariusc93 1c6091f
chore: add example
dariusc93 414222c
chore: misc changes
dariusc93 94a8dcd
chore: fmt
dariusc93 f0bdcc0
chore: add a backoff to prevent using the relay for a specific durati…
dariusc93 1f83b52
chore: fmt
dariusc93 b9912e4
Merge branch 'master' into feat/autorelay
dariusc93 0554051
chore: fmt
dariusc93 adab099
chore: update changeling
dariusc93 0ce7695
chore: add function to enable/disable autorelay
dariusc93 4163c10
chore: add support for static relays and add status change event
dariusc93 d721741
chore: notify swarm about relay availability
dariusc93 d2a2719
chore: perform a check on empty external addr events
dariusc93 2ae8654
chore: allow overriding autorelay with status
dariusc93 e2feef5
Merge branch 'master' into feat/autorelay
dariusc93 31be754
chore: cleanup
dariusc93 f384982
chore: record previous successful relays
dariusc93 252b2f5
chore: add backoff for static relay
dariusc93 e436da5
chore: add backoff on failure
dariusc93 05f55d9
chore: update test
dariusc93 e3aa8a5
chore: cleanup
dariusc93 4a22b25
chore: prioritize static relays
dariusc93 efa2768
chore: cleanup test
dariusc93 0b9109a
chore: misc cleanup and linting
dariusc93 79c88f7
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 0f3545b
chore: update example
dariusc93 3f127a4
Merge branch 'master' into feat/autorelay
dariusc93 081806b
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 bdc57d5
Merge branch 'master' into feat/autorelay
dariusc93 710cb13
Merge branch 'master' into feat/autorelay
dariusc93 8906802
chore: support multiple addresses for a static peer
dariusc93 db97c82
Merge remote-tracking branch 'origin/feat/autorelay' into feat/autorelay
dariusc93 aa31f50
chore: use `ConnectionEvent::RemoteProtocolChange` only
dariusc93 f984fb3
chore: mark peer connection as not supported if peer id is diff
dariusc93 3d73abb
chore: return early if connection doesnt exist
dariusc93 2615a51
chore: cleanup failures
dariusc93 81c9889
chore: determinate candidates selection through lowest connection per…
dariusc93 2252585
chore: use web_time::Instant
dariusc93 bbc8412
chore: use SystemTime for previous relays
dariusc93 b843545
chore: fmt
dariusc93 6884c65
chore: remove allow(deprecated)
dariusc93 b778f2e
chore: perform reservation cool down against peer instead of connection
dariusc93 5b7509c
chore: add logging
dariusc93 cbcfe37
chore: switch relay_peer_id from trait member to fn
dariusc93 0c1a579
chore: fmt
dariusc93 ca2817d
chore: fmt
dariusc93 afdc276
chore: linting
dariusc93 0c1e399
Merge branch 'master' into feat/autorelay
dariusc93 9a4e7fe
Merge branch 'master' into feat/autorelay
dariusc93 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| use std::{error::Error, num::NonZeroU8}; | ||
|
|
||
| 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(), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.