Skip to content

friglet: peer pool with discovery and failover; own node optional (SNB-855) - #85

Open
setavenger wants to merge 6 commits into
snb-854-kernel-node-p2pfrom
snb-855-peer-pool
Open

setavenger wants to merge 6 commits into
snb-854-kernel-node-p2pfrom
snb-855-peer-pool

Conversation

@setavenger

@setavenger setavenger commented Oct 30, 2026 •

Copy link
Copy Markdown
Owner

Stacked on #84 (snb-854-kernel-node-p2p); merge that first. Implements SNB-855.

What and why

p2p_node_addr becomes optional. With it set, friglet talks only to that node (blocks, headers, broadcast), with the same retries, timeouts and error messages as before. Left empty, friglet finds peers of the network through its DNS seeds and address gossip and asks them one after another, so a user without their own node can still scan. Regtest still requires a node. No Tor yet (SNB-857) and broadcast is still single-peer (SNB-856).

  • blindbit-lib::net (new): PeerSource (OwnNode | Discovered(PeerPool)), Dialer (the TCP seam a SOCKS5 dialer slots into), Seeds (kernel-node's DNS seed lists, x9. first; none on regtest), shared USER_AGENT (unchanged /SwiftSync:0.1.0/). The pool keeps bitcoin-address-book new/tried tables (Kyoto's sizes) and learns from PeerEvents: tried promotion on handshake, 10 min rest when unreachable/unhelpful (forgotten after 3 unreachable), 2 min when behind, session ban after a bad or stripped block, IPv6 skipped after the first "network unreachable", and the peer that served the last block is asked first.
  • Block fetch: BlockFetcher::new(Arc<PeerSource>). Discovered peers: up to 8 different peers per round, no waits, NETWORK|WITNESS required at the handshake, peers below the block height skipped before getdata, getaddr while fewer than 200 addresses are known (once per peer), a per-connection cap against trickling peers. FetchFailure gains NoPeers/Behind; BlockFetchError.from: FetchedFrom and the discovered-peer message summarises what the peers did ("asked 3 peers: 2 were unreachable, 1 sent a bad block and is ignored now. Check this computer's internet connection, or set p2p_node_addr to your own signet node.").
  • Electrum: headers from the pooled connection, up to 3 peers when one fails or lacks the header; blockchain.relayfee = median of fresh feefilters from at least 3 (up to 5) peers, sampled in parallel at Electrum start and when short, the lowest seen until the quorum is there (own node: its own, as before); broadcast/recheck go to one relay peer (moving on, up to 3, when a peer cannot be asked).
  • Daemon/API: one Arc<PeerSource> built in main after resolve, seeding starts at daemon start. StatusInfo.p2p ("your node 1.2.3.4:38333" / "random peers: 157 known, 1 ignored", #[serde(default)]).

Config and UI

  • p2p_node_addr: empty/whitespace = unset (normalised in resolve); regtest without it: "regtest has no public peers: set p2p_node_addr to your regtest node". Existing TOML/env/CLI unchanged.
  • Tray: field "Your Bitcoin node (optional)", placeholder "empty: random peers of the network", privacy hint; required only on regtest (setup check reads FRIGLET_NETWORK or the file's network). Status tab: "Oracle / P2P" row split into "Oracle" and "Blocks from".
  • blindbit-cli --p2p-node-addr optional; fetch_block example accepts dns as the peer.
  • README: new "Privacy: your node or random peers" section; AGENTS.md notes.

Deviations from the plan

  • PeerEvent has two more variants: Served (sets the preferred peer) and NoRoute (skips IPv6 for the session). wants_addr takes the peer.
  • FetchedFrom::OwnNode(PeerAddr) (not SocketAddr), and Network { tried, exhausted }, so the message can say the round ran out of peers.
  • Seeds are asked again after 5 min, or after 30 s while they gave nothing, so the daemon's retries after a total DNS failure really reseed.
  • With every known peer resting, the one whose rest ends first is handed out rather than NoPeers (a small signet book would otherwise stall for up to 10 min); after 64 unlucky random draws, any usable known peer is taken.
  • Gossip from a public peer may not add loopback/private addresses (a LAN/localhost peer may); gossiped ports must be the network's or >= 1024; DNS seed answers must be public IPs.
  • A peer forgotten after 3 unreachable tries stays out for an hour (not re-added by the next seeding); IPv6 is retried 30 min after "network unreachable".
  • Relay fee: a quorum of 3 peers before the median (the plan had "median of the last <= 5"), because in practice the cache otherwise held one peer's value.
  • Bitcoin Core may send addrv2 after the block, so a connection that asked for addresses waits up to 3 s for them on its own thread after handing the block over; the scan does not wait.
  • RetryPolicy gains connect_timeout; Scanner.p2p_retry is an Option (None = the source's default).
  • The thread soak test is its own integration-test binary (blindbit-lib/tests/thread_soak.rs) so parallel tests' threads do not count.

Verification

  • cargo build --workspace, cargo clippy --no-deps -p friglet -p friglet-ipc -p friglet-tray --all-targets -- -D warnings, cargo fmt --all -- --check: pass.
  • cargo test --workspace: blindbit-lib 95 passed (new: 10 pool tests, 9 discovery tests on 127.0.0.x), thread_soak 1, friglet 81 (new: two-peer header test, relayfee median, three-peer relayfee sampling), friglet-ipc 28 + 1, friglet-tray 67 + 7; 0 failed. Network tests repeated 5x: stable.
  • After rebasing onto integration 929419b, the testkit T0 tests build their scanners with an own-node PeerSource; cargo test -p blindbit-lib --features serde: 105 passed, 0 failed.
  • Live signet (debug builds): fetch_block dns on block 274010 (> 1 week old) and the tip: DNS seeds (54 addresses), serving peer logged, 0.36 s / 1.5 s; IPv6 peers failed with "network unreachable" and were skipped after the first. Own node: no DNS lookup, no getaddr.
  • No funded signet SP wallet exists on the test box, so the scan comparison used the daemon's witness restore: a state file whose wallet graph holds 5 real signet segwit transactions (heights 280000–328626) stored without witnesses. Daemon without p2p_node_addr and daemon with the own node each refetched the 5 blocks and restored them; the resulting graph transactions, anchors and heights are identical.
  • Daemon without p2p_node_addr: GetStatus → "p2p": "random peers: 157 known"; blockchain.headers.subscribe answered from discovered peers; blockchain.relayfee = 1e-6 BTC/kvB from four peers' feefilters (100, 1000, 100, 100 sat/kvB). Tray screenshots (Xvfb, FRIGLET_TRAY_SHOW_ON_START, captured with ffmpeg x11grab; no SNI tray host installed on that box, so no tray-icon shot): Status tab shows "Blocks from: random peers: 146 known"; Settings shows the optional node field and hint.
  • Performance (release builds of the fetch_block example; signet; 5 blocks × 10 rounds per binary, 854 and 855 interleaved; own node 152.53.151.148; one fetch per process). Own node, median 854 vs 855: 82.6 vs 82.0 ms (run 1, one 2.2 s outlier on 855), 84.2 vs 84.9 ms (run 2), 87.6 vs 86.0 ms (run 3, final code). Discovery cold time to first block (fresh process, DNS seeding included), 20 runs each: 20/20 served; final code median 528 ms, p90 5.7 s, max 8.3 s (earlier runs: max 10.3 s). The slow cases are IPv4 peers that drop the SYN, 5 s connect timeout each. Warm (daemon, sticky peer, debug build): 100–180 ms per block.

Known limits

  • Random peers see this computer's IP and which blocks it downloads; the sticky peer sees most. README says so and recommends the own node (Tor: SNB-857).
  • Broadcast with discovered peers trusts one peer's mempool answer until SNB-856, and rechecks of pending broadcasts ask a random peer, which may not have the transaction yet (it is then re-announced to that peer).
  • A random peer that drops the SYN costs the 5 s connect timeout; racing connections would cut cold-start tail latency (follow-up if needed).
  • The address book is in memory only (persistence comes with SNB-857).

setavenger and others added 6 commits November 1, 2026 15:44
…B-855)

Without p2p_node_addr, friglet now finds peers of the network through its
DNS seeds and address gossip and downloads blocks, headers and (for now)
broadcasts through them. With an own node configured, every connection
still goes only to that node, with the same retries, timeouts and messages.

- blindbit-lib `net`: PeerSource (OwnNode | Discovered(PeerPool)), Dialer
  (the TCP seam for a later proxy), Seeds (kernel-node's DNS seed lists,
  x9. filter first; none on regtest), shared USER_AGENT. The pool keeps
  bitcoin-address-book new/tried tables (Kyoto's sizes) and learns from
  PeerEvents: tried promotion, rest after failures, session ban after a bad
  block, IPv6 skipped without a route, the serving peer asked first.
- BlockFetcher takes Arc<PeerSource>. Discovered: up to 8 different peers
  per round, NETWORK|WITNESS required, peers below the block skipped before
  getdata, addresses requested while few are known, an overall deadline per
  connection. FetchFailure gains NoPeers and Behind; BlockFetchError says
  who was asked (FetchedFrom) and summarises discovered peers' failures.
- friglet: p2p_node_addr optional (empty = unset, regtest still needs a
  node); one PeerSource shared by scanner, Electrum (headers try up to 3
  peers, relayfee = median of recent peers' feefilters, relay jobs move on
  when a peer cannot be asked) and GetStatus (new `p2p` field).
- Tray: node field optional except on regtest, privacy hint, "Blocks from"
  status row. blindbit-cli: --p2p-node-addr optional. fetch_block example
  accepts `dns`.
- Tests: pool unit tests, multi-peer discovery tests on 127.0.0.x, a thread
  soak test, chain tests with two fake peers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…SNB-855)

A peer that already gossiped is not sent getaddr again (Bitcoin Core answers
repeats from a cache), so a sticky block peer stops resending the same
addresses with every block.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Relay fee with discovered peers: the median needs fresh feefilters from
  three peers; with fewer, friglet samples more peers at once (and at
  Electrum start), and reports the lowest seen until the quorum is there,
  so one peer cannot raise the fee. Even counts take the lower middle.
- DNS seed answers that are not public addresses are dropped (a captive
  portal or hijacked resolver could otherwise make "local" peers that may
  gossip local addresses). Gossiped peers must use the network's port or
  one above 1023. More reserved IPv4 ranges count as not public.
- A peer forgotten after three unreachable tries stays out for an hour
  instead of coming straight back with the next seeding or gossip.
- IPv6 is tried again 30 min after "network unreachable".
- A discovered peer that completed the handshake and then hit the
  per-connection deadline is reported as unhelpful, not unreachable.
- Scanner asserts (debug) that it and its peers are on one network.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Scanner::new takes an Arc<PeerSource> instead of a socket address since the peer pool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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