Skip to content

feat: take a name as the peer, look it up before each dial, and dial its addresses in a random order until one takes the connect (#44) - #89

Merged
lorenzolfm merged 6 commits into
mainfrom
peer-name
Nov 14, 2026
Merged

lorenzolfm merged 6 commits into
mainfrom
peer-name

Conversation

@lorenzolfm

@lorenzolfm lorenzolfm commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Part of #44. Slice 1: a name as the peer.

What

  • The peer on the command line can be a name: elo testnet4 seed.testnet4.wiz.biz. A name has labels of letters, digits and -, has at most 253 bytes, and can end with .. Its last label is no number: not only digits, and not 0x with only hex digits. A bad name is bad usage (exit 2).
  • net::Destination is the peer of the command line: a net::PeerAddress, or a net::Name with a port that is not 0 (NonZeroU16). Only Name::new makes a name.
  • net::PeerAddress is an address that names a peer: its port is not 0, and its IP is not 0.0.0.0 or ::. Only PeerAddress::new makes one. The command line and the lookup both use it, so each address that elo dials names a peer. Network::default_port gives a NonZeroU16.
  • net::handshake takes the destination. Before each dial, it looks up a name with the resolver of the OS (tokio::net::lookup_host), within 30 s. It keeps the answers that PeerAddress::new takes, at most 256 of them, drops a second copy of an address, and puts them in a random order. net::Addresses { before, last } holds them, so a dial has at least one address with no check. It tries the TCP connect to each one in order, and does the handshake on the first that takes it. Connection::address gives that address.
  • A failed lookup is the new HandshakeError::Lookup. It retries after 5 s, as a failed connect does.
  • When each connect fails, HandshakeError::Connect has the last address, its error, and the count of addresses: connect to <address>, the last of 3 addresses: <error>.
  • The end line has the destination in its own key, destination, so for a name it has the name. peer is always the address of a connection, on the version and tip lines.
  • A log value has at most 260 bytes, and no longer 256. A const assert holds the longest destination under it.
  • The runtime shuts down in the background, so a stop does not wait for a lookup that is still on its thread.
  • README: the usage, the lookup, and the destination of the end line.
  • Tests:
    • L3 peer_with_a_name_gets_a_lookup_and_a_dial_to_its_address: localhost:<port> reaches bitcoind in the first dial, and its end has the name.
    • connect_takes_the_first_address_that_takes_it: a refused address before and after one that listens.
    • connect_to_no_address_gives_the_last_one_and_the_count.
    • dial_order_drops_the_answers_that_name_no_peer_and_keeps_at_most_the_count_max, also an answer with port 0.
    • dial_order_keeps_each_address_one_time.
    • lookup_gives_the_addresses_of_a_name_with_the_port, and longest_destination_has_the_length_max.
    • Rows for names in the parse test, also 0x7f000001, 127.0x1 and 0x0.
  • Proof:
    • 212 tests pass in a netns, 15 of them L3. nix flake check passes.
    • cargo mutants --in-diff on the whole PR (on d6f4730): 40 mutants, 28 caught, 12 unviable, 0 missed. On the 4 commits after it: 16 mutants, 8 caught, 8 unviable, 0 missed. A hand mutant with no dedup fails dial_order_keeps_each_address_one_time. 5 hand mutants, and a test catches each one: no try of the addresses before the last, no filter of 0.0.0.0 and ::, no cap, no strip of the . at the end, and no hex in is_number.
    • Real testnet4 sync by name (on 960ecc3): elo testnet4 seed.testnet4.bitcoin.sprovoost.nl. reached 161296 from the address that the seed gave. A SIGTERM then stopped it with exit 0.

Why

A name first, then the seeds. This is the smallest slice of #44. A DNS seed is a name, so elo mainnet seed.bitcoin.sipa.be already gets a peer from a seed. The next PR can make the peer optional, and use the seeds of the network when there is none. That is the "Usage" question of #44. The other questions of #44 (fixed seeds, more than one seed, how many peers) are still open.

Each address in one dial, as Core does for a name. Core puts the answers in a random order and tries each one until a connect works (net.cpp:406-408, :442, :508-511). Only a failed connect goes to the next address. A failed handshake ends the dial, as in Core. Rejected: one random address in each dial, and the next one after the wait of 5 s. On a host with no IPv6 route, a name with IPv4 and IPv6 addresses would then fail about half of its dials. With that, the L3 test with localhost would also be flaky.

The lookup is in net. The bound of 256 addresses and the loop that it bounds are in one module, so connect_first asserts it. Name is in a module of its own, so no code can make a name without its check. Rejected: the lookup in main. There, the cap and the loop are in two crates.

Destination, and not Peer. net_processing::Peer is the peer of the protocol. A second Peer in net would give one name two meanings. Core calls the string of a peer pszDest.

Our own random order. The resolver of the OS can sort the answers and keep them in a cache. Then each dial would start on the same address for the time of the cache. The order needs no secret, so it comes from the keys of RandomState, as the nonces in net.rs do.

At most 256 addresses. This is Core's limit for a name (Lookup(pszDest, default_port, ..., 256), net.cpp:406). Each try takes at most the connect timeout of 5 s, so a dial takes at most about 21 min. A signal still stops elo at once.

A lookup timeout of 30 s, and a shutdown in the background. By default, the resolver of glibc waits 5 s for a name server, and tries each of at most 3 servers two times. tokio cannot stop a lookup, so it stays on its blocking thread after the timeout or a stop. The drop of the runtime waits for each blocking thread, so main uses shutdown_background.

The end line has the name, in the key destination. The end says what elo dials again, and the next dial looks up the name again, so it can get another address. The version line has the address that took the connect. A key has one kind of value, so a query that joins an end to its version by peer does not get a name in place of an address. Rejected: the name in peer (the first version of this PR). Then that join was right for an IP address and wrong for a name. Rejected: a new event for the lookup. It adds an event to the log interface for one case.

An address that names a peer is a type. A connect to 0.0.0.0 or :: reaches this host, and a connect to port 0 fails each 5 s forever. Before, main::parse checked the port and the IP, and dial_order checked the IP again. Code that makes a Destination with no parse, such as the seeds of the next slice, did not get the check. Now PeerAddress::new and NonZeroU16 hold it, and parse has two checks less.

Each address of a lookup one time. A hosts file can have the same name and address on two lines. Without the drop, a dead address costs the connect timeout two times, and the count in the error counts it two times. The bound of 256 is on the answers before the drop.

A name has a strict form. The IPv4 parse of glibc also reads short forms, in decimal, octal and hex. 127.1, 0x7f000001 and 127.0x1 are each 127.0.0.1, and 0x0 is 0.0.0.0. That parse needs each part to be a number, so a last label that is no number goes to DNS. Rejected: a last label that starts with a letter. That rule is simpler, but it refuses a name such as 1a, or the hex ID that a container gets as its host name. A . at the end is accepted, because Core spells each seed name with one (kernel/chainparams.cpp:384-385).

The last address and the count in a failed connect. A dual-stack name can fail on each IPv4 address, and then on an IPv6 address with no route. The error of the last address alone does not show that. Rejected: the error of each address. 256 errors do not fit in one log value.

A log value of 260 bytes. The longest destination is a name of 253 bytes, its ., and :65535. With 256 bytes, its end line cut the port. A line stays under the 16 KiB of Docker.

No new dependency. tokio::net::lookup_host is in the net feature, which elo already has. A seed name with services, such as x9.<seed>, is also only a name for the resolver.

Not confident:

  • No test holds the random order or shutdown_background. A test cannot make a lookup that does not end without a name server of its own.
  • A peer from a seed is a stranger. Find peers: DNS seeds and hostnames #44 "Trust in a seed" is still open. Now a person who names a seed takes that risk.
  • The errors of the addresses before the last one are not in the log, only their count.

…its addresses in a random order until one takes the connect (#44)
…ve the destination, its lookup and the address bound into `net` (#44)

- `Name::new` refuses a last label that is a number: only digits, or `0x` and only hex digits. glibc reads `0x7f000001` and `127.0x1` as `127.0.0.1`, and `0x0` as `0.0.0.0`.
- `net::Destination` and `net::Name` take the place of `Peer` in `main`. Only `Name::new` makes a name. `Event::End.peer` is a `&Destination`.
- `net::handshake` takes the destination and does the lookup. `End::Lookup` is now `HandshakeError::Lookup`.
- `connect_first` asserts `ADDRESSES_COUNT_MAX`, in the module of the `take` in `dial_order`.
- `HandshakeError::Connect` has the last address and the count of addresses. `ConnectError` holds the `io` error or the timeout.
- `VALUE_LENGTH_MAX` is 260, and a const assert holds `DESTINATION_LENGTH_MAX` under it, so the `end` line of the longest name keeps its port.
- `address` is the name of each `SocketAddr`, and `peer` stays the peer of the protocol.
…ith `net::PeerAddress` and a port that is not 0 (#44)

- `net::PeerAddress` is a `SocketAddr` with a port that is not 0, and an IP that is not `0.0.0.0` or `::`. A connect to those reaches this host. Only `PeerAddress::new` makes one.
- `Destination::Address` holds a `PeerAddress`. `Destination::Name` has a `NonZeroU16` port, and `Network::default_port` gives one.
- `main::parse` loses its two checks of the port and the IP: `PeerAddress::new` and the parse of a `NonZeroU16` refuse them.
- `dial_order` keeps the answers that `PeerAddress::new` takes, so it also refuses port 0. Before, it refused only the IP.
- `connect_first` and `connect` take `PeerAddress`, so each dial goes to an address that names a peer, also from a writer that does not use `parse`, such as the seeds of the next slice.
…esses { before, last }` (#44)

- `net::Addresses` holds the addresses of one dial: `before`, then `last`. `connect_first` takes it, so its `panic!` for no address goes away.
- `dial_order` takes `last` with a `pop`. No address is `LookupError::Empty` there, in place of the check of an empty list.
- `HandshakeError::Connect.addresses_count` is `before.len() + 1`, so it is at least 1.
- `connect_first` still asserts the bound of `ADDRESSES_COUNT_MAX`, on `before`.
- `dial_order` drops an address that the lookup gives again, before the random order. A hosts file can have the same name and address on two lines, and a second try of a dead address costs up to the connect timeout of 5 s again. The count of `connect to <address>, the last of N addresses` also counted it two times.
- The bound of 256 is on the answers before the drop, so it still bounds the work of the lookup.
- Test: `dial_order_keeps_each_address_one_time`.
…always the address of a connection (#44)

- The `end` line has `destination=<the peer of the command line>`, in place of `peer=`. `Event::End.peer` is now `Event::End.destination`.
- Before, `peer` was an address on the `version` and `tip` lines and the name on the `end` line. A log query that joins an `end` to its `version` by `peer` got the join right for an IP address and wrong for a name.
- README: the `end` row and the example line.
@lorenzolfm
lorenzolfm merged commit 90fc14d into main Nov 14, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant