Repository navigation
Conversation
`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.
aa56ef8 to
5a824e9
Compare
|
Not sure this is really better than my collapsed diff using |
|
(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. |
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. |
For sure, but the difference is that the more-used |
|
It feels messy to be potentially storing integer values outside of the regular Would rather explore having 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 But I think bitcoin#36318 is fine. |
|
@hodlinator , thanks for checking and opining on this! |
|
Closing this, alternative discarded. RIP, it was a good agent :D |
Alternative solution for bitcoin#36318