utxo split/7 async handoff

willcl-ark/bitcoin: PR #46

Provider refusals

write_verifier: Provider declined the request because of a cybersecurity restriction. Experiments remain incomplete; static verification can continue.

1 major finding · 3 minor findings · 3 suggestions

Findings

Link

[major] · Active-tip download cap can prevent a higher-work reorganization

A valid fork whose chainwork overtakes the fixed active tip beyond the 1,024-block download cap can stop synchronization on the lower-work branch; no production occurrence was identified.

Location: src/net_processing.cpp:1685 (head).

Details and suggested fix

Let the active tip have height H, and an otherwise eligible peer advertise a valid higher-work fork whose blocks through H + 1024 still have less chainwork than that tip. With a sufficiently large missing suffix, header direct fetching falls back to parallel download. FindNextBlocks() advances pindexLastCommonBlock across stored fork blocks, but the new min(lastCommonHeight, activeHeight) pins further requests at H + 1024. TryAddBlockIndexCandidate() excludes every downloaded prefix block, so the connector cannot move the tip or advance the cap. Once requests drain, there is no in-flight blocker to time out or identify as a staller. The compact-block path is limited to tip + 2. Base instead advanced the window with the received frontier and could complete the reorganization.

This applies both during and outside IBD, and restarting alone preserves the fixed point. Active-chain growth or manual intervention can restore progress. The trigger requires an unusual valid work profile, implausible on mainnet without substantial proof of work; Testnet4 still permits minimum-difficulty blocks. Apply the tip-based cap only when pindexLastCommonBlock descends from the active tip, and otherwise retain the received-frontier window. A reduced-window fork test can cover the crossover beyond the cap.


Link

[minor] · IBD request sizing traps high-latency peers at one block

During IBD, consistently greater-than-two-second request-to-arrival latency locks a peer at one outstanding block even if its bandwidth supports pipelining; the throughput impact is derived, not measured.

Location: src/net_processing.cpp:497 (head).

Details and suggested fix

BlocksToAskFor() starts at one request. Whenever nothing remains on the wire, SendMessages() starts the next sample at request time, so the BLOCK handler measures RTT plus serving and transfer time. If those samples remain above two seconds, 1 + BLOCK_DOWNLOAD_QUEUE_TIME / m_block_delivery_time stays at one. The peer never develops the pipelined inter-arrival samples that could demonstrate its transfer rate. Base allowed sixteen outstanding requests, and this new rule applies even when handoff is disabled. For small, latency-dominated blocks, the model permits roughly sixteen-fold lower per-peer throughput, though no Tor/I2P prevalence or slowdown was measured. The existing request-rule test models delay without separating latency from bandwidth. Do not use raw request-to-arrival time alone as the transfer-rate estimate: seed the estimate with bounded pipelined arrivals, retain the per-peer cap and protection against slow transfers, and add a pure-RTT-delay case.


Link

[minor] · Compact-filter requests can disconnect peers awaiting block activation

With -peerblockfilters, a compact-filter request for a valid, just-stored tip block can disconnect its peer before asynchronous activation; no current client was identified that requests it before a headers/inv announcement.

Location: src/net_processing.cpp:3875 (head).

Details and suggested fix

Outside IBD, a valid tip-extending block followed immediately by a supported getcfilters, getcfheaders, or getcfcheckpt can reach this window. Production ProcessBlock() now returns after storage and leaves activation to the connector, but all three filter-request rows are TipLagTolerated. Before consulting the filter index, PrepareBlockFilterRequest() calls BlockRequestAllowed(), which requires active-chain membership or BLOCK_VALID_SCRIPTS; neither holds yet, so it sets fDisconnect. Base connected the preceding block before taking the request, while a lagging filter index merely caused lookup failure without disconnection. Serving requires -peerblockfilters and its required filter index. Mark these three rows PendingBlocksConnected and add a block-followed-by-filter-request regression test.


Link

[minor] · Deferred relay cleanup can destroy the pool on its own worker

If deferred relay cleanup outlives all external pool owners, releasing its block can destroy ThreadPool on a worker and assert; no current caller was identified that reaches this teardown timing.

Location: src/net_processing.cpp:2423 (head).

Details and suggested fix

Handler-path blocks now come from NewBlock(false), whose deleter captures a shared owner of the connect pool. NewPoWValidBlock() captures the previous block in a pool task and resets it there. If that reset drops the last block reference after all external pool owners have gone, destroying the deleter releases the final pool owner on the worker. ~ThreadPool() calls Stop(), which asserts against being called by a registered worker. FinishBlockTasks() covers receive tasks, not this cleanup, and ~ChainstateManager() does not stop or drain the connect pool. Normal node shutdown unregisters PeerManager well before flushing state and resetting the manager, making this timing unlikely; such a shutdown assertion would follow the state flushes. Once producers and receive tasks have quiesced, stop and drain the pool on a controller thread while retaining an owning reference.


