Skip to content

Commit

Permalink
refactor(iroh)!: improve reexport structure (#3023)
Browse files Browse the repository at this point in the history
## Description

Currently this simply normalizes `iroh_base` based reexports, but this
shows that a lot of the types in `iroh-base` are not very pleasant to
import


## Breaking Changes

- `iroh-base`
    - `key` -> `iroh_base::NodeId` etc
    - `hash` -> `iroh_base::Hash` etc
    - `node_addr` -> `iroh_base::NodeAddr`
    - `relay_url`  -> `iroh_base::RelayUrl`
    - `relay_map` -> use `iroh_relay::relay_map`

- `iroh`
  - `hash` ->  `iroh_base`
  - `ticket` -> `iroh_base::ticket`)
  - `relay` -> removed, use `iroh_relay` directly if needed
  - `key` -> `NodeId`, etc top level now
  - `endpoint::Bytes` removed use `bytes::Bytes`
  - `endpoint::NodeAddr`, use `iroh::NodeAddr`

## 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.
  • Loading branch information
dignifiedquire authored Dec 12, 2024
1 parent 6e009a8 commit d9fb470
Show file tree
Hide file tree
Showing 55 changed files with 224 additions and 212 deletions.
18 changes: 16 additions & 2 deletions iroh-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,24 @@ serde_test = "1"
[features]
default = ["hash", "ticket", "relay"]
hash = ["dep:blake3", "dep:data-encoding", "dep:postcard", "dep:derive_more", "base32"]
ticket = ["base32", "key"]
ticket = ["base32", "key", "hash"]
base32 = ["dep:data-encoding", "dep:postcard"]
redb = ["dep:redb"]
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"]
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",
"base32",
]
wasm = ["getrandom?/js"]
relay = ["dep:url", "dep:derive_more"]

Expand Down
27 changes: 20 additions & 7 deletions iroh-base/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
//! Base types and utilities for Iroh
#![cfg_attr(iroh_docsrs, feature(doc_auto_cfg))]

// TODO: remove
#[cfg(feature = "base32")]
pub mod base32;

// TODO: move to own crate
#[cfg(feature = "ticket")]
pub mod ticket;

#[cfg(feature = "hash")]
pub mod hash;
mod hash;
#[cfg(feature = "key")]
pub mod key;
mod key;
#[cfg(feature = "key")]
pub mod node_addr;
#[cfg(feature = "relay")]
pub mod relay_map;
mod node_addr;
#[cfg(feature = "relay")]
mod relay_url;
#[cfg(feature = "ticket")]
pub mod ticket;

#[cfg(feature = "hash")]
pub use self::hash::{BlobFormat, Hash, HashAndFormat};
#[cfg(feature = "key")]
pub use self::key::{
KeyParsingError, NodeId, PublicKey, SecretKey, SharedSecret, Signature, PUBLIC_KEY_LENGTH,
};
#[cfg(feature = "key")]
pub use self::node_addr::NodeAddr;
#[cfg(feature = "relay")]
pub use self::relay_url::RelayUrl;
3 changes: 1 addition & 2 deletions iroh-base/src/node_addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use std::{collections::BTreeSet, net::SocketAddr};

use serde::{Deserialize, Serialize};

use crate::key::{NodeId, PublicKey};
pub use crate::relay_url::RelayUrl;
use crate::{NodeId, PublicKey, RelayUrl};

/// Network-level addressing information for an iroh node.
///
Expand Down
2 changes: 1 addition & 1 deletion iroh-dns-server/benches/write.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use iroh::{discovery::pkarr::PkarrRelayClient, dns::node_info::NodeInfo, key::SecretKey};
use iroh::{discovery::pkarr::PkarrRelayClient, dns::node_info::NodeInfo, SecretKey};
use iroh_dns_server::{config::Config, server::Server, ZoneStore};
use tokio::runtime::Runtime;

Expand Down
3 changes: 1 addition & 2 deletions iroh-dns-server/examples/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use iroh::{
pkarr::{PkarrRelayClient, N0_DNS_PKARR_RELAY_PROD, N0_DNS_PKARR_RELAY_STAGING},
},
dns::node_info::{to_z32, NodeInfo, IROH_TXT_NAME},
key::SecretKey,
NodeId,
NodeId, SecretKey,
};
use url::Url;

