test: Rework SRD insufficient balance test
What changed, and why it matters
This commit only changes test code for Bitcoin Core's coin selection logic. It moves and expands tests that check what happens when a wallet doesn't have enough funds to make a transaction. No production wallet code is changed, so this cannot directly affect real users' funds or transaction behavior.
No security action required. Review as ordinary test-quality/code-coverage change.
Security signals we found
No production code modified
Test-only refactor
No input validation or cryptographic changes
No bug fix or vulnerability remediation visible in diff
Evidence from the diff
The commit refactors unit tests for the Single Random Draw (SRD) coin selection algorithm. It removes an older insufficient-balance test from coinselector_tests.cpp and adds a new helper TestSRDFail plus several failure variants (empty pool, undershoot minimum change, spend more than available, spend everything) in coinselection_tests.cpp. The production SelectCoinsSRD implementation is untouched.
Changed components
src/wallet/test/coinselection_tests.cppsrc/wallet/test/coinselector_tests.cppInspect captured patch +25 / −18
diff --git a/src/wallet/test/coinselection_tests.cpp b/src/wallet/test/coinselection_tests.cpp
index 347e00f7..fb3b0b19 100644
--- a/src/wallet/test/coinselection_tests.cpp
+++ b/src/wallet/test/coinselection_tests.cpp
@@ -236,5 +236,30 @@ BOOST_AUTO_TEST_CASE(bnb_feerate_sensitivity_test)
TestBnBSuccess("Prefer two light inputs over two heavy inputs at high feerates", high_feerate_pool, /*selection_target=*/13 * CENT, /*expected_input_amounts=*/{3 * CENT, 10 * CENT}, high_feerate_params);
}
+static void TestSRDFail(std::string test_title, std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target, const CoinSelectionParams& cs_params = default_cs_params, int max_selection_weight = MAX_STANDARD_TX_WEIGHT, const bool expect_max_weight_exceeded = false)
+{
+ const auto result = SelectCoinsSRD(utxo_pool, selection_target, cs_params.m_change_fee, cs_params.rng_fast, max_selection_weight);
+ BOOST_CHECK_MESSAGE(!result, "SRD-Fail: " + test_title);
+ bool max_weight_exceeded = util::ErrorString(result).original.find("The inputs size exceeds the maximum weight") != std::string::npos;
+ BOOST_CHECK(expect_max_weight_exceeded == max_weight_exceeded);
+}
+
+BOOST_AUTO_TEST_CASE(srd_test)
+{
+ for (int feerate : FEERATES) {
+ std::vector<OutputGroup> utxo_pool;
+
+ const CoinSelectionParams cs_params = init_cs_params(feerate);
+
+ TestSRDFail("Empty UTXO pool", utxo_pool, /*selection_target=*/1 * CENT, cs_params);
+
+ AddCoins(utxo_pool, {1 * CENT, 3 * CENT, 5 * CENT}, cs_params);
+
+ TestSRDFail("Undershoot minimum change by one sat", utxo_pool, /*selection_target=*/9 * CENT - cs_params.m_change_fee - CHANGE_LOWER + 1, cs_params);
+ TestSRDFail("Spend more than available", utxo_pool, /*selection_target=*/9 * CENT + 1, cs_params);
+ TestSRDFail("Spend everything", utxo_pool, /*selection_target=*/9 * CENT, cs_params);
+ }
+}
+
BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet
diff --git a/src/wallet/test/coinselector_tests.cpp b/src/wallet/test/coinselector_tests.cpp
index fe33e934..740079d2 100644
--- a/src/wallet/test/coinselector_tests.cpp
+++ b/src/wallet/test/coinselector_tests.cpp
@@ -1217,24 +1217,6 @@ BOOST_AUTO_TEST_CASE(srd_tests)
/*avoid_partial=*/false,
};
- {
- // #########################################################
- // 1) Insufficient funds, select all provided coins and fail
- // #########################################################
- CAmount target = 49.5L * COIN;
- int max_selection_weight = 10000; // high enough to not fail for this reason.
- const auto& res = SelectCoinsSRD(target, dummy_params, m_node, max_selection_weight, [&](CWallet& wallet) {
- CoinsResult available_coins;
- for (int j = 0; j < 10; ++j) {
- add_coin(available_coins, wallet, CAmount(1 * COIN));
- add_coin(available_coins, wallet, CAmount(2 * COIN));
- }
- return available_coins;
- });
- BOOST_CHECK(!res);
- BOOST_CHECK(util::ErrorString(res).empty()); // empty means "insufficient funds"
- }
-
{
// ###########################
// 2) Test max weight exceeded
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.