Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. LLM Linter (✨ experimental)Possible places where named args for integral literals may be used (e.g.
2026-11-24 22:47:15 |
cc4bac4 to
8963bb6
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
8963bb6 to
d7e6035
Compare
|
While working on this PR, one instance was identified where the invariant Options to resolve this: A. Since the return value is ignored, ignore the The ignoring of flush result (A.) is also proposed in #29700 (cc: @ryanofsky ). The minor behavior-change could be also omitted from this PR, limiting strictly to a no-behavior-change refactor:
|
d7e6035 to
a3a5c03
Compare
|
Concept ACK |
|
Concept ACK. Preferring the return value over output parameters is a useful and I would say necessary improment. But could we avoid using {
BlockValidationState state;
if (...) {
state.Invalid(...);
}
return state;
}I would prefer something like: {
if (...) {
return BlockValidationState::Invalid(...);
}
return BlockValidationState::Valid();
} |
I fully agree, shorter-scoped variables mean less chance of potential interplay between calls, less complexity. I did change this in several places, but not everywhere. In some places there was a real chance for interaction between subsequent calls (e.g. error reason is set conditional of already set value), and I kept it to be safe (w.r.t. behavior changes). But I should review and change it more aggressively. |
4626ef2 to
8207ac1
Compare
|
Applied some improvements:
|
There was a problem hiding this comment.
concept ACK 8207ac1bb79f76c9005e8e2d695b725539b3c5c0
Looks good overall. I'd push this one step further and use util::Expected here, it's a natural fit and follows the PR's own motivation. The PR already cleans up the bool + state out-param inconsistency by making state the return value; util::Expected<T, BlockValidationState> does the same thing but at the type level instead of by convention. Success and failure become distinct alternatives of the type.
Concretely, a bare BlockValidationState still carries all three modes (M_VALID/M_INVALID/M_ERROR) in the return value even though M_VALID is now redundant with "the call succeeded". With util::Expected that mode collapses into has_value() and is no longer stored, the failure modes move to the error channel, and out-params like ppindex fold into the success channel. util/result.h also explicitly steers low-level functions toward util::Expected.
// pindex folded into the success channel
[[nodiscard]] util::Expected<CBlockIndex*, BlockValidationError> AcceptBlockHeader(
const CBlockHeader& block, bool min_pow_checked);
auto res = AcceptBlockHeader(header, /*min_pow_checked=*/true);
if (!res) {
if (res.error().IsInvalid()) MaybePunishNodeForBlock(...); // peer's fault
return util::Unexpected{std::move(res).error()};
}
CBlockIndex* pindex = res.value(); // only reachable on successOne open question (happy to leave it as discussion): where does M_INVALID fit best? Above I put it in the error channel next to M_ERROR, but it's arguably a successful, expected outcome of validation, the function did run and produced a verdict ("this block is invalid"), whereas M_ERROR is a genuine runtime failure that prevented producing a result. So an alternative split would keep Valid/Invalid in the value channel and reserve the error channel for runtime errors only. I don't have a strong preference.
| * @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers | ||
| * @returns false if AcceptBlockHeader fails on any of the headers, true otherwise (including if headers were already known) | ||
| * @returns BlockValidationState indicating the result. IsValid() returns true if all headers | ||
| * were accepted. On failure, IsInvalid() is false and the state contains the specific |
There was a problem hiding this comment.
On failure, IsInvalid() is false
Would not be "true"?
There was a problem hiding this comment.
Good catch. On failure IsValid() is false, moreover IsInvalid() is true. I state: "On failure, IsValid() is false and the state contains the specific validation failure reason. Never returns Error state.". From the last bit it follows that IsInvalid() is true, but I don't mention it here, only IsValid(), as that should be used to check for normal vs. exceptional case.
|
Concept ACK
There is a thread in #33856 where this was also brought up. I included a POC in the last comment of that thread which keeps the value channel for In general, I think stripping the runtime failure from |
774c2c0 to
7b7392e
Compare
optout21
left a comment
There was a problem hiding this comment.
Fixed the point raised by @arejula27.
git range-diff 774c2c0c667de49f4181953fae5177db34eae5b2...7b7392e61f35fdab5e22d7348ae05154a81e2b58
| // corrupted, so this should be impossible unless we're having hardware | ||
| // problems. | ||
| return FatalError(m_chainman.GetNotifications(), state, _("Corrupt block found indicating potential hardware failure.")); | ||
| state = FatalError(m_chainman.GetNotifications(), _("Corrupt block found indicating potential hardware failure.")); |
There was a problem hiding this comment.
I concluded that overwriting the rejection reason here is the right way to go, as a new, higher level error is created from the previous invalid result. Also, it's not recommended to mix Error status with Invalid-style reject reasons.
My fix is adding a specific log and comment about the upgrading of the error.
I've also checked for other potential places where an Invalid state can be upgraded to Error, but found none (checked changed in validation.cpp, especially around the patterns state = ..., MakeError, FatalError).
In fact, with these changes the conditional change of reject reason in ValidationState::Error is not needed, it is only called with a fresh Valid state, the if could be changed to an assert or removed. I haven't done so (just tried it).
|
tACK 7b7392e Corrupted a block on disk and forced a reorg to read it, then compared the logs: master: this PR: The reason is still logged, so this looks good to me. I agree with your view that a new, higher level error is forwarded. Not sure if it should be discussed deeper (long related discussion on #35646) but it is ok for me. |
| // then that is a failure of our local system -- we should abort | ||
| // rather than stay on a less work chain. | ||
| FatalError(m_chainman.GetNotifications(), state, _("Failed to disconnect block.")); | ||
| state = FatalError(m_chainman.GetNotifications(), _("Failed to disconnect block.")); |
There was a problem hiding this comment.
Self-review note: In this line a previous non-valid state may be overwritten. The DisconnectTip() -> FlushStateToDisk() call may return an Error state. In the case FatalError() is called twice.
|
🐙 This pull request conflicts with the target branch and needs rebase. |
Add `MakeInvalid` static helper to create invalid `BlockValidationState` instance, for easier construction in error branches. Instead of the 3 statements of declaration, setting, and return with the exisiting non-static method, now return&construction is possible in one statement.
Change the (internal) method `CheckBlockHeader` to return the validation result in the return value instead of an output parameter.
Change the (internal) method `ContextualCheckBlockHeader` to return the validation result in the return value instead of an output parameter.
Change the method `AcceptBlockHeader` to return the `BlockValidationState` validation result in the return value instead of an output parameter.
Return BlockValidationState by value instead of using an out-parameter, similar to the TestBlockValidity refactoring in 74690f4. Remove redundant int return from btck_chainstate_manager_process_block_header. Previously returned both an int result and an output validation state parameter, creating ambiguity where non-zero could mean either invalid header or processing failure. Since ProcessNewBlockHeaders already provides complete validation info, the int return was redundant. Co-authored-by: stringintech <[email protected]> Co-authored-by: stickies-v <[email protected]>
Change the (internal) method `CheckMerkleRoot` to return the `BlockValidationState` validation result in the return value instead of an output parameter.
Change the method `CheckBlock` to return the `BlockValidationState` validation result in the return value instead of an output parameter.
Change the (internal) method `CheckWitnessMalleation` to return the `BlockValidationState` validation result in the return value instead of an output parameter.
Change the (internal) method `ContextualCheckBlock` to return the `BlockValidationState` validation result in the return value instead of an output parameter.
Change the method `FatalError` to return the constructed `BlockValidationState` object in a return value instead of an output parameter. Add a helper `MakeError` factory method to the `BlockValidationState`, similar to `MakeInvalid`. Special care is taken when an Invalid state is upgraded to Error. Co-authored-by: Íñigo Aréjula Aísa <[email protected]>
Change the method `FlushStateToDisk` to return the `BlockValidationState` validation result in the return value instead of an output parameter.
Change the method `AcceptBlock` to return the `BlockValidationState` validation result in the return value instead of an output parameter. Co-authored-by: Íñigo Aréjula Aísa <[email protected]>
7b7392e to
beb7dc9
Compare
|
Straightforward rebase (due to #36782), no other change. |

Summary. Refactor validation result to be a return value instead of an output parameter in several
validation.cppmethods.Motivation. The benefits of the change are:
stateare inconsistentThis has grown out from #33856, mentioned in comment here and here.
Details. Many methods follow the scheme where the validation state is returned in an output parameter (
BlockValidationState& state), and and additionalboolreturn value indicating success. In success case the convention is thatstate.IsValid()and the return value are both true. After the change there is only aBlockValidationStatereturn value, which is either success (state.IsValid() == true), or an invalid/error case.This change is a highly localized refactor, but touching a sensitive file.
Relevant methods called by
ProcessNewBlockHeadersandAcceptBlock(directly and indirectly) are touched.Changes are separated into commits by touched methods, ordered by bottom-to-top in the call hierarchy.