Skip to content

Commit

Permalink
feat!: split relay configuration between production and staging (#2425)
Browse files Browse the repository at this point in the history
- [x] regular tests
- [x] netsim (nothing to do, it uses its own local relays)
- [ ] ~~CLI integration tests~~

## Breaking Changes

- `iroh_net::defaults` is now split into `prod` and `staging`
- added `iroh_net::RelayMode::Staging`
- `iroh_net::discovery::dns:: N0_DNS_NODE_ORIGIN` is now
`N0_DNS_NODE_ORIGIN_PROD` and there is now `N0_DNS_NODE_ORIGIN_STAGING`

Closes #2420
  • Loading branch information
dignifiedquire authored Jul 2, 2024
1 parent cdad25a commit d421ece
Show file tree
Hide file tree
Showing 16 changed files with 145 additions and 55 deletions.
6 changes: 5 additions & 1 deletion iroh-cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{

use anyhow::{anyhow, bail, Context, Result};
use iroh::net::{
defaults::{default_eu_relay_node, default_na_relay_node},
defaults,
relay::{RelayMap, RelayNode},
};
use iroh::node::GcPolicy;
Expand Down Expand Up @@ -62,6 +62,10 @@ pub(crate) struct NodeConfig {

impl Default for NodeConfig {
fn default() -> Self {
#[cfg(not(test))]
use defaults::prod::{default_eu_relay_node, default_na_relay_node};
#[cfg(test)]
use defaults::staging::{default_eu_relay_node, default_na_relay_node};
Self {
// TODO(ramfox): this should probably just be a relay map
relay_nodes: [default_na_relay_node(), default_eu_relay_node()].into(),
Expand Down
7 changes: 5 additions & 2 deletions iroh-dns-server/examples/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{bail, Result};
use clap::{Parser, ValueEnum};
use iroh_net::{
discovery::{
dns::N0_DNS_NODE_ORIGIN,
dns::N0_DNS_NODE_ORIGIN_PROD,
pkarr_publish::{PkarrRelayClient, N0_DNS_PKARR_RELAY},
},
dns::node_info::{to_z32, NodeInfo, IROH_TXT_NAME},
Expand Down Expand Up @@ -85,7 +85,10 @@ async fn main() -> Result<()> {
match args.env {
Env::Default => {
println!(" cargo run --example resolve -- node {}", node_id);
println!(" dig {} TXT", fmt_domain(&node_id, N0_DNS_NODE_ORIGIN))
println!(
" dig {} TXT",
fmt_domain(&node_id, N0_DNS_NODE_ORIGIN_PROD)
)
}
Env::Dev => {
println!(
Expand Down
4 changes: 2 additions & 2 deletions iroh-dns-server/examples/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use hickory_resolver::{
AsyncResolver,
};
use iroh_net::{
discovery::dns::N0_DNS_NODE_ORIGIN,
discovery::dns::N0_DNS_NODE_ORIGIN_PROD,
dns::{node_info::TxtAttrs, DnsResolver},
NodeId,
};
Expand Down Expand Up @@ -45,7 +45,7 @@ async fn main() -> anyhow::Result<()> {
let (resolver, origin) = match args.env {
Env::Default => (
iroh_net::dns::default_resolver().clone(),
N0_DNS_NODE_ORIGIN,
N0_DNS_NODE_ORIGIN_PROD,
),
Env::Dev => (
resolver_with_nameserver(LOCALHOST_DNS.parse()?),
Expand Down
3 changes: 2 additions & 1 deletion iroh-gossip/examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,8 @@ impl FromStr for Ticket {
fn fmt_relay_mode(relay_mode: &RelayMode) -> String {
match relay_mode {
RelayMode::Disabled => "None".to_string(),
RelayMode::Default => "Default Relay servers".to_string(),
RelayMode::Default => "Default Relay (production) servers".to_string(),
RelayMode::Staging => "Default Relay (staging) servers".to_string(),
RelayMode::Custom(map) => map
.urls()
.map(|url| url.to_string())
Expand Down
109 changes: 79 additions & 30 deletions iroh-net/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ use url::Url;

use crate::relay::{RelayMap, RelayNode};

/// Hostname of the default NA relay.
pub const NA_RELAY_HOSTNAME: &str = "use1-1.relay.iroh.network.";
/// Hostname of the default EU relay.
pub const EU_RELAY_HOSTNAME: &str = "euw1-1.relay.iroh.network.";

/// The default STUN port used by the Relay server.
///
/// The STUN port as defined by [RFC
Expand All @@ -24,34 +19,88 @@ pub const DEFAULT_HTTPS_PORT: u16 = 443;
/// The default metrics port used by the Relay server.
pub const DEFAULT_METRICS_PORT: u16 = 9090;

/// Get the default [`RelayMap`].
pub fn default_relay_map() -> RelayMap {
RelayMap::from_nodes([default_na_relay_node(), default_eu_relay_node()])
.expect("default nodes invalid")
}
/// Production configuration.
pub mod prod {
use super::*;

/// Hostname of the default NA relay.
pub const NA_RELAY_HOSTNAME: &str = "use1-1.relay.iroh.network.";
/// Hostname of the default EU relay.
pub const EU_RELAY_HOSTNAME: &str = "euw1-1.relay.iroh.network.";

/// Get the default [`RelayMap`].
pub fn default_relay_map() -> RelayMap {
RelayMap::from_nodes([default_na_relay_node(), default_eu_relay_node()])
.expect("default nodes invalid")
}

/// Get the default [`RelayNode`] for NA.
pub fn default_na_relay_node() -> RelayNode {
// The default NA relay server run by number0.
let url: Url = format!("https://{NA_RELAY_HOSTNAME}")
.parse()
.expect("default url");
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
}
}

/// Get the default [`RelayNode`] for NA.
pub fn default_na_relay_node() -> RelayNode {
// The default NA relay server run by number0.
let url: Url = format!("https://{NA_RELAY_HOSTNAME}")
.parse()
.expect("default url");
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
/// Get the default [`RelayNode`] for EU.
pub fn default_eu_relay_node() -> RelayNode {
// The default EU relay server run by number0.
let url: Url = format!("https://{EU_RELAY_HOSTNAME}")
.parse()
.expect("default_url");
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
}
}
}

/// Get the default [`RelayNode`] for EU.
pub fn default_eu_relay_node() -> RelayNode {
// The default EU relay server run by number0.
let url: Url = format!("https://{EU_RELAY_HOSTNAME}")
.parse()
.expect("default_url");
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
/// Staging configuration.
///
/// Used by tests and might have incompatible changes deployed
pub mod staging {
use super::*;

/// Hostname of the default NA relay.
pub const NA_RELAY_HOSTNAME: &str = "staging-use1-1.relay.iroh.network.";
/// Hostname of the default EU relay.
pub const EU_RELAY_HOSTNAME: &str = "staging-euw1-1.relay.iroh.network.";

/// Get the default [`RelayMap`].
pub fn default_relay_map() -> RelayMap {
RelayMap::from_nodes([default_na_relay_node(), default_eu_relay_node()])
.expect("default nodes invalid")
}

/// Get the default [`RelayNode`] for NA.
pub fn default_na_relay_node() -> RelayNode {
// The default NA relay server run by number0.
let url: Url = format!("https://{NA_RELAY_HOSTNAME}")
.parse()
.expect("default url");
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
}
}

/// Get the default [`RelayNode`] for EU.
pub fn default_eu_relay_node() -> RelayNode {
// The default EU relay server run by number0.
let url: Url = format!("https://{EU_RELAY_HOSTNAME}")
.parse()
.expect("default_url");
RelayNode {
url: url.into(),
stun_only: false,
stun_port: DEFAULT_STUN_PORT,
}
}
}
18 changes: 14 additions & 4 deletions iroh-net/src/discovery/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ use crate::{
Endpoint, NodeId,
};

/// The n0 testing DNS node origin
pub const N0_DNS_NODE_ORIGIN: &str = "dns.iroh.link";
/// The n0 testing DNS node origin, for production.
pub const N0_DNS_NODE_ORIGIN_PROD: &str = "dns.iroh.link";
/// The n0 testing DNS node origin, for testing.
pub const N0_DNS_NODE_ORIGIN_STAGING: &str = "staging-dns.iroh.link";
const DNS_STAGGERING_MS: &[u64] = &[200, 300];

/// DNS node discovery
Expand Down Expand Up @@ -44,9 +46,17 @@ impl DnsDiscovery {
Self { origin_domain }
}

/// Create a new DNS discovery which uses the [`N0_DNS_NODE_ORIGIN`] origin domain.
/// Create a new DNS discovery which uses the [`N0_DNS_NODE_ORIGIN_PROD`] origin domain and in testing
/// uses [`N0_DNS_NODE_ORIGIN_STAGING`].
pub fn n0_dns() -> Self {
Self::new(N0_DNS_NODE_ORIGIN.to_string())
#[cfg(not(any(test, feature = "test-utils")))]
{
Self::new(N0_DNS_NODE_ORIGIN_PROD.to_string())
}
#[cfg(any(test, feature = "test-utils"))]
{
Self::new(N0_DNS_NODE_ORIGIN_STAGING.to_string())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ async fn stagger_call<T, F: Fn() -> Fut, Fut: Future<Output = Result<T>>>(
pub(crate) mod tests {
use std::sync::atomic::AtomicUsize;

use crate::defaults::NA_RELAY_HOSTNAME;
use crate::defaults::staging::NA_RELAY_HOSTNAME;

use super::*;
const TIMEOUT: Duration = Duration::from_secs(5);
Expand Down
6 changes: 4 additions & 2 deletions iroh-net/src/dns/node_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
//! - `<z32-node-id>` is the [z-base-32] encoding of the [`NodeId`].
//!
//! - `<origin-domain>` is the domain name of the publishing DNS server,
//! [`N0_DNS_NODE_ORIGIN`] is the server operated by number0.
//! [`N0_DNS_NODE_ORIGIN_PROD`] is the server operated by number0 for production.
//! [`N0_DNS_NODE_ORIGIN_STAGING`] is the server operated by number0 for testing.
//!
//! - `TXT` is the DNS record type.
//!
Expand All @@ -28,7 +29,8 @@
//! [z-base-32]: https://philzimmermann.com/docs/human-oriented-base-32-encoding.txt
//! [RFC1464]: https://www.rfc-editor.org/rfc/rfc1464
//! [`RelayUrl`]: iroh_base::node_addr::RelayUrl
//! [`N0_DNS_NODE_ORIGIN`]: crate::discovery::dns::N0_DNS_NODE_ORIGIN
//! [`N0_DNS_NODE_ORIGIN_PROD`]: crate::discovery::dns::N0_DNS_NODE_ORIGIN_PROD
//! [`N0_DNS_NODE_ORIGIN_STAGING`]: crate::discovery::dns::N0_DNS_NODE_ORIGIN_STAGING
use std::{
collections::{BTreeMap, BTreeSet},
Expand Down
15 changes: 11 additions & 4 deletions iroh-net/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use tracing::{debug, info_span, trace, warn};
use url::Url;

use crate::{
defaults::default_relay_map,
defaults,
discovery::{Discovery, DiscoveryTask},
dns::{default_resolver, DnsResolver},
key::{PublicKey, SecretKey},
Expand Down Expand Up @@ -85,9 +85,15 @@ pub struct Builder {

impl Default for Builder {
fn default() -> Self {
// Use staging in testing
#[cfg(not(any(test, feature = "test-utils")))]
let relay_mode = RelayMode::Default;
#[cfg(any(test, feature = "test-utils"))]
let relay_mode = RelayMode::Staging;

Self {
secret_key: Default::default(),
relay_mode: RelayMode::Default,
relay_mode,
alpn_protocols: Default::default(),
transport_config: Default::default(),
concurrent_connections: Default::default(),
Expand Down Expand Up @@ -118,7 +124,8 @@ impl Builder {
pub async fn bind(self, bind_port: u16) -> Result<Endpoint> {
let relay_map = match self.relay_mode {
RelayMode::Disabled => RelayMap::empty(),
RelayMode::Default => default_relay_map(),
RelayMode::Default => defaults::prod::default_relay_map(),
RelayMode::Staging => defaults::staging::default_relay_map(),
RelayMode::Custom(relay_map) => {
ensure!(!relay_map.is_empty(), "Empty custom relay server map",);
relay_map
Expand Down Expand Up @@ -369,7 +376,7 @@ impl Endpoint {

// # Methods relating to construction.

/// Returns the builder for an [`Endpoint`].
/// Returns the builder for an [`Endpoint`], with a production configuration.
pub fn builder() -> Builder {
Builder::default()
}
Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2750,7 +2750,7 @@ mod tests {
use iroh_test::CallOnDrop;
use rand::RngCore;

use crate::{defaults::EU_RELAY_HOSTNAME, relay::RelayMode, tls, Endpoint};
use crate::{defaults::staging::EU_RELAY_HOSTNAME, relay::RelayMode, tls, Endpoint};

use super::*;

Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/netcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ mod tests {
use tokio::time;
use tracing::info;

use crate::defaults::{DEFAULT_STUN_PORT, EU_RELAY_HOSTNAME};
use crate::defaults::{staging::EU_RELAY_HOSTNAME, DEFAULT_STUN_PORT};
use crate::ping::Pinger;
use crate::relay::RelayNode;

Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/netcheck/reportgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ mod tests {

use super::*;

use crate::defaults::{default_eu_relay_node, default_na_relay_node};
use crate::defaults::staging::{default_eu_relay_node, default_na_relay_node};

#[test]
fn test_update_report_stun_working() {
Expand Down
2 changes: 1 addition & 1 deletion iroh-net/src/netcheck/reportgen/probes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn sort_relays<'a>(
mod tests {
use pretty_assertions::assert_eq;

use crate::defaults::default_relay_map;
use crate::defaults::staging::default_relay_map;
use crate::netcheck::RelayLatencies;

use super::*;
Expand Down
4 changes: 3 additions & 1 deletion iroh-net/src/relay/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ use super::RelayUrl;
pub enum RelayMode {
/// Disable relay servers completely.
Disabled,
/// Use the default relay map, with relay servers from n0.
/// Use the default relay map, with production relay servers from n0.
Default,
/// Use the staging relay servers from n0.
Staging,
/// Use a custom relay map.
Custom(RelayMap),
}
Expand Down
16 changes: 14 additions & 2 deletions iroh/src/node/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,19 @@ fn mk_external_rpc() -> BoxedServerEndpoint {

impl Default for Builder<iroh_blobs::store::mem::Store> {
fn default() -> Self {
// Use staging in testing
#[cfg(not(any(test, feature = "test-utils")))]
let relay_mode = RelayMode::Default;
#[cfg(any(test, feature = "test-utils"))]
let relay_mode = RelayMode::Staging;

Self {
storage: StorageConfig::Mem,
bind_port: None,
secret_key: SecretKey::generate(),
blobs_store: Default::default(),
keylog: false,
relay_mode: RelayMode::Default,
relay_mode,
dns_resolver: None,
rpc_endpoint: mk_external_rpc(),
rpc_port: None,
Expand All @@ -210,13 +216,19 @@ impl<D: Map> Builder<D> {
docs_storage: DocsStorage,
storage: StorageConfig,
) -> Self {
// Use staging in testing
#[cfg(not(any(test, feature = "test-utils")))]
let relay_mode = RelayMode::Default;
#[cfg(any(test, feature = "test-utils"))]
let relay_mode = RelayMode::Staging;

Self {
storage,
bind_port: None,
secret_key: SecretKey::generate(),
blobs_store,
keylog: false,
relay_mode: RelayMode::Default,
relay_mode,
dns_resolver: None,
rpc_endpoint: mk_external_rpc(),
rpc_port: None,
Expand Down
Loading

0 comments on commit d421ece

Please sign in to comment.