From eff9e798b95ef06e0899f348f142703e8504ac5d Mon Sep 17 00:00:00 2001 From: Murch Date: Fri, 20 Mar 2026 15:19:52 -0700 Subject: [PATCH] coinselection: Tiebreak SRD eviction by weight When UTXOs tie in effective value, prefer keeping the lower weight UTXO. Co-authored-by: Yancy --- src/wallet/coinselection.cpp | 2 +- src/wallet/test/coinselection_tests.cpp | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/wallet/coinselection.cpp b/src/wallet/coinselection.cpp index d6ea6851e64..1b569abda2c 100644 --- a/src/wallet/coinselection.cpp +++ b/src/wallet/coinselection.cpp @@ -529,7 +529,7 @@ class MinOutputGroupComparator public: int operator() (const OutputGroup& group1, const OutputGroup& group2) const { - return group1.GetSelectionAmount() > group2.GetSelectionAmount(); + return descending_effval_weight(group1, group2); } }; diff --git a/src/wallet/test/coinselection_tests.cpp b/src/wallet/test/coinselection_tests.cpp index 6e60af1c0e3..68969b6a077 100644 --- a/src/wallet/test/coinselection_tests.cpp +++ b/src/wallet/test/coinselection_tests.cpp @@ -283,6 +283,13 @@ BOOST_AUTO_TEST_CASE(srd_test) AddDuplicateCoins(utxo_pool, /*count=*/3, /*amount=*/7 * CENT, cs_params); TestSRDSuccess("Select most valuable UTXOs for acceptable weight", utxo_pool, /*selection_target=*/20 * CENT, cs_params, /*max_selection_weight=*/4 * 4 * (P2WPKH_INPUT_VSIZE - 1 )); TestSRDFail("No acceptable weight possible", utxo_pool, /*selection_target=*/25 * CENT, cs_params, /*max_selection_weight=*/4 * 3 * P2WPKH_INPUT_VSIZE, /*expect_max_weight_exceeded=*/true); + + // Create UTXO pool with UTXOs of same effective value but different weights + std::vector mixed_weight_pool; + AddDuplicateCoins(mixed_weight_pool, /*count=*/100, /*amount=*/5 * CENT, cs_params); + mixed_weight_pool.push_back(MakeCoin(5 * CENT, true, cs_params, /*custom_spending_vsize=*/P2WPKH_INPUT_VSIZE - 1)); + TestSRDSuccess("Tie-break same effective value with lower weight", utxo_pool, /*selection_target=*/9 * CENT, cs_params, + /*max_selection_weight=*/4 * 3 * (P2WPKH_INPUT_VSIZE - 1)); } }