From 00b7c0386893cc0ce184a1fbf6269aa56683054c Mon Sep 17 00:00:00 2001 From: Oliver Anyanwu <113263811+oliv3rdrt@users.noreply.github.com> Date: Wed, 18 Nov 2026 12:04:10 +0100 Subject: [PATCH 1/3] fix(wallet): skip inputs that don't cover their fee in coin selection get_effective_value expects an input's fee to be less than its value and panics otherwise, but select_coins relied on its caller to filter such inputs out. When fees are paid with the inputs' currency, select_coins now leaves them out itself before running any algorithm. --- CHANGELOG.md | 3 + wallet/src/account/utxo_selector/mod.rs | 8 ++- wallet/src/account/utxo_selector/tests.rs | 84 +++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 374a532a19..48354f7014 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,9 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/ as the target is met, and among equally economical solutions the one spending fewer inputs is preferred, so default sends spend fewer UTXOs and produce smaller change. + - coin selection itself now leaves out inputs that aren't worth more than the fee of + spending them, instead of relying on its caller to filter them out first, so such + an input can no longer make it panic. - Node: - `node-daemon` and `node-gui`: the "reckless" mode of the chainstate database is now diff --git a/wallet/src/account/utxo_selector/mod.rs b/wallet/src/account/utxo_selector/mod.rs index 3def738284..f5793d81ce 100644 --- a/wallet/src/account/utxo_selector/mod.rs +++ b/wallet/src/account/utxo_selector/mod.rs @@ -709,7 +709,7 @@ pub enum CoinSelectionAlgo { } pub fn select_coins( - utxo_pool: Vec, + mut utxo_pool: Vec, selection_target: Amount, pay_fees: PayFee, cost_of_change: Amount, @@ -719,6 +719,12 @@ pub fn select_coins( if selection_target == Amount::ZERO { return Ok(SelectionResult::new(selection_target)); } + + // When the fees are paid with this currency, an input that isn't worth more than the fee of + // spending it can't help reach the target, and its effective value would underflow. + if pay_fees == PayFee::PayFeeWithThisCurrency { + utxo_pool.retain(|group| group.value > group.fee); + } ensure!(!utxo_pool.is_empty(), UtxoSelectorError::NoUtxos); let total_available_value = utxo_pool diff --git a/wallet/src/account/utxo_selector/tests.rs b/wallet/src/account/utxo_selector/tests.rs index ed0b9bcd0a..2b5ec32740 100644 --- a/wallet/src/account/utxo_selector/tests.rs +++ b/wallet/src/account/utxo_selector/tests.rs @@ -41,6 +41,11 @@ fn add_output(value: Amount, groups: &mut Vec) { }); } +fn add_output_with_fee(value: Amount, fee: Amount, groups: &mut Vec) { + add_output(value, groups); + groups.last_mut().expect("the group was just added").fee = fee; +} + #[rstest] #[trace] #[case(Seed::from_entropy())] @@ -631,3 +636,82 @@ fn test_select_random_coins_prefers_fewer_inputs_on_equal_waste(#[case] _seed: S ); } } + +/// When the fees are paid with this currency, an input that isn't worth more than the fee of +/// spending it has no positive effective value, and computing it would underflow. Coin selection +/// must leave such inputs out instead of panicking, whichever algorithm is used. +#[test] +fn test_select_coins_skips_inputs_not_covering_their_fee() { + let mut groups = vec![]; + add_output(Amount::from_atoms(100), &mut groups); + add_output(Amount::from_atoms(60), &mut groups); + // One input worth less than its fee and one worth exactly its fee. + add_output_with_fee(Amount::from_atoms(5), Amount::from_atoms(10), &mut groups); + add_output_with_fee(Amount::from_atoms(10), Amount::from_atoms(10), &mut groups); + + let target_value = Amount::from_atoms(100); + let select = |algo| { + select_coins( + groups.clone(), + target_value, + PayFee::PayFeeWithThisCurrency, + Amount::ZERO, + algo, + 100, + ) + }; + + // Spending the preselected inputs leaves out the two that don't cover their fee. + let result = select(CoinSelectionAlgo::UsePreselected).unwrap(); + assert_eq!(result.num_selected_inputs(), 2); + assert_eq!(result.effective_value, Amount::from_atoms(160)); + + // The random selection doesn't pick them either. They are the only inputs with a fee, so + // a zero total fee shows that neither was selected. + let result = select(CoinSelectionAlgo::Randomize).unwrap(); + assert!(result.effective_value >= target_value); + assert_eq!(result.get_total_fees(), Amount::ZERO); +} + +/// If no input covers the fee of spending it, there is nothing to select from, which is reported +/// as an error rather than a panic. +#[test] +fn test_select_coins_no_inputs_covering_their_fee() { + let mut groups = vec![]; + add_output_with_fee(Amount::from_atoms(5), Amount::from_atoms(10), &mut groups); + add_output_with_fee(Amount::from_atoms(10), Amount::from_atoms(10), &mut groups); + + for algo in [CoinSelectionAlgo::UsePreselected, CoinSelectionAlgo::Randomize] { + let result = select_coins( + groups.clone(), + Amount::from_atoms(1), + PayFee::PayFeeWithThisCurrency, + Amount::ZERO, + algo, + 100, + ); + assert_eq!(result.unwrap_err(), UtxoSelectorError::NoUtxos); + } +} + +/// When the fees are paid with another currency (e.g. for token inputs), an input's fee doesn't +/// reduce its value, so an input worth less than its fee is still selected. +#[test] +fn test_select_coins_keeps_inputs_when_fee_paid_with_other_currency() { + let mut groups = vec![]; + add_output_with_fee(Amount::from_atoms(5), Amount::from_atoms(10), &mut groups); + + for algo in [CoinSelectionAlgo::UsePreselected, CoinSelectionAlgo::Randomize] { + let result = select_coins( + groups.clone(), + Amount::from_atoms(5), + PayFee::DoNotPayFeeWithThisCurrency, + Amount::ZERO, + algo, + 100, + ) + .unwrap(); + assert_eq!(result.num_selected_inputs(), 1); + assert_eq!(result.effective_value, Amount::from_atoms(5)); + } +} From 92b1dedad8617cb13f99a5f87ea3552c7c74d492 Mon Sep 17 00:00:00 2001 From: nullPointerEnjoyer Date: Wed, 18 Nov 2026 22:23:56 +0100 Subject: [PATCH 2/3] fix(wallet): document fee-coverage filter ownership, seed its test - cross-reference comments on both filter sites (select_coins guard is authoritative; the caller keeps a copy only for the conservative input-count estimate) - note the pre-existing silent omission of preselected dust inputs - seed the fee-coverage test per the file convention (assertions are shuffle-independent) --- wallet/src/account/mod.rs | 4 ++++ wallet/src/account/utxo_selector/mod.rs | 3 +++ wallet/src/account/utxo_selector/tests.rs | 7 +++++-- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/wallet/src/account/mod.rs b/wallet/src/account/mod.rs index 7d5735fcfb..5731cc52ba 100644 --- a/wallet/src/account/mod.rs +++ b/wallet/src/account/mod.rs @@ -562,6 +562,10 @@ impl Account { .map(utxo_to_output_group) .filter(|group| { group.as_ref().map_or(true, |group| { + // Same predicate as the guard in `select_coins`; kept here + // only so the input-count estimate below stays conservative. + // Note: preselected inputs dropped by this rule are omitted + // from the transaction silently (pre-existing behavior). currency != *pay_fee_with_currency || group.value > group.fee }) }) diff --git a/wallet/src/account/utxo_selector/mod.rs b/wallet/src/account/utxo_selector/mod.rs index f5793d81ce..552849af71 100644 --- a/wallet/src/account/utxo_selector/mod.rs +++ b/wallet/src/account/utxo_selector/mod.rs @@ -722,6 +722,9 @@ pub fn select_coins( // When the fees are paid with this currency, an input that isn't worth more than the fee of // spending it can't help reach the target, and its effective value would underflow. + // Keep only the groups that are worth more than the fee of spending them; note this + // is the authoritative filter — the caller in `account::create_transaction` applies + // the same predicate only to keep its input-count estimate conservative. if pay_fees == PayFee::PayFeeWithThisCurrency { utxo_pool.retain(|group| group.value > group.fee); } diff --git a/wallet/src/account/utxo_selector/tests.rs b/wallet/src/account/utxo_selector/tests.rs index 2b5ec32740..e29ed665a6 100644 --- a/wallet/src/account/utxo_selector/tests.rs +++ b/wallet/src/account/utxo_selector/tests.rs @@ -640,8 +640,11 @@ fn test_select_random_coins_prefers_fewer_inputs_on_equal_waste(#[case] _seed: S /// When the fees are paid with this currency, an input that isn't worth more than the fee of /// spending it has no positive effective value, and computing it would underflow. Coin selection /// must leave such inputs out instead of panicking, whichever algorithm is used. -#[test] -fn test_select_coins_skips_inputs_not_covering_their_fee() { +#[rstest] +#[trace] +#[case(Seed::from_entropy())] +fn test_select_coins_skips_inputs_not_covering_their_fee(#[case] seed: Seed) { + let _ = seed; // assertions are shuffle-independent; seeding matches file convention let mut groups = vec![]; add_output(Amount::from_atoms(100), &mut groups); add_output(Amount::from_atoms(60), &mut groups); From 234dccaa67e29295af01f3f84e88ac220efed8d2 Mon Sep 17 00:00:00 2001 From: nullPointerEnjoyer Date: Wed, 18 Nov 2026 22:30:23 +0100 Subject: [PATCH 3/3] fix(wallet): correct the filter cross-references and seed style --- wallet/src/account/mod.rs | 3 ++- wallet/src/account/utxo_selector/mod.rs | 2 +- wallet/src/account/utxo_selector/tests.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/wallet/src/account/mod.rs b/wallet/src/account/mod.rs index 5731cc52ba..790a33664e 100644 --- a/wallet/src/account/mod.rs +++ b/wallet/src/account/mod.rs @@ -563,7 +563,8 @@ impl Account { .filter(|group| { group.as_ref().map_or(true, |group| { // Same predicate as the guard in `select_coins`; kept here - // only so the input-count estimate below stays conservative. + // only so the `max_possible_num_inputs` estimate stays + // conservative. // Note: preselected inputs dropped by this rule are omitted // from the transaction silently (pre-existing behavior). currency != *pay_fee_with_currency || group.value > group.fee diff --git a/wallet/src/account/utxo_selector/mod.rs b/wallet/src/account/utxo_selector/mod.rs index 552849af71..a4c1749311 100644 --- a/wallet/src/account/utxo_selector/mod.rs +++ b/wallet/src/account/utxo_selector/mod.rs @@ -723,7 +723,7 @@ pub fn select_coins( // When the fees are paid with this currency, an input that isn't worth more than the fee of // spending it can't help reach the target, and its effective value would underflow. // Keep only the groups that are worth more than the fee of spending them; note this - // is the authoritative filter — the caller in `account::create_transaction` applies + // is the authoritative filter — `utxo_output_groups_by_currency` in `account` applies // the same predicate only to keep its input-count estimate conservative. if pay_fees == PayFee::PayFeeWithThisCurrency { utxo_pool.retain(|group| group.value > group.fee); diff --git a/wallet/src/account/utxo_selector/tests.rs b/wallet/src/account/utxo_selector/tests.rs index e29ed665a6..a9a4fa2014 100644 --- a/wallet/src/account/utxo_selector/tests.rs +++ b/wallet/src/account/utxo_selector/tests.rs @@ -643,8 +643,8 @@ fn test_select_random_coins_prefers_fewer_inputs_on_equal_waste(#[case] _seed: S #[rstest] #[trace] #[case(Seed::from_entropy())] -fn test_select_coins_skips_inputs_not_covering_their_fee(#[case] seed: Seed) { - let _ = seed; // assertions are shuffle-independent; seeding matches file convention +fn test_select_coins_skips_inputs_not_covering_their_fee(#[case] _seed: Seed) { + // Assertions are shuffle-independent; seeding matches the file convention. let mut groups = vec![]; add_output(Amount::from_atoms(100), &mut groups); add_output(Amount::from_atoms(60), &mut groups);