Skip to content

Conversation

@yuvicc
Copy link
Contributor

@yuvicc yuvicc commented Nov 11, 2025

Motivation

This PR refactors ProcessNewBlockHeaders() to return BlockValidationState by value instead of using out-parameters or boolean returns. This follows the pattern established in #31981 (commit 74690f4) which refactored TestBlockValidity() in a similar manner.

Current Issues

ProcessNewBlockHeaders(): Uses an out-parameter BlockValidationState& state, making the API less intuitive.

As noted by @TheCharlatan in #33822 comment and can be a fix for that too:

One thing that could be considered here is returning the BlockValidationState directly instead of having an in/out param. To safely do that I think we'd need to refactor ProcessNewBlockHeaders though, similarly to what was done in 74690f4.

The changes are split into two commits, see individual commit message for more info.

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 11, 2025

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33856.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK w0xlt
Concept ACK hodlinator, exp3rimenter
Stale ACK danielabrozzoni

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:

  • #33822 (kernel: Add block header support and validation by yuvicc)
  • #32740 (refactor: Header sync optimisations & simplifications by danielabrozzoni)

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.

Copy link
Contributor

@hodlinator hodlinator left a comment

Choose a reason for hiding this comment

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

Concept ACK moving out-reference-parameters to return value

Not familiar with the kernel API but had a brief look at net_processing and validation.

@w0xlt
Copy link
Contributor

w0xlt commented Nov 13, 2025

Concept ACK

@yuvicc yuvicc force-pushed the 2025-11-refractor_bool_to_blockvalidationstate branch 2 times, most recently from 6cdba25 to 1f589e1 Compare November 16, 2025 07:23
@DrahtBot
Copy link
Contributor

🚧 At least one of the CI tasks failed.
Task Linux->Windows cross, no tests: https://github.com/bitcoin/bitcoin/actions/runs/19402124289/job/55511133875
LLM reason (✨ experimental): Compilation failed due to a syntax error in validation.cpp (missing semicolon before BlockValidationState in ProcessNewBlockHeaders).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

@yuvicc
Copy link
Contributor Author

yuvicc commented Nov 16, 2025

Thanks for the review @hodlinator

  • Added Assume(!headers.empty()) before instantiating validation state as suggested in comment
  • Removed shadow state comment

@exp3rimenter
Copy link

Concept ACK on refactoring bool to return state.

Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

This PR eliminates the ambiguity of the boolean return value and improves the clarity of both the internal and kernel APIs.

ACK 1f589e1 (with the above-mentioned nit)

@yuvicc
Copy link
Contributor Author

yuvicc commented Nov 23, 2025

Thanks for the review @w0xlt

@yuvicc yuvicc force-pushed the 2025-11-refractor_bool_to_blockvalidationstate branch from 8d97193 to 857ebdd Compare November 25, 2025 14:23
@yuvicc
Copy link
Contributor Author

yuvicc commented Nov 25, 2025

Thanks for the review @TheCharlatan.

  • Addressed comment on using post-condition checks.
  • Dropped the change to use separate state for reporting errors and not invalidity as suggested by @TheCharlatan comment

@yuvicc yuvicc force-pushed the 2025-11-refractor_bool_to_blockvalidationstate branch from 857ebdd to f31f7c2 Compare November 27, 2025 11:27
@yuvicc
Copy link
Contributor Author

yuvicc commented Nov 27, 2025

I agree with @sedited comment, the belt-and-suspenders check in ActivateBestChain in validation.cpp is broken by this change. When m_disabled is true, ActivateBestChain returns false without setting the BlockValidationState to invalid. This causes the new NONFATAL_UNREACHABLE() assert in ProcessNewBlock to trigger incorrectly. The belt-and-suspenders check exists precisely because we can't guarantee ActivateBestChain won't be called on a disabled chainstate.

So dropping the change in ProcessNewBlock and only keeping ProcessNewBlockHeaders now.

@yuvicc yuvicc changed the title kernel, validation: Refactor ProcessNewBlock(Headers) to return BlockValidationState kernel, validation: Refactor ProcessNewBlockHeaders to return BlockValidationState Nov 27, 2025
Copy link
Member

@danielabrozzoni danielabrozzoni left a comment

Choose a reason for hiding this comment

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

light ACK f31f7c21ff7093a4c90199cd0bb2128d19bf2d33

Code looks good to me, and the interface is cleaner now. I'm only light-acking sicne I'm not familiar with the kernel :)

As I pointed out in a comment, I don't think we needed the Assume(!headers.empty()) at the start of ProcessNewBlockHeaders, but I'm ok with keeping it.

Add C API functions for managing BlockValidationState lifecycle:
  - btck_block_validation_state_create()
  - btck_block_validation_state_copy()
  - btck_block_validation_state_destroy()

Introduce BlockValidationStateApi<> template to share common getter methods between BlockValidationState (Handle) and BlockValidationStateView (View) classes in the C++ wrapper.

Update ValidationInterface::BlockChecked to use BlockValidationStateView since it doesn't need ownership.

This changes prepares the kernel API to return BlockValidationState by value in subsequent commits.
Return BlockValidationState by value instead of using an out-parameter, similar to the TestBlockValidity refactoring in 74690f4.

This provides a cleaner API and enables callers to inspect detailed validation state without relying on side effects through reference parameters.
@yuvicc yuvicc force-pushed the 2025-11-refractor_bool_to_blockvalidationstate branch from f31f7c2 to be379fd Compare December 10, 2025 04:55
@yuvicc
Copy link
Contributor Author

yuvicc commented Dec 10, 2025

Rebased f31f7c2 -> be379fd.

Copy link
Contributor

@w0xlt w0xlt left a comment

Choose a reason for hiding this comment

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

reACK be379fd

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.

8 participants