Repository navigation
net: defer compact filter requests that race the filter index - #35354
randomlogin wants to merge 4 commits into
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process. ConflictsNo conflicts as of last run. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
fff4fe5 to
b68a721
Compare
|
Squashed two commits in one (previously there was a distinct commit for tests addition). |
|
Reading through this, I don't think this should be handled on the index level. While I'm still not sold that this is not something that is just a bit flawed in the spec, I would prefer an approach where net_processing dealt with this directly in the case of an index query returning nothing by checking if the requested block lies in the two block tolerance range and if so syncing the validation interface queue. |
|
@randomlogin how do you want to proceed here? |
|
@sedited sorry for a long response. There are two ways: the first one is simply to move race-window check logic to On the other hand, instead of blocking the caller ( Something like This will not block other p2p messages while we wait for filters being constructed. However I'm not sure that it's worth, as with the window gap being small enough we'd save only fractions of second. Additionally we could use Does that sound right to you? |
That seems like a plausible approach to me, fwiw. Just set a flag in That will mean cfilter responses for that peer may be out of order vs other messages on that connection, but I think that's okay? |
There was a problem hiding this comment.
The problem formulation in the OP is not precise: Currently, when we receive a block, we first build the filter and then advertise the block, so there usually is no race.The race can only happen if the client asks for the newest headers themselves unprompted. This could happen because they just newly connected, or because they do active getheaders polling for some other reason. But this explains why this is very rare.
In my opinion, both a SyncWithValidationInterfaceQueue call in net_processing (it's not like we have anything more urgent to do than processing the block anyway?) and to schedule it for later sound fine.
OP is precise for BIP152 block transmission, when we go through BIP152, we transmit compact block header to peers before we run Also I took a moment to review how other requests are handled when we announced compact block header:
Then maybe it is better to have some kind of a unified approach for both Deferring and responding later does not increase blocking time, and personally is more appealing and I'd go this way. |
But how does this make sense / how is this a relevant use case? To be chosen as a high-bandwidth block relay peer, you need to be a full node. In this case you don't need to download the blockfilters for new blocks from peers - this is a feature meant for light clients. |
|
@mzumsande you are right, I confused BIP152 with BIP130 (light clients use BIP130). And as you pointed, this bug cannot happen with BIP130 header transmission. |
My understanding is that the way we currently work is:
Apart from the hb compact block path, we don't announce blocks until we've validated them, but we do announce them before we've calculated their compact filter. I don't think we should block the message handling thread further, because we do have more important things to do: responding to Those "immediate" requests could come from peers that selected us as hb for cb, or could just be a very quick response to a headers announcement compared to the calculations required to update the compact filters. I think those calcs are slow enough that this is likely to occur occasionally even for peers that aren't getting the high-bandwidth compact block path, so is worth handling, but it would probably good to confirm the exact flow and timings? (How long does calculating the compact filters actually take? How much does that vary across blocks? What's the real turn-around time for real compact filter clients between seeing a header and requesting the compact filter?) |
|
Yes, that makes sense - I hadn't thought of
I'd say it's
|
b68a721 to
6c325d0
Compare
ChangesAdded deferring of a response, we store the racing request in a To avoid code repetition, added a helper Rewrote the test. If we receive two requests while the first one has not yet been responded, we do not overwrite it, thus responding only to the first one. BenchmarksAdded benchmarks commit (b1d4868) to this PR (I'll squash it later). Here is the data for the initial filter construction since genesis as well as for processing during normal operation, i.e. I have an already running node in which I turned on filters and measured the times. Initial blocks' measurements are shown over latest 100k blocks, since ~861k).
Maximum time (112 ms for filters construction) is perhaps some I/O problem or something else not connected to normal behaviour. All times include I/O. We can say filters processing takes around 10% of block processing. Computer that runs benchmarks has the following specs:
Going further
|
b68a721 to
21c65a0
Compare
cef32b9 to
1526b81
Compare
|
I took some time to gain more understanding on how Some suggestions on top of your commit:
|
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
1526b81 to
4849c05
Compare
|
@ajtowns what do you think? |
4849c05 to
ce90db5
Compare
ce90db5 to
0dc77bc
Compare
ajtowns
left a comment
There was a problem hiding this comment.
Looks good to me, I think. PR description needs an update.
…sing Move the serving logic of the getcfilters/getcfheaders/getcfcheckpt handlers into ServeGetCFilters()/ServeGetCFHeaders()/ServeGetCFCheckPt(), so they can be reused when the request gets parked in the next commit. No behavior change. Co-authored-by: Anthony Towns <[email protected]>
Previously compact filters requests might falsely be ignored because the requested block has been connected to the chain, but the filters have not yet been constructed. This commit adds deferring of such racing requests. We store the pending request in a per-peer `m_pending_request`, and defer processing further messages from that peer until the pending request can be dealt with. Checking if we have a race condition is done via inspecting `GetSummary` of the index. Pending requests are resolved when the index catches up with the tip, or when the requested stop hash is covered by the index. Also adds a unit test for the race-condition. Co-authored-by: Anthony Towns <[email protected]>
During IBD, answer cfilter requests immediately instead of parking them Co-authored-by: Anthony Towns <[email protected]>
Changed PendingRequest into a struct. Added `park_time` and `last_retry_time` fields to a parked cf request. We prevent retrying to respond to the same parked request if the previous attempt was less than `PARKED_CF_RETRY_INTERVAL`. If a request is parked for longer than `MAX_CF_PARK_INTERVAL`, we serve what we can (and clear the parked request) to start processing further peer's messages. Added two tests for the above behaviour. Co-authored-by: Anthony Towns <[email protected]>
0dc77bc to
8520f9c
Compare
mzumsande
left a comment
There was a problem hiding this comment.
The OP is still not describing the problem correctly, as I mentioned in #35354 (review).
Given the extent of the changes to net_processing for what seems like a special-case problem to me (only light clients doing active polling for headers instead of waiting for block notifications are affected), I think this need a better and more precise motivation as to why this is important enough.
If the goal is enabling cross-polling, i.e. asking BIP157 peer A for a filter of a block that only peer B announced to us (which is currently not permitted by the BIP but apparently still done by some implementations) is the goal, this should be stated and the BIP should be adjusted.
Alternatively, this could be motivated as a step towards a major change of the net_processing code towards more async behavior to tackle problems like #35023 (this is @ajtowns angle in #35354 (comment)), but this should also be stated explicitly in the OP.
|
@mzumsande thanks for your comments, I changed the PR description in favor of a prerequisite step towards more async block validation and block broadcast. About changing the BIP:
Still I thought we could
Also I when opened this PR I emailed BIPs authors, but received no response. |

This PR defers cfilter-related P2P messages if they ask for information from a new block which has been validated, but compact filters are not yet constructed. This PR is a prerequisite for a bigger change of decoupling index construction from block connection to make block announcement faster.
Fixes: #29655, #27085
Related discussion: 1
When a new block arrives, block connection has several steps (described very broadly):
If a light peer sends
getheadersafter the first step has passed, but the second has not, the light peer wouldthen send
getcfheaders(or another cfilter request) with the stop hash of the new block. Core has no filters yet and thus doesn't answer, and the light peer thinks Core misbehaves and disconnects.This race condition is extremely rare right now, because normally headers are received via BIP130 and in that case the light client would not receive a new header until Core is ready to serve the filters (Core does not announce block until the filters are constructed, but can respond with a new header if explicitly asked). So to hit the race condition, the light client has to actively poll the Core node with
getheaders.Active polling is not prohibited by BIP157 and in my opinion it should not be (at the very least, light clients need
to sync and have to send
getheaderswith a zero stop hash, which might hit the race). Once Core has sent the block in aheadersreply, the stop hash "is known to belong to a block accepted by the receiving peer".This race condition would occur far more frequently (on every new block if light client is fast enough) if new header
announcements were made without waiting for index construction (which we'd love to see in the future). It is discussed here: #35023. In that case light clients would receive new header announcements from Core without the need to actively poll it.
The approach used (via #35023 (comment)) is to stop processing all messages from a peer if there is a request from it which we cannot answer right now, but will be able to shortly. So this PR introduces a way to stop processing requests from a peer, which later can be used to park a peer who sent us a new block (#35354 (comment)).
What it adds:
A
getcfilters,getcfheadersorgetcfcheckptrequest 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.m_pending_requestand we stop processing further messages from that peer. This keeps responses in order; other peers and outgoing messages are not affected.PARKED_CF_RETRY_INTERVAL) to avoid takingcs_maintoo often.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.A new
cfilter_race_testssuite (src/test/cfilter_race_tests.cpp) uses aPreFilterBlockerCValidationInterfaceregistered before the filter index, so its ownBlockConnectedfires first on the scheduler thread and stalls it before the filter index gets to runCustomAppend. This reproduces the race window deterministically. The tests drive the real message processing path viaConnmanTestMsgand 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.
P.S. Light clients (kyoto, neutrino) also do cross-polling: they ask for filters from a peer that didn't announce the
block, naturally receive no answer, and might disconnect it. Disconnection due to cross-polling occurs more frequently than those due to active polling and this PR does not address it directly, as it IMO is a mistake by light clients, not Core's one. Still, this PR gives such cross-polled filter requests an additional window to resolve if the asked node is in the process of filter construction.
Related neutrino discussion: lightninglabs/neutrino#338.