From 57fe8acc8a8471ec41ac5f3c3764458431880e4e Mon Sep 17 00:00:00 2001 From: Murch Date: Thu, 24 Jul 2025 13:57:53 -0700 Subject: [PATCH] test: Check max_weight_exceeded error This slays the mutants 14 and 39 Bruno reported via https://gist.github.com/brunoerg/834063398d5002f738506d741513e310, that changing the intial or subsequent value of `max_tx_weight_exceeded` in BnB would not fail any tests: diff --git a/src/wallet/coinselection.cpp b/muts/coinselection.mutant.14.cpp index cee558088f..947bf7b642 100644 --- a/src/wallet/coinselection.cpp +++ b/muts/coinselection.mutant.14.cpp @@ -118,7 +118,7 @@ util::Result SelectCoinsBnB(std::vector& utxo_pool CAmount best_waste = MAX_MONEY; bool is_feerate_high = utxo_pool.at(0).fee > utxo_pool.at(0).long_term_fee; - bool max_tx_weight_exceeded = false; + bool max_tx_weight_exceeded = true; // Depth First search loop for choosing the UTXOs for (size_t curr_try = 0, utxo_pool_index = 0; curr_try < TOTAL_TRIES; ++curr_try, ++utxo_pool_index) { diff --git a/src/wallet/coinselection.cpp b/muts/coinselection.mutant.39.cpp index cee558088f..bbfdc23889 100644 --- a/src/wallet/coinselection.cpp +++ b/muts/coinselection.mutant.39.cpp @@ -129,7 +129,7 @@ util::Result SelectCoinsBnB(std::vector& utxo_pool (curr_waste > best_waste && is_feerate_high)) { // Don't select things which we know will be more wasteful if the waste is increasing backtrack = true; } else if (curr_selection_weight > max_selection_weight) { // Selected UTXOs weight exceeds the maximum weight allowed, cannot find more solutions by adding more inputs - max_tx_weight_exceeded = true; // at least one selection attempt exceeded the max weight + max_tx_weight_exceeded = false; // at least one selection attempt exceeded the max weight backtrack = true; } else if (curr_value >= selection_target) { // Selected value is within range curr_waste += (curr_value - selection_target); // This is the excess value which is added to the waste for the below comparison --- src/wallet/test/coinselection_tests.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wallet/test/coinselection_tests.cpp b/src/wallet/test/coinselection_tests.cpp index 4e4b4b67aa1..99283b719a7 100644 --- a/src/wallet/test/coinselection_tests.cpp +++ b/src/wallet/test/coinselection_tests.cpp @@ -109,9 +109,12 @@ static void TestBnBSuccess(std::string test_title, std::vector& utx BOOST_CHECK_MESSAGE(result->GetSelectedValue() == expected_amount, strprintf("Selected amount mismatch in BnB-Success: %s. Expected %d, but got %d", test_title, expected_amount, result->GetSelectedValue())); } -static void TestBnBFail(std::string test_title, std::vector& utxo_pool, const CAmount& selection_target) +static void TestBnBFail(std::string test_title, std::vector& utxo_pool, const CAmount& selection_target, const bool expect_max_weight_exceeded = false) { - BOOST_CHECK_MESSAGE(!SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/default_cs_params.m_cost_of_change, /*max_selection_weight=*/MAX_STANDARD_TX_WEIGHT), "BnB-Fail: " + test_title); + const auto result = SelectCoinsBnB(utxo_pool, selection_target, /*cost_of_change=*/default_cs_params.m_cost_of_change, /*max_selection_weight=*/MAX_STANDARD_TX_WEIGHT); + BOOST_CHECK_MESSAGE(!result, "BnB-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(bnb_test)