Skip to content

Commit 6a988a5

Browse files
refactor: remove AddrInfo (#3024)
## Description Simplify type structures TODO - [x] fix tests - [x] resolve ticket breakage ## Breaking Changes - remove `iroh_base::node_addr::AddrInfo` - remove `iroh_base::node_addr::AddrInfoOptions` - introduce `ticket` feature for `iroh_base`, to use `iroh_base::ticket` ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [ ] Self-review. - [ ] 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. - [ ] All breaking changes documented.
1 parent 3e31196 commit 6a988a5

File tree

19 files changed

+289
-339
lines changed

19 files changed

+289
-339
lines changed

iroh-base/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ serde_json = "1"
4646
serde_test = "1"
4747

4848
[features]
49-
default = ["hash", "base32", "relay"]
49+
default = ["hash", "ticket", "relay"]
5050
hash = ["dep:blake3", "dep:data-encoding", "dep:postcard", "dep:derive_more", "base32"]
51+
ticket = ["base32", "key"]
5152
base32 = ["dep:data-encoding", "dep:postcard"]
5253
redb = ["dep:redb"]
5354
key = ["dep:ed25519-dalek", "dep:once_cell", "dep:rand", "dep:rand_core", "dep:ssh-key", "dep:ttl_cache", "dep:aead", "dep:crypto_box", "dep:zeroize", "dep:url", "dep:derive_more", "dep:getrandom"]

iroh-base/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub mod key;
1111
pub mod node_addr;
1212
#[cfg(feature = "relay")]
1313
pub mod relay_map;
14-
#[cfg(any(feature = "relay", feature = "key"))]
14+
#[cfg(feature = "relay")]
1515
mod relay_url;
16-
#[cfg(feature = "base32")]
16+
#[cfg(feature = "ticket")]
1717
pub mod ticket;

iroh-base/src/node_addr.rs

Lines changed: 20 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,34 @@ pub use crate::relay_url::RelayUrl;
4141
pub struct NodeAddr {
4242
/// The node's identifier.
4343
pub node_id: NodeId,
44-
/// Addressing information to connect to [`Self::node_id`].
45-
pub info: AddrInfo,
44+
/// The node's home relay url.
45+
pub relay_url: Option<RelayUrl>,
46+
/// Socket addresses where the peer might be reached directly.
47+
pub direct_addresses: BTreeSet<SocketAddr>,
4648
}
4749

4850
impl NodeAddr {
49-
/// Creates a new [`NodeAddr`] with empty [`AddrInfo`].
51+
/// Creates a new [`NodeAddr`] with no `relay_url` and no `direct_addresses`.
5052
pub fn new(node_id: PublicKey) -> Self {
5153
NodeAddr {
5254
node_id,
53-
info: Default::default(),
55+
relay_url: None,
56+
direct_addresses: Default::default(),
5457
}
5558
}
5659

57-
/// Adds a relay url to the node's [`AddrInfo`].
60+
/// Adds a relay url.
5861
pub fn with_relay_url(mut self, relay_url: RelayUrl) -> Self {
59-
self.info.relay_url = Some(relay_url);
62+
self.relay_url = Some(relay_url);
6063
self
6164
}
6265

63-
/// Adds the given direct addresses to the peer's [`AddrInfo`].
66+
/// Adds the given direct addresses.
6467
pub fn with_direct_addresses(
6568
mut self,
6669
addresses: impl IntoIterator<Item = SocketAddr>,
6770
) -> Self {
68-
self.info.direct_addresses = addresses.into_iter().collect();
71+
self.direct_addresses = addresses.into_iter().collect();
6972
self
7073
}
7174

@@ -77,32 +80,24 @@ impl NodeAddr {
7780
) -> Self {
7881
Self {
7982
node_id,
80-
info: AddrInfo {
81-
relay_url,
82-
direct_addresses: direct_addresses.into_iter().collect(),
83-
},
83+
relay_url,
84+
direct_addresses: direct_addresses.into_iter().collect(),
8485
}
8586
}
8687

87-
/// Applies the options to `self`.
88-
///
89-
/// This is used to more tightly control the information stored in a [`NodeAddr`]
90-
/// received from another API. E.g. to ensure a [discovery] service is used the
91-
/// `AddrInfoOptions::Id`] option could be used to remove all other addressing details.
92-
///
93-
/// [discovery]: https://docs.rs/iroh/*/iroh/index.html#node-discovery
94-
pub fn apply_options(&mut self, opts: AddrInfoOptions) {
95-
self.info.apply_options(opts);
88+
/// Returns true, if only a [`NodeId`] is present.
89+
pub fn is_empty(&self) -> bool {
90+
self.relay_url.is_none() && self.direct_addresses.is_empty()
9691
}
9792

9893
/// Returns the direct addresses of this peer.
9994
pub fn direct_addresses(&self) -> impl Iterator<Item = &SocketAddr> {
100-
self.info.direct_addresses.iter()
95+
self.direct_addresses.iter()
10196
}
10297

10398
/// Returns the relay url of this peer.
10499
pub fn relay_url(&self) -> Option<&RelayUrl> {
105-
self.info.relay_url.as_ref()
100+
self.relay_url.as_ref()
106101
}
107102
}
108103

@@ -111,10 +106,8 @@ impl From<(PublicKey, Option<RelayUrl>, &[SocketAddr])> for NodeAddr {
111106
let (node_id, relay_url, direct_addresses_iter) = value;
112107
NodeAddr {
113108
node_id,
114-
info: AddrInfo {
115-
relay_url,
116-
direct_addresses: direct_addresses_iter.iter().copied().collect(),
117-
},
109+
relay_url,
110+
direct_addresses: direct_addresses_iter.iter().copied().collect(),
118111
}
119112
}
120113
}
@@ -124,77 +117,3 @@ impl From<NodeId> for NodeAddr {
124117
NodeAddr::new(node_id)
125118
}
126119
}
127-
128-
/// Network paths to contact an iroh node.
129-
///
130-
/// This contains zero or more network paths to establish a connection to an iroh node.
131-
/// Unless a [discovery service] is used at least one path is required to connect to an
132-
/// other node, see [`NodeAddr`] for details.
133-
///
134-
/// [discovery]: https://docs.rs/iroh/*/iroh/index.html#node-discovery
135-
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default, PartialOrd, Ord)]
136-
pub struct AddrInfo {
137-
/// The node's home relay url.
138-
pub relay_url: Option<RelayUrl>,
139-
/// Socket addresses where the peer might be reached directly.
140-
pub direct_addresses: BTreeSet<SocketAddr>,
141-
}
142-
143-
impl AddrInfo {
144-
/// Returns whether this addressing information is empty.
145-
pub fn is_empty(&self) -> bool {
146-
self.relay_url.is_none() && self.direct_addresses.is_empty()
147-
}
148-
149-
/// Applies the options to `self`.
150-
///
151-
/// This is used to more tightly control the information stored in ab [`AddrInfo`]
152-
/// received from another API. E.g. to ensure a [discovery] service is used the
153-
/// `AddrInfoOptions::Id`] option could be used to remove all other addressing details.
154-
///
155-
/// [discovery]: https://docs.rs/iroh/*/iroh/index.html#node-discovery
156-
pub fn apply_options(&mut self, opts: AddrInfoOptions) {
157-
match opts {
158-
AddrInfoOptions::Id => {
159-
self.direct_addresses.clear();
160-
self.relay_url = None;
161-
}
162-
AddrInfoOptions::RelayAndAddresses => {
163-
// nothing to do
164-
}
165-
AddrInfoOptions::Relay => {
166-
self.direct_addresses.clear();
167-
}
168-
AddrInfoOptions::Addresses => {
169-
self.relay_url = None;
170-
}
171-
}
172-
}
173-
}
174-
175-
/// Options to configure what is included in a [`NodeAddr`] and [`AddrInfo`].
176-
#[derive(
177-
Copy,
178-
Clone,
179-
PartialEq,
180-
Eq,
181-
Default,
182-
Debug,
183-
derive_more::Display,
184-
derive_more::FromStr,
185-
Serialize,
186-
Deserialize,
187-
)]
188-
pub enum AddrInfoOptions {
189-
/// Only the Node ID is added.
190-
///
191-
/// This usually means that iroh-dns discovery is used to find address information.
192-
#[default]
193-
Id,
194-
/// Includes the Node ID and both the relay URL, and the direct addresses.
195-
RelayAndAddresses,
196-
/// Includes the Node ID and the relay URL.
197-
Relay,
198-
/// Includes the Node ID and the direct addresses.
199-
Addresses,
200-
}

