Skip to content

validation: Ensure Invalid ValidationState has result - #24

Closed
optout21 wants to merge 4 commits into
masterfrom
2609-invalid-must-have-reason
Closed

optout21 wants to merge 4 commits into
masterfrom
2609-invalid-must-have-reason

Conversation

@optout21

@optout21 optout21 commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

Block and transaction validation results are represented in ValidationState, and invalid results in "Invalid" states. Invalid validation results have a result, an enum value, and a reject_reason, a structured string.

The change is to ensure that an invalid result always has a set result and reject reason. This increases code robustness/security.

One instance was identified where result was not set, and is being fixed by a new BLOCK_INCONCLUSIVE result. PR bitcoin#31981 moved a previous-block check from outside of TestBlockValidity() to inside it, and introduced a new usage of BlockValidationState with unset result. The result field is not checked outside. This instance is changed now to use the newly-introduced BLOCK_INCONCLUSIVE value.

The new enum value is added to the kernel C API and its C++ wrapper. In P2P, a BLOCK_INCONCLUSIVE result does not lead to punishing the peer.

A few test-only instances with empty reject reason are changed to use a dummy value ("dummy-reason").

History: this was found on the side of bitcoin#35570, but left untouched there; see bitcoin#35570 (comment) and bitcoin#35570 (comment).

The goal is to ensure that an Invalid `ValidationState` always has a set result.
However, there is a violating usage in `TestBlockValidity`.
As a preparation to address that, a new value is added to the `BlockValidationResult`
enum, `BLOCK_INCONCLUSIVE`, representing a result where validity could not be
determined, e.g. due to some missing data (previous block).
A corresponding value is added to the kernel C API
(btck_BlockValidationResult_INCONCLUSIVE) and its C++ wrapper.
In P2P, a `BLOCK_INCONCLUSIVE` result does not lead to punishing the peer.
The new value is used in a later commit.
The goal is to ensure that an Invalid ValidationState always has a set result.
A violating usage in `TestBlockValidity` is being changed: instead of the
default `BLOCK_RESULT_UNSET`, it now uses `BLOCK_INCONCLUSIVE`.
The result field from `TestBlockValidity` is currently not inspected by callers,
but the constraint "an Invalid state must have a set result"
is satisfied this way.
Ensure that an Invalid `ValidationState`, constructed through `Invalid()`,
always has a set result field, by using an `Assume`.
This increases code robustness/security.
In addition to ensuring a set result, also ensure that `reject_reason`
is not empty for an Invalid `ValidationState`.
Also remove its empty default value, as it should never be empty.
Some test-only usages, where empty reject reason was used, are changed
to use a dummy value.
@optout21
optout21 force-pushed the 2609-invalid-must-have-reason branch from a3227fb to 22cf9d5 Compare September 23, 2026 11:41
@optout21 optout21 changed the title validation: Ensure Invalid ValidationState has reason validation: Ensure Invalid ValidationState has result Sep 23, 2026
@optout21

Copy link
Copy Markdown
Owner Author

--> bitcoin#36318

@optout21 optout21 closed this Sep 23, 2026
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.

1 participant