From b342f74dd64d7bc332fec584abab907afd8201f5 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 11:45:27 -0300 Subject: [PATCH 1/6] docs: a log value is at most 260 bytes (#44) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 87aac2f..4a815ae 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ level=info event=stop signal=SIGTERM The names of the events and of the fields are an interface, so a rename breaks the dashboards and alerts that match on them. `src/logging.rs` holds the one list of them. A value with a space, a `=`, -a `"` or a control character is in quotes, with escapes. A value is at most 256 bytes, so a line is +a `"` or a control character is in quotes, with escapes. A value is at most 260 bytes, so a line is at most 16 KiB, whatever the peer sends. When a write to stdout fails, elo writes the `exit` line to stderr only, because the write that failed can leave part of its line on stdout. From a373085797f4cf1d7de68b563f333c03f358c798 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 11:45:27 -0300 Subject: [PATCH 2/6] feat: with no peer, dial a seed of the network at random, and only its answers that the internet routes to (#44) --- README.md | 14 +- src/chainparams.rs | 31 ++++ src/logging.rs | 7 +- src/main.rs | 101 +++++++----- src/net.rs | 374 +++++++++++++++++++++++++++++++++++++++++---- src/protocol.rs | 3 + 6 files changed, 459 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 4a815ae..5de0a7b 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,12 @@ handshake on, it pings the peer every 2 min. It drops a peer that does not answer a `ping` within 20 min, or a `getheaders` within 2 min. It also drops a peer that sends `wtxidrelay` or `sendaddrv2` after the handshake. When a connection ends, elo logs the reason, waits 5 s and dials -the peer again. Each connection logs the `version` and the `tip` again. The +again. Each connection logs the `version` and the `tip` again. The chain stays in memory, so the next connection syncs from its tip. A chain below the minimum work starts again from genesis. ```sh -elo [:port] # IPv6 as [::1]:8333 +elo [[:port]] # IPv6 as [::1]:8333 ``` The peer is an IP address or a name. Before each dial, elo looks up the name @@ -60,6 +60,14 @@ connect. So a name with many addresses, such as a DNS seed, gives a new peer on each dial. A failed lookup is an `end`, and elo looks up the name again after 5 s. +With no peer, each dial looks up one DNS seed of the network, at random, as +`x9.`: a seed answers that name with nodes that serve each block and its +witness data. Some seeds have no such names, so when that lookup gives no +address to dial, elo looks up the seed itself. elo takes at most 32 of its +addresses that the internet routes to, so not this host, a private network, a +link or a test range. A seed chose those peers, and elo trusts them as much as +a peer that you name. Regtest has no seeds, so `elo regtest` needs a peer. + SIGTERM and SIGINT (Ctrl-C) stop elo at any step, also in the wait before a dial. It logs the signal and exits with 0. elo exits with 1 only on a fault of this host: its async runtime or its signal handlers do not start, a write to @@ -82,7 +90,7 @@ level=info event=stop signal=SIGTERM | --- | --- | --- | | `version` | `info` | `peer` `version` `services` `user_agent` `start_height` `relay` | | `tip` | `info` | `peer` `height` `hash` `work` | -| `end` | `warn` | `destination` `reason` `wait_ms`: elo dials `destination` again after `wait_ms`. `destination` is the peer of the command line, also a name. `peer` is always the address of a connection | +| `end` | `warn` | `destination` `reason` `wait_ms`: elo dials again after `wait_ms`. `destination` is what the dial looked up or dialed: the peer of the command line, also a name, or the seed, such as `seed.bitcoin.sipa.be.:8333`. `peer` is always the address of a connection | | `exit` | `error` | `reason`: a fault of this host, and elo exits with 1 | | `stop` | `info` | `signal`: elo exits with 0 | | `panic` | `error` | `reason`: a bug, and elo exits with 101 | diff --git a/src/chainparams.rs b/src/chainparams.rs index e28219a..da4ac10 100644 --- a/src/chainparams.rs +++ b/src/chainparams.rs @@ -69,6 +69,25 @@ const PORT_TESTNET4: std::num::NonZeroU16 = std::num::NonZeroU16::new(48333).exp const PORT_SIGNET: std::num::NonZeroU16 = std::num::NonZeroU16::new(38333).expect("not 0"); const PORT_REGTEST: std::num::NonZeroU16 = std::num::NonZeroU16::new(18444).expect("not 0"); +const SEEDS_MAINNET: [&str; 8] = [ + "seed.bitcoin.sipa.be.", + "dnsseed.bluematt.me.", + "seed.bitcoin.jonasschnelli.ch.", + "seed.btc.petertodd.net.", + "seed.bitcoin.sprovoost.nl.", + "dnsseed.emzy.de.", + "seed.bitcoin.wiz.biz.", + "seed.mainnet.achownodes.xyz.", +]; +const SEEDS_TESTNET4: [&str; 2] = [ + "seed.testnet4.bitcoin.sprovoost.nl.", + "seed.testnet4.wiz.biz.", +]; +const SEEDS_SIGNET: [&str; 2] = [ + "seed.signet.bitcoin.sprovoost.nl.", + "seed.signet.achownodes.xyz.", +]; + impl Network { /// The bytes that start every message. pub(crate) const fn magic(self) -> [u8; MAGIC_SIZE] { @@ -90,6 +109,18 @@ impl Network { } } + /// The names of the DNS seeds of the network. A seed answers the lookup of its name with the + /// addresses of nodes of the network that it chose. Regtest has no real network, so it has no + /// seeds. + pub(crate) const fn seeds(self) -> &'static [&'static str] { + match self { + Network::Mainnet => &SEEDS_MAINNET, + Network::Testnet4 => &SEEDS_TESTNET4, + Network::Signet => &SEEDS_SIGNET, + Network::Regtest => &[], + } + } + /// The highest target that a header of the network may have, big-endian. pub(crate) const fn pow_limit(self) -> [u8; 32] { match self { diff --git a/src/logging.rs b/src/logging.rs index 95203e5..8ac1bd6 100644 --- a/src/logging.rs +++ b/src/logging.rs @@ -55,9 +55,10 @@ pub enum Event<'a> { peer: std::net::SocketAddr, tip: &'a crate::chain::Tip, }, - /// The connection to `destination` ended for `reason`, and elo dials it again after `wait`. - /// `destination` is the peer of the command line: an IP address, or a name that elo looks up - /// again. It has its own key, so `peer` is always the address of a connection. + /// The connection to `destination` ended for `reason`, and elo dials again after `wait`. + /// `destination` is what the dial looked up or dialed: the IP address or the name of the + /// command line, or a seed of the network. It has its own key, so `peer` is always the address + /// of a connection. End { destination: &'a crate::net::Destination, reason: &'a dyn std::fmt::Display, diff --git a/src/main.rs b/src/main.rs index ba9dc6f..10f1c00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,22 @@ -//! `elo [:port]`: dials one peer, does the handshake, and logs the peer's -//! `version`. The host is an IP address or a name. Before each dial, elo looks up the name, and -//! dials its addresses in a random order until one takes the connect. Then it syncs the header -//! chain from the peer and logs the tip. It asks the peer to announce each new header, takes it on -//! the tip, and logs the tip when it moves. It pings the peer and answers its `ping` messages -//! until the connection ends. Then it waits and dials the peer again. The chain stays in memory, -//! so the next connection syncs from its tip. SIGTERM and SIGINT stop it at any of these steps. -//! Each step writes a line of `elo::logging` to stdout. +//! `elo [[:port]]`: dials one peer, does the handshake, and logs the peer's +//! `version`. The host is an IP address or a name. With no host, each dial takes a seed of the +//! network, at random. Before each dial, elo looks up the name or the seed, and dials its addresses +//! in a random order until one takes the connect. Then it syncs the header chain from the peer and +//! logs the tip. It asks the peer to announce each new header, takes it on the tip, and logs the +//! tip when it moves. It pings the peer and answers its `ping` messages until the connection ends. +//! Then it waits and dials again. The chain stays in memory, so the next connection syncs from its +//! tip. SIGTERM and SIGINT stop it at any of these steps. Each step writes a line of +//! `elo::logging` to stdout. -const USAGE: &str = "usage: elo [:port]"; +const USAGE: &str = "usage: elo [[:port]] +regtest has no seeds, so it needs a peer"; /// The exit code for bad usage. A stop by a signal exits with 0, and a panic with 101. Every /// other exit is 1, because only a fault of this host ends elo. const EXIT_USAGE: u8 = 2; -/// How long elo waits after a connection ends before it dials the peer again. So a peer that is -/// down, or that ends each connection at once, gets at most one try in each wait. +/// How long elo waits after a connection ends before it dials again. So a peer that is down, or +/// that ends each connection at once, gets at most one try in each wait. const RECONNECT_WAIT: std::time::Duration = std::time::Duration::from_secs(5); fn main() -> std::process::ExitCode { @@ -28,7 +30,7 @@ fn main() -> std::process::ExitCode { })); let args: Vec = std::env::args_os().skip(1).collect(); - let Some((network, destination)) = parse(&args) else { + let Some((network, source)) = parse(&args) else { eprintln!("{USAGE}"); return std::process::ExitCode::from(EXIT_USAGE); }; @@ -42,17 +44,18 @@ fn main() -> std::process::ExitCode { Ok(runtime) => runtime, Err(error) => return exit(&format_args!("runtime: {error}")), }; - let code = runtime.block_on(run(network, destination)); + let code = runtime.block_on(run(network, source)); // A lookup runs on a blocking thread of the runtime, and a stop or a lookup timeout does not // end it. The drop of the runtime would wait for that thread, so the runtime does not wait. runtime.shutdown_background(); code } -/// Dials `destination` again after each end that retries, until a signal or an end that exits. +/// Dials a destination of `source` again after each end that retries, until a signal or an end +/// that exits. async fn run( network: elo::chainparams::Network, - destination: elo::net::Destination, + source: elo::net::Source, ) -> std::process::ExitCode { // Before the first dial, so a signal from here on stops elo. As PID 1 in a container, a // process with no handler never sees a SIGTERM: the kernel drops it, and the stop ends in @@ -67,6 +70,7 @@ async fn run( // The loop has no count bound, because elo keeps its one peer for as long as it runs. Only // a signal or an end that a new connection cannot fix gets out. loop { + let destination = source.destination(); // The drop of the connection closes its socket. elo keeps nothing on disk, so the stop // has nothing else to finish. let end = match signals.unless(dial(&destination, &mut chain)).await { @@ -79,8 +83,8 @@ async fn run( if !end.retries() { return exit(&end); } - // The destination of the command line, and not the address of the connection: the next - // dial looks up a name again. + // The destination of the dial, and not the address of the connection: the next dial looks + // up a name again, and a seed shows which seed gave the peer. let logged = log(&elo::logging::Event::End { destination: &destination, reason: &end, @@ -281,15 +285,13 @@ async fn dial( } } -/// The args after the program name. `None` is bad usage. -/// -/// An IPv6 address needs brackets even without a port, because `::1:8333` is an IPv6 address -/// and not `::1` with a port. -fn parse( - args: &[std::ffi::OsString], -) -> Option<(elo::chainparams::Network, elo::net::Destination)> { - let [network, peer] = args else { - return None; +/// The args after the program name. `None` is bad usage. With no peer, the source is the seeds +/// of the network, so a network with no seeds needs a peer. +fn parse(args: &[std::ffi::OsString]) -> Option<(elo::chainparams::Network, elo::net::Source)> { + let (network, peer) = match args { + [network] => (network, None), + [network, peer] => (network, Some(peer)), + _ => return None, }; let network = match network.to_str()? { "mainnet" => elo::chainparams::Network::Mainnet, @@ -298,8 +300,18 @@ fn parse( "regtest" => elo::chainparams::Network::Regtest, _ => return None, }; + let source = match peer { + Some(peer) => elo::net::Source::Destination(destination(peer.to_str()?, network)?), + None => elo::net::Source::Seeds(elo::net::Seeds::new(network)?), + }; + Some((network, source)) +} - let peer = peer.to_str()?; +/// `peer` as the destination of each dial, or `None` if it is no IP address and no name. +/// +/// An IPv6 address needs brackets even without a port, because `::1:8333` is an IPv6 address +/// and not `::1` with a port. +fn destination(peer: &str, network: elo::chainparams::Network) -> Option { let destination = if let Some(address) = address(peer, network.default_port()) { // Port 0, `0.0.0.0`, `[::]` and `[::ffff:0.0.0.0]` name no peer. elo::net::Destination::Address(elo::net::PeerAddress::new(address)?) @@ -314,7 +326,7 @@ fn parse( port, } }; - Some((network, destination)) + Some(destination) } /// `peer` as an IP address with an optional port, or `None` if it is no IP address. @@ -331,7 +343,7 @@ fn address(peer: &str, default_port: std::num::NonZeroU16) -> Option Option<(elo::chainparams::Network, elo::net::Destination)> { + fn parse(args: &[&str]) -> Option<(elo::chainparams::Network, elo::net::Source)> { let args: Vec = args.iter().map(std::ffi::OsString::from).collect(); super::parse(&args) } @@ -390,7 +402,7 @@ mod tests { elo::net::Destination::Address(elo::net::PeerAddress::new(address).unwrap()); assert_eq!( parse(&[name, "127.0.0.1"]), - Some((network, destination)), + Some((network, elo::net::Source::Destination(destination))), "{name}" ); let destination = elo::net::Destination::Name { @@ -399,7 +411,7 @@ mod tests { }; assert_eq!( parse(&[name, "localhost"]), - Some((network, destination)), + Some((network, elo::net::Source::Destination(destination))), "{name}" ); } @@ -472,16 +484,37 @@ mod tests { ("0x7f.example", name("0x7f.example", 18444)), ("", None), ] { - let parsed = parse(&["regtest", peer]).map(|(_, destination)| destination); - assert_eq!(parsed, expected, "{peer}"); + let parsed = parse(&["regtest", peer]).map(|(_, source)| source); + assert_eq!( + parsed, + expected.map(elo::net::Source::Destination), + "{peer}" + ); + } + } + + #[test] + fn no_peer_is_the_seeds_of_the_network() { + for (name, network) in [ + ("mainnet", elo::chainparams::Network::Mainnet), + ("testnet4", elo::chainparams::Network::Testnet4), + ("signet", elo::chainparams::Network::Signet), + ] { + let seeds = elo::net::Seeds::new(network).unwrap(); + assert_eq!( + parse(&[name]), + Some((network, elo::net::Source::Seeds(seeds))), + "{name}" + ); } } #[test] - fn usage_is_a_network_name_and_a_peer() { + fn usage_is_a_network_name_and_a_peer_and_regtest_needs_the_peer() { for args in [ &[][..], &["regtest"], + &["testnet3"], &["testnet3", "127.0.0.1"], &["regtest", "127.0.0.1", "127.0.0.1"], ] { diff --git a/src/net.rs b/src/net.rs index 639014a..53426d5 100644 --- a/src/net.rs +++ b/src/net.rs @@ -25,6 +25,12 @@ const LOOKUP_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(30); /// this bounds the time of a dial. const ADDRESSES_COUNT_MAX: usize = 256; +/// The most addresses of a seed that one dial tries. A seed has many nodes, and the next dial asks +/// a seed again, so a seed whose answers are down costs at most this many tries. +const SEED_ADDRESSES_COUNT_MAX: usize = 32; + +const _: () = assert!(SEED_ADDRESSES_COUNT_MAX <= ADDRESSES_COUNT_MAX); + /// The most bytes of a name, with no `.` at the end. const NAME_LENGTH_MAX: usize = 253; @@ -36,8 +42,87 @@ const LABEL_LENGTH_MAX: usize = 63; /// IPv6 address with a scope. pub(crate) const DESTINATION_LENGTH_MAX: usize = NAME_LENGTH_MAX + ".".len() + ":65535".len(); -/// The peer that the command line names. +/// Where the destination of each dial comes from: the peer that the command line names, or the +/// seeds of the network when it names none. #[derive(Debug, PartialEq, Eq)] +pub enum Source { + /// The same destination for each dial. + Destination(Destination), + /// A seed of the network for each dial, at random. + Seeds(Seeds), +} + +impl Source { + /// The destination of the next dial. + pub fn destination(&self) -> std::borrow::Cow<'_, Destination> { + match self { + Source::Destination(destination) => std::borrow::Cow::Borrowed(destination), + Source::Seeds(seeds) => std::borrow::Cow::Owned(seeds.choose()), + } + } +} + +/// The seeds of a network that has seeds. Only `Seeds::new` makes one, so it has at least one. +#[derive(Debug, PartialEq, Eq)] +pub struct Seeds { + first: &'static str, + rest: &'static [&'static str], + port: std::num::NonZeroU16, +} + +impl Seeds { + /// The seeds of `network`, or `None` if it has none. + pub fn new(network: crate::chainparams::Network) -> Option { + let (&first, rest) = network.seeds().split_first()?; + Some(Seeds { + first, + rest, + port: network.default_port(), + }) + } + + /// One of the seeds, at random, at the default port of the network. The choice needs no + /// secret, so it comes from the keys of `RandomState`: the seed with the lowest hash. + fn choose(&self) -> Destination { + let random = std::hash::RandomState::new(); + let first = (self.first, random.hash_one(self.first)); + let (seed, _) = self.rest.iter().fold(first, |chosen, &seed| { + let hash = random.hash_one(seed); + if hash < chosen.1 { + (seed, hash) + } else { + chosen + } + }); + Destination::Seed { + seed: Seed(seed), + port: self.port, + } + } +} + +/// A seed of a network. Only `Seeds::choose` makes one, so it is on the list of its network. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Seed(&'static str); + +impl Seed { + /// The name of the seed with the services that elo asks for. A seed answers + /// `x.` with only the nodes that offer each one of them. Some seeds have + /// no such names, and answer only the seed itself. + fn services_name(&self) -> String { + let services = crate::protocol::ServiceFlags::NETWORK_WITNESS; + format!("x{services:x}.{}", self.0) + } +} + +impl std::fmt::Display for Seed { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{}", self.0) + } +} + +/// What one dial looks up and connects to. +#[derive(Clone, Debug, PartialEq, Eq)] pub enum Destination { /// An IP address, which `handshake` dials with no lookup. Address(PeerAddress), @@ -46,6 +131,13 @@ pub enum Destination { name: Name, port: std::num::NonZeroU16, }, + /// A seed, which `handshake` looks up with the services that elo asks for, and then without + /// them if that gives no address. Of its answers, it dials only the addresses that the + /// internet routes to. + Seed { + seed: Seed, + port: std::num::NonZeroU16, + }, } impl std::fmt::Display for Destination { @@ -53,6 +145,7 @@ impl std::fmt::Display for Destination { match self { Destination::Address(address) => write!(f, "{}", address.0), Destination::Name { name, port } => write!(f, "{name}:{port}"), + Destination::Seed { seed, port } => write!(f, "{seed}:{port}"), } } } @@ -75,7 +168,7 @@ impl PeerAddress { } /// The name of a host. Only `Name::new` makes one, so each name has the form that it checks. -#[derive(Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Name(String); impl Name { @@ -105,6 +198,42 @@ impl std::fmt::Display for Name { } } +/// Whether the internet routes to `ip`. The others are this host, a private network, a link, or +/// a range for documentation or tests. A node on the internet cannot have them, so a seed that +/// gives one is wrong, or points elo at a host that is not on the network. An IPv4 address in IPv6 +/// is checked as the IPv4 address. A multicast address passes, and its connect fails. +fn is_routable(ip: std::net::IpAddr) -> bool { + match ip.to_canonical() { + std::net::IpAddr::V4(ip) => { + let [first, second, _, _] = ip.octets(); + let is_this_network = first == 0; + let is_shared = first == 100 && (64..=127).contains(&second); + let is_benchmarking = first == 198 && (second == 18 || second == 19); + !(is_this_network + || ip.is_loopback() + || ip.is_private() + || ip.is_link_local() + || is_shared + || is_benchmarking + || ip.is_documentation() + || ip.is_broadcast()) + } + std::net::IpAddr::V6(ip) => { + let [first, second, ..] = ip.segments(); + let is_documentation = first == 0x2001 && second == 0x0db8; + // ORCHID, `2001:10::/28`, and ORCHIDv2, `2001:20::/28`: names that no route reaches. + let is_orchid = + first == 0x2001 && (second & 0xfff0 == 0x0010 || second & 0xfff0 == 0x0020); + !(ip.is_unspecified() + || ip.is_loopback() + || ip.is_unique_local() + || ip.is_unicast_link_local() + || is_documentation + || is_orchid) + } + } +} + /// Whether `label` is a label of a name: 1 to `LABEL_LENGTH_MAX` letters, digits and `-`, with no /// `-` at the start or the end. fn is_label(label: &str) -> bool { @@ -144,7 +273,7 @@ pub enum LookupError { Failed(std::io::Error), /// The lookup took longer than `LOOKUP_TIMEOUT`. Timeout, - /// The name has no address that names a peer. + /// The lookup gave no address that `handshake` may dial. Empty, } @@ -352,8 +481,8 @@ enum PartialFrame { }, } -/// Looks up `destination` if it is a name. Then dials the first of its addresses that takes the -/// TCP connect, in the order of `dial_order`, on the network of `chain`, and runs the handshake. +/// Looks up `destination` if it is a name or a seed. Then dials the first of its addresses that +/// takes the TCP connect, in a random order, on the network of `chain`, and runs the handshake. /// Returns the connection for the messages after it, and their `version`. The sync of the /// connection extends `chain` from its tip, so the next connection with the same chain does not /// get those headers again. A chain below the minimum work starts again from genesis. A peer @@ -363,17 +492,31 @@ pub async fn handshake<'a>( destination: &Destination, chain: &'a mut crate::chain::Chain, ) -> Result<(Connection<'a>, crate::protocol::Version), HandshakeError> { - let addresses = match destination { - Destination::Address(address) => Addresses { + let addresses = addresses(destination) + .await + .map_err(HandshakeError::Lookup)?; + let (address, stream) = connect_first(&addresses).await?; + handshake_stream(address, stream, chain).await +} + +/// The addresses that a dial to `destination` tries: its address, or the answers of its lookup in +/// the order of `name_order` or `seed_order`. +async fn addresses(destination: &Destination) -> Result { + match destination { + Destination::Address(address) => Ok(Addresses { before: Vec::new(), last: *address, - }, - Destination::Name { name, port } => { - lookup(name, *port).await.map_err(HandshakeError::Lookup)? + }), + Destination::Name { name, port } => name_order(lookup(&name.0, *port).await?), + Destination::Seed { seed, port } => { + // The error of the second lookup is the one that the dial gives. + let services = lookup(&seed.services_name(), *port).await; + match services.and_then(seed_order) { + Ok(addresses) => Ok(addresses), + Err(_) => seed_order(lookup(seed.0, *port).await?), + } } - }; - let (address, stream) = connect_first(&addresses).await?; - handshake_stream(address, stream, chain).await + } } /// The addresses that one dial tries, in order: each one of `before`, then `last`. So a dial has @@ -384,27 +527,52 @@ struct Addresses { last: PeerAddress, } -/// The addresses of `name` at `port`, as `dial_order` gives them. -async fn lookup(name: &Name, port: std::num::NonZeroU16) -> Result { - let answers = tokio::net::lookup_host((name.0.as_str(), port.get())); +/// The addresses of `host` at `port`, as the resolver of the OS gives them. +async fn lookup( + host: &str, + port: std::num::NonZeroU16, +) -> Result, LookupError> { + let answers = tokio::net::lookup_host((host, port.get())); match tokio::time::timeout(LOOKUP_TIMEOUT, answers).await { - Ok(Ok(answers)) => dial_order(answers), + Ok(Ok(answers)) => Ok(answers.collect()), Ok(Err(error)) => Err(LookupError::Failed(error)), Err(_elapsed) => Err(LookupError::Timeout), } } -/// The `answers` of a lookup that name a peer, at most `ADDRESSES_COUNT_MAX`, each one time, in a -/// random order. So each dial can start on another address of a name that has many, such as a -/// seed. The order needs no secret, so it comes from the keys of `RandomState`. -fn dial_order( - answers: impl Iterator, +/// The `answers` of the lookup of a name that name a peer, at most `ADDRESSES_COUNT_MAX`, in the +/// order of `dial_order`. The person who names the host chose its addresses, so a name can be on +/// this host or on a private network. +fn name_order( + answers: impl IntoIterator, ) -> Result { // Some resolvers give `0.0.0.0` or `::` for a name that they block. - let mut before: Vec = answers + let answers = answers.into_iter().filter_map(PeerAddress::new); + dial_order(answers, ADDRESSES_COUNT_MAX) +} + +/// The `answers` of the lookup of a seed that name a peer that the internet routes to, at most +/// `SEED_ADDRESSES_COUNT_MAX`, in the order of `dial_order`. The seed chose its answers, and not +/// the person who runs elo. +fn seed_order( + answers: impl IntoIterator, +) -> Result { + let routable = answers + .into_iter() .filter_map(PeerAddress::new) - .take(ADDRESSES_COUNT_MAX) - .collect(); + .filter(|address| is_routable(address.0.ip())); + dial_order(routable, SEED_ADDRESSES_COUNT_MAX) +} + +/// At most `count_max` of `answers`, each one time, in a random order. So each dial can start on +/// another address of a name that has many, such as a seed. The order needs no secret, so it comes +/// from the keys of `RandomState`. +fn dial_order( + answers: impl Iterator, + count_max: usize, +) -> Result { + assert!(count_max <= ADDRESSES_COUNT_MAX, "{count_max}"); + let mut before: Vec = answers.take(count_max).collect(); // A hosts file can have the same name and address on two lines. A second try of an address // costs up to `CONNECT_TIMEOUT` again. before.sort_unstable_by_key(|address| address.0); @@ -1716,14 +1884,14 @@ mod tests { } #[test] - fn dial_order_drops_the_answers_that_name_no_peer_and_keeps_at_most_the_count_max() { + fn name_order_drops_the_answers_that_name_no_peer_and_keeps_at_most_the_count_max() { let unspecified = [ std::net::SocketAddr::from(([0, 0, 0, 0], 1)), std::net::SocketAddr::from((std::net::Ipv6Addr::UNSPECIFIED, 1)), std::net::SocketAddr::from((std::net::Ipv4Addr::UNSPECIFIED.to_ipv6_mapped(), 1)), std::net::SocketAddr::from(([127, 0, 0, 1], 0)), ]; - let result = super::dial_order(unspecified.into_iter()); + let result = super::name_order(unspecified); assert!( matches!(result, Err(super::LookupError::Empty)), "{result:?}" @@ -1734,17 +1902,17 @@ mod tests { let answers: Vec = (1..=super::ADDRESSES_COUNT_MAX + 1) .map(|port| std::net::SocketAddr::from(([127, 0, 0, 1], u16::try_from(port).unwrap()))) .collect(); - let kept = super::dial_order(unspecified.into_iter().chain(answers.iter().copied())); + let kept = super::name_order(unspecified.into_iter().chain(answers.iter().copied())); let mut kept = dialed(kept.unwrap()); kept.sort_unstable_by_key(|address| address.0); assert_eq!(kept, peer_addresses(&answers[..super::ADDRESSES_COUNT_MAX])); } #[test] - fn dial_order_keeps_each_address_one_time() { + fn name_order_keeps_each_address_one_time() { let first = std::net::SocketAddr::from(([127, 0, 0, 1], 1)); let second = std::net::SocketAddr::from(([127, 0, 0, 1], 2)); - let addresses = super::dial_order([first, second, first, first].into_iter()).unwrap(); + let addresses = super::name_order([first, second, first, first]).unwrap(); let mut kept = dialed(addresses); kept.sort_unstable_by_key(|address| address.0); assert_eq!(kept, peer_addresses(&[first, second])); @@ -1754,13 +1922,157 @@ mod tests { #[test] fn lookup_gives_the_addresses_of_a_name_with_the_port() { let name = super::Name::new("localhost").unwrap(); - let addresses = block_on(super::lookup(&name, std::num::NonZeroU16::MIN)).unwrap(); + let answers = block_on(super::lookup(&name.0, std::num::NonZeroU16::MIN)).unwrap(); + let addresses = super::name_order(answers).unwrap(); for super::PeerAddress(address) in dialed(addresses) { assert!(address.ip().is_loopback(), "{address}"); assert_eq!(address.port(), 1, "{address}"); } } + #[test] + fn seed_order_keeps_at_most_the_seed_count_max_of_the_routable_answers() { + let loopback = std::net::SocketAddr::from(([127, 0, 0, 1], 1)); + let result = super::seed_order([loopback]); + assert!( + matches!(result, Err(super::LookupError::Empty)), + "{result:?}" + ); + + // One more than the count max, after one that the internet does not route to. Each port + // is new, so the ones that it keeps are the first ones. + let answers: Vec = (1..=super::SEED_ADDRESSES_COUNT_MAX + 1) + .map(|port| std::net::SocketAddr::from(([1, 1, 1, 1], u16::try_from(port).unwrap()))) + .collect(); + let kept = super::seed_order([loopback].into_iter().chain(answers.iter().copied())); + let mut kept = dialed(kept.unwrap()); + kept.sort_unstable_by_key(|address| address.0); + assert_eq!( + kept, + peer_addresses(&answers[..super::SEED_ADDRESSES_COUNT_MAX]) + ); + } + + #[test] + fn routable_is_each_address_but_this_host_private_networks_links_and_test_ranges() { + let v4 = |a, b, c, d| std::net::IpAddr::V4(std::net::Ipv4Addr::new(a, b, c, d)); + let v6 = |text: &str| std::net::IpAddr::V6(text.parse().unwrap()); + // The first and the last address of each range that the internet does not route to, and + // the addresses next to them. + for (ip, routable) in [ + (v4(0, 0, 0, 1), false), + (v4(0, 255, 255, 255), false), + (v4(1, 0, 0, 0), true), + (v4(9, 255, 255, 255), true), + (v4(10, 0, 0, 0), false), + (v4(10, 255, 255, 255), false), + (v4(11, 0, 0, 0), true), + (v4(100, 63, 255, 255), true), + (v4(100, 64, 0, 0), false), + (v4(100, 127, 255, 255), false), + (v4(100, 128, 0, 0), true), + (v4(126, 255, 255, 255), true), + (v4(127, 0, 0, 0), false), + (v4(127, 255, 255, 255), false), + (v4(128, 0, 0, 0), true), + (v4(169, 253, 255, 255), true), + (v4(169, 254, 0, 0), false), + (v4(169, 254, 255, 255), false), + (v4(169, 255, 0, 0), true), + (v4(172, 15, 255, 255), true), + (v4(172, 16, 0, 0), false), + (v4(172, 31, 255, 255), false), + (v4(172, 32, 0, 0), true), + (v4(192, 0, 1, 255), true), + (v4(192, 0, 2, 0), false), + (v4(192, 0, 2, 255), false), + (v4(192, 0, 3, 0), true), + (v4(192, 167, 255, 255), true), + (v4(192, 168, 0, 0), false), + (v4(192, 168, 255, 255), false), + (v4(192, 169, 0, 0), true), + (v4(198, 17, 255, 255), true), + (v4(198, 18, 0, 0), false), + (v4(198, 19, 255, 255), false), + (v4(198, 20, 0, 0), true), + (v4(198, 51, 99, 255), true), + (v4(198, 51, 100, 0), false), + (v4(198, 51, 100, 255), false), + (v4(198, 51, 101, 0), true), + (v4(203, 0, 112, 255), true), + (v4(203, 0, 113, 0), false), + (v4(203, 0, 113, 255), false), + (v4(203, 0, 114, 0), true), + (v4(255, 255, 255, 254), true), + (v4(255, 255, 255, 255), false), + (v6("::"), false), + (v6("::1"), false), + (v6("::2"), true), + (v6("::ffff:127.0.0.1"), false), + (v6("::ffff:10.0.0.1"), false), + (v6("::ffff:1.1.1.1"), true), + (v6("2001:f::"), true), + (v6("2001:10::"), false), + (v6("2001:2f:ffff:ffff:ffff:ffff:ffff:ffff"), false), + (v6("2001:30::"), true), + (v6("2001:db7:ffff:ffff:ffff:ffff:ffff:ffff"), true), + (v6("2001:db8::"), false), + (v6("2001:db8:ffff:ffff:ffff:ffff:ffff:ffff"), false), + (v6("2001:db9::"), true), + (v6("2a01::1"), true), + (v6("fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), true), + (v6("fc00::"), false), + (v6("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), false), + (v6("fe00::"), true), + (v6("fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), true), + (v6("fe80::"), false), + (v6("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), false), + (v6("fec0::"), true), + ] { + assert_eq!(super::is_routable(ip), routable, "{ip}"); + } + } + + #[test] + fn each_network_but_regtest_has_seeds_that_are_names_at_its_default_port() { + assert_eq!( + super::Seeds::new(crate::chainparams::Network::Regtest), + None + ); + for network in [ + crate::chainparams::Network::Mainnet, + crate::chainparams::Network::Testnet4, + crate::chainparams::Network::Signet, + ] { + let seeds = super::Seeds::new(network).unwrap(); + assert_eq!(seeds.port, network.default_port(), "{network:?}"); + for seed in std::iter::once(seeds.first).chain(seeds.rest.iter().copied()) { + let seed = super::Seed(seed); + assert_eq!(seed.services_name(), format!("x9.{seed}")); + for name in [seed.services_name(), seed.to_string()] { + assert!(super::Name::new(&name).is_some(), "{name}"); + } + } + } + } + + #[test] + fn a_seeds_source_chooses_each_seed_of_the_network() { + let network = crate::chainparams::Network::Mainnet; + let source = super::Source::Seeds(super::Seeds::new(network).unwrap()); + let mut chosen = std::collections::BTreeSet::new(); + // 8 seeds: a run of 1000 choices misses one of them once in about 2^190 runs. + for _ in 0..1000 { + let super::Destination::Seed { seed, port } = source.destination().into_owned() else { + panic!(); + }; + assert_eq!(port, network.default_port()); + chosen.insert(seed.0); + } + let seeds: std::collections::BTreeSet<&str> = network.seeds().iter().copied().collect(); + assert_eq!(chosen, seeds); + } + #[test] fn longest_destination_has_the_length_max() { let label = "a".repeat(super::LABEL_LENGTH_MAX); diff --git a/src/protocol.rs b/src/protocol.rs index 3f6ae10..bfaf06e 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -94,6 +94,9 @@ pub struct ServiceFlags(u64); impl ServiceFlags { /// No services. pub(crate) const NONE: Self = Self(0); + /// A node that serves each block, `NODE_NETWORK` (bit 0), with the witness of each + /// transaction, `NODE_WITNESS` (bit 3). + pub(crate) const NETWORK_WITNESS: Self = Self(0b1001); } impl std::fmt::LowerHex for ServiceFlags { From f573211832ead7d4bbdb43468f9b22cdca9e33fc Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 12:01:50 -0300 Subject: [PATCH 3/6] fix: end a seed dial at a timeout of its first lookup, and check the cap of a seed in `dial_order` (#44) - A timeout of the `x9.` lookup ends the dial. The resolver does not answer, so a lookup of the seed itself only waits `LOOKUP_TIMEOUT` again. A failed lookup or no address still looks up the seed itself. The match names each `LookupError`, so a new error needs a choice here. - The doc of `Destination::Seed` and the README say that a failed lookup also gives the lookup of the seed itself. Before, they said only "no address". - `lookup` gives the iterator of the resolver, and not a `Vec`. `name_order` and `seed_order` take at most their count max of it, so a lookup with many answers does not copy them all. - `dial_order` asserts `before.len() < count_max` after the `pop`. `connect_first` checks only `ADDRESSES_COUNT_MAX`, so before, no check held the cap of 32 for a seed. - `is_routable` is now `is_reserved`, with no `!` on its 8 and 6 terms. `seed_order` keeps an address that is not reserved. `is_benchmarking` uses a range, as `is_shared` does. --- README.md | 8 +++++--- src/net.rs | 48 +++++++++++++++++++++++++++++------------------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 5de0a7b..0e8940d 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,13 @@ after 5 s. With no peer, each dial looks up one DNS seed of the network, at random, as `x9.`: a seed answers that name with nodes that serve each block and its -witness data. Some seeds have no such names, so when that lookup gives no -address to dial, elo looks up the seed itself. elo takes at most 32 of its +witness data. Some seeds have no such names, so when that lookup fails or gives +no address to dial, elo looks up the seed itself. elo takes at most 32 of its addresses that the internet routes to, so not this host, a private network, a link or a test range. A seed chose those peers, and elo trusts them as much as -a peer that you name. Regtest has no seeds, so `elo regtest` needs a peer. +a peer that you name. A lookup that times out ends the dial at once, because +the resolver does not answer. Regtest has no seeds, so `elo regtest` needs a +peer. SIGTERM and SIGINT (Ctrl-C) stop elo at any step, also in the wait before a dial. It logs the signal and exits with 0. elo exits with 1 only on a fault of diff --git a/src/net.rs b/src/net.rs index 53426d5..8070f2b 100644 --- a/src/net.rs +++ b/src/net.rs @@ -132,8 +132,8 @@ pub enum Destination { port: std::num::NonZeroU16, }, /// A seed, which `handshake` looks up with the services that elo asks for, and then without - /// them if that gives no address. Of its answers, it dials only the addresses that the - /// internet routes to. + /// them if that lookup fails or gives no address. Of its answers, it dials only the addresses + /// that the internet routes to. Seed { seed: Seed, port: std::num::NonZeroU16, @@ -198,25 +198,25 @@ impl std::fmt::Display for Name { } } -/// Whether the internet routes to `ip`. The others are this host, a private network, a link, or -/// a range for documentation or tests. A node on the internet cannot have them, so a seed that +/// Whether the internet does not route to `ip`: this host, a private network, a link, or a range +/// for documentation or tests. A node on the internet cannot have such an address, so a seed that /// gives one is wrong, or points elo at a host that is not on the network. An IPv4 address in IPv6 -/// is checked as the IPv4 address. A multicast address passes, and its connect fails. -fn is_routable(ip: std::net::IpAddr) -> bool { +/// is checked as the IPv4 address. A multicast address is not reserved, and its connect fails. +fn is_reserved(ip: std::net::IpAddr) -> bool { match ip.to_canonical() { std::net::IpAddr::V4(ip) => { let [first, second, _, _] = ip.octets(); let is_this_network = first == 0; let is_shared = first == 100 && (64..=127).contains(&second); - let is_benchmarking = first == 198 && (second == 18 || second == 19); - !(is_this_network + let is_benchmarking = first == 198 && (18..=19).contains(&second); + is_this_network || ip.is_loopback() || ip.is_private() || ip.is_link_local() || is_shared || is_benchmarking || ip.is_documentation() - || ip.is_broadcast()) + || ip.is_broadcast() } std::net::IpAddr::V6(ip) => { let [first, second, ..] = ip.segments(); @@ -224,12 +224,12 @@ fn is_routable(ip: std::net::IpAddr) -> bool { // ORCHID, `2001:10::/28`, and ORCHIDv2, `2001:20::/28`: names that no route reaches. let is_orchid = first == 0x2001 && (second & 0xfff0 == 0x0010 || second & 0xfff0 == 0x0020); - !(ip.is_unspecified() + ip.is_unspecified() || ip.is_loopback() || ip.is_unique_local() || ip.is_unicast_link_local() || is_documentation - || is_orchid) + || is_orchid } } } @@ -509,11 +509,18 @@ async fn addresses(destination: &Destination) -> Result }), Destination::Name { name, port } => name_order(lookup(&name.0, *port).await?), Destination::Seed { seed, port } => { - // The error of the second lookup is the one that the dial gives. - let services = lookup(&seed.services_name(), *port).await; + let services_name = seed.services_name(); + let services = lookup(&services_name, *port).await; match services.and_then(seed_order) { Ok(addresses) => Ok(addresses), - Err(_) => seed_order(lookup(seed.0, *port).await?), + // The resolver does not answer, so a lookup of the seed itself would only wait + // `LOOKUP_TIMEOUT` again. + Err(LookupError::Timeout) => Err(LookupError::Timeout), + // A seed with no such names fails the lookup. The error of the lookup of the seed + // itself is the one that the dial gives. + Err(LookupError::Failed(_) | LookupError::Empty) => { + seed_order(lookup(seed.0, *port).await?) + } } } } @@ -527,14 +534,15 @@ struct Addresses { last: PeerAddress, } -/// The addresses of `host` at `port`, as the resolver of the OS gives them. +/// The addresses of `host` at `port`, as the resolver of the OS gives them. The caller takes at +/// most its count max of them, so it does not copy the others. async fn lookup( host: &str, port: std::num::NonZeroU16, -) -> Result, LookupError> { +) -> Result, LookupError> { let answers = tokio::net::lookup_host((host, port.get())); match tokio::time::timeout(LOOKUP_TIMEOUT, answers).await { - Ok(Ok(answers)) => Ok(answers.collect()), + Ok(Ok(answers)) => Ok(answers), Ok(Err(error)) => Err(LookupError::Failed(error)), Err(_elapsed) => Err(LookupError::Timeout), } @@ -560,7 +568,7 @@ fn seed_order( let routable = answers .into_iter() .filter_map(PeerAddress::new) - .filter(|address| is_routable(address.0.ip())); + .filter(|address| !is_reserved(address.0.ip())); dial_order(routable, SEED_ADDRESSES_COUNT_MAX) } @@ -582,6 +590,8 @@ fn dial_order( let Some(last) = before.pop() else { return Err(LookupError::Empty); }; + // `connect_first` checks only `ADDRESSES_COUNT_MAX`, so the cap of a seed is checked here. + assert!(before.len() < count_max, "{} {count_max}", before.len()); Ok(Addresses { before, last }) } @@ -2029,7 +2039,7 @@ mod tests { (v6("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), false), (v6("fec0::"), true), ] { - assert_eq!(super::is_routable(ip), routable, "{ip}"); + assert_eq!(super::is_reserved(ip), !routable, "{ip}"); } } From a6ef34696c03e27af1942e6d684b018fc150b5b7 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 12:30:08 -0300 Subject: [PATCH 4/6] refactor: make the cap of `dial_order` a const parameter, so the compiler checks it against `ADDRESSES_COUNT_MAX` (#44) - `dial_order::` takes the cap as a const parameter. Its only values are `ADDRESSES_COUNT_MAX` and `SEED_ADDRESSES_COUNT_MAX`, so a run-time `assert!` on the argument checked a fact that the build knows. - `const { assert!(COUNT_MAX <= ADDRESSES_COUNT_MAX) }` checks each cap when it compiles. A cap of 257 does not compile. - The `const _` assert on `SEED_ADDRESSES_COUNT_MAX` goes: the check in `dial_order` holds each cap that a caller uses. - The assert of `before.len() < COUNT_MAX` after the `pop` stays. --- src/net.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/net.rs b/src/net.rs index 8070f2b..fff157d 100644 --- a/src/net.rs +++ b/src/net.rs @@ -29,8 +29,6 @@ const ADDRESSES_COUNT_MAX: usize = 256; /// a seed again, so a seed whose answers are down costs at most this many tries. const SEED_ADDRESSES_COUNT_MAX: usize = 32; -const _: () = assert!(SEED_ADDRESSES_COUNT_MAX <= ADDRESSES_COUNT_MAX); - /// The most bytes of a name, with no `.` at the end. const NAME_LENGTH_MAX: usize = 253; @@ -556,7 +554,7 @@ fn name_order( ) -> Result { // Some resolvers give `0.0.0.0` or `::` for a name that they block. let answers = answers.into_iter().filter_map(PeerAddress::new); - dial_order(answers, ADDRESSES_COUNT_MAX) + dial_order::(answers) } /// The `answers` of the lookup of a seed that name a peer that the internet routes to, at most @@ -569,18 +567,18 @@ fn seed_order( .into_iter() .filter_map(PeerAddress::new) .filter(|address| !is_reserved(address.0.ip())); - dial_order(routable, SEED_ADDRESSES_COUNT_MAX) + dial_order::(routable) } -/// At most `count_max` of `answers`, each one time, in a random order. So each dial can start on +/// At most `COUNT_MAX` of `answers`, each one time, in a random order. So each dial can start on /// another address of a name that has many, such as a seed. The order needs no secret, so it comes /// from the keys of `RandomState`. -fn dial_order( +fn dial_order( answers: impl Iterator, - count_max: usize, ) -> Result { - assert!(count_max <= ADDRESSES_COUNT_MAX, "{count_max}"); - let mut before: Vec = answers.take(count_max).collect(); + // `connect_first` takes at most `ADDRESSES_COUNT_MAX`, so each cap fits in it. + const { assert!(COUNT_MAX <= ADDRESSES_COUNT_MAX) }; + let mut before: Vec = answers.take(COUNT_MAX).collect(); // A hosts file can have the same name and address on two lines. A second try of an address // costs up to `CONNECT_TIMEOUT` again. before.sort_unstable_by_key(|address| address.0); @@ -591,7 +589,7 @@ fn dial_order( return Err(LookupError::Empty); }; // `connect_first` checks only `ADDRESSES_COUNT_MAX`, so the cap of a seed is checked here. - assert!(before.len() < count_max, "{} {count_max}", before.len()); + assert!(before.len() < COUNT_MAX, "{} {COUNT_MAX}", before.len()); Ok(Addresses { before, last }) } From 7457beefba6f96acd31888030ccc828e9bd448a8 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 12:31:25 -0300 Subject: [PATCH 5/6] feat: name the host of a lookup that timed out, so the `end` line of a seed shows which of its two lookups did not end (#44) - `LookupError::Timeout` has the `host` that `lookup` looked up. Its text is `the lookup of took longer than 30 s`. - A dial to a seed looks up `x9.`, and then the seed itself. Before, a timeout of each one gave the same `reason`, and the `destination` of the `end` line is the seed itself. So a timeout of `x9.` looked like a timeout of the seed, which can answer. - The seed arm of `addresses` gives the timeout of `x9.` as it is, with no copy of the variant. - Rejected: a `ServicesTimeout` variant only for `x9.`. The match on the result of `seed_order` then needs an arm for a variant that `lookup` never gives. --- src/main.rs | 6 ++++-- src/net.rs | 15 +++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 10f1c00..6ce74a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -217,7 +217,7 @@ impl End { End::Handshake( elo::net::HandshakeError::Lookup( elo::net::LookupError::Failed(_) - | elo::net::LookupError::Timeout + | elo::net::LookupError::Timeout { .. } | elo::net::LookupError::Empty, ) | elo::net::HandshakeError::Connect { @@ -369,7 +369,9 @@ mod tests { ), ( super::End::Handshake(elo::net::HandshakeError::Lookup( - elo::net::LookupError::Timeout, + elo::net::LookupError::Timeout { + host: String::from("localhost"), + }, )), true, ), diff --git a/src/net.rs b/src/net.rs index fff157d..25e7c7e 100644 --- a/src/net.rs +++ b/src/net.rs @@ -269,8 +269,9 @@ fn is_number(label: &str) -> bool { pub enum LookupError { /// The resolver of the OS failed. Failed(std::io::Error), - /// The lookup took longer than `LOOKUP_TIMEOUT`. - Timeout, + /// The lookup of `host` took longer than `LOOKUP_TIMEOUT`. A dial to a seed has two lookups, + /// so the error names the one that did not end. + Timeout { host: String }, /// The lookup gave no address that `handshake` may dial. Empty, } @@ -279,9 +280,9 @@ impl std::fmt::Display for LookupError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { LookupError::Failed(error) => write!(f, "lookup: {error}"), - LookupError::Timeout => write!( + LookupError::Timeout { host } => write!( f, - "the lookup took longer than {} s", + "the lookup of {host} took longer than {} s", LOOKUP_TIMEOUT.as_secs() ), LookupError::Empty => write!(f, "the lookup gave no address"), @@ -513,7 +514,7 @@ async fn addresses(destination: &Destination) -> Result Ok(addresses) => Ok(addresses), // The resolver does not answer, so a lookup of the seed itself would only wait // `LOOKUP_TIMEOUT` again. - Err(LookupError::Timeout) => Err(LookupError::Timeout), + Err(error @ LookupError::Timeout { .. }) => Err(error), // A seed with no such names fails the lookup. The error of the lookup of the seed // itself is the one that the dial gives. Err(LookupError::Failed(_) | LookupError::Empty) => { @@ -542,7 +543,9 @@ async fn lookup( match tokio::time::timeout(LOOKUP_TIMEOUT, answers).await { Ok(Ok(answers)) => Ok(answers), Ok(Err(error)) => Err(LookupError::Failed(error)), - Err(_elapsed) => Err(LookupError::Timeout), + Err(_elapsed) => Err(LookupError::Timeout { + host: host.to_owned(), + }), } } From 11672ee9062e0b37890285fdb9836bf935da8ce4 Mon Sep 17 00:00:00 2001 From: Lorenzo Date: Sat, 14 Nov 2026 12:40:21 -0300 Subject: [PATCH 6/6] refactor: choose a seed with `std::cmp::min` on its hash, so no comparison is left that a mutant can flip with no change of the choice (#44) --- src/net.rs | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/net.rs b/src/net.rs index 25e7c7e..3608755 100644 --- a/src/net.rs +++ b/src/net.rs @@ -83,15 +83,12 @@ impl Seeds { /// secret, so it comes from the keys of `RandomState`: the seed with the lowest hash. fn choose(&self) -> Destination { let random = std::hash::RandomState::new(); - let first = (self.first, random.hash_one(self.first)); - let (seed, _) = self.rest.iter().fold(first, |chosen, &seed| { - let hash = random.hash_one(seed); - if hash < chosen.1 { - (seed, hash) - } else { - chosen - } - }); + let first = (random.hash_one(self.first), self.first); + let (_, seed) = self + .rest + .iter() + .map(|&seed| (random.hash_one(seed), seed)) + .fold(first, std::cmp::min); Destination::Seed { seed: Seed(seed), port: self.port,