Closed. The stalled handshake this document chased was this project's own:chains.Regtest()returned the parent chain's network magic, so every peer the instance opened to a regtest node completed the key exchange and then had its packets dropped, which reads on the client as a handshake that reads one byte at a time and then gets EOF. With the magic the node actually uses (0xdab5bffa), the instance handshakes in milliseconds, syncs headers to the tip and fills bodies.TestDaemonFillsFromANodepasses. Nothing in btcd is implicated. The diagnosis below is retracted; what follows records the measurements that were taken while the cause was still unknown, and the ones that ruled btcd out.
Module: github.com/btcsuite/btcd/v2transport v1.1.0.
Function: (*Peer).CompleteHandshake, v2transport/transport.go.
Status: open, not diagnosed. This document records what is measured and retracts an earlier diagnosis of it that the measurements do not support.
Against Bitcoin Core 30.3 (regtest, BIP324 on by default), the instance's handshake reaches the key exchange and then reads one byte at a time until the node closes the socket:
fill: connected to 127.0.0.1:19264
transport wrote 1064 bytes the initiator's ellswift key and handshake
transport read 64 bytes the node's ellswift key
transport wrote 16 bytes the node's garbage terminator, echoed
transport wrote 20 bytes the version packet
transport read 16 bytes
transport read 1 bytes
transport read 1 bytes ... for about sixty seconds
fill: v2peer: v2 handshake: EOF
The node's own log for that peer says V2 handshake timeout, disconnecting
after sixty seconds. So the node waits, the client waits, and neither completes.
An earlier version of this document said the cause was btcd's garbage scan,
which compares its accumulated buffer's last sixteen bytes against the
garbage terminator and therefore cannot find a terminator that arrives with a
packet behind it. That claim is supported by a real asymmetry - bitcoind's
probe answers a v2 key exchange with 128 bytes, and bitcoind writes its
terminator and its packet together - but it is not what blocks the
connection, because btcd's scanner does not fail under that timing:
| connection | two btcd peers, CompleteHandshake |
|---|---|
bitcoind 30.3 over loopback | fails after sixty seconds |
| two btcd peers over a channel that hands every queued write over at once | completes, reads [64, 16, 3, 17] |
| two btcd peers over a loopback TCP socket | completes, reads [64, 16, 3, 17, 3, 17] |
| two btcd peers through a proxy that joins every queued write into one send | completes |
The read sizes show the coalescing happens in the fixtures; the scanner finds the terminator anyway. So the scan is not sufficient to explain the failure.
is not exercised by any of them, and the failing side is Core. Core may expect something of the client's stream that btcd does not send, or send something btcd does not expect.
v2peer.Dial hands CompleteHandshakethe socket the daemon dialed; the fixtures hand it channels and loopback sockets with no deadline. A read deadline set on the socket before the handshake is the one environmental difference not yet excluded.
Run the daemon's dial against a bitcoind on a machine where the failure does
not occur, or run the fixtures against a bitcoind in the same process. Either
one turns item 1 into a fact. Until then, this document names a failure and the
measurements that do not reproduce it, and nothing more.
The scan above is not the whole story and this version of it is corrected by
measurement. Two of btcd's own v2transport.Peer instances, in one process,
over a loopback TCP pair, with counting wrappers on both sockets:
dialler: CompleteHandshake ok, socket wrote 100
dialler: Send(version) returned 108, socket wrote 208
responder: CompleteHandshake ok, socket wrote 120
responder: Send(version) returned 108, socket wrote 228
dialler wrote 208 read 228; responder wrote 228 read 208
(neither V2ReceivePacket ever returned)
Both peers complete the handshake, both write their version packet - the bytes
are on the wire and are read by the other side - and neither peer's
V2ReceivePacket below them ever sees it. The packet is consumed by the
terminator scan and discarded.
CompleteHandshake reads 16 bytes, then one byte at a time, comparing the last
sixteen accumulated bytes against recvGarbageTerm. On a match it hands
recvGarbage[:len-16] to V2ReceivePacket and returns - everything the peer
wrote after its terminator, which is where a peer's first real packet goes,
is dropped on the floor. A packet that arrives before the scan finishes is lost
with no error and no way for a caller to recover it, because CompleteHandshake
returns only an error and retains nothing.
Against Bitcoin Core this never shows: Core's stream leaves the socket empty at the moment the scan reads, so the packet arrives after the scan has returned. It shows every time between two btcd peers on loopback, where the packet is already buffered. That is why every gate that talks to a node passes and the in-process relay test does not.
CompleteHandshake should retain the bytes it read past the terminator and
offer them to the next V2ReceivePacket, or return them to the caller. Either
is a small change to transport.go; the stream state to hold them already
exists as p.recvGarbage.
The instance's peer path is measured against live nodes and passes, so the
workaround is not to change the handshake but to test the relay against a real
node, which is what internal/gate does for the daemon and the checkpoint
chain. The in-process fake peer is not a usable instrument until this is fixed,
and the gate that used it says so.