This PR is another approach to fix a race condition bug when a yet-unconstructed compact filter is requested for an already known block.
Fixes: #29655, #27085 Related discussion: 1, 2 Another approach which received NACK: #35262
The problem: Bitcoin Core receives a new block and processes it, then advertises it; however, by that moment the compact filter might not yet have been constructed. If a peer asks for a filter in that time window, Bitcoin Core simply does not respond. From the peer's perspective this is a misbehaviour (we advertised a block and don't provide filters for it), which results in disconnects or bans. While it does not critically affect core nodes, it breaks the network topology for BIP 157 clients.
More precisely, BIP157 specifies that for getcfilters requestor:
StopHash MUST be known to belong to a block accepted by the receiving peer. This is the case if the peer had previously sent a headers or inv message with that block or any descendents. A node that receives getcfilters with an unknown StopHash SHOULD NOT respond.
Current behaviour clearly contradicts the above specification.
Proposed solution:
A getcfilters, getcfheaders or getcfcheckpt request that is racing the filter index is parked instead of being ignored:
PeerManagerImpl::ShouldDeferCFilterRequestchecks whether a request is racing the index construction: the index is synced, but has not yet caught up with the tip, and does not cover the requested stop hash yet. It never defers during IBD, where the index can lag the tip for a long time.- If so, the request is stored in the peer's
m_pending_requestand we stop processing further messages from that peer. This keeps responses in order; other peers and outgoing messages are not affected. - The parked request is retried whenever we process that peer's messages, at most once every 100ms (
PARKED_CF_RETRY_INTERVAL) to avoid takingcs_maintoo often. - If the request is still parked after 5s (
MAX_CF_PARK_INTERVAL), we stop waiting and serve what we can: the peer gets the answer if the filters exist by then, otherwise the request is ignored, as it was before this PR. The peer then continues to be processed normally.
Tests:
A new cfilter_race_tests suite (src/test/cfilter_race_tests.cpp) uses a PreFilterBlocker CValidationInterface registered before the filter index, so its own BlockConnected fires first on the scheduler thread and stalls it before the filter index gets to run CustomAppend. This reproduces the race window deterministically. The tests drive the real message processing path via ConnmanTestMsg and advance the mocked clock where time matters.
Cases covered:
deferred_cfilter_response_reaches_peer— a racinggetcfiltersis parked, not answered and not dropped, and is answered once the index catches up.deferred_cfheaders_response_reaches_peer— the same forgetcfheaders.pending_request_pauses_message_processing— while a request is parked, a laterpingfrom the same peer is not processed, and the responses come back in request order.parked_request_retries_are_throttled— a parked request is not retried before the retry interval has passed, even if the filter is ready.parked_request_is_released_after_timeout— after the park timeout the peer is no longer paused (a laterpingis answered), and no filter is sent for a filter that does not exist.
I've used Claude Code for this PR.