mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 10:47:36 +00:00
First steps towards a stateless, side-effect free validation library #35906
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!35906
Loading…
Reference in a new issue
No description provided.
Delete branch "validation"
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?
This implements the following actions from #35904:
Expose the validation interface
Make the existing
CheckBlockHeader,ContextualCheckBlockHeader, andContextualCheckBlockfunctions part of the public validation interface by declaring them invalidation.h.Interface Segregation: Remove dependency on
ChainstateManagerContextualCheck*functions need aChainstateManageronly to access theconsensus parameters. Pass
Consensus::Paramsinstead and access them at the call site.Make environmental dependencies explicit
Pass the validation time explicitly to
ContextualCheckBlockHeaderinstead of reading the system clock internally.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:
-Wunusedby fanquake)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.
This conflicts with https://github.com/bitcoin/bitcoin/pull/35557, which is an alternative to https://github.com/bitcoin/bitcoin/pull/35496.
No opinion on the merge order, but I think it would be good if the authors of all three alternatives provided reviews on each pull request, so that ideally the same line of code is only changed once.
this partially reverts
78adef1753. Maybe that is fine, but if the motivation is better tests, maybe the refactor change would come with better tests included?E.g. this pull conflicts with https://github.com/bitcoin/bitcoin/pull/34895, which seems to be adding tests.
Good point. Done.
t ACK.
This PR undertakes three (small) refactors that increase what is practical to test. All three functions were
staticbefore, soCheckBlockHeader,ContextualCheckBlockHeaderandContextualCheckBlockcould only be tested indirectly, by pushing a block through validation (from a unit test or by submitting it over RPC or p2p). With these changes they can be called directly. (Big improvement IMHO)PR is part of a bigger refactor to make a side-effect validation library possible.
Concept ACK
Loving the new tests. Will be digging into this more next week, but at first glance the changes seem relatively straightforward. Also very happy to see
ContextualCheckBlockHeaderno longer reading system time.Any reason we don't use
BOOST_FIXTURE_TEST_SUITElike the othervalidation_*_tests.cppdo?This test reuses BlockValidationState across CheckBlock calls, the other tests create new ones. Should we make it consistent?
What about
BOOST_CHECK_EQUAL?Should these doc comments move to the header now?
params.GetConsensus()for CheckBlock andGetConsensus()for ContectualBlockCheck. Should they be aligned?I would suggest adding something like:
/** Context-dependent validity checks */for consistency with line 394.obsolete with the comment below.
You are right. the docs should be part of the declaration.
ok.
Is there an advantage?
Since the functions under test are stateless, there is not much test setup that would justify a fixture.
No strong opinion for this test. I think the state parameter is a smell anyway and it should (and will) become a return type in the long run, which will solve this.
Make sense - thank you!
I think the error messages are better, but not worth touching again for this.
Concept ACK
ACK
4d4f0331cfThis change reduces complexity around the
ContextualCheckBlockandContextualCheckBlockHeadermethods, and adds tests (forCheckBlock,ContextualCheckBlock,CheckBlockHeaderandContextualCheckBlockHeader).The two
Contextual...methods take only consensus parameters instead of chainstate manager, as only those are needed. TheContextualCheckBlockHeadermethod takes the current clock vale as parameter, and does not retrieve it internally. The new tests drive the methods into various error cases, by using relatively simple input data combinations (this is enabled by the changes).Some minor comments: (one additional raised in-line)
Nit: The test claim could be made stronger, by showing that with
nNonce = 0and no prior check the result is invalid (I know that this combination accidentally is checked in another test, but for completeness it could be included here as well).Base:
3eca89820e1fb152ebf0315e3924fc374cb35958Head:
4d4f0331cf50d9721e28e728df1c9b2573a083a4Findings
Suggestions
Document ContextualCheckBlock’s structural precondition (src/validation.h:420, head)
Please document that callers must first pass the block through CheckBlock. When BIP34 is active, this function accesses block.vtx[0]->vin[0] without structural guards. CheckBlock rejects an empty block, and its transaction checks reject an empty input list. The checked AcceptBlock and TestBlockValidity paths preserve this order, so no current production crash has been demonstrated. If this function is intended to handle arbitrary blocks independently, add guards instead.
Share the buried-deployment activation predicate (src/deploymentstatus.h:14, head)
Could the VersionBitsCache-taking overload delegate to this overload? Both duplicate the ValidDeployment assertion and height comparison, so changes to the buried-deployment predicate would need to keep both implementations synchronized. Delegation would preserve the existing call signatures and behavior while centralizing the rule.
Review coverage was incomplete.
Review limitations
Full review report
I think this would be a good opportunity to showcase the more declarative testing style this enables instead of having to repeat the same boilerplate. Could these two test files be compressed a bit with that in mind?
I am not sure we use the same definition of the word "declarative". I think the test cases that @janb84 contributed are very close to a declarative ideal: They are independent and each test case is responsible for the setup of its test data. Some procedural code remains, which is due to how the types under test are implemented.
To explain, code like the following implements a state machine:
The variable
headerstarts its life in the default state, with every new assignment to its data members being a state-transition for the value of the whole. The following would be a declarative alternative, where no later state transition of the test data is even possible:However, as long as
CBlockHeaderprevents aggregate initialization by providing a default constructor, some procedural code has to remain. Adding/removing/modifying constructors for primitive types is outside the scope of this PR.But I assume this is not what you meant here. Please clarify.
I was more looking at the transaction mutations, which could be done more in the style of #36694. That said, I think some helper functions/structs to collapse the boiler plate would still be good. I think ideally we should afford a structure here where edge cases can be added as easily as for the script interpreter in https://github.com/bitcoin/bitcoin/pull/36458 (though we need to take this as far as enabling a json import).