Enable Iroh local network discovery
This commit is contained in:
parent
280472cecb
commit
bfc5df698c
12 changed files with 136 additions and 16 deletions
|
|
@ -8,6 +8,7 @@ pub const DEFAULT_CONFIG_TOML: &str = r#"# geth local node config
|
|||
# Values: "default", "staging", "disabled", "custom".
|
||||
[iroh]
|
||||
relay_mode = "default"
|
||||
local_discovery = true
|
||||
# relay_map = "home"
|
||||
#
|
||||
# [iroh.relay_maps.home]
|
||||
|
|
@ -101,6 +102,8 @@ pub enum ConfigError {
|
|||
TomlParse(#[from] toml_edit::TomlError),
|
||||
#[error("invalid iroh relay_mode: {0}")]
|
||||
InvalidRelayMode(String),
|
||||
#[error("invalid iroh local_discovery value: {0}")]
|
||||
InvalidLocalDiscovery(String),
|
||||
#[error("custom iroh relay_mode requires iroh.relay_map")]
|
||||
MissingRelayMapSelection,
|
||||
#[error("selected iroh relay map does not exist: {0}")]
|
||||
|
|
@ -136,6 +139,7 @@ impl GethConfig {
|
|||
pub fn parse(text: &str) -> Result<Self, ConfigError> {
|
||||
let document = text.parse::<toml_edit::DocumentMut>()?;
|
||||
let relay_maps = parse_relay_maps(&document)?;
|
||||
let local_discovery = parse_local_discovery(&document)?;
|
||||
let relay_mode = match document.get("iroh").and_then(|iroh| iroh.get("relay_mode")) {
|
||||
Some(item) => {
|
||||
let value = item
|
||||
|
|
@ -150,15 +154,27 @@ impl GethConfig {
|
|||
iroh: IrohConfig {
|
||||
relay_mode,
|
||||
relay_maps,
|
||||
local_discovery,
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct IrohConfig {
|
||||
pub relay_mode: RelayMode,
|
||||
pub relay_maps: BTreeMap<String, RelayMapConfig>,
|
||||
pub local_discovery: bool,
|
||||
}
|
||||
|
||||
impl Default for IrohConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
relay_mode: RelayMode::Default,
|
||||
relay_maps: BTreeMap::new(),
|
||||
local_discovery: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
|
|
@ -269,6 +285,18 @@ fn validate_relay_url(map: &str, relay_url: &str) -> Result<(), ConfigError> {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_local_discovery(document: &toml_edit::DocumentMut) -> Result<bool, ConfigError> {
|
||||
match document
|
||||
.get("iroh")
|
||||
.and_then(|iroh| iroh.get("local_discovery"))
|
||||
{
|
||||
Some(item) => item
|
||||
.as_bool()
|
||||
.ok_or_else(|| ConfigError::InvalidLocalDiscovery(item.to_string())),
|
||||
None => Ok(true),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -277,6 +305,7 @@ mod tests {
|
|||
fn default_config_uses_iroh_relays() {
|
||||
let config = GethConfig::parse(GethConfig::default_toml()).expect("parse config");
|
||||
assert_eq!(config.iroh.relay_mode, RelayMode::Default);
|
||||
assert!(config.iroh.local_discovery);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -345,4 +374,16 @@ mod tests {
|
|||
.expect_err("error");
|
||||
assert!(matches!(error, ConfigError::UnknownRelayMap(map) if map == "home"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_parses_local_discovery_toggle() {
|
||||
let config = GethConfig::parse("[iroh]\nlocal_discovery = false\n").expect("parse");
|
||||
assert!(!config.iroh.local_discovery);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn config_rejects_non_bool_local_discovery() {
|
||||
let error = GethConfig::parse("[iroh]\nlocal_discovery = \"yes\"\n").expect_err("error");
|
||||
assert!(matches!(error, ConfigError::InvalidLocalDiscovery(_)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue