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

Open
optout21 wants to merge 3 commits from 2611-validationstate-reason-overwrite into master AGit
optout21 commented 2026-11-06 18:12:42 +00:00 (Migrated from github.com)

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.

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 commented 2026-11-06 18:12:55 +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
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.

<!--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/36792 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36792.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36792#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 | | ---- | --------- | | Stale ACK | [Pranav-error](https://github.com/bitcoin/bitcoin/pull/36792#issuecomment-6971586730) | 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: * [#36326](https://github.com/bitcoin/bitcoin/pull/36326) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36326.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Flush-only failures lose their sole fatalError() notification

A block-file flush failure during disconnection now sends only flush_error; bitcoind still shuts down, but the kernel callback category changes rather than merely losing a duplicate notification.

Location: src/validation.cpp:3302 (head).

🔵 Suggestion · DisconnectTip()'s already-error return lacks targeted coverage

The abort-node, direct flush-failure, and new mutated-block tests cannot detect restoration of the unconditional FatalError() here; adapting the existing flush-failure injection to a reorg remains unconfirmed.

Location: src/validation.cpp:3303 (head).

Review coverage was incomplete.


Reviewed: becd5c27a77a · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:806938d40e279b6d701b2e78128bb1a09b55193a0b697245a1dce6241d3081c1:3607c760b63c01fb7f1f349adfc3fd0614b148a7df02e42f2f0edc2d448925a1 --> <!-- ralph-base:0e8f70a86314bc73539b110d947c41d40c4886fc --> <!-- ralph-head:becd5c27a77ad5fa4bda2de3ebed4a906d007857 --> <!-- ralph-case:65613a2cb57abe6e88bfc6c31b2d57baaef1b39b607f2052b599fe8a7ecf0130 --> **🔵 Suggestion · Flush-only failures lose their sole `fatalError()` notification** A block-file flush failure during disconnection now sends only `flush_error`; `bitcoind` still shuts down, but the kernel callback category changes rather than merely losing a duplicate notification. Location: [`src/validation.cpp:3302`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/becd5c27a77ad5fa4bda2de3ebed4a906d007857/src/validation.cpp#L3302>) (head). **🔵 Suggestion · `DisconnectTip()`'s already-error return lacks targeted coverage** The abort-node, direct flush-failure, and new mutated-block tests cannot detect restoration of the unconditional `FatalError()` here; adapting the existing flush-failure injection to a reorg remains unconfirmed. Location: [`src/validation.cpp:3303`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/becd5c27a77ad5fa4bda2de3ebed4a906d007857/src/validation.cpp#L3303>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`becd5c27a77a`](<https://git.fish.foo/bitcoin/bitcoin/commit/becd5c27a77ad5fa4bda2de3ebed4a906d007857>) · [Review report](<https://review.fish.foo/report/36792-becd5c27a77ad5fa4bda2de3ebed4a906d007857-3141-3.html>) · [Previous reports](<https://review.fish.foo/traces/36792/>) [permalink](<https://review.fish.foo/report/36792-becd5c27a77ad5fa4bda2de3ebed4a906d007857-3141-3.html>)
Pranav-error commented 2026-11-18 07:02:33 +00:00 (Migrated from github.com)

tACK b4b95702b

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.
tACK b4b95702b 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 commented 2026-11-19 07:17:22 +00:00 (Migrated from github.com)

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

> 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."
DrahtBot commented 2026-11-25 00:54:23 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
optout21 commented 2026-11-25 07:25:23 +00:00 (Migrated from github.com)

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

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

Non-contentious rebase (due to #36109), no other changes. (`git range-diff f12cc4a..b4b9570 3e4c803..becd5c2` `git range-diff b4b9570...becd5c2`)
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!36792
No description provided.