Repository navigation
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
Conversation
…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.
This was referenced Nov 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #44. Slice 1: a name as the peer.
What
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 not0xwith only hex digits. A bad name is bad usage (exit 2).net::Destinationis the peer of the command line: anet::PeerAddress, or anet::Namewith a port that is not 0 (NonZeroU16). OnlyName::newmakes a name.net::PeerAddressis an address that names a peer: its port is not 0, and its IP is not0.0.0.0or::. OnlyPeerAddress::newmakes one. The command line and the lookup both use it, so each address that elo dials names a peer.Network::default_portgives aNonZeroU16.net::handshaketakes 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 thatPeerAddress::newtakes, 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::addressgives that address.HandshakeError::Lookup. It retries after 5 s, as a failed connect does.HandshakeError::Connecthas the last address, its error, and the count of addresses:connect to <address>, the last of 3 addresses: <error>.endline has the destination in its own key,destination, so for a name it has the name.peeris always the address of a connection, on theversionandtiplines.destinationof theendline.peer_with_a_name_gets_a_lookup_and_a_dial_to_its_address:localhost:<port>reaches bitcoind in the first dial, and itsendhas 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, andlongest_destination_has_the_length_max.0x7f000001,127.0x1and0x0.nix flake checkpasses.cargo mutants --in-diffon 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 nodedupfailsdial_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 of0.0.0.0and::, no cap, no strip of the.at the end, and no hex inis_number.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.bealready 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 withlocalhostwould also be flaky.The lookup is in
net. The bound of 256 addresses and the loop that it bounds are in one module, soconnect_firstasserts it.Nameis in a module of its own, so no code can make a name without its check. Rejected: the lookup inmain. There, the cap and the loop are in two crates.Destination, and notPeer.net_processing::Peeris the peer of the protocol. A secondPeerinnetwould give one name two meanings. Core calls the string of a peerpszDest.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 innet.rsdo.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
mainusesshutdown_background.The
endline has the name, in the keydestination. Theendsays what elo dials again, and the next dial looks up the name again, so it can get another address. Theversionline has the address that took the connect. A key has one kind of value, so a query that joins anendto itsversionbypeerdoes not get a name in place of an address. Rejected: the name inpeer(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.0or::reaches this host, and a connect to port 0 fails each 5 s forever. Before,main::parsechecked the port and the IP, anddial_orderchecked the IP again. Code that makes aDestinationwith no parse, such as the seeds of the next slice, did not get the check. NowPeerAddress::newandNonZeroU16hold it, andparsehas 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,0x7f000001and127.0x1are each127.0.0.1, and0x0is0.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 as1a, 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, itsendline cut the port. A line stays under the 16 KiB of Docker.No new dependency.
tokio::net::lookup_hostis in thenetfeature, which elo already has. A seed name with services, such asx9.<seed>, is also only a name for the resolver.Not confident:
shutdown_background. A test cannot make a lookup that does not end without a name server of its own.