Expand Down
2 changes: 1 addition & 1 deletion iroh-dns-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod tests {
use iroh::{
discovery::pkarr::PkarrRelayClient,
dns::{node_info::NodeInfo, DnsResolver, ResolverExt},
key::SecretKey,
SecretKey,
};
use pkarr::{PkarrClient, SignedPacket};
use testresult::TestResult;
Expand Down
2 changes: 0 additions & 2 deletions iroh-net-report/src/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Default values used in net_report.
pub(crate) use iroh_base::relay_map::{DEFAULT_RELAY_QUIC_PORT, DEFAULT_STUN_PORT};

/// Contains all timeouts that we use in `iroh-net-report`.
pub(crate) mod timeouts {
use std::time::Duration;
Expand Down
14 changes: 6 additions & 8 deletions iroh-net-report/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use std::{
use anyhow::{anyhow, Context as _, Result};
use bytes::Bytes;
use hickory_resolver::TokioResolver as DnsResolver;
use iroh_base::relay_map::{RelayMap, RelayNode, RelayUrl};
use iroh_base::RelayUrl;
#[cfg(feature = "metrics")]
use iroh_metrics::inc;
use iroh_relay::protos::stun;
use iroh_relay::{protos::stun, RelayMap};
use netwatch::{IpFamily, UdpSocket};
use tokio::{
sync::{self, mpsc, oneshot},
Expand Down Expand Up @@ -808,16 +808,13 @@ mod test_utils {
use std::sync::Arc;

use iroh_base::relay_map::QuicConfig;
use iroh_relay::server;

use crate::RelayNode;
use iroh_relay::{server, RelayNode, RelayQuicConfig};

pub(crate) async fn relay() -> (server::Server, Arc<RelayNode>) {
let server = server::Server::spawn(server::testing::server_config())
.await
.expect("should serve relay");
let quic = Some(QuicConfig {
let quic = Some(RelayQuicConfig {
port: server.quic_addr().expect("server should run quic").port(),
});
let node_desc = RelayNode {
Expand Down Expand Up @@ -864,14 +861,15 @@ mod tests {
use std::{net::IpAddr, sync::Arc};

use anyhow::Result;
use iroh_base::RelayUrl;
use iroh_relay::RelayNode;
use tokio::{
net,
sync::{oneshot, Mutex},
};
use tracing::{debug, trace};

use super::*;
use crate::{RelayMap, RelayNode, RelayUrl};

/// A drop guard to clean up test infrastructure.
///
Expand Down
11 changes: 8 additions & 3 deletions iroh-net-report/src/reportgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,15 @@ use std::{

use anyhow::{anyhow, bail, Context as _, Result};
use hickory_resolver::TokioResolver as DnsResolver;
use iroh_base::RelayUrl;
#[cfg(feature = "metrics")]
use iroh_metrics::inc;
use iroh_relay::{http::RELAY_PROBE_PATH, protos::stun};
use iroh_relay::{
defaults::{DEFAULT_RELAY_QUIC_PORT, DEFAULT_STUN_PORT},
http::RELAY_PROBE_PATH,
protos::stun,
RelayMap, RelayNode,
};
use netwatch::{interfaces, UdpSocket};
use rand::seq::IteratorRandom;
use tokio::{
Expand All @@ -45,10 +51,9 @@ use url::Host;
use crate::Metrics;
use crate::{
self as net_report,
defaults::{DEFAULT_RELAY_QUIC_PORT, DEFAULT_STUN_PORT},
dns::ResolverExt,
ping::{PingError, Pinger},
RelayMap, RelayNode, RelayUrl, Report,
Report,
};

mod hairpin;
Expand Down
4 changes: 3 additions & 1 deletion iroh-net-report/src/reportgen/probes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use std::{collections::BTreeSet, fmt, sync::Arc};

use anyhow::{ensure, Result};
use iroh_base::RelayUrl;
use iroh_relay::{RelayMap, RelayNode};
use netwatch::interfaces;
use tokio::time::Duration;

use crate::{RelayMap, RelayNode, RelayUrl, Report};
use crate::Report;

/// The retransmit interval used when net_report first runs.
///
Expand Down
2 changes: 1 addition & 1 deletion iroh-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ http = "1"
http-body-util = "0.1.0"
hyper = { version = "1", features = ["server", "client", "http1"] }
hyper-util = "0.1.1"
iroh-base = { version = "0.29.0", path = "../iroh-base", features = ["key"] }
iroh-base = { version = "0.29.0", path = "../iroh-base", default-features = false, features = ["key", "relay"] }
iroh-metrics = { version = "0.29.0", default-features = false }
libc = "0.2.139"
num_enum = "0.7"
Expand Down
3 changes: 1 addition & 2 deletions iroh-relay/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use hyper::{
Request,
};
use hyper_util::rt::TokioIo;
use iroh_base::key::{NodeId, PublicKey, SecretKey};
use iroh_base::{NodeId, PublicKey, RelayUrl, SecretKey};
use rand::Rng;
use rustls::client::Resumption;
use streams::{downcast_upgrade, MaybeTlsStream, ProxyStream};
Expand All @@ -46,7 +46,6 @@ use crate::{
defaults::timeouts::*,
http::{Protocol, RELAY_PATH},
protos::relay::DerpCodec,
RelayUrl,
};

pub(crate) mod conn;
Expand Down
2 changes: 1 addition & 1 deletion iroh-relay/src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use futures_util::{
stream::{SplitSink, SplitStream, StreamExt},
SinkExt,
};
use iroh_base::key::{NodeId, SecretKey};
use iroh_base::{NodeId, SecretKey};
use tokio::sync::mpsc;
use tokio_tungstenite_wasm::WebSocketStream;
use tokio_util::{
Expand Down
11 changes: 10 additions & 1 deletion iroh-relay/src/defaults.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
//! Default values used in the relay.
pub use iroh_base::relay_map::{DEFAULT_RELAY_QUIC_PORT, DEFAULT_STUN_PORT};
/// The default STUN port used by the Relay server.
///
/// The STUN port as defined by [RFC 8489](<https://www.rfc-editor.org/rfc/rfc8489#section-18.6>)
pub const DEFAULT_STUN_PORT: u16 = 3478;

/// The default QUIC port used by the Relay server to accept QUIC connections
/// for QUIC address discovery
///
/// The port is "QUIC" typed on a phone keypad.
pub const DEFAULT_RELAY_QUIC_PORT: u16 = 7842;

/// The default HTTP port used by the Relay server.
pub const DEFAULT_HTTP_PORT: u16 = 80;
Expand Down
14 changes: 9 additions & 5 deletions iroh-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ pub mod quic;
#[cfg(feature = "server")]
pub mod server;

mod relay_map;

#[cfg(test)]
mod dns;

pub use iroh_base::node_addr::RelayUrl;
pub use protos::relay::MAX_PACKET_SIZE;

pub use self::client::{
conn::{Conn as RelayConn, ReceivedMessage},
Client as HttpClient, ClientBuilder as HttpClientBuilder, ClientError as HttpClientError,
ClientReceiver as HttpClientReceiver,
pub use self::{
client::{
conn::{Conn as RelayConn, ReceivedMessage},
Client as HttpClient, ClientBuilder as HttpClientBuilder, ClientError as HttpClientError,
ClientReceiver as HttpClientReceiver,
},
relay_map::{RelayMap, RelayNode, RelayQuicConfig},
};
4 changes: 2 additions & 2 deletions iroh-relay/src/protos/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use std::time::Duration;

use anyhow::{bail, ensure};
use bytes::{Buf, BufMut, Bytes, BytesMut};
#[cfg(feature = "server")]
#[cfg(any(test, feature = "server"))]
use futures_lite::{Stream, StreamExt};
use futures_sink::Sink;
use futures_util::SinkExt;
use iroh_base::key::{PublicKey, SecretKey, Signature, PUBLIC_KEY_LENGTH};
use iroh_base::{PublicKey, SecretKey, Signature, PUBLIC_KEY_LENGTH};
use postcard::experimental::max_size::MaxSize;
use serde::{Deserialize, Serialize};
use tokio_util::codec::{Decoder, Encoder};
Expand Down
2 changes: 1 addition & 1 deletion iroh-relay/src/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl QuicClient {
}
}

#[cfg(test)]
#[cfg(all(test, feature = "server"))]
mod tests {
use std::net::Ipv4Addr;

Expand Down
27 changes: 9 additions & 18 deletions iroh-base/src/relay_map.rs → iroh-relay/src/relay_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,10 @@
use std::{collections::BTreeMap, fmt, sync::Arc};

use anyhow::{ensure, Result};
use iroh_base::RelayUrl;
use serde::{Deserialize, Serialize};

pub use crate::relay_url::RelayUrl;

/// The default STUN port used by the Relay server.
///
/// The STUN port as defined by [RFC 8489](<https://www.rfc-editor.org/rfc/rfc8489#section-18.6>)
pub const DEFAULT_STUN_PORT: u16 = 3478;

/// The default QUIC port used by the Relay server to accept QUIC connections
/// for QUIC address discovery
///
/// The port is "QUIC" typed on a phone keypad.
pub const DEFAULT_RELAY_QUIC_PORT: u16 = 7842;
use crate::defaults::{DEFAULT_RELAY_QUIC_PORT, DEFAULT_STUN_PORT};

/// Configuration of all the relay servers that can be used.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -77,7 +67,7 @@ impl RelayMap {
url,
stun_only: false,
stun_port,
quic: Some(QuicConfig::default()),
quic: Some(RelayQuicConfig::default()),
}
.into(),
);
Expand Down Expand Up @@ -138,21 +128,22 @@ pub struct RelayNode {
/// When `None`, we will not attempt to do QUIC address discovery
/// with this relay server.
#[serde(default = "quic_config")]
pub quic: Option<QuicConfig>,
pub quic: Option<RelayQuicConfig>,
}

fn quic_config() -> Option<QuicConfig> {
Some(QuicConfig::default())
fn quic_config() -> Option<RelayQuicConfig> {
Some(RelayQuicConfig::default())
}

/// Configuration for speaking to the QUIC endpoint on the relay
/// server to do QUIC address discovery.
#[derive(Debug, Deserialize, Serialize, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub struct QuicConfig {
pub struct RelayQuicConfig {
/// The port on which the connection should be bound to.
pub port: u16,
}

impl Default for QuicConfig {
impl Default for RelayQuicConfig {
fn default() -> Self {
Self {
port: DEFAULT_RELAY_QUIC_PORT,
Expand Down
4 changes: 2 additions & 2 deletions iroh-relay/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use http::{
};
use hyper::body::Incoming;
#[cfg(feature = "test-utils")]
use iroh_base::node_addr::RelayUrl;
use iroh_base::RelayUrl;
use iroh_metrics::inc;
use tokio::{
net::{TcpListener, UdpSocket},
Expand Down Expand Up @@ -760,7 +760,7 @@ mod tests {

use bytes::Bytes;
use http::header::UPGRADE;
use iroh_base::{key::SecretKey, node_addr::RelayUrl};
use iroh_base::SecretKey;

use super::*;
use crate::{
Expand Down
4 changes: 2 additions & 2 deletions iroh-relay/src/server/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{collections::HashMap, time::Duration};

use anyhow::{bail, Result};
use bytes::Bytes;
use iroh_base::key::NodeId;
use iroh_base::NodeId;
use iroh_metrics::{inc, inc_by};
use time::{Date, OffsetDateTime};
use tokio::sync::mpsc;
Expand Down Expand Up @@ -244,7 +244,7 @@ impl ClientCounter {
#[cfg(test)]
mod tests {
use bytes::Bytes;
use iroh_base::key::SecretKey;
use iroh_base::SecretKey;
use tokio::io::DuplexStream;
use tokio_util::codec::Framed;

Expand Down
Loading

0 comments on commit d9fb470

Please sign in to comment.