Skip to content

Commit bd5e4fa

Browse files
authored
feat(iroh-net)!: Allow using a NodeId directly in connect. (#2774)
## Description Allow using anything can be converted to a NodeAddr when connecting. This means that we can directly connect with NodeIds, and connect_by_node_id is no longer needed. Also deprecate connect_by_node_id since it is no longer needed. ## Breaking Changes - iroh-net: Endpoint::connect now takes an `impl Into<NodeAddr>` instead of a `NodeAddr` - iroh-net: Endpoint::connect_by_node_id is deprecated (to be removed next release) ## Notes & open questions Note: Doing this means that the instrumentation of connect no longer contains the remote, which is bad I guess. Not sure how to work around this. One way would be to have connect and connect_impl - connect_impl is like the current connect and can be instrumented as before. Any other way to do this? Note: since there is a From<NodeTicket> for NodeAddr, you can also directly dial using a ticket, which is kinda neat. ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [ ] Tests if relevant. - [x] All breaking changes documented.
1 parent 73ca58a commit bd5e4fa

File tree

5 files changed

+31
-20
lines changed

5 files changed

+31
-20
lines changed

iroh-net/examples/dht_discovery.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,7 @@ async fn chat_client(args: Args) -> anyhow::Result<()> {
118118
.bind()
119119
.await?;
120120
println!("We are {} and connecting to {}", node_id, remote_node_id);
121-
let connection = endpoint
122-
.connect_by_node_id(remote_node_id, CHAT_ALPN)
123-
.await?;
121+
let connection = endpoint.connect(remote_node_id, CHAT_ALPN).await?;
124122
println!("connected to {}", remote_node_id);
125123
let (mut writer, mut reader) = connection.open_bi().await?;
126124
let _copy_to_stdout =

iroh-net/src/dialer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Dialer {
5151
let res = tokio::select! {
5252
biased;
5353
_ = cancel.cancelled() => Err(anyhow!("Cancelled")),
54-
res = endpoint.connect_by_node_id(node_id, alpn) => res
54+
res = endpoint.connect(node_id, alpn) => res
5555
};
5656
(node_id, res)
5757
});

iroh-net/src/discovery.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
//! Node address discovery.
22
//!
3-
//! To connect to an iroh-net node a [`NodeAddr`] is needed, which needs to contain either a
4-
//! [`RelayUrl`] or one or more *direct addresses*. However it is often more desirable to
5-
//! be able to connect with only the [`NodeId`], as [`Endpoint::connect_by_node_id`] does.
3+
//! To connect to an iroh-net node a [`NodeAddr`] is needed, which may contain a
4+
//! [`RelayUrl`] or one or more *direct addresses* in addition to the [`NodeId`].
65
//!
7-
//! For connecting by [`NodeId`] to work however, the endpoint has to get the addressing
8-
//! information by other means. This can be done by manually calling
9-
//! [`Endpoint::add_node_addr`], but that still requires knowing the other addressing
10-
//! information.
6+
//! Since there is a conversion from [`NodeId`] to [`NodeAddr`], you can also use
7+
//! connect directly with a [`NodeId`].
8+
//!
9+
//! For this to work however, the endpoint has to get the addressing information by
10+
//! other means. This can be done by manually calling [`Endpoint::add_node_addr`],
11+
//! but that still requires knowing the other addressing information.
1112
//!
1213
//! Node discovery is an automated system for an [`Endpoint`] to retrieve this addressing
1314
//! information. Each iroh-net node will automatically publish their own addressing
@@ -761,7 +762,7 @@ mod test_dns_pkarr {
761762
.await?;
762763

763764
// we connect only by node id!
764-
let res = ep2.connect(ep1.node_id().into(), TEST_ALPN).await;
765+
let res = ep2.connect(ep1.node_id(), TEST_ALPN).await;
765766
assert!(res.is_ok(), "connection established");
766767
Ok(())
767768
}
@@ -782,7 +783,7 @@ mod test_dns_pkarr {
782783
.await?;
783784

784785
// we connect only by node id!
785-
let res = ep2.connect(ep1.node_id().into(), TEST_ALPN).await;
786+
let res = ep2.connect(ep1.node_id(), TEST_ALPN).await;
786787
assert!(res.is_ok(), "connection established");
787788
Ok(())
788789
}

iroh-net/src/endpoint.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -432,10 +432,12 @@ impl Endpoint {
432432

433433
/// Connects to a remote [`Endpoint`].
434434
///
435-
/// A [`NodeAddr`] is required. It must contain the [`NodeId`] to dial and may also
436-
/// contain a [`RelayUrl`] and direct addresses. If direct addresses are provided, they
437-
/// will be used to try and establish a direct connection without involving a relay
438-
/// server.
435+
/// A value that can be converted into a [`NodeAddr`] is required. This can be either a
436+
/// [`NodeAddr`], a [`NodeId`] or a [`iroh_base::ticket::NodeTicket`].
437+
///
438+
/// The [`NodeAddr`] must contain the [`NodeId`] to dial and may also contain a [`RelayUrl`]
439+
/// and direct addresses. If direct addresses are provided, they will be used to try and
440+
/// establish a direct connection without involving a relay server.
439441
///
440442
/// If neither a [`RelayUrl`] or direct addresses are configured in the [`NodeAddr`] it
441443
/// may still be possible a connection can be established. This depends on other calls
@@ -450,8 +452,14 @@ impl Endpoint {
450452
/// The `alpn`, or application-level protocol identifier, is also required. The remote
451453
/// endpoint must support this `alpn`, otherwise the connection attempt will fail with
452454
/// an error.
453-
#[instrument(skip_all, fields(me = %self.node_id().fmt_short(), remote = %node_addr.node_id.fmt_short(), alpn = ?String::from_utf8_lossy(alpn)))]
454-
pub async fn connect(&self, node_addr: NodeAddr, alpn: &[u8]) -> Result<quinn::Connection> {
455+
#[instrument(skip_all, fields(me = %self.node_id().fmt_short(), alpn = ?String::from_utf8_lossy(alpn)))]
456+
pub async fn connect(
457+
&self,
458+
node_addr: impl Into<NodeAddr>,
459+
alpn: &[u8],
460+
) -> Result<quinn::Connection> {
461+
let node_addr = node_addr.into();
462+
tracing::Span::current().record("remote", node_addr.node_id.fmt_short());
455463
// Connecting to ourselves is not supported.
456464
if node_addr.node_id == self.node_id() {
457465
bail!(
@@ -502,6 +510,10 @@ impl Endpoint {
502510
/// information being provided by either the discovery service or using
503511
/// [`Endpoint::add_node_addr`]. See [`Endpoint::connect`] for the details of how it
504512
/// uses the discovery service to establish a connection to a remote node.
513+
#[deprecated(
514+
since = "0.27.0",
515+
note = "Please use `connect` directly with a NodeId. This fn will be removed in 0.28.0."
516+
)]
505517
pub async fn connect_by_node_id(
506518
&self,
507519
node_id: NodeId,

iroh/examples/custom-protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl BlobSearch {
194194
// Establish a connection to our node.
195195
// We use the default node discovery in iroh, so we can connect by node id without
196196
// providing further information.
197-
let conn = self.endpoint.connect_by_node_id(node_id, ALPN).await?;
197+
let conn = self.endpoint.connect(node_id, ALPN).await?;
198198

199199
// Open a bi-directional in our connection.
200200
let (mut send, mut recv) = conn.open_bi().await?;

0 commit comments

Comments
 (0)