From 857f57e962b24f51621ddac188fa6e42d0ac6f41 Mon Sep 17 00:00:00 2001 From: bitflash-maintainer Date: Sun, 9 Aug 2026 02:43:44 +0000 Subject: pool: wait to be paired before wrapping the rendezvous socket Upstream #30 split rendezvous registration from waiting for a dial, so RvServiceRegister now returns as soon as the relay lists the service and no longer consumes the relay's 0x01 pairing byte. It updated its own caller in net.cpp and left this one behind. AcceptOneFn went straight from RvServiceRegister to BtfServiceWrap, so BtfServiceWrap read that 0x01 as the first byte of the miner's 32-byte ephemeral key and every byte after it was shifted by one. The X25519 handshake has no confirmation step, so it completed against a garbage shared secret, the tunnel was handed to the Stratum event loop, and nothing failed loudly: the pool logged a miner arriving and then dropped it as "never authorized", while the miner logged a pool that closed on it with gotSubscribeAck=0. Loopback miners were unaffected, which is exactly what made it look like a relay problem. Block in RvServiceWaitPaired before wrapping, and close and re-register if the relay drops us first. test_rvaccept.cpp covers both shapes against a real bitflashd on loopback: the pre-fix order establishes a tunnel that carries no bytes, the fixed order round-trips. Also corrects the btftunnel.h contract comment, which still described the pre-#30 behaviour and is what the old code was written against -- upstream's own test_relayproc.cpp, test_btftunnel.cpp and test_btfrv.cpp are still on that stale contract and fail the same way. (cherry picked from commit 3e3a36b2bf1e6ffb4861507089b743688619377f) --- src/rpc.cpp | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) (limited to 'src/rpc.cpp') diff --git a/src/rpc.cpp b/src/rpc.cpp index 3dd05bb..21a8d77 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -6,8 +6,8 @@ // Architecture: // PoolEventLoop -- single thread, owns all miner fds, all job/share/payout // state. No locks needed on any of that data. -// AcceptOneFn -- one thread per relay. Blocks on RvServiceRegister + -// BtfServiceWrap, pushes finished socket to gReadyQueue. +// AcceptOneFn -- one thread per relay. Registers, waits to be paired, +// wraps the tunnel, pushes finished socket to gReadyQueue. // AcceptLoopFn -- spawns AcceptOneFn threads, re-polls relay list every // 60s so dynamically discovered relays get picked up. // @@ -1388,12 +1388,11 @@ static void AcceptOneFn(void* arg) LogPrint("pool", "[pool] accept thread started for relay %s\n", relay.c_str()); while (!fShutdown && gPoolRunning) { - // Advertise this relay as the pool's meeting node BEFORE registering: - // RvServiceRegister blocks until a miner pairs, and miners can only - // find us if the published descriptor already names a relay where the + // Advertise this relay as the pool's meeting node BEFORE registering, + // so that miners which resolve our descriptor find a relay where the // pool (not the node's peer-accept) holds the registration. BtfSetActiveRelay(relay); - // Blocks until a miner connects to us at this relay + // Returns as soon as the relay lists us -- registered, not yet paired. btf::RvSocket rv = btf::RvServiceRegister(host.c_str(), (unsigned short)port, pk); if (fShutdown || !gPoolRunning) { @@ -1407,6 +1406,23 @@ static void AcceptOneFn(void* arg) continue; } + // Block for a miner to actually arrive. RvServiceRegister used to do + // this itself and returned with the relay's 0x01 pairing byte already + // consumed; it no longer does. Without waiting here, BtfServiceWrap + // reads that 0x01 as the first byte of the miner's 32-byte ephemeral + // key -- every subsequent byte is shifted by one, so the X25519 + // handshake completes against a garbage shared secret (it has no + // confirmation step to catch it), the tunnel is handed to the event + // loop, and the miner's mining.subscribe never decrypts into a valid + // line. The miner sees the pool close on it; the pool sees a peer that + // never authorized. + if (!btf::RvServiceWaitPaired(rv)) { + btf::RvClose(rv); + LogPrint("pool", "[pool] relay %s: dropped us before any miner dialed" + " -- re-registering\n", relay.c_str()); + continue; + } + // Echan handshake -- blocking but fast (local + relay round-trip) btf_socket_t fd = btf::BtfServiceWrap(rv, sk); if (fd == INVALID_SOCKET) { -- cgit v1.2.3