Skip to content

feat(relay): implement autorelay - #6156

Open
dariusc93 wants to merge 64 commits into
libp2p:masterfrom
dariusc93:feat/autorelay
Open

feat(relay): implement autorelay#6156
dariusc93 wants to merge 64 commits into
libp2p:masterfrom
dariusc93:feat/autorelay

Conversation

@dariusc93

@dariusc93 dariusc93 commented Sep 2, 2025

Copy link
Copy Markdown
Member

Description

This PR implements a basic autorelay system that would automatically make reservation as soon as it is discovered that the connected node supports HOP protocol.

resolves #4651.

Notes & open questions

Currently, this PR is a PoC, but ideally would be the initial foundation for autorelay implementation. Currently, we will only support making reservation as we discover them, but ideally, we should implement events to notify swarm about no relays available, which would allow the node to perform its own discovery of relays (ie walking DHT, etc), as well as supporting static relays that would have priority or be used in the event of no relays being discovered, although it is all up for discussion to determine what is needed vs wants.

Change checklist

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

Comment thread protocols/relay/CHANGELOG.md Outdated
Comment thread protocols/relay/src/autorelay/handler.rs Outdated
@dariusc93
dariusc93 requested review from elenaf9 and jxs May 17, 2026 00:04
Comment thread examples/relay-client/src/main.rs
Comment thread protocols/relay/src/autorelay.rs Outdated
Comment thread protocols/relay/src/autorelay.rs Outdated
Comment thread protocols/relay/src/autorelay.rs Outdated
return;
}

if let Some((peer_id, connection_id)) = self.reservations.get(&listener_id).copied()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should also store any new reservation that been established outside of auto relay. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you use auto relay with relay in parallel? What would be the advantage?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should run along side the relay client and not built in, keeping logic separate (and also following the original idea of having a separate module).

Comment thread protocols/relay/CHANGELOG.md Outdated
@dariusc93
dariusc93 marked this pull request as ready for review June 4, 2026 13:19

@jxs jxs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this Darius, overall looks good to me! Left some comments

Comment thread protocols/relay/src/autorelay/handler.rs Outdated
Comment thread protocols/relay/src/autorelay.rs
}
}

fn is_peer_idle(&self, peer_id: &PeerId) -> bool {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this and subsequent methods that take the PeerId as input parameter, return Option<> on the return signatures for when the peer doesn't exist in the connections?

.any(|((pid, _), info)| pid == peer_id && !info.address.is_relayed())
}

fn evict_for_static_peer(&mut self, new_static: PeerId) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return a bool for then the eviction was successful?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that im against it, but would it make sense to return a bool if the callers dont check its return? We could definitely use some logging though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah indeed, let's go for logging then. Thanks Darius

Comment thread protocols/relay/src/autorelay.rs Outdated
use libp2p_core::{Multiaddr, multiaddr::Protocol};
use libp2p_identity::PeerId;

pub(crate) trait MultiaddrExt {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this trait is only implemented on Multiaddr there's no need for polymorphism, why not a function that takes Multiaddr?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the additional member of the trait, i was just reusing the trait there and expanding on it (since it does make sense to a degree), but I can move it to a free function instead that takes Multiaddr, but for the other trait member, that might be more suited for a separate PR (just to keep things consistent and reduce touching additional parts of the code).

return;
}

if let Some((peer_id, connection_id)) = self.reservations.get(&listener_id).copied()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you use auto relay with relay in parallel? What would be the advantage?

@dariusc93
dariusc93 requested a review from jxs July 1, 2026 15:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

auto-relay: automatically make reservations on relays as we discover them

2 participants