Optional improvements

Link

[suggestion] · Receive-task tests do not force the critical executor boundaries

Current tests do not deterministically check reserved-worker capacity, shutdown with a pending receive-task finish, or the end-of-IBD drain of tasks not yet handed to the connector.

Location: src/net_processing.cpp:3910 (head).

Details and suggested fix

DrainWindow() waits for pool work while holding cs_main, so receive-task admission must leave a worker free. Each task must also remain counted across block I/O until its worker finish ends. The common unit fixture sets blocks_on_message_handler, and functional nodes default to one pool worker. The dedicated handoff test stays in IBD, while p2p_tip_connector.py exits IBD before receiving its test blocks. Add an explicitly asynchronous two-worker fixture with barriers around real receive tasks. It should assert that admission leaves the reserved worker able to drain a failed window, that FinishBlockTasks() remains blocked between WriteNewBlock() and FinishNewBlock(), and that a post-IBD transaction waits for an outstanding task before connector completion. Avoid an intervening ping in the transition case, since its independent task wait would mask removal of the receive-task drain. Existing tests can retain their synchronous default.


Link

[suggestion] · c662f0e2 bundles independently reviewable policies with the hand-off

Separating the connector abstraction, IBD request-rate policy, and source-retention policy would let each change and its tests be reviewed and bisected independently of receive-task scheduling.

Location: src/validation.cpp:6635 at c662f0e24bc0 (commit) (commit).

Details and suggested fix

c662f0e24bc0f38afa65818b143022532ffbfbd6 contains three useful preparatory units in addition to the hand-off. First, move handoff_machine.h, its tests, and the connector integration into their own commit: the switch includes new assertions and changes the yield predicate from active work not attempted to active work not taken. Second, move BLOCK_DOWNLOAD_QUEUE_TIME, BlocksToAskFor(), delivery sampling, and p2p_ibd_request_rule.py into a request-policy commit. Count vBlocksInFlight.size() there, then add the with_pool exclusion with hand-off. That test uses the single-worker functional default, so it already exercises the policy without receive tasks. Third, move pending-verdict source retention and p2p_block_source_duplicate.py before hand-off; IsBlockBeingStored() already exists at base, and the duplicate test exercises stored, out-of-order blocks without receive tasks. This isolates attribution policy from executor timing.

Keep 1dae81a054d2938298a64bd7e742ff825cc6c5f2, which adds off-path relay-data freeing, separate as it is now. Splitting the first commit loses one-to-one traceability to benchcoin PR 201, but gives the counter, throughput, and attribution changes smaller validation units before asynchronous scheduling depends on them.


Link

[suggestion] · Mid-write GETDATA retry and source retention lack deterministic coverage

The new tests do not hold a block mid-write, so they do not reliably protect queued GETDATA retries or first-sender retention through IsBlockBeingStored().

Location: src/net_processing.cpp:2763 (head).

Details and suggested fix

ProcessGetBlockData() now returns false while the announced block is being stored, and ProcessGetData() must leave that request queued. BlockProcessed() separately relies on IsBlockBeingStored() to retain the first sender before BLOCK_HAVE_DATA is set. p2p_failed_block_not_served.py requests only after connection or failure, and p2p_block_source_duplicate.py sends the second copy after the first is stored. Use staged acceptance to hold a tip block after AcceptNewBlock() and before WriteNewBlock(). Assert that GETDATA remains queued without a response, deliver a duplicate with the first sender recorded, and check that its source entry survives. Then finish the block and assert serving for a valid block and refusal for a block that fails connection. Calling AcceptNewBlock() directly does not record a peer source, so the attribution portion needs explicit PeerManager fixture setup or a test hook.


Coverage details

Background

Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.

Unresolved disagreements

The inspected attribution discussion does not resolve a general policy for retaining source records indefinitely versus restricting punishment/reward to in-order validation. Retention preserves delayed punishment, but later discussion also describes lingering entries and proposes either documenting/testing the quirks or limiting attribution to in-order cases. A reported mutation-based wrong-peer disconnection was subsequently described as fixed; that particular report should not be treated as an outstanding present defect.

This limits the historical conclusion available for duplicate-source retention: there is a clear accountability motivation, but no inspected resolution establishing first-sender retention as the universally preferred policy across stale, duplicate, compact, and out-of-order blocks.

Source 1 · Source 2 · Source 3