Skip to content

Commit 9b0bfd9

Browse files
committed
adapt non-default discovery mechanisms
1 parent a1042ad commit 9b0bfd9

File tree

17 files changed

+95
-88
lines changed

17 files changed

+95
-88
lines changed

iroh-base/src/endpoint_addr.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ impl EndpointAddr {
6767
}
6868

6969
/// Creates a new [`EndpointAddr`] from its parts.
70-
pub fn from_parts(id: impl Into<EndpointId>, addrs: impl IntoIterator<Item = TransportAddr>) -> Self {
70+
pub fn from_parts(
71+
id: impl Into<EndpointId>,
72+
addrs: impl IntoIterator<Item = TransportAddr>,
73+
) -> Self {
7174
Self {
7275
id: id.into(),
7376
addrs: addrs.into_iter().collect(),

iroh-base/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ mod relay_url;
1313
#[cfg(feature = "key")]
1414
pub use self::endpoint_addr::{EndpointAddr, TransportAddr};
1515
#[cfg(feature = "key")]
16-
pub use self::key::{EndpointId, PublicKey, KeyParsingError, SecretKey, Signature, SignatureError};
16+
pub use self::key::{EndpointId, KeyParsingError, PublicKey, SecretKey, Signature, SignatureError};
1717
#[cfg(feature = "relay")]
1818
pub use self::relay_url::{RelayUrl, RelayUrlParseError};

iroh-dns-server/examples/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use iroh::{
88
dns::{N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING},
99
pkarr::{N0_DNS_PKARR_RELAY_PROD, N0_DNS_PKARR_RELAY_STAGING, PkarrRelayClient},
1010
},
11-
endpoint_info::{PublicKeyExt, EndpointInfo, IROH_TXT_NAME},
11+
endpoint_info::{EndpointInfo, IROH_TXT_NAME, PublicKeyExt},
1212
};
1313
use n0_error::{Result, StackResultExt};
1414
use url::Url;

iroh-relay/src/endpoint_info.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ use std::{
4040
str::{FromStr, Utf8Error},
4141
};
4242

43-
use iroh_base::{EndpointAddr, EndpointId, KeyParsingError, PublicKey, RelayUrl, SecretKey, TransportAddr};
43+
use iroh_base::{
44+
EndpointAddr, EndpointId, KeyParsingError, PublicKey, RelayUrl, SecretKey, TransportAddr,
45+
};
4446
use n0_error::{e, ensure, stack_error};
4547
use url::Url;
4648

@@ -321,7 +323,10 @@ impl From<&TxtAttrs<IrohAttr>> for EndpointInfo {
321323
data.set_user_data(user_data);
322324
data.add_addrs(relay_urls.chain(ip_addrs));
323325

324-
Self { endpoint_id: endpoint_id.into(), data }
326+
Self {
327+
endpoint_id: endpoint_id.into(),
328+
data,
329+
}
325330
}
326331
}
327332

@@ -338,7 +343,7 @@ impl From<EndpointAddr> for EndpointInfo {
338343
info
339344
}
340345
}
341-
346+
342347
impl EndpointInfo {
343348
/// Creates a new [`EndpointInfo`] with an empty [`EndpointData`].
344349
pub fn new(endpoint_id: impl Into<EndpointId>) -> Self {
@@ -347,7 +352,10 @@ impl EndpointInfo {
347352

348353
/// Creates a new [`EndpointInfo`] from its parts.
349354
pub fn from_parts(endpoint_id: impl Into<EndpointId>, data: EndpointData) -> Self {
350-
Self { endpoint_id: endpoint_id.into(), data }
355+
Self {
356+
endpoint_id: endpoint_id.into(),
357+
data,
358+
}
351359
}
352360

353361
/// Sets the relay URL and returns the updated endpoint info.
@@ -674,7 +682,7 @@ mod tests {
674682
use iroh_base::{PublicKey, SecretKey, TransportAddr};
675683
use n0_error::{Result, StdResultExt};
676684

677-
use super::{EndpointData, PublicKeyExt, EndpointInfo};
685+
use super::{EndpointData, EndpointInfo, PublicKeyExt};
678686
use crate::dns::TxtRecordData;
679687

680688
#[test]

iroh-relay/src/protos/relay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use std::num::NonZeroU16;
1111

1212
use bytes::{Buf, BufMut, Bytes, BytesMut};
13-
use iroh_base::{PublicKey, KeyParsingError};
13+
use iroh_base::{KeyParsingError, PublicKey};
1414
use n0_error::{e, ensure, stack_error};
1515
use n0_future::time::Duration;
1616

iroh-relay/src/server/client.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,7 @@ impl Client {
161161
self.disco_send_queue.try_send(Packet { src, data })
162162
}
163163

164-
pub(super) fn try_send_peer_gone(
165-
&self,
166-
key: PublicKey,
167-
) -> Result<(), TrySendError<PublicKey>> {
164+
pub(super) fn try_send_peer_gone(&self, key: PublicKey) -> Result<(), TrySendError<PublicKey>> {
168165
self.peer_gone.try_send(key)
169166
}
170167
}

iroh/examples/locally-discovered-nodes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use std::time::Duration;
77

88
use iroh::{
9-
Endpoint, PublicKey,
9+
Endpoint, EndpointId,
1010
discovery::mdns::{DiscoveryEvent, MdnsDiscovery},
1111
endpoint_info::UserData,
1212
};
@@ -32,7 +32,7 @@ async fn main() -> Result<()> {
3232
let ud = user_data.clone();
3333
let discovery_stream_task = tokio::spawn(async move {
3434
let mut discovery_stream = mdns.subscribe().await;
35-
let mut discovered_endpoints: Vec<PublicKey> = vec![];
35+
let mut discovered_endpoints: Vec<EndpointId> = vec![];
3636
while let Some(event) = discovery_stream.next().await {
3737
match event {
3838
DiscoveryEvent::Discovered { endpoint_info, .. } => {

iroh/examples/transfer.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ use clap::{Parser, Subcommand};
99
use data_encoding::HEXLOWER;
1010
use indicatif::HumanBytes;
1111
use iroh::{
12-
Endpoint, EndpointAddr, EndpointId, PublicKey, RelayMap, RelayMode, RelayUrl, SecretKey, TransportAddr, discovery::{
12+
Endpoint, EndpointAddr, EndpointId, PublicKey, RelayMap, RelayMode, RelayUrl, SecretKey,
13+
TransportAddr,
14+
discovery::{
1315
dns::DnsDiscovery,
1416
pkarr::{N0_DNS_PKARR_RELAY_PROD, N0_DNS_PKARR_RELAY_STAGING, PkarrPublisher},
15-
}, dns::{DnsResolver, N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING}, endpoint::ConnectionError
17+
},
18+
dns::{DnsResolver, N0_DNS_ENDPOINT_ORIGIN_PROD, N0_DNS_ENDPOINT_ORIGIN_STAGING},
19+
endpoint::ConnectionError,
1620
};
1721
use n0_error::{Result, StackResultExt, StdResultExt};
1822
use n0_future::task::AbortOnDropHandle;

iroh/src/discovery.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,10 +1059,7 @@ mod test_dns_pkarr {
10591059
.await?;
10601060
println!("resolved {resolved:?}");
10611061

1062-
let expected_addr = EndpointAddr::from_parts(
1063-
endpoint_id,
1064-
relay_url,
1065-
);
1062+
let expected_addr = EndpointAddr::from_parts(endpoint_id, relay_url);
10661063

10671064
assert_eq!(resolved.to_endpoint_addr(), expected_addr);
10681065
assert_eq!(resolved.user_data(), Some(&user_data));

iroh/src/discovery/mdns.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ pub struct MdnsDiscovery {
9898
enum Message {
9999
Discovery(String, Peer),
100100
Resolve(
101-
EndpointId,
101+
PublicKey,
102102
mpsc::Sender<Result<DiscoveryItem, DiscoveryError>>,
103103
),
104-
Timeout(EndpointId, usize),
104+
Timeout(PublicKey, usize),
105105
Subscribe(mpsc::Sender<DiscoveryEvent>),
106106
}
107107

@@ -190,7 +190,7 @@ impl MdnsDiscoveryBuilder {
190190
/// # Panics
191191
/// This relies on [`tokio::runtime::Handle::current`] and will panic if called outside of the context of a tokio runtime.
192192
pub fn build(self, endpoint_id: EndpointId) -> Result<MdnsDiscovery, IntoDiscoveryError> {
193-
MdnsDiscovery::new(endpoint_id, self.advertise, self.service_name)
193+
MdnsDiscovery::new(endpoint_id.expect_ed(), self.advertise, self.service_name)
194194
}
195195
}
196196

@@ -241,7 +241,7 @@ impl MdnsDiscovery {
241241
/// # Panics
242242
/// This relies on [`tokio::runtime::Handle::current`] and will panic if called outside of the context of a tokio runtime.
243243
fn new(
244-
endpoint_id: EndpointId,
244+
endpoint_id: PublicKey,
245245
advertise: bool,
246246
service_name: String,
247247
) -> Result<Self, IntoDiscoveryError> {
@@ -332,7 +332,7 @@ impl MdnsDiscovery {
332332
);
333333
endpoint_addrs.remove(&discovered_endpoint_id);
334334
subscribers.send(DiscoveryEvent::Expired {
335-
endpoint_id: discovered_endpoint_id,
335+
endpoint_id: discovered_endpoint_id.into(),
336336
});
337337
continue;
338338
}
@@ -352,7 +352,8 @@ impl MdnsDiscovery {
352352
);
353353

354354
let mut resolved = false;
355-
let item = peer_to_discovery_item(&peer_info, &discovered_endpoint_id);
355+
let item =
356+
peer_to_discovery_item(&peer_info, &discovered_endpoint_id.into());
356357
if let Some(senders) = senders.get(&discovered_endpoint_id) {
357358
trace!(?item, senders = senders.len(), "sending DiscoveryItem");
358359
resolved = true;
@@ -372,21 +373,21 @@ impl MdnsDiscovery {
372373
});
373374
}
374375
}
375-
Message::Resolve(endpoint_id, sender) => {
376+
Message::Resolve(public_key, sender) => {
376377
let id = last_id + 1;
377378
last_id = id;
378-
trace!(?endpoint_id, "MdnsDiscovery Message::SendAddrs");
379-
if let Some(peer_info) = endpoint_addrs.get(&endpoint_id) {
380-
let item = peer_to_discovery_item(peer_info, &endpoint_id);
379+
trace!(?public_key, "MdnsDiscovery Message::SendAddrs");
380+
if let Some(peer_info) = endpoint_addrs.get(&public_key) {
381+
let item = peer_to_discovery_item(peer_info, &public_key);
381382
debug!(?item, "sending DiscoveryItem");
382383
sender.send(Ok(item)).await.ok();
383384
}
384-
if let Some(senders_for_endpoint_id) = senders.get_mut(&endpoint_id) {
385+
if let Some(senders_for_endpoint_id) = senders.get_mut(&public_key) {
385386
senders_for_endpoint_id.insert(id, sender);
386387
} else {
387388
let mut senders_for_endpoint_id = HashMap::new();
388389
senders_for_endpoint_id.insert(id, sender);
389-
senders.insert(endpoint_id, senders_for_endpoint_id);
390+
senders.insert(public_key, senders_for_endpoint_id);
390391
}
391392
let timeout_sender = task_sender.clone();
392393
timeouts.spawn(async move {
@@ -398,12 +399,12 @@ impl MdnsDiscovery {
398399
.ok();
399400
});
400401
}
401-
Message::Timeout(endpoint_id, id) => {
402-
trace!(?endpoint_id, "MdnsDiscovery Message::Timeout");
403-
if let Some(senders_for_endpoint_id) = senders.get_mut(&endpoint_id) {
402+
Message::Timeout(public_key, id) => {
403+
trace!(?public_key, "MdnsDiscovery Message::Timeout");
404+
if let Some(senders_for_endpoint_id) = senders.get_mut(&public_key) {
404405
senders_for_endpoint_id.remove(&id);
405406
if senders_for_endpoint_id.is_empty() {
406-
senders.remove(&endpoint_id);
407+
senders.remove(&public_key);
407408
}
408409
}
409410
}
@@ -488,7 +489,7 @@ impl MdnsDiscovery {
488489
}
489490
}
490491

491-
fn peer_to_discovery_item(peer: &Peer, endpoint_id: &EndpointId) -> DiscoveryItem {
492+
fn peer_to_discovery_item(peer: &Peer, public_key: &PublicKey) -> DiscoveryItem {
492493
let ip_addrs: BTreeSet<SocketAddr> = peer
493494
.addrs()
494495
.iter()
@@ -507,7 +508,7 @@ fn peer_to_discovery_item(peer: &Peer, endpoint_id: &EndpointId) -> DiscoveryIte
507508
} else {
508509
None
509510
};
510-
let endpoint_info = EndpointInfo::new(*endpoint_id)
511+
let endpoint_info = EndpointInfo::new(*public_key)
511512
.with_ip_addrs(ip_addrs)
512513
.with_user_data(user_data);
513514
DiscoveryItem::new(endpoint_info, NAME, None)
@@ -520,11 +521,13 @@ impl Discovery for MdnsDiscovery {
520521
) -> Option<BoxStream<Result<DiscoveryItem, DiscoveryError>>> {
521522
use futures_util::FutureExt;
522523

524+
let public_key = endpoint_id.as_ed()?;
525+
523526
let (send, recv) = mpsc::channel(20);
524527
let discovery_sender = self.sender.clone();
525528
let stream = async move {
526529
discovery_sender
527-
.send(Message::Resolve(endpoint_id, send))
530+
.send(Message::Resolve(public_key, send))
528531
.await
529532
.ok();
530533
tokio_stream::wrappers::ReceiverStream::new(recv)
@@ -762,19 +765,19 @@ mod tests {
762765

763766
// Create a discovery service using the default
764767
// service name
765-
let id_a = SecretKey::generate(&mut rng).public();
768+
let id_a = SecretKey::generate(&mut rng).public().into();
766769
let discovery_a = MdnsDiscovery::builder().build(id_a)?;
767770

768771
// Create a discovery service using a custom
769772
// service name
770-
let id_b = SecretKey::generate(&mut rng).public();
773+
let id_b = SecretKey::generate(&mut rng).public().into();
771774
let discovery_b = MdnsDiscovery::builder()
772775
.service_name("different.name")
773776
.build(id_b)?;
774777

775778
// Create a discovery service using the same
776779
// custom service name
777-
let id_c = SecretKey::generate(&mut rng).public();
780+
let id_c = SecretKey::generate(&mut rng).public().into();
778781
let discovery_c = MdnsDiscovery::builder()
779782
.service_name("different.name")
780783
.build(id_c)?;
@@ -818,8 +821,8 @@ mod tests {
818821
fn make_discoverer<R: CryptoRng + ?Sized>(
819822
rng: &mut R,
820823
advertise: bool,
821-
) -> Result<(PublicKey, MdnsDiscovery)> {
822-
let endpoint_id = SecretKey::generate(rng).public();
824+
) -> Result<(EndpointId, MdnsDiscovery)> {
825+
let endpoint_id = SecretKey::generate(rng).public().into();
823826
Ok((
824827
endpoint_id,
825828
MdnsDiscovery::builder()

0 commit comments

Comments
 (0)