Skip to content

Commit cadee48

Browse files
committed
Remove Other from EndpointId and make it non_exhaustive
1 parent 7cc2d23 commit cadee48

File tree

5 files changed

+26
-32
lines changed

5 files changed

+26
-32
lines changed

iroh-base/src/key.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,16 @@ pub struct PublicKey(CompressedEdwardsY);
2525

2626
/// The identifier for an endpoint in the (iroh) network.
2727
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
28+
#[non_exhaustive]
2829
pub enum EndpointId {
2930
/// An Ed25519 public key.
3031
Ed25519(PublicKey),
31-
/// Other types of endpoint identifiers.
32-
Other([u8; 8]),
3332
}
3433

3534
impl PartialEq<PublicKey> for EndpointId {
3635
fn eq(&self, other: &PublicKey) -> bool {
3736
match self {
3837
EndpointId::Ed25519(key) => key == other,
39-
EndpointId::Other(_) => false,
4038
}
4139
}
4240
}
@@ -45,7 +43,6 @@ impl Display for EndpointId {
4543
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4644
match self {
4745
EndpointId::Ed25519(key) => write!(f, "Ed25519({})", key),
48-
EndpointId::Other(bytes) => write!(f, "Other({})", data_encoding::HEXLOWER.encode(bytes)),
4946
}
5047
}
5148
}
@@ -55,7 +52,6 @@ impl EndpointId {
5552
pub fn as_ed(self) -> Option<PublicKey> {
5653
match self {
5754
EndpointId::Ed25519(key) => Some(key),
58-
EndpointId::Other(_) => None,
5955
}
6056
}
6157

@@ -68,7 +64,6 @@ impl EndpointId {
6864
pub fn fmt_short(&self) -> String {
6965
match self {
7066
EndpointId::Ed25519(key) => key.fmt_short().to_string(),
71-
EndpointId::Other(_) => "Other".to_string(),
7267
}
7368
}
7469
}

iroh/examples/transfer.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,10 @@ use clap::{Parser, Subcommand};
99
use data_encoding::HEXLOWER;
1010
use indicatif::HumanBytes;
1111
use iroh::{
12-
Endpoint, EndpointAddr, PublicKey, RelayMap, RelayMode, RelayUrl, SecretKey, TransportAddr,
13-
discovery::{
12+
Endpoint, EndpointAddr, EndpointId, PublicKey, RelayMap, RelayMode, RelayUrl, SecretKey, TransportAddr, discovery::{
1413
dns::DnsDiscovery,
1514
pkarr::{N0_DNS_PKARR_RELAY_PROD, N0_DNS_PKARR_RELAY_STAGING, PkarrPublisher},
16-
},
17-
dns::{DnsResolver, N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING},
18-
endpoint::ConnectionError,
15+
}, dns::{DnsResolver, N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING}, endpoint::ConnectionError
1916
};
2017
use n0_error::{Result, StackResultExt, StdResultExt};
2118
use n0_future::task::AbortOnDropHandle;
@@ -404,7 +401,7 @@ async fn fetch(endpoint: Endpoint, remote_addr: EndpointAddr) -> Result<()> {
404401
let conn = endpoint.connect(remote_addr, TRANSFER_ALPN).await?;
405402
println!("Connected to {}", remote_id);
406403
// Spawn a background task that prints connection type changes. Will be aborted on drop.
407-
let _guard = watch_conn_type(&endpoint, remote_id.expect_ed());
404+
let _guard = watch_conn_type(&endpoint, remote_id);
408405

409406
// Use the Quinn API to send and recv content.
410407
let (mut send, mut recv) = conn.open_bi().await.anyerr()?;
@@ -521,7 +518,7 @@ fn parse_byte_size(s: &str) -> std::result::Result<u64, parse_size::Error> {
521518
cfg.parse_size(s)
522519
}
523520

524-
fn watch_conn_type(endpoint: &Endpoint, endpoint_id: PublicKey) -> AbortOnDropHandle<()> {
521+
fn watch_conn_type(endpoint: &Endpoint, endpoint_id: EndpointId) -> AbortOnDropHandle<()> {
525522
let mut stream = endpoint.conn_type(endpoint_id).unwrap().stream();
526523
let task = tokio::task::spawn(async move {
527524
while let Some(conn_type) = stream.next().await {

iroh/src/endpoint.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::{
1616
sync::Arc,
1717
};
1818

19-
use iroh_base::{EndpointAddr, EndpointId, PublicKey, RelayUrl, SecretKey, TransportAddr};
19+
use iroh_base::{EndpointAddr, EndpointId, RelayUrl, SecretKey, TransportAddr};
2020
use iroh_relay::{RelayConfig, RelayMap};
2121
use n0_error::{e, ensure, stack_error};
2222
use n0_future::time::Duration;
@@ -978,15 +978,17 @@ impl Endpoint {
978978
/// become inaccessible.
979979
///
980980
/// Will return `None` if we do not have any address information for the given `endpoint_id`.
981-
pub fn conn_type(&self, endpoint_id: PublicKey) -> Option<n0_watcher::Direct<ConnectionType>> {
982-
self.msock.conn_type(endpoint_id)
981+
pub fn conn_type(&self, endpoint_id: EndpointId) -> Option<n0_watcher::Direct<ConnectionType>> {
982+
let public_key = endpoint_id.as_ed()?;
983+
self.msock.conn_type(public_key)
983984
}
984985

985986
/// Returns the currently lowest latency for this endpoint.
986987
///
987988
/// Will return `None` if we do not have any address information for the given `endpoint_id`.
988-
pub fn latency(&self, endpoint_id: PublicKey) -> Option<Duration> {
989-
self.msock.latency(endpoint_id)
989+
pub fn latency(&self, endpoint_id: EndpointId) -> Option<Duration> {
990+
let public_key = endpoint_id.as_ed()?;
991+
self.msock.latency(public_key)
990992
}
991993

992994
/// Returns the DNS resolver used in this [`Endpoint`].
@@ -1968,7 +1970,7 @@ mod tests {
19681970

19691971
async fn wait_for_conn_type_direct(ep: &Endpoint, endpoint_id: PublicKey) -> Result {
19701972
let mut stream = ep
1971-
.conn_type(endpoint_id)
1973+
.conn_type(endpoint_id.into())
19721974
.expect("connection exists")
19731975
.stream();
19741976
let src = ep.id().fmt_short();

iroh/src/endpoint/connection.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl Future for IncomingFuture {
178178
Ok(conn) => conn,
179179
Err(err) => return Poll::Ready(Err(err.into())),
180180
};
181-
try_send_rtt_msg(conn.quinn_connection(), this.ep, conn.remote_id());
181+
try_send_rtt_msg(conn.quinn_connection(), this.ep, conn.remote_id);
182182
Poll::Ready(Ok(conn))
183183
}
184184
}
@@ -442,7 +442,7 @@ impl Future for Connecting {
442442
}
443443
};
444444

445-
try_send_rtt_msg(conn.quinn_connection(), this.ep, conn.remote_id());
445+
try_send_rtt_msg(conn.quinn_connection(), this.ep, conn.remote_id);
446446
Poll::Ready(Ok(conn))
447447
}
448448
}
@@ -520,7 +520,7 @@ impl Future for Accepting {
520520
Err(err) => return Poll::Ready(Err(err.into())),
521521
};
522522

523-
try_send_rtt_msg(conn.quinn_connection(), this.ep, conn.remote_id());
523+
try_send_rtt_msg(conn.quinn_connection(), this.ep, conn.remote_id);
524524
Poll::Ready(Ok(conn))
525525
}
526526
}
@@ -824,8 +824,8 @@ impl OutgoingZeroRttConnection {
824824
/// connection.
825825
///
826826
/// [`PublicKey`]: iroh_base::PublicKey
827-
pub fn remote_id(&self) -> Result<PublicKey, RemoteEndpointIdError> {
828-
remote_id_from_quinn_conn(&self.inner)
827+
pub fn remote_id(&self) -> Result<EndpointId, RemoteEndpointIdError> {
828+
remote_id_from_quinn_conn(&self.inner).map(EndpointId::from)
829829
}
830830

831831
/// A stable identifier for this connection.
@@ -1135,8 +1135,8 @@ impl IncomingZeroRttConnection {
11351135
/// connection.
11361136
///
11371137
/// [`PublicKey`]: iroh_base::PublicKey
1138-
pub fn remote_id(&self) -> Result<PublicKey, RemoteEndpointIdError> {
1139-
remote_id_from_quinn_conn(&self.inner)
1138+
pub fn remote_id(&self) -> Result<EndpointId, RemoteEndpointIdError> {
1139+
remote_id_from_quinn_conn(&self.inner).map(EndpointId::from)
11401140
}
11411141

11421142
/// A stable identifier for this connection.
@@ -1434,8 +1434,8 @@ impl Connection {
14341434
/// connection.
14351435
///
14361436
/// [`PublicKey`]: iroh_base::PublicKey
1437-
pub fn remote_id(&self) -> PublicKey {
1438-
self.remote_id
1437+
pub fn remote_id(&self) -> EndpointId {
1438+
self.remote_id.into()
14391439
}
14401440

14411441
/// A stable identifier for this connection.
@@ -1495,7 +1495,7 @@ impl Connection {
14951495
/// If we can't notify the actor that will impact performance a little, but we can still
14961496
/// function.
14971497
fn try_send_rtt_msg(conn: &quinn::Connection, ep: &Endpoint, remote_id: PublicKey) {
1498-
let Some(conn_type_changes) = ep.conn_type(remote_id) else {
1498+
let Some(conn_type_changes) = ep.conn_type(remote_id.into()) else {
14991499
warn!(?conn, "failed to create conn_type stream");
15001500
return;
15011501
};

iroh/src/protocol.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::{
4242
sync::{Arc, Mutex},
4343
};
4444

45-
use iroh_base::PublicKey;
45+
use iroh_base::EndpointId;
4646
use n0_error::{AnyError, e, stack_error};
4747
use n0_future::{
4848
join_all,
@@ -558,7 +558,7 @@ async fn handle_connection(incoming: crate::endpoint::Incoming, protocols: Arc<P
558558
pub struct AccessLimit<P: ProtocolHandler + Clone> {
559559
proto: P,
560560
#[debug("limiter")]
561-
limiter: Arc<dyn Fn(PublicKey) -> bool + Send + Sync + 'static>,
561+
limiter: Arc<dyn Fn(EndpointId) -> bool + Send + Sync + 'static>,
562562
}
563563

564564
impl<P: ProtocolHandler + Clone> AccessLimit<P> {
@@ -568,7 +568,7 @@ impl<P: ProtocolHandler + Clone> AccessLimit<P> {
568568
/// connect, and `false` otherwise.
569569
pub fn new<F>(proto: P, limiter: F) -> Self
570570
where
571-
F: Fn(PublicKey) -> bool + Send + Sync + 'static,
571+
F: Fn(EndpointId) -> bool + Send + Sync + 'static,
572572
{
573573
Self {
574574
proto,

0 commit comments

Comments
 (0)