Repository navigation
First steps towards a stateless, side-effect free validation library - #35906
purpleKarrot wants to merge 7 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.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, 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. |
|
|
||
| // Check timestamp | ||
| if (block.Time() > NodeClock::now() + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) { | ||
| if (block.Time() > now + std::chrono::seconds{MAX_FUTURE_BLOCK_TIME}) { |
There was a problem hiding this comment.
| if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_HEIGHTINCB)) || | ||
| (block.nVersion < 3 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_DERSIG)) || | ||
| (block.nVersion < 4 && DeploymentActiveAfter(pindexPrev, chainman, Consensus::DEPLOYMENT_CLTV))) { | ||
| if ((block.nVersion < 2 && DeploymentActiveAfter(pindexPrev, consensusParams, Consensus::DEPLOYMENT_HEIGHTINCB)) || |
There was a problem hiding this comment.
maybe the refactor change would come with better tests included
Good point. Done.
ab1674d to
6b9891e
Compare
There was a problem hiding this comment.
t ACK 6b9891e
This PR undertakes three (small) refactors that increase what is practical to test. All three functions were static before, so CheckBlockHeader, ContextualCheckBlockHeader and ContextualCheckBlock could 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 |
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(validation_check_block_tests) |
There was a problem hiding this comment.
Any reason we don't use BOOST_FIXTURE_TEST_SUITE like the other validation_*_tests.cpp do?
There was a problem hiding this comment.
Since the functions under test are stateless, there is not much test setup that would justify a fixture.
There was a problem hiding this comment.
Make sense - thank you!
|
|
||
| BOOST_CHECK(!CheckBlockHeader(header, state, consensusParams)); | ||
| BOOST_CHECK(state.IsInvalid()); | ||
| BOOST_CHECK(state.GetRejectReason() == "high-hash"); |
There was a problem hiding this comment.
What about BOOST_CHECK_EQUAL?
There was a problem hiding this comment.
Is there an advantage?
There was a problem hiding this comment.
I think the error messages are better, but not worth touching again for this.
Make the existing `CheckBlockHeader`, `ContextualCheckBlockHeader`, and `ContextualCheckBlock` functions part of the public validation interface by declaring them in `validation.h`.
`ContextualCheck*` functions need a `ChainstateManager` only to access the consensus parameters. Pass `Consensus::Params` instead and access them at the call site.
Pass the validation time explicitly to `ContextualCheckBlockHeader` instead of reading the system clock internally.
6b9891e to
4d4f033
Compare
|
Concept ACK |
optout21
left a comment
There was a problem hiding this comment.
ACK 4d4f033
This change reduces complexity around the ContextualCheckBlock and ContextualCheckBlockHeader methods, and adds tests (for CheckBlock, ContextualCheckBlock, CheckBlockHeader and ContextualCheckBlockHeader).
The two Contextual... methods take only consensus parameters instead of chainstate manager, as only those are needed. The ContextualCheckBlockHeader method 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)
- It's not mentioned that the methods are exposed so that the tests can use them. This fact may be mentioned in the relevant commit and in the PR description.
- Inconsistencies with the prefixes in the commit messages: non-test code commits are not prefixed, test-only commits have "validation:" prefix. I would expect the non-test commits to have "validation:" prefix, and test-only commits have "test:" or "validation:" or both.
| BOOST_REQUIRE(block.fChecked); | ||
|
|
||
| // Invalidate the proof of work. The block is still accepted and valid, which | ||
| // is only possible if the cached result is returned without re-running the checks. |
There was a problem hiding this comment.
Nit: The test claim could be made stronger, by showing that with nNonce = 0 and 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).
ContextualCheckBlockHeader never reads BlockManager. bitcoin#35906 tests pass params and a clock. DeploymentActiveAfter on this tip still needs a VersionBitsCache, so those tests pass one.
| #include <string> | ||
| #include <vector> | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(validation_check_header_tests) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
CBlockHeader header;
header.nVersion = ...;
header.hashPrevBlock = ...;
...The variable header starts 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:
const auto header = CBlockHeader{
.nVersion = ...,
.hashPrevBlock = ...,
...
};However, as long as CBlockHeader prevents 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.
There was a problem hiding this comment.
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 #36458 (though we need to take this as far as enabling a json import).

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.