diff --git a/CHANGELOG.md b/CHANGELOG.md index 008576c8bd..fb12fbbbec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,10 @@ The format is loosely based on [Keep a Changelog](https://keepachangelog.com/en/ send is rejected for forming an oversized mempool cluster; the full UTXO set is still used when the confirmed UTXOs cannot cover the payment. + - 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 enabled by default during the initial block download or bootstrapping, which diff --git a/wallet/src/account/mod.rs b/wallet/src/account/mod.rs index 7fd4931ddb..a75fa9716d 100644 --- a/wallet/src/account/mod.rs +++ b/wallet/src/account/mod.rs @@ -603,6 +603,11 @@ 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 `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 670109ee5f..77d1350fb1 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,15 @@ 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. + // Keep only the groups that are worth more than the fee of spending them; note this + // 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); + } 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 3abad3be14..13b0042403 100644 --- a/wallet/src/account/utxo_selector/tests.rs +++ b/wallet/src/account/utxo_selector/tests.rs @@ -59,6 +59,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())] @@ -761,3 +766,85 @@ 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. +#[rstest] +#[trace] +#[case(Seed::from_entropy())] +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); + // 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)); + } +}