mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
validation: Leave some pruned blocks in m_blocks_unlinked, fix another nSequenceId issue #36149
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36149
Loading…
Reference in a new issue
No description provided.
Delete branch "202609_fix_cbi_pruning_unlinked"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
m_blocks_unlinkedserves two purposes:setBlockIndexCandidatesonce their parents arereceived
m_chain_tx_countonce the parents are receivedIn case the parent block of a previously pruned block is received, 2. is currently not being done, because pruning removed the entries from
m_blocks_unlinked.This can result in
CheckBlockIndexfailures (#31512, #36021) and incorrect rpc results, plus we are in a temporarily inconsistent state (m_chain_tx_countis currently not set, but would get set if we restarted).This scenario is unlikely to happen during normal node operation unless there are huge reorgs, but it can happen, for example, in
getblockfrompeerscenarios.Fix this by leaving pruned blocks in
m_blocks_unlinkedthat don't havem_chain_tx_countset, and also adding them on restart.This was one of two reasons, the fuzz target
block_index_treecouldn't cover pruning of blocks not in the main chain.The other one is similar to #34521 and #35070 - if we receive a block a second time (after pruning), we could change
nSequenceIdwhile the block is insetBlockIndexCandidates- fix this by attempting to remove it from the set before re-adding it.After the two issues are fixed, the restriction from
block_index_treefuzz target is removed.Fixes #36021
Fixes #31512
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
Concept ACK, thanks for fixing it. I will try to review it a bit later.
Concept ACK
Taking out the
setBlockIndexCandidateserase, the extendedblock_index_treetarget breaks on an input that is already in the qa-assets corpus.nit (feel free to ignore):
predecessorsappears only here insrc/, andancestorssounds to be the preference, including the comment this line replaces.Pre-existing, so feel free to ignore.
This assertion is what made me look.
The
getchaintipshelp callsvalid-headers"All blocks are available for this branch", but the only availability test behind the status isHaveNumChainTxs(), whose own doc says it "Does not imply the transactions are still stored on disk". I get the same on master with a stale fork received in full and then pruned, wheregetblockon those blocks fails with "Block not available (pruned data)".Would "received at some point" be closer than "are available" there?
ACK
40a31d97abThe main fix in
7c7ccc9cc0looks correct to me. Ran the fuzz test for a while, no issues. Commit80477ba978fixes previously existing (potential) UB insetBlockIndexCandidatesthat is now caught by the fuzz target. Nice.In the PR description,
should be
m_blocks_unlinkedinstead.Also, left a non-blocking comment on the functional test.
Thanks for fixing this cleanly!
We could also test submitting a missing parent of a pruned child before restarting, as in https://github.com/bitcoin/bitcoin/issues/36021. This test currently passes with the old behavior of removing a pruned block from
m_blocks_unlinked, because it gets added back on start up. Could add a separate 2-block fork to cover the no restart case?Thanks for the reviews! I will address the comments (and also most likely rework the PR a little bit, see other comment) next week, putting into draft until then.
A slightly different approach here (suggested by @l0rinc) would be to remove conditionally on
m_chain_txbeing set.I will explore this next week.
done
Makes sense - since there were two other spots in the vicinity that had a similar issue I added a separate commit that changes these.
I added a
generate()call before the restart, so that CheckBlockIndex is invoked. I think that should cover the situation, too.40a31d9 to 532ffaa:
In addition to addressing review comments, I changed the approach of the first commit, taking some things of a branch from @l0rinc (thanks, added you as coauthor).
In the previous PR branch, pruning never removed anything from
m_blocks_unlinked. Now, a pruned block is only kept there if it doesn't have itsm_chain_tx_countyet, i.e. if it is still waiting for an ancestor's transactions. Blocks that are already linked are removed on pruning, as on master. Also, blocks without data are no longer added to setBlockIndexCandidates when they get linked.The approaches are very similar, the main functional difference is for the (admittedly far-fetched) case that a block is pruned, downloaded again (e.g. via getblockfrompeer) and then pruned again. Another advantage is that we can now have slightly stricter checks in
CheckBlockIndex().🔵 Suggestion · Fuzz re-downloads bypass the known-invalid block guard
Off-chain pruning lets the fuzz target re-download an index still marked
BLOCK_FAILED_VALID, a receipt transition that production rejects beforeReceivedBlockTransactions.Location:
src/test/fuzz/block_index_tree.cpp:156(head).🔵 Suggestion · Sequence-ID re-receipt fix lacks a direct regression oracle
The added functional test never re-receives an existing candidate, and the fuzz target lacks ordering and pointer-uniqueness checks for the repaired sequence-ID invariant.
Location:
src/validation.cpp:3821(head).🔵 Suggestion · Fuzz pruning duplicates the production retention rule
The fuzz target applies its own copy of the new
HaveNumChainTxs()retention condition, so later production changes can leave it exercising a different pruning rule.Location:
src/test/fuzz/block_index_tree.cpp:164(head).Review coverage was incomplete.
Reviewed:
532ffaa26d05· Full review report · Previous reportspermalink
Concept ACK.
As I understand it,
m_blocks_unlinkedis used both to reconsider blocks forsetBlockIndexCandidateswhen a missing ancestor arrives and to setm_chain_tx_countfor their descendants. If B's transactions were received but an ancestor’s were not, pruning B clearsBLOCK_HAVE_DATAwithout clearingnTx.Removing B from the map at that point means that when the ancestor arrives, we no longer reach B or any blocks waiting behind it. This seems unlikely in normal sync, but the stale fork case shows it can happen.
So I think it makes sense to keep B in the map while its
m_chain_tx_countis still unknown, and to reconstruct that state on restart. B still shouldn’t become a chain candidate while its block data is missing.The nSequenceId change also seems justified since it affects the candidate set’s ordering, though that feels like a separate implementation detail.
Left some nits and suggestions below
In commit https://github.com/bitcoin/bitcoin/pull/36149/changes/12208a02ff5f795369fb2ab28884dc37b2db245d: validation: keep pruned blocks in m_blocks_unlinked
This still checks for pprev here:
github.com/enochaz/bitcoin@532ffaa26d/src/validation.cpp (L3840-L3843)But the else branch already guarantees pprev is non-null, and
AddUnlinkedBlocknow checks it as a precondition. I think it can be removed here. Leaving it is fine, but it feels redundant and might obscure theBLOCK_VALID_TREEcondition for someone reading the code.In commit https://github.com/bitcoin/bitcoin/pull/36149/changes/12208a02ff5f795369fb2ab28884dc37b2db245d: validation: don't change nSequenceId of a block that is a chain candidate
Could we have a deterministic regression test for this, something like the unit test below? This would let us check that redownloading a block already in
setBlockIndexCandidatespreserves the set’s ordering, rather than relying on the fuzzer to hit the same sequence again.In commit
12208a02ff: validation: keep pruned blocks in m_blocks_unlinkedThis confused me a bit. I’d prefer these assertions grouped together, since both check whether a block can be in
m_blocks_unlinked.Concept ACK
Keeping the block index and candidate ordering reliable after pruning matters before we add more indexes or start batching and parallelizing this work. The distinction between knowing a block’s transactions and having its data on disk will probably matter for
pruneassumevalidtoo.Did a first pass, I’d prefer smaller commits that show the old behavior in a test, then flip the relevant check with the fix to prove why each change is needed - I left a few examples inline. The PR description could also use a minor reformatting pass.
A lot is happening here, could we split this part into three commits?
AddUnlinkedBlockaccept pruned blocks with a parent and received transactions.LoadBlockIndex.CheckBlockIndexrequire those entries even after pruning.We could pull the metadata updates and the conditional
RemoveUnlinkedBlockcall into onePruneBlockhelper, soPruneOneBlockFileand the fuzz test share the same pruning logic.The suggestion below squashes
BLOCK_HAVE_DATAandBLOCK_HAVE_UNDOintoBLOCK_HAVE_MASKand marks pruned entries dirty in the fuzzer (which won't have any effect there as far as I can tell, but should be more realistic regardless).Extract PruneBlock and reuse it in the fuzz test
I'd reload before the missing parent arrives and check that submitting it fills in the pruned child's count right away.
Check linking after reload
The erase here also makes the old comment above
AddUnlinkedBlockinFindMostWorkChainobsolete.If a duplicate were to make
ReceivedBlockTransactionsvisit the same block twice, the erase would still run before each sequence ID change.I'd drop that explanation (but I hate verbose comments in general, so maybe an update would be more approriate :p).
Drop the obsolete duplicate-entry rationale
Alternatively we could reuse the existing fork test - the erase in the next commit will let us flip the last check.
Show the ordering before the fix
We could add a check that shows what happens before the
BLOCK_HAVE_DATAguard here: the child also enters the candidate set.Show candidate membership before the data check
We can probably simplify this if we submit the grandchild before restarting.
Exercise pruning and linking with one restart
Could we submit the parent before restarting and check that the pruned child gets its transaction count? The extra
generate()in the functional test already checks that the child stays inm_blocks_unlinked.Check linking before restart
That works as well, reusing the fork test gives us a deterministic regression case. Once the last check is flipped to is_sorted, could we also check that block100 is still a candidate after the redownload?
BOOST_CHECK(candidates.contains(block100));Since the fix now erases block100 before readding it,
is_sortedalone wouldn't notice if a future change dropped it from the set.