iroh-base/src/ticket.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
use crate::base32;
1+
use std::{collections::BTreeSet, net::SocketAddr};
2+
3+
use serde::{Deserialize, Serialize};
4+
5+
use crate::{base32, key::NodeId, relay_url::RelayUrl};
26

3-
#[cfg(feature = "key")]
47
mod blob;
5-
#[cfg(feature = "key")]
68
mod node;
7-
#[cfg(feature = "key")]
9+
810
pub use self::{blob::BlobTicket, node::NodeTicket};
911

1012
/// A ticket is a serializable object combining information required for an operation.
@@ -70,3 +72,15 @@ pub enum Error {
7072
#[error("verification failed: {_0}")]
7173
Verify(&'static str),
7274
}
75+
76+
#[derive(Serialize, Deserialize)]
77+
struct Variant0NodeAddr {
78+
node_id: NodeId,
79+
info: Variant0AddrInfo,
80+
}
81+
82+
#[derive(Serialize, Deserialize)]
83+
struct Variant0AddrInfo {
84+
relay_url: Option<RelayUrl>,
85+
direct_addresses: BTreeSet<SocketAddr>,
86+
}

iroh-base/src/ticket/blob.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::str::FromStr;
44
use anyhow::Result;
55
use serde::{Deserialize, Serialize};
66

7+
use super::{Variant0AddrInfo, Variant0NodeAddr};
78
use crate::{
89
hash::{BlobFormat, Hash},
910
node_addr::NodeAddr,
@@ -31,21 +32,47 @@ pub struct BlobTicket {
3132
/// postcard to add a discriminator.
3233
#[derive(Serialize, Deserialize)]
3334
enum TicketWireFormat {
34-
Variant0(BlobTicket),
35+
Variant0(Variant0BlobTicket),
36+
}
37+
38+
// Legacy
39+
#[derive(Serialize, Deserialize)]
40+
struct Variant0BlobTicket {
41+
node: Variant0NodeAddr,
42+
format: BlobFormat,
43+
hash: Hash,
3544
}
3645

3746
impl Ticket for BlobTicket {
3847
const KIND: &'static str = "blob";
3948

4049
fn to_bytes(&self) -> Vec<u8> {
41-
let data = TicketWireFormat::Variant0(self.clone());
50+
let data = TicketWireFormat::Variant0(Variant0BlobTicket {
51+
node: Variant0NodeAddr {
52+
node_id: self.node.node_id,
53+
info: Variant0AddrInfo {
54+
relay_url: self.node.relay_url.clone(),
55+
direct_addresses: self.node.direct_addresses.clone(),
56+
},
57+
},
58+
format: self.format,
59+
hash: self.hash,
60+
});
4261
postcard::to_stdvec(&data).expect("postcard serialization failed")
4362
}
4463

4564
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, ticket::Error> {
4665
let res: TicketWireFormat = postcard::from_bytes(bytes).map_err(ticket::Error::Postcard)?;
47-
let TicketWireFormat::Variant0(res) = res;
48-
Ok(res)
66+
let TicketWireFormat::Variant0(Variant0BlobTicket { node, format, hash }) = res;
67+
Ok(Self {
68+
node: NodeAddr {
69+
node_id: node.node_id,
70+
relay_url: node.info.relay_url,
71+
direct_addresses: node.info.direct_addresses,
72+
},
73+
format,
74+
hash,
75+
})
4976
}
5077
}
5178

iroh-base/src/ticket/node.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::str::FromStr;
55
use anyhow::Result;
66
use serde::{Deserialize, Serialize};
77

8+
use super::{Variant0AddrInfo, Variant0NodeAddr};
89
use crate::{
910
node_addr::NodeAddr,
1011
ticket::{self, Ticket},
@@ -36,21 +37,41 @@ pub struct NodeTicket {
3637
/// Wire format for [`NodeTicket`].
3738
#[derive(Serialize, Deserialize)]
3839
enum TicketWireFormat {
39-
Variant0(NodeTicket),
40+
Variant0(Variant0NodeTicket),
41+
}
42+
43+
// Legacy
44+
#[derive(Serialize, Deserialize)]
45+
struct Variant0NodeTicket {
46+
node: Variant0NodeAddr,
4047
}
4148

4249
impl Ticket for NodeTicket {
4350
const KIND: &'static str = "node";
4451

4552
fn to_bytes(&self) -> Vec<u8> {
46-
let data = TicketWireFormat::Variant0(self.clone());
53+
let data = TicketWireFormat::Variant0(Variant0NodeTicket {
54+
node: Variant0NodeAddr {
55+
node_id: self.node.node_id,
56+
info: Variant0AddrInfo {
57+
relay_url: self.node.relay_url.clone(),
58+
direct_addresses: self.node.direct_addresses.clone(),
59+
},
60+
},
61+
});
4762
postcard::to_stdvec(&data).expect("postcard serialization failed")
4863
}
4964

5065
fn from_bytes(bytes: &[u8]) -> std::result::Result<Self, ticket::Error> {
5166
let res: TicketWireFormat = postcard::from_bytes(bytes).map_err(ticket::Error::Postcard)?;
52-
let TicketWireFormat::Variant0(res) = res;
53-
Ok(res)
67+
let TicketWireFormat::Variant0(Variant0NodeTicket { node }) = res;
68+
Ok(Self {
69+
node: NodeAddr {
70+
node_id: node.node_id,
71+
relay_url: node.info.relay_url,
72+
direct_addresses: node.info.direct_addresses,
73+
},
74+
})
5475
}
5576
}
5677

iroh-dns-server/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ mod tests {
183183
let res = resolver.lookup_by_id(&node_id, origin).await?;
184184

185185
assert_eq!(res.node_id, node_id);
186-
assert_eq!(res.info.relay_url.map(Url::from), Some(relay_url));
186+
assert_eq!(res.relay_url.map(Url::from), Some(relay_url));
187187

188188
server.shutdown().await?;
189189
Ok(())
@@ -255,7 +255,7 @@ mod tests {
255255
let res = resolver.lookup_by_id(&node_id, origin).await?;
256256

257257
assert_eq!(res.node_id, node_id);
258-
assert_eq!(res.info.relay_url.map(Url::from), Some(relay_url));
258+
assert_eq!(res.relay_url.map(Url::from), Some(relay_url));
259259

260260
server.shutdown().await?;
261261
for mut node in testnet.nodes {

0 commit comments

Comments
 (0)