Skip to content

First steps towards a stateless, side-effect free validation library - #35906

Open
purpleKarrot wants to merge 7 commits into
bitcoin:masterfrom
purpleKarrot:validation
Open

purpleKarrot wants to merge 7 commits into
bitcoin:masterfrom
purpleKarrot:validation

Conversation

@purpleKarrot

Copy link
Copy Markdown
Contributor

This implements the following actions from #35904:

  1. Expose the validation interface

    Make the existing CheckBlockHeader, ContextualCheckBlockHeader, and ContextualCheckBlock functions part of the public validation interface by declaring them in validation.h.

  2. Interface Segregation: Remove dependency on ChainstateManager

    ContextualCheck* functions need a ChainstateManager only to access the
    consensus parameters. Pass Consensus::Params instead and access them at the call site.

  3. Make environmental dependencies explicit

    Pass the validation time explicitly to ContextualCheckBlockHeader instead of reading the system clock internally.

@DrahtBot

DrahtBot commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

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.

Type Reviewers
ACK optout21
Concept ACK josibake, yuvicc
Stale ACK janb84

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:

  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #36167 (build: Enable -Wunused by fanquake)
  • #35557 (kernel, validation: Add btck_chainstate_manager_set_clock_time by ryanofsky)

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.

Comment thread src/validation.cpp

// 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}) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conflicts with #35557, which is an alternative to #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.

Comment thread src/validation.cpp
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)) ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this partially reverts 78adef1. 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 #34895, which seems to be adding tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the refactor change would come with better tests included

Good point. Done.

@janb84 janb84 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@josibake

Copy link
Copy Markdown
Member

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 ContextualCheckBlockHeader no longer reading system time.

Comment thread src/validation.h Resolved
Comment thread src/validation.cpp Resolved
Comment thread src/validation.cpp Resolved
#include <utility>
#include <vector>

BOOST_AUTO_TEST_SUITE(validation_check_block_tests)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason we don't use BOOST_FIXTURE_TEST_SUITE like the other validation_*_tests.cpp do?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the functions under test are stateless, there is not much test setup that would justify a fixture.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense - thank you!


BOOST_CHECK(!CheckBlockHeader(header, state, consensusParams));
BOOST_CHECK(state.IsInvalid());
BOOST_CHECK(state.GetRejectReason() == "high-hash");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about BOOST_CHECK_EQUAL?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there an advantage?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the error messages are better, but not worth touching again for this.

purpleKarrot and others added 7 commits August 20, 2026 21:24
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.
@yuvicc

yuvicc commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Concept ACK

@optout21 optout21 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

nervana21 added a commit to nervana21/bitcoin that referenced this pull request Oct 6, 2026
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants