Skip to content

[Alt] validation: Ensure Invalid ValidationState has result - #32

Closed
optout21 wants to merge 4 commits into
masterfrom
2611-invalid-must-have-reason2
Closed

optout21 wants to merge 4 commits into
masterfrom
2611-invalid-must-have-reason2

Conversation

@optout21

Copy link
Copy Markdown
Owner

Alternative solution for bitcoin#36318

`TestBlockValidity` may reject a block for a reason that is specific
to checking block templates / proposals: the block is not built on
top of the current tip. Such a result does not belong in
`BlockValidationResult`, which is also used for blocks received from
peers and is exposed through the kernel API.

As a preparation, introduce `BlockTemplateValidationResult` and
`BlockTemplateValidationState`, and return the latter from
`TestBlockValidity`. `BlockTemplateValidationResult` has the same
underlying type as `BlockValidationResult`, so that all
`BlockValidationResult` values can be converted to it with a plain
cast (`ToBlockTemplateValidationResult`), without repeating them. Extra
values specific to template checks will use values above the range of
`BlockValidationResult`.

The checks that need a `BlockValidationState` are moved into a helper,
`TestBlockValidityInner`, whose result is converted to a
`BlockTemplateValidationState`.

Callers only use the generic `ValidationState` methods, so they need
only type changes. `BIP22ValidationResult` is made a template, as it
is used with both state types.

No behavior change.
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 unset result, it now uses a new value,
`BlockTemplateValidationResult::BLOCK_PREV_NOT_TIP`, representing a result
for when the block being checked is not built on top of the current tip.
This value is specific to `TestBlockValidity`, and is not part of
`BlockValidationResult`, so it does not affect P2P or the kernel API.
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.
Add a unit test to verify the result and reject reason in this case.
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 2611-invalid-must-have-reason2 branch from aa56ef8 to 5a824e9 Compare November 24, 2026 12:20
@hodlinator

Copy link
Copy Markdown

Not sure this is really better than my collapsed diff using util::Expected in bitcoin#36318 (review).

@hodlinator

Copy link
Copy Markdown

(But thanks for exploring this direction!)

I think it's less complex to add an enum value at the end rather than imposing constraints on the allowed enum integer values and then adding another enum etc.

@optout21

Copy link
Copy Markdown
Owner Author

Not sure this is really better than my collapsed diff using util::Expected in bitcoin#36318 (review).

Quite similar indeed. The difference is that returning an (expanded) Result struct has the same structure, fields, no need for extra diff. But there is need to convert from one enum to the other.

@optout21

Copy link
Copy Markdown
Owner Author

I think it's less complex to add an enum value at the end rather than imposing constraints on the allowed enum integer values and then adding another enum etc.

For sure, but the difference is that the more-used BlockValidationState is not affected at all, so less changes are needed, no need to expose the new value in kernel, no need to handle the new value in a few exhaustive switch statements.

@hodlinator

Copy link
Copy Markdown

It feels messy to be potentially storing integer values outside of the regular BlockValidationState enum set as is currently done here.

Would rather explore having BlockTemplateValidationState be a more custom util::Expected, like:

class BlockTemplateValidationState
{
private:
    util::Expected<BlockValidationState, std::string> m_inner;

public:
    BlockTemplateValidationState(BlockValidationState&& state): m_inner{std::move(state)} {}
    BlockTemplateValidationState(std::string error): m_inner{util::Unexpected{std::move(error)}} {}

    const std::string& GetRejectReason() const
    {
        if (m_inner.has_value()) {
            return m_inner->GetRejectReason();
        } else {
            return m_inner.error();
        }
    }
    std::string GetDebugMessage() const { return m_inner ? m_inner->GetDebugMessage() : ""; }
    bool IsValid() const { return m_inner && m_inner->IsValid(); }
    ...
};

The redirected function calls minimize diffs outside of TestBlockValidity(), like a "decorator" pattern. Not sure how to wrap GetResult(), maybe return an std::optional.

But I think bitcoin#36318 is fine.

@optout21

Copy link
Copy Markdown
Owner Author

@hodlinator , thanks for checking and opining on this!
I like your suggestion, using util::Expected<BlockValidationState, std::string> inside BlockTemplateValidationState.
But I'm still leaning towards the current state of bitcoin#36318 (option 1).

@optout21

Copy link
Copy Markdown
Owner Author

Closing this, alternative discarded. RIP, it was a good agent :D

@optout21 optout21 closed this Nov 27, 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.

2 participants