From f091fb2a9188f686e5b1f9012c510a1b3c8ad840 Mon Sep 17 00:00:00 2001 From: tv Date: Fri, 30 Oct 2026 16:32:23 +0100 Subject: move transport config checks to respective modules --- src/transport/libp2p.rs | 59 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 15 deletions(-) (limited to 'src/transport/libp2p.rs') diff --git a/src/transport/libp2p.rs b/src/transport/libp2p.rs index 2d67d2c..d023079 100644 --- a/src/transport/libp2p.rs +++ b/src/transport/libp2p.rs @@ -204,6 +204,48 @@ pub(crate) struct Libp2pConfig { pub(crate) memory: bool, } +/// The multiaddrs of option `option`. +fn parse_addresses(option: &str, addresses: &[String]) -> Result> { + addresses + .iter() + .map(|address| { + address + .parse::() + .with_context(|| format!("{option} {address:?}")) + }) + .collect() +} + +/// The ranges of `libp2p-ignore`. +fn parse_ranges(ranges: &[String]) -> Result> { + ranges + .iter() + .map(|range| range.parse().context("libp2p-ignore")) + .collect() +} + +/// This transport's options, checked when the configuration is loaded +/// (`config::Settings::check`), so `--print-config` tells a wrong one too: the +/// multiaddrs and ranges parse, and a relay that is no node (`--relay-only`) can be +/// dialled and keeps its peer ID. +pub(crate) fn check(settings: &crate::config::Settings) -> Result<()> { + parse_addresses("libp2p-listen", &settings.libp2p_listen)?; + parse_addresses("libp2p-announce", &settings.libp2p_announce)?; + parse_addresses("libp2p-relays", &settings.libp2p_relays)?; + parse_ranges(&settings.libp2p_ignore)?; + if settings.relay_only { + if settings.libp2p_listen.is_empty() { + bail!("relay-only needs libp2p-listen: where nodes reach the relay"); + } + if settings.libp2p_key_file.is_none() { + bail!( + "relay-only needs libp2p-key-file: without it, the relay's peer ID changes with every start and nodes lose it" + ); + } + } + Ok(()) +} + impl Libp2pConfig { /// The configuration `settings` give; `relay_only`: a relay that is no node /// (`--relay-only`). @@ -211,16 +253,7 @@ impl Libp2pConfig { settings: &crate::config::Settings, relay_only: bool, ) -> Result { - let parse = |option: &str, addresses: &[String]| { - addresses - .iter() - .map(|address| { - address - .parse::() - .with_context(|| format!("{option} {address:?}")) - }) - .collect::>>() - }; + let parse = parse_addresses; Ok(Self { key: Self::load_key(settings.libp2p_key_file.as_deref())?, listen: parse("libp2p-listen", &settings.libp2p_listen)?, @@ -230,11 +263,7 @@ impl Libp2pConfig { continuum: !relay_only, mdns: settings.libp2p_mdns, upnp: settings.libp2p_upnp, - ignore: settings - .libp2p_ignore - .iter() - .map(|range| range.parse().context("libp2p-ignore")) - .collect::>>()?, + ignore: parse_ranges(&settings.libp2p_ignore)?, public: settings.libp2p_public, memory: false, }) -- cgit v1.3.1