test: cover cumulative input value range in CheckTxInput tests. #36686

Manually merged
KY-U merged 3 commits from 2026-10-checktxinputs into master 2026-11-04 16:27:20 +00:00 AGit
KY-U commented 2026-10-28 17:45:15 +00:00 (Migrated from github.com)

In Consensus::CheckTxInputs, the inputs are invalid if, for every input coin, either coin.out.nValue or nValueIn is out of range. However, the current checktxinputs_invalid_transactions_test only covers cases with one input, so the sum nValueIn is always equal to coin.out.nValue, leaving the following mutant alive.

--- a/src/consensus/tx_verify.cpp
+++ b/src/consensus/tx_verify.cpp
@@ -189,7 +189,7 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state,
 
         // Check for negative or overflow input values
         nValueIn += coin.out.nValue;
-        if (!MoneyRange(coin.out.nValue) || !MoneyRange(nValueIn)) {
+        if (!MoneyRange(coin.out.nValue) && !MoneyRange(nValueIn)) {
             return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputvalues-outofrange");
         }
     }

https://github.com/bitcoin/bitcoin/pull/36686/commits/f8c6cd84e08043f206ca338d0ba7e4dc8bef5214 adds support for multiple inputs in the test helper.

https://github.com/bitcoin/bitcoin/pull/36686/commits/133714d1695bc40277b213afd252c3e320cac73e adds a test case with multiple valid inputs, whose sum exceed MAX_MONEY, while also having nValueIn != coin.out.nValue, killing the mutant.

https://github.com/bitcoin/bitcoin/pull/36686/commits/98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1 adds a test for uncovered negative input case.

In `Consensus::CheckTxInputs`, the inputs are invalid if, for every input coin, either `coin.out.nValue` or `nValueIn` is out of range. However, the current `checktxinputs_invalid_transactions_test` only covers cases with one input, so the sum `nValueIn` is always equal to `coin.out.nValue`, leaving the [following mutant](https://mutanthub.space/mutants/275) alive. ```diff --- a/src/consensus/tx_verify.cpp +++ b/src/consensus/tx_verify.cpp @@ -189,7 +189,7 @@ bool Consensus::CheckTxInputs(const CTransaction& tx, TxValidationState& state, // Check for negative or overflow input values nValueIn += coin.out.nValue; - if (!MoneyRange(coin.out.nValue) || !MoneyRange(nValueIn)) { + if (!MoneyRange(coin.out.nValue) && !MoneyRange(nValueIn)) { return state.Invalid(TxValidationResult::TX_CONSENSUS, "bad-txns-inputvalues-outofrange"); } } ``` https://github.com/bitcoin/bitcoin/pull/36686/commits/f8c6cd84e08043f206ca338d0ba7e4dc8bef5214 adds support for multiple inputs in the test helper. https://github.com/bitcoin/bitcoin/pull/36686/commits/133714d1695bc40277b213afd252c3e320cac73e adds a test case with multiple valid inputs, whose sum exceed `MAX_MONEY`, while also having `nValueIn` != `coin.out.nValue`, killing the mutant. https://github.com/bitcoin/bitcoin/pull/36686/commits/98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1 adds a test for uncovered negative input case.
DrahtBot commented 2026-10-28 17:45:26 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK mercie-ux, ViniciusCestarii

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #35511 (RFC: consensus: Make CAmount a class by hodlinator)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36686 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36686.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36686#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [mercie-ux](https://github.com/bitcoin/bitcoin/pull/36686#issuecomment-6571672647), [ViniciusCestarii](https://github.com/bitcoin/bitcoin/pull/36686#pullrequestreview-5754752867) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#35511](https://github.com/bitcoin/bitcoin/pull/35511) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35511.svg"></sub> (RFC: consensus: Make `CAmount` a class by hodlinator) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

No verified findings are available from this review.

Review coverage was incomplete.


Reviewed: 98f9eb6afdfd · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:965af1044d9e41839565702e4d9bd0a751eda81f6e8b6402747fa296ef84ebea:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:6c01bd0b2dc201c21e80ac3635828abf854ee5a0 --> <!-- ralph-head:98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1 --> <!-- ralph-case:9b6483714be5e8d431a071520412e70731f65b5d0d86cd189cbd7c9ac077a1bf --> No verified findings are available from this review. Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`98f9eb6afdfd`](<https://git.fish.foo/bitcoin/bitcoin/commit/98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1>) · [Full review report](<https://review.fish.foo/traces/36686-98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1-1960-4.html>) · [Previous reports](<https://review.fish.foo/traces/36686/>) [permalink](<https://review.fish.foo/traces/36686-98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1-1960-4.html>)
DrahtBot commented 2026-10-28 18:16:11 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/40738712606/job/122994163824
LLM reason (✨ experimental): CI failed the commit_msg lint check because both commits lack a blank line between the subject and body.

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `lint`: https://github.com/bitcoin/bitcoin/actions/runs/40738712606/job/122994163824</sub> <sub>LLM reason (✨ experimental): CI failed the `commit_msg` lint check because both commits lack a blank line between the subject and body.</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
ValeraFinebits (Migrated from github.com) reviewed 2026-10-29 13:30:23 +00:00
ValeraFinebits (Migrated from github.com) commented 2026-10-29 13:30:23 +00:00

Optional: use output_value = 1 here so the || -> && mutant passes the fee check and incorrectly returns true. The current case already catches the mutant via the reject reason, this would catch erroneous acceptance directly.

                  /*output_value=*/1,
Optional: use `output_value = 1` here so the `||` -> `&&` mutant passes the fee check and incorrectly returns `true`. The current case already catches the mutant via the reject reason, this would catch erroneous acceptance directly. ```suggestion /*output_value=*/1, ```
ValeraFinebits (Migrated from github.com) reviewed 2026-10-29 13:35:08 +00:00
ValeraFinebits (Migrated from github.com) commented 2026-10-29 13:35:08 +00:00

Or we could keep the zero-output case and add another with output_value = 1. The existing case also checks that the input value error takes precedence over the fee error.

Or we could keep the zero-output case and add another with `output_value = 1`. The existing case also checks that the input value error takes precedence over the fee error.
KY-U (Migrated from github.com) reviewed 2026-10-29 15:14:53 +00:00
KY-U (Migrated from github.com) commented 2026-10-29 15:14:53 +00:00

Changed the output value to 1 as suggested in the first comment. I'd prefer to change the existing case rather than add another one. That way we keep new test cases to a minimum while exercising the erroneous acceptance and killing the mutant. Thanks for the suggestions!

Changed the output value to 1 as suggested in the first comment. I'd prefer to change the existing case rather than add another one. That way we keep new test cases to a minimum while exercising the erroneous acceptance and killing the mutant. Thanks for the suggestions!
mercie-ux (Migrated from github.com) reviewed 2026-11-01 09:06:21 +00:00
mercie-ux (Migrated from github.com) left a comment

ACK 133714d169

Verified the mutant is killed. I have left an optional suggestion for the per-coin check, which is still uncovered.

ACK 133714d1695bc40277b213afd252c3e320cac73e Verified the mutant is killed. I have left an optional suggestion for the per-coin check, which is still uncovered.
mercie-ux (Migrated from github.com) commented 2026-11-01 09:05:27 +00:00
    check_invalid(/*input_values=*/{2 * COIN, -1 * COIN},
                  /*output_value=*/1,
                  /*coinbase=*/false,
                  /*spend_height=*/2,
                  TxValidationResult::TX_CONSENSUS, /*expected_reason=*/"bad-txns-inputvalues-outofrange");

Could add a case for the per-coin check too, removing it currently doesn't break any test.

```cpp check_invalid(/*input_values=*/{2 * COIN, -1 * COIN}, /*output_value=*/1, /*coinbase=*/false, /*spend_height=*/2, TxValidationResult::TX_CONSENSUS, /*expected_reason=*/"bad-txns-inputvalues-outofrange"); ``` Could add a case for the per-coin check too, removing it currently doesn't break any test.
KY-U (Migrated from github.com) reviewed 2026-11-02 01:54:47 +00:00
KY-U (Migrated from github.com) commented 2026-11-02 01:54:47 +00:00

Thanks for the suggestion! Added.

Thanks for the suggestion! Added.
mercie-ux commented 2026-11-02 07:35:09 +00:00 (Migrated from github.com)

reACK 98f9eb6afd

reACK 98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1
ViniciusCestarii (Migrated from github.com) reviewed 2026-11-03 15:19:18 +00:00
ViniciusCestarii (Migrated from github.com) left a comment

ACK 98f9eb6afd nice!

ACK 98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1 nice!
sedited (Migrated from github.com) approved these changes 2026-11-04 16:03:52 +00:00
sedited (Migrated from github.com) left a comment
ACK 98f9eb6afdfd2bb1cd2872bd7d2eb439ec2043a1
sedited (Migrated from github.com) approved these changes 2026-11-04 16:22:04 +00:00
sedited (Migrated from github.com) left a comment

.

.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36686
No description provided.