Skip to content

Commit ae67021

Browse files
committed
Provide a default CoinSelectionSource implementation via a new trait
Certain users may not care how their UTXOs are selected, or their wallet may not expose enough controls to fully implement the `CoinSelectionSource` trait. As an alternative, we introduce another trait `WalletSource` they could opt to implement instead, which is much simpler as it just returns the set of confirmed UTXOs that may be used. This trait implementation is then consumed into a wrapper `Wallet` which implements the `CoinSelectionSource` trait using a "smallest above-dust-after-spend first" coin selection algorithm.
1 parent 8acdb79 commit ae67021

File tree

1 file changed

+140
-1
lines changed

1 file changed

+140
-1
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 140 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::ln::chan_utils::{
2424
};
2525
use crate::events::Event;
2626
use crate::prelude::HashMap;
27+
use crate::sync::Mutex;
2728
use crate::util::logger::Logger;
2829

2930
use bitcoin::{OutPoint, PackedLockTime, Sequence, Script, Transaction, Txid, TxIn, TxOut, Witness};
@@ -306,7 +307,8 @@ pub struct CoinSelection {
306307

307308
/// An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
308309
/// sign for them. The coin selection method aims to mimic Bitcoin Core's `fundrawtransaction` RPC,
309-
/// which most wallets should be able to satisfy.
310+
/// which most wallets should be able to satisfy. Otherwise, consider implementing [`WalletSource`],
311+
/// which can provide a default implementation of this trait when used with [`Wallet`].
310312
pub trait CoinSelectionSource {
311313
/// Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
312314
/// available to spend. Implementations are free to pick their coin selection algorithm of
@@ -347,6 +349,143 @@ pub trait CoinSelectionSource {
347349
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
348350
}
349351

352+
/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
353+
/// provide a default implementation to [`CoinSelectionSource`].
354+
pub trait WalletSource {
355+
/// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
356+
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()>;
357+
/// Returns a script to use for change above dust resulting from a successful coin selection
358+
/// attempt.
359+
fn get_change_script(&self) -> Result<Script, ()>;
360+
/// Signs and provides the full witness for all inputs within the transaction known to the
361+
/// wallet (i.e., any provided via [`WalletSource::list_confirmed_utxos`]).
362+
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()>;
363+
}
364+
365+
/// A wrapper over [`WalletSource`] that implements [`CoinSelection`] by preferring UTXOs that would
366+
/// avoid conflicting double spends. If not enough UTXOs are available to do so, conflicting double
367+
/// spends may happen.
368+
pub struct Wallet<W: Deref> where W::Target: WalletSource {
369+
source: W,
370+
// TODO: Do we care about cleaning this up once the UTXOs have a confirmed spend? We can do so
371+
// by checking whether any UTXOs that exist in the map are no longer returned in
372+
// `list_confirmed_utxos`.
373+
locked_utxos: Mutex<HashMap<OutPoint, ClaimId>>,
374+
}
375+
376+
impl<W: Deref> Wallet<W> where W::Target: WalletSource {
377+
/// Returns a new instance backed by the given [`WalletSource`] that serves as an implementation
378+
/// of [`CoinSelectionSource`].
379+
pub fn new(source: W) -> Self {
380+
Self { source, locked_utxos: Mutex::new(HashMap::new()) }
381+
}
382+
383+
/// Performs coin selection on the set of UTXOs obtained from
384+
/// [`WalletSource::list_confirmed_utxos`]. Its algorithm can be described as "smallest
385+
/// above-dust-after-spend first", with a slight twist: we may skip UTXOs that are above dust at
386+
/// the target feerate after having spent them in a separate claim transaction if
387+
/// `force_conflicting_utxo_spend` is unset to avoid producing conflicting transactions.
388+
fn select_confirmed_utxos_internal(
389+
&self, claim_id: ClaimId, force_conflicting_utxo_spend: bool,
390+
target_feerate_sat_per_1000_weight: u32, preexisting_tx_weight: u64, target_amount: u64,
391+
) -> Result<CoinSelection, ()> {
392+
let mut utxos = self.source.list_confirmed_utxos()?.into_iter().filter_map(|utxo| {
393+
let fee_to_spend_utxo = target_feerate_sat_per_1000_weight as u64 *
394+
((41 * WITNESS_SCALE_FACTOR) as u64 + utxo.witness_weight);
395+
let utxo_value_after_fee = utxo.output.value.saturating_sub(fee_to_spend_utxo);
396+
if utxo_value_after_fee > 0 {
397+
Some((utxo, fee_to_spend_utxo))
398+
} else {
399+
None
400+
}
401+
}).collect::<Vec<_>>();
402+
utxos.sort_unstable_by_key(|(utxo, _)| utxo.output.value);
403+
404+
let mut selected_amount = 0;
405+
let mut total_fees = preexisting_tx_weight * target_feerate_sat_per_1000_weight as u64;
406+
let selected_utxos = {
407+
let mut locked_utxos = self.locked_utxos.lock().unwrap();
408+
let selected_utxos = utxos.into_iter().scan(
409+
(&mut selected_amount, &mut total_fees), |(selected_amount, total_fees), (utxo, fee_to_spend_utxo)| {
410+
if let Some(utxo_claim_id) = locked_utxos.get(&utxo.outpoint) {
411+
if *utxo_claim_id != claim_id && !force_conflicting_utxo_spend {
412+
return None;
413+
}
414+
}
415+
let need_more_inputs = **selected_amount < target_amount + **total_fees;
416+
if need_more_inputs {
417+
**selected_amount += utxo.output.value;
418+
**total_fees += fee_to_spend_utxo;
419+
Some(utxo)
420+
} else {
421+
None
422+
}
423+
}
424+
).collect::<Vec<_>>();
425+
let need_more_inputs = selected_amount < target_amount + total_fees;
426+
if need_more_inputs {
427+
return Err(());
428+
}
429+
for utxo in &selected_utxos {
430+
locked_utxos.insert(utxo.outpoint, claim_id);
431+
}
432+
selected_utxos
433+
};
434+
435+
let remaining_amount = selected_amount - target_amount - total_fees;
436+
let change_script = self.source.get_change_script()?;
437+
let change_output_fee = target_feerate_sat_per_1000_weight as u64
438+
* (8 + change_script.consensus_encode(&mut sink()).unwrap() as u64);
439+
let change_output_amount = remaining_amount.saturating_sub(change_output_fee);
440+
let change_output = if change_output_amount < change_script.dust_value().to_sat() {
441+
None
442+
} else {
443+
Some(TxOut { script_pubkey: change_script, value: change_output_amount })
444+
};
445+
446+
Ok(CoinSelection {
447+
confirmed_utxos: selected_utxos,
448+
change_output,
449+
})
450+
}
451+
}
452+
453+
impl<W: Deref> CoinSelectionSource for Wallet<W> where W::Target: WalletSource {
454+
fn select_confirmed_utxos(
455+
&self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: Vec<TxOut>,
456+
target_feerate_sat_per_1000_weight: u32,
457+
) -> Result<CoinSelection, ()> {
458+
// TODO: Use fee estimation utils when we upgrade to bitcoin v0.30.0.
459+
let base_tx_weight = 4 /* version */ + 1 /* input count */ + 1 /* output count */ + 4 /* locktime */;
460+
let total_input_weight = must_spend.len() *
461+
(32 /* txid */ + 4 /* vout */ + 4 /* sequence */ + 1 /* script sig */);
462+
let total_output_weight: usize = must_pay_to.iter().map(|output|
463+
8 /* value */ + 1 /* script len */ + output.script_pubkey.len()
464+
).sum();
465+
let total_non_witness_weight = base_tx_weight + total_input_weight + total_output_weight;
466+
let total_witness_weight: u64 = must_spend.iter().map(|input| input.witness_weight).sum();
467+
468+
let preexisting_tx_weight = 2 /* segwit marker & flag */ + total_witness_weight +
469+
(total_non_witness_weight * WITNESS_SCALE_FACTOR) as u64;
470+
let target_amount = must_pay_to.iter().map(|output| output.value).sum();
471+
let do_coin_selection = |force_conflicting_utxo_spend: bool| {
472+
self.select_confirmed_utxos_internal(
473+
claim_id, force_conflicting_utxo_spend, target_feerate_sat_per_1000_weight,
474+
preexisting_tx_weight, target_amount,
475+
)
476+
};
477+
do_coin_selection(false).or_else(|_| do_coin_selection(true))
478+
}
479+
480+
fn get_change_script(&self) -> Result<Script, ()> {
481+
self.source.get_change_script()
482+
}
483+
484+
fn sign_tx(&self, tx: &mut Transaction) -> Result<(), ()> {
485+
self.source.sign_tx(tx)
486+
}
487+
}
488+
350489
/// A handler for [`Event::BumpTransaction`] events that sources confirmed UTXOs from a
351490
/// [`CoinSelectionSource`] to fee bump transactions via Child-Pays-For-Parent (CPFP) or
352491
/// Replace-By-Fee (RBF).

0 commit comments

Comments
 (0)