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

Open
purpleKarrot wants to merge 7 commits from validation into master AGit
purpleKarrot commented 2026-08-05 18:50:31 +00:00 (Migrated from github.com)

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.

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 commented 2026-08-05 18:50:38 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35906 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35906.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35906#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [optout21](https://github.com/bitcoin/bitcoin/pull/35906#pullrequestreview-5129791192) | | Concept ACK | [josibake](https://github.com/bitcoin/bitcoin/pull/35906#issuecomment-5293252907), [yuvicc](https://github.com/bitcoin/bitcoin/pull/35906#issuecomment-5473519576) | | Stale ACK | [janb84](https://github.com/bitcoin/bitcoin/pull/35906#pullrequestreview-4916125267) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36904](https://github.com/bitcoin/bitcoin/pull/36904) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36904.svg"></sub> (log: Remove trailing and embedded newlines from log messages by maflcko) * [#36167](https://github.com/bitcoin/bitcoin/pull/36167) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36167.svg"></sub> (build: Enable `-Wunused` by fanquake) * [#35557](https://github.com/bitcoin/bitcoin/pull/35557) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35557.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
maflcko (Migrated from github.com) reviewed 2026-08-06 06:59:33 +00:00
maflcko (Migrated from github.com) commented 2026-08-06 06:54:10 +00:00

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 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.
maflcko (Migrated from github.com) commented 2026-08-06 06:57:11 +00:00

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.

this partially reverts 78adef17536edef833a0bfca06b61ce28120e486. 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.
purpleKarrot (Migrated from github.com) reviewed 2026-08-07 11:13:39 +00:00
purpleKarrot (Migrated from github.com) commented 2026-08-07 11:13:39 +00:00

maybe the refactor change would come with better tests included

Good point. Done.

> maybe the refactor change would come with better tests included Good point. Done.
janb84 (Migrated from github.com) reviewed 2026-08-12 11:50:16 +00:00
janb84 (Migrated from github.com) left a comment

t ACK.

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.

t ACK. 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 commented 2026-08-14 12:24:49 +00:00 (Migrated from github.com)

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.

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.
alexanderwiederin (Migrated from github.com) reviewed 2026-08-20 14:39:14 +00:00
alexanderwiederin (Migrated from github.com) commented 2026-08-20 14:34:04 +00:00

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

Any reason we don't use `BOOST_FIXTURE_TEST_SUITE` like the other `validation_*_tests.cpp` do?
alexanderwiederin (Migrated from github.com) commented 2026-08-20 14:35:43 +00:00

This test reuses BlockValidationState across CheckBlock calls, the other tests create new ones. Should we make it consistent?

This test reuses BlockValidationState across CheckBlock calls, the other tests create new ones. Should we make it consistent?
alexanderwiederin (Migrated from github.com) commented 2026-08-20 14:38:30 +00:00

What about BOOST_CHECK_EQUAL?

What about `BOOST_CHECK_EQUAL`?
Lines 4093-4105
alexanderwiederin (Migrated from github.com) commented 2026-08-20 14:15:30 +00:00

Should these doc comments move to the header now?

Should these doc comments move to the header now?
Lines 4364-4365
alexanderwiederin (Migrated from github.com) commented 2026-08-20 14:22:40 +00:00

params.GetConsensus() for CheckBlock and GetConsensus() for ContectualBlockCheck. Should they be aligned?

`params.GetConsensus()` for CheckBlock and `GetConsensus()` for ContectualBlockCheck. Should they be aligned?
alexanderwiederin (Migrated from github.com) commented 2026-08-20 14:14:00 +00:00

I would suggest adding something like: /** Context-dependent validity checks */ for consistency with line 394.

I would suggest adding something like: `/** Context-dependent validity checks */` for consistency with line 394.
purpleKarrot (Migrated from github.com) reviewed 2026-08-20 19:43:28 +00:00
purpleKarrot (Migrated from github.com) commented 2026-08-20 19:43:28 +00:00

obsolete with the comment below.

obsolete with the comment below.
purpleKarrot (Migrated from github.com) reviewed 2026-08-20 19:43:55 +00:00
Lines 4093-4105
purpleKarrot (Migrated from github.com) commented 2026-08-20 19:43:55 +00:00

You are right. the docs should be part of the declaration.

You are right. the docs should be part of the declaration.
purpleKarrot (Migrated from github.com) reviewed 2026-08-20 19:44:28 +00:00
Lines 4364-4365
purpleKarrot (Migrated from github.com) commented 2026-08-20 19:44:28 +00:00

ok.

ok.
purpleKarrot (Migrated from github.com) reviewed 2026-08-20 19:48:53 +00:00
purpleKarrot (Migrated from github.com) commented 2026-08-20 19:48:53 +00:00

Is there an advantage?

Is there an advantage?
purpleKarrot (Migrated from github.com) reviewed 2026-08-20 19:50:05 +00:00
purpleKarrot (Migrated from github.com) commented 2026-08-20 19:50:05 +00:00

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

Since the functions under test are stateless, there is not much test setup that would justify a fixture.
purpleKarrot (Migrated from github.com) reviewed 2026-08-20 19:52:43 +00:00
purpleKarrot (Migrated from github.com) commented 2026-08-20 19:52:43 +00:00

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.

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.
alexanderwiederin (Migrated from github.com) reviewed 2026-08-20 23:27:49 +00:00
alexanderwiederin (Migrated from github.com) commented 2026-08-20 23:27:49 +00:00

Make sense - thank you!

Make sense - thank you!
alexanderwiederin (Migrated from github.com) reviewed 2026-08-20 23:28:28 +00:00
alexanderwiederin (Migrated from github.com) commented 2026-08-20 23:28:28 +00:00

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

I think the error messages are better, but not worth touching again for this.
yuvicc commented 2026-08-31 04:00:29 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
optout21 (Migrated from github.com) reviewed 2026-09-07 09:08:32 +00:00
optout21 (Migrated from github.com) left a comment

ACK 4d4f0331cf

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.
ACK 4d4f0331cf50d9721e28e728df1c9b2573a083a4 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.
optout21 (Migrated from github.com) commented 2026-09-07 08:39:58 +00:00

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

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).
First-time contributor

Base: 3eca89820e1fb152ebf0315e3924fc374cb35958
Head: 4d4f0331cf50d9721e28e728df1c9b2573a083a4

Findings

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
  • adversarial_glm: Model response reached its 32,000-token output limit.
  • verifier: The linked #35904 and #35187 comment histories could not be retrieved, so their comment-level rationale was not independently verified. The code findings and current PR discussion were checked.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:0c398e5fd579cb8b00b6b62f2903ad741ac3f867996353a69082cd6ac57373b5:9d9e25a7349f2e108262b6ed44577d47304e975faf1917f1972defa0a828ae3d --> Base: `3eca89820e1fb152ebf0315e3924fc374cb35958` Head: `4d4f0331cf50d9721e28e728df1c9b2573a083a4` ##### Findings **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. <details> <summary>Review limitations</summary> - adversarial_glm: Model response reached its 32,000-token output limit. - verifier: The linked #35904 and #35187 comment histories could not be retrieved, so their comment-level rationale was not independently verified. The code findings and current PR discussion were checked. </details> [Full review report](<https://review.fish.foo/traces/35906-4d4f0331cf50d9721e28e728df1c9b2573a083a4-1055-3.html>)
sedited (Migrated from github.com) reviewed 2026-10-22 07:48:58 +00:00
sedited (Migrated from github.com) commented 2026-10-22 07:48:58 +00:00

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 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?
purpleKarrot (Migrated from github.com) reviewed 2026-10-22 10:24:23 +00:00
purpleKarrot (Migrated from github.com) commented 2026-10-22 10:24:23 +00:00

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.

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: ```cpp 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: ```cpp 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.
sedited (Migrated from github.com) reviewed 2026-11-05 12:02:43 +00:00
sedited (Migrated from github.com) commented 2026-11-05 12:02:43 +00:00

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

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).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35906
No description provided.