Skip to content

validation: avoid partial override of non-valid validation state - #36792

Open
optout21 wants to merge 3 commits into
bitcoin:masterfrom
optout21:2611-validationstate-reason-overwrite
Open

optout21 wants to merge 3 commits into
bitcoin:masterfrom
optout21:2611-validationstate-reason-overwrite

Conversation

@optout21

@optout21 optout21 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

ValidationState::Error() only sets the reject reason if the state is still valid. When it is called on a state that is already Invalid or Error, the mode changes but the earlier reason, result and debug message are kept. The result is a mix of the old and new error. Code review in #35570 found two places where this happens. This makes the in-and-out use of the state parameter harder to follow and to remove later.

This PR makes both cases explicit:

  1. ConnectBlock() → CheckBlock() with a mutated block (BLOCK_MUTATED, e.g. a merkle root mismatch). This is treated as a sign of corruption and reported through FatalError(), but the resulting Error state kept BLOCK_MUTATED / bad-txnmrklroot. Now the Invalid state is logged and then replaced, so the Error state carries the corruption message.

  2. ActivateBestChainStep() → DisconnectTip() → FlushStateToDisk() failure. FlushStateToDisk() has already reported a fatal error, and ActivateBestChainStep() then called FatalError() a second time with a less specific message. Now the existing error is logged and returned unchanged, and no second fatal error is reported.

Behavior changes:

  • Case 1: the returned state has the reject reason "Corrupt block found indicating potential hardware failure.", no result and no debug message. The original reason is logged.
  • Case 2: the duplicate fatal-error notification is no longer sent. The returned reject reason does not change.

The first commit adds a unit test for case 1 that captures the current behavior, and the second commit updates its expectations. Case 2 has no test, because a flush failure cannot be triggered without fault injection. The path where DisconnectTip() fails while the state is still valid is covered by feature_abortnode.py and is unchanged.

@DrahtBot

DrahtBot commented Nov 6, 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
Stale ACK Pranav-error

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:

  • #36326 (kernel: Use typed errors for fatal and flush error notifications by arejula27)

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.

@Pranav-error

Copy link
Copy Markdown

tACK b4b9570

Built PR head b4b95702b on macOS. All validation_block_tests pass.

Also ran a mutation check by restoring the pre-fix validation.cpp (89bd6a3a6) while keeping the new test. connectblock_mutated_block_is_fatal_error fails as expected because the corruption message is missing from the debug log. With the fix restored, it passes.

Case 2 (ActivateBestChainStep) is not covered by a test in this PR. From DisconnectTip():

  • Block read failure: state remains Valid.
  • DisconnectBlock() failure: state remains Valid.
  • FlushStateToDisk() failure: state becomes Error.

@optout21

optout21 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Case 2 (ActivateBestChainStep) is not covered by a test

Thanks for checking, that's true, known. The Valid-state path (DisconnectBlock failure) is exercised by feature_abortnode.py, and its behavior is unchanged. The Error-state path needs FlushStateToDisk(IF_NEEDED) to both decide to write and then fail (low disk space, a block-file flush error, or an exception from the coins DB), and none of these can be injected in tests today. I prefer not to add fault-injection scaffolding in this PR, but I'm happy to do it as a follow-up if reviewers think it's worth it.
The PR description already contains: "Case 2 has no test, because a flush failure cannot be triggered without fault injection."

Add a unit test for the case when a mutated block is encountered
in ConnectBlock, to document the exact return state and reject reason.
In order to avoid confusing partial override of error state,
log the old state and override it in ConnectBlock().
When CheckBlock() returns an Invalid state due to a mutated block,
the error is upgraded to Error state. A new log entry prints the
Invalid state, then it is overridden to an Error state.
The behavior change is that the reject reason was previously
kept, now it is overwritten, and result and debug messages are reset.
In order to avoid confusing partial override of error state,
don't overwrite the error in ActivateBestChainStep().
When DisconnectTip() returns an error state, the fatal error has
already been reported (by FlushStateToDisk()). Log it and return,
without reporting a second, less specific fatal error.
Behavior change: the duplicate fatalError notification is no longer
emitted in this case.
@optout21
optout21 force-pushed the 2611-validationstate-reason-overwrite branch from b4b9570 to 225057d Compare November 25, 2026 07:25
@optout21

optout21 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Non-contentious rebase (due to #36109), no other changes.

(git range-diff f12cc4a..b4b9570 3e4c803..becd5c2 git range-diff b4b9570...becd5c2)

@optout21
optout21 force-pushed the 2611-validationstate-reason-overwrite branch from 225057d to becd5c2 Compare November 25, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants