From 82ef92c8d006b3f5c3baaf00e5f8200d289d85d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Sat, 31 Jan 2026 19:20:20 +0100 Subject: [PATCH] consensus/doc: explain unreachable `bad-txns-fee-outofrange` check After the previous conditions were validated, document why `bad-txns-fee-outofrange` is unreachable once `nValueIn` and `value_out` are `MoneyRange` and `nValueIn >= value_out`. Although unreachable, keep the check itself in place (instead of removing) as it's part of consensus-critical code; the comment serves as a proof for future refactors. Inspired by b-c-cov coverage reports: * "bad-txns-fee-outofrange" - https://maflcko.github.io/b-c-cov/test_bitcoin.coverage/src/consensus/tx_verify.cpp.gcov.html#L200 --- src/consensus/tx_verify.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/consensus/tx_verify.cpp b/src/consensus/tx_verify.cpp index 00022a33dd3..ec612a55e6f 100644 --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -197,6 +197,11 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, // Tally transaction fees const CAmount txfee_aux = nValueIn - value_out; if (!MoneyRange(txfee_aux)) { + // Unreachable, given the following preconditions: + // * `value_out` comes from `tx.GetValueOut()`, which throws unless `MoneyRange(value_out)` and asserts `MoneyRange(nValueOut)` on return. + // * `MoneyRange(nValueIn)` was enforced in the input loop. + // * `nValueIn < value_out` was handled above, so `nValueIn >= value_out` here (and `txfee_aux >= 0`). + // Therefore `0 <= txfee_aux = nValueIn - value_out <= nValueIn <= MAX_MONEY`. return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-fee-outofrange"); }