From aa87aae14f9eee79e3a0fb9c0f5ff3eaa97433e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Sat, 31 Jan 2026 21:02:19 +0100 Subject: [PATCH] consensus/test: add `MoneyRange` unit tests for `CheckTxInputs` Add minimal unit tests exercising `Consensus::CheckTxInputs` reject reasons for coinbase maturity (`bad-txns-premature-spend-of-coinbase`), input value range failures (`bad-txns-inputvalues-outofrange`), and for `nValueIn < value_out` (`bad-txns-in-belowout`). Inspired by b-c-cov coverage reports: * "bad-txns-premature-spend-of-coinbase" - https://maflcko.github.io/b-c-cov/test_bitcoin.coverage/src/consensus/tx_verify.cpp.gcov.html#L180 * "bad-txns-inputvalues-outofrange" - https://maflcko.github.io/b-c-cov/test_bitcoin.coverage/src/consensus/tx_verify.cpp.gcov.html#L187 * "bad-txns-in-belowout" - https://maflcko.github.io/b-c-cov/test_bitcoin.coverage/src/consensus/tx_verify.cpp.gcov.html#L193 --- src/test/transaction_tests.cpp | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/test/transaction_tests.cpp b/src/test/transaction_tests.cpp index 3919f226d69..0f175c7d6f3 100644 --- a/src/test/transaction_tests.cpp +++ b/src/test/transaction_tests.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -1114,6 +1115,46 @@ BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops) BOOST_CHECK(!::AreInputsStandard(CTransaction(tx_max_sigops), coins)); } +BOOST_AUTO_TEST_CASE(checktxinputs_invalid_transactions_test) +{ + auto check_invalid{[](CAmount input_value, CAmount output_value, bool coinbase, int spend_height, TxValidationResult expected_result, std::string_view expected_reason) { + CCoinsView coins_dummy; + CCoinsViewCache inputs(&coins_dummy); + + const COutPoint prevout{Txid::FromUint256(uint256::ONE), 0}; + inputs.AddCoin(prevout, Coin{{input_value, CScript() << OP_TRUE}, /*nHeightIn=*/1, coinbase}, /*possible_overwrite=*/false); + + CMutableTransaction mtx; + mtx.vin.emplace_back(prevout); + mtx.vout.emplace_back(output_value, CScript() << OP_TRUE); + + TxValidationState state; + CAmount txfee{0}; + BOOST_CHECK(!Consensus::CheckTxInputs(CTransaction{mtx}, state, inputs, spend_height, txfee)); + BOOST_CHECK(state.IsInvalid()); + BOOST_CHECK_EQUAL(state.GetResult(), expected_result); + BOOST_CHECK_EQUAL(state.GetRejectReason(), expected_reason); + }}; + + check_invalid(/*input_value=*/MAX_MONEY + 1, + /*output_value=*/0, + /*coinbase=*/false, + /*spend_height=*/2, + TxValidationResult::TX_CONSENSUS, /*expected_reason=*/"bad-txns-inputvalues-outofrange"); + + check_invalid(/*input_value=*/1 * COIN, + /*output_value=*/2 * COIN, + /*coinbase=*/false, + /*spend_height=*/2, + TxValidationResult::TX_CONSENSUS, /*expected_reason=*/"bad-txns-in-belowout"); + + check_invalid(/*input_value=*/1 * COIN, + /*output_value=*/0, + /*coinbase=*/true, + /*spend_height=*/COINBASE_MATURITY, + TxValidationResult::TX_PREMATURE_SPEND, /*expected_reason=*/"bad-txns-premature-spend-of-coinbase"); +} + /** Sanity check the return value of SpendsNonAnchorWitnessProg for various output types. */ BOOST_AUTO_TEST_CASE(spends_witness_prog) {