lightning/events/bump_transaction/
mod.rs

1// This file is Copyright its original authors, visible in version control
2// history.
3//
4// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7// You may not use this file except in accordance with one or both of these
8// licenses.
9
10//! Utilities for bumping transactions originating from [`Event`]s.
11//!
12//! [`Event`]: crate::events::Event
13
14pub mod sync;
15
16use alloc::collections::BTreeMap;
17use core::ops::Deref;
18
19use crate::chain::chaininterface::{
20	compute_feerate_sat_per_1000_weight, fee_for_weight, BroadcasterInterface,
21};
22use crate::chain::ClaimId;
23use crate::io_extras::sink;
24use crate::ln::chan_utils;
25use crate::ln::chan_utils::{
26	shared_anchor_script_pubkey, HTLCOutputInCommitment, ANCHOR_INPUT_WITNESS_WEIGHT,
27	BASE_INPUT_WEIGHT, BASE_TX_SIZE, EMPTY_SCRIPT_SIG_WEIGHT, EMPTY_WITNESS_WEIGHT,
28	HTLC_SUCCESS_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_P2A_ANCHOR_WITNESS_WEIGHT,
29	HTLC_TIMEOUT_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT, HTLC_TIMEOUT_INPUT_P2A_ANCHOR_WITNESS_WEIGHT,
30	P2WSH_TXOUT_WEIGHT, SEGWIT_MARKER_FLAG_WEIGHT, TRUC_CHILD_MAX_WEIGHT, TRUC_MAX_WEIGHT,
31};
32use crate::ln::types::ChannelId;
33use crate::prelude::*;
34use crate::sign::ecdsa::EcdsaChannelSigner;
35use crate::sign::{
36	ChannelDerivationParameters, HTLCDescriptor, SignerProvider, P2WPKH_WITNESS_WEIGHT,
37};
38use crate::sync::Mutex;
39use crate::util::async_poll::{AsyncResult, MaybeSend, MaybeSync};
40use crate::util::logger::Logger;
41
42use bitcoin::amount::Amount;
43use bitcoin::consensus::Encodable;
44use bitcoin::constants::WITNESS_SCALE_FACTOR;
45use bitcoin::locktime::absolute::LockTime;
46use bitcoin::policy::MAX_STANDARD_TX_WEIGHT;
47use bitcoin::secp256k1;
48use bitcoin::secp256k1::ecdsa::Signature;
49use bitcoin::secp256k1::{PublicKey, Secp256k1};
50use bitcoin::transaction::Version;
51use bitcoin::{
52	OutPoint, Psbt, PubkeyHash, ScriptBuf, Sequence, Transaction, TxIn, TxOut, WPubkeyHash, Witness,
53};
54
55/// A descriptor used to sign for a commitment transaction's anchor output.
56#[derive(Clone, Debug, PartialEq, Eq)]
57pub struct AnchorDescriptor {
58	/// The parameters required to derive the signer for the anchor input.
59	pub channel_derivation_parameters: ChannelDerivationParameters,
60	/// The transaction input's outpoint corresponding to the commitment transaction's anchor
61	/// output.
62	pub outpoint: OutPoint,
63	/// Zero-fee-commitment anchors have variable value, which is tracked here.
64	pub value: Amount,
65}
66
67impl AnchorDescriptor {
68	/// Returns the UTXO to be spent by the anchor input, which can be obtained via
69	/// [`Self::unsigned_tx_input`].
70	pub fn previous_utxo(&self) -> TxOut {
71		let tx_params = &self.channel_derivation_parameters.transaction_parameters;
72		let script_pubkey = if tx_params.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
73			let channel_params = tx_params.as_holder_broadcastable();
74			chan_utils::get_keyed_anchor_redeemscript(
75				&channel_params.broadcaster_pubkeys().funding_pubkey,
76			)
77		} else {
78			assert!(tx_params.channel_type_features.supports_anchor_zero_fee_commitments());
79			shared_anchor_script_pubkey()
80		};
81		TxOut { script_pubkey, value: self.value }
82	}
83
84	/// Returns the unsigned transaction input spending the anchor output in the commitment
85	/// transaction.
86	pub fn unsigned_tx_input(&self) -> TxIn {
87		TxIn {
88			previous_output: self.outpoint.clone(),
89			script_sig: ScriptBuf::new(),
90			sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
91			witness: Witness::new(),
92		}
93	}
94
95	/// Returns the fully signed witness required to spend the anchor output in the commitment
96	/// transaction.
97	pub fn tx_input_witness(&self, signature: &Signature) -> Witness {
98		let tx_params = &self.channel_derivation_parameters.transaction_parameters;
99		if tx_params.channel_type_features.supports_anchors_zero_fee_htlc_tx() {
100			let channel_params =
101				self.channel_derivation_parameters.transaction_parameters.as_holder_broadcastable();
102			chan_utils::build_keyed_anchor_input_witness(
103				&channel_params.broadcaster_pubkeys().funding_pubkey,
104				signature,
105			)
106		} else {
107			debug_assert!(tx_params.channel_type_features.supports_anchor_zero_fee_commitments());
108			Witness::from_slice(&[&[]])
109		}
110	}
111}
112
113/// Represents the different types of transactions, originating from LDK, to be bumped.
114#[derive(Clone, Debug, PartialEq, Eq)]
115pub enum BumpTransactionEvent {
116	/// Indicates that a channel featuring anchor outputs is to be closed by broadcasting the local
117	/// commitment transaction. Since commitment transactions have a static feerate pre-agreed upon,
118	/// they may need additional fees to be attached through a child transaction using the popular
119	/// [Child-Pays-For-Parent](https://bitcoinops.org/en/topics/cpfp) fee bumping technique. This
120	/// child transaction must include the anchor input described within `anchor_descriptor` along
121	/// with additional inputs to meet the target feerate. Failure to meet the target feerate
122	/// decreases the confirmation odds of the transaction package (which includes the commitment
123	/// and child anchor transactions), possibly resulting in a loss of funds. Once the transaction
124	/// is constructed, it must be fully signed for and broadcast by the consumer of the event
125	/// along with the `commitment_tx` enclosed. Note that the `commitment_tx` must always be
126	/// broadcast first, as the child anchor transaction depends on it. It is also possible that the
127	/// feerate of the commitment transaction is already sufficient, in which case the child anchor
128	/// transaction is not needed and only the commitment transaction should be broadcast.
129	///
130	/// In zero-fee commitment channels, the commitment transaction and the anchor transaction
131	/// form a 1-parent-1-child package that conforms to BIP 431 (known as TRUC transactions).
132	/// The anchor transaction must be version 3, and its size must be no more than 1000 vB.
133	/// The anchor transaction is usually needed to bump the fee of the commitment transaction
134	/// as the commitment transaction is not explicitly assigned any fees. In those cases the
135	/// anchor transaction must be broadcast together with the commitment transaction as a
136	/// `child-with-parents` package (usually using the Bitcoin Core `submitpackage` RPC).
137	///
138	/// The consumer should be able to sign for any of the additional inputs included within the
139	/// child anchor transaction. To sign its keyed-anchor input, an [`EcdsaChannelSigner`] should
140	/// be re-derived through [`SignerProvider::derive_channel_signer`]. The anchor input signature
141	/// can be computed with [`EcdsaChannelSigner::sign_holder_keyed_anchor_input`], which can then
142	/// be provided to [`build_keyed_anchor_input_witness`] along with the `funding_pubkey` to
143	/// obtain the full witness required to spend. Note that no signature or witness data is
144	/// required to spend the keyless anchor used in zero-fee commitment channels.
145	///
146	/// It is possible to receive more than one instance of this event if a valid child anchor
147	/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
148	/// be taken by the consumer of the event to ensure any future iterations of the child anchor
149	/// transaction adhere to the [Replace-By-Fee
150	/// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
151	/// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
152	/// these events is not user-controlled, users may ignore/drop the event if they are no longer
153	/// able to commit external confirmed funds to the child anchor transaction.
154	///
155	/// The set of `pending_htlcs` on the commitment transaction to be broadcast can be inspected to
156	/// determine whether a significant portion of the channel's funds are allocated to HTLCs,
157	/// enabling users to make their own decisions regarding the importance of the commitment
158	/// transaction's confirmation. Note that this is not required, but simply exists as an option
159	/// for users to override LDK's behavior. On commitments with no HTLCs (indicated by those with
160	/// an empty `pending_htlcs`), confirmation of the commitment transaction can be considered to
161	/// be not urgent.
162	///
163	/// [`EcdsaChannelSigner`]: crate::sign::ecdsa::EcdsaChannelSigner
164	/// [`EcdsaChannelSigner::sign_holder_keyed_anchor_input`]: crate::sign::ecdsa::EcdsaChannelSigner::sign_holder_keyed_anchor_input
165	/// [`build_keyed_anchor_input_witness`]: crate::ln::chan_utils::build_keyed_anchor_input_witness
166	ChannelClose {
167		/// The `channel_id` of the channel which has been closed.
168		channel_id: ChannelId,
169		/// Counterparty in the closed channel.
170		counterparty_node_id: PublicKey,
171		/// The unique identifier for the claim of the anchor output in the commitment transaction.
172		///
173		/// The identifier must map to the set of external UTXOs assigned to the claim, such that
174		/// they can be reused when a new claim with the same identifier needs to be made, resulting
175		/// in a fee-bumping attempt.
176		claim_id: ClaimId,
177		/// The target feerate that the transaction package, which consists of the commitment
178		/// transaction and the to-be-crafted child anchor transaction, must meet.
179		package_target_feerate_sat_per_1000_weight: u32,
180		/// The channel's commitment transaction to bump the fee of. This transaction should be
181		/// broadcast along with the anchor transaction constructed as a result of consuming this
182		/// event.
183		commitment_tx: Transaction,
184		/// The absolute fee in satoshis of the commitment transaction. This can be used along the
185		/// with weight of the commitment transaction to determine its feerate.
186		commitment_tx_fee_satoshis: u64,
187		/// The descriptor to sign the anchor input of the anchor transaction constructed as a
188		/// result of consuming this event.
189		anchor_descriptor: AnchorDescriptor,
190		/// The set of pending HTLCs on the commitment transaction that need to be resolved once the
191		/// commitment transaction confirms.
192		pending_htlcs: Vec<HTLCOutputInCommitment>,
193	},
194	/// Indicates that a channel featuring anchor outputs has unilaterally closed on-chain by a
195	/// holder commitment transaction and its HTLC(s) need to be resolved on-chain. In all such
196	/// channels, the pre-signed HTLC transactions have a zero fee, thus requiring additional
197	/// inputs and/or outputs to be attached for a timely confirmation within the chain. These
198	/// additional inputs and/or outputs must be appended to the resulting HTLC transaction to
199	/// meet the target feerate. Failure to meet the target feerate decreases the confirmation
200	/// odds of the transaction, possibly resulting in a loss of funds. Once the transaction
201	/// meets the target feerate, it must be signed for and broadcast by the consumer of the
202	/// event.
203	///
204	/// In zero-fee commitment channels, you must set the version of the HTLC claim transaction
205	/// to version 3 as the counterparty's signature commits to the version of
206	/// the transaction. You must also make sure that this claim transaction does not grow
207	/// bigger than 10,000 vB, the maximum vsize of any TRUC transaction as specified in
208	/// BIP 431. It is possible for [`htlc_descriptors`] to be long enough such
209	/// that claiming all the HTLCs therein in a single transaction would exceed this limit.
210	/// In this case, you must claim all the HTLCs in [`htlc_descriptors`] using multiple
211	/// transactions. Finally, note that while HTLCs in zero-fee commitment channels no
212	/// longer have the 1 CSV lock, LDK will still emit this event only after the commitment
213	/// transaction has 1 confirmation.
214	///
215	/// The consumer should be able to sign for any of the non-HTLC inputs added to the resulting
216	/// HTLC transaction. To sign HTLC inputs, an [`EcdsaChannelSigner`] should be re-derived
217	/// through [`SignerProvider::derive_channel_signer`]. Each HTLC input's signature can be
218	/// computed with [`EcdsaChannelSigner::sign_holder_htlc_transaction`], which can then be
219	/// provided to [`HTLCDescriptor::tx_input_witness`] to obtain the fully signed witness required
220	/// to spend.
221	///
222	/// It is possible to receive more than one instance of this event if a valid HTLC transaction
223	/// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
224	/// the consumer of the event to ensure any future iterations of the HTLC transaction adhere to
225	/// the [Replace-By-Fee
226	/// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
227	/// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
228	/// these events is not user-controlled, users may ignore/drop the event if either they are no
229	/// longer able to commit external confirmed funds to the HTLC transaction or the fee committed
230	/// to the HTLC transaction is greater in value than the HTLCs being claimed.
231	///
232	/// [`EcdsaChannelSigner`]: crate::sign::ecdsa::EcdsaChannelSigner
233	/// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::sign::ecdsa::EcdsaChannelSigner::sign_holder_htlc_transaction
234	/// [`htlc_descriptors`]: `BumpTransactionEvent::HTLCResolution::htlc_descriptors`
235	HTLCResolution {
236		/// The `channel_id` of the channel which has been closed.
237		channel_id: ChannelId,
238		/// Counterparty in the closed channel.
239		counterparty_node_id: PublicKey,
240		/// The unique identifier for the claim of the HTLCs in the confirmed commitment
241		/// transaction.
242		///
243		/// The identifier must map to the set of external UTXOs assigned to the claim, such that
244		/// they can be reused when a new claim with the same identifier needs to be made, resulting
245		/// in a fee-bumping attempt.
246		claim_id: ClaimId,
247		/// The target feerate that the resulting HTLC transaction must meet.
248		target_feerate_sat_per_1000_weight: u32,
249		/// The set of pending HTLCs on the confirmed commitment that need to be claimed, preferably
250		/// by the same transaction.
251		htlc_descriptors: Vec<HTLCDescriptor>,
252		/// The locktime required for the resulting HTLC transaction.
253		tx_lock_time: LockTime,
254	},
255}
256
257/// An input that must be included in a transaction when performing coin selection through
258/// [`CoinSelectionSource::select_confirmed_utxos`]. It is guaranteed to be a SegWit input, so it
259/// must have an empty [`TxIn::script_sig`] when spent.
260#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
261pub struct Input {
262	/// The unique identifier of the input.
263	pub outpoint: OutPoint,
264	/// The UTXO being spent by the input.
265	pub previous_utxo: TxOut,
266	/// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and
267	/// [`TxIn::witness`], each with their lengths included, required to satisfy the output's
268	/// script.
269	pub satisfaction_weight: u64,
270}
271
272/// An unspent transaction output that is available to spend resulting from a successful
273/// [`CoinSelection`] attempt.
274#[derive(Clone, Debug, Hash, PartialOrd, Ord, PartialEq, Eq)]
275pub struct Utxo {
276	/// The unique identifier of the output.
277	pub outpoint: OutPoint,
278	/// The output to spend.
279	pub output: TxOut,
280	/// The upper-bound weight consumed by the input's full [`TxIn::script_sig`] and [`TxIn::witness`], each
281	/// with their lengths included, required to satisfy the output's script. The weight consumed by
282	/// the input's `script_sig` must account for [`WITNESS_SCALE_FACTOR`].
283	pub satisfaction_weight: u64,
284}
285
286impl_writeable_tlv_based!(Utxo, {
287	(1, outpoint, required),
288	(3, output, required),
289	(5, satisfaction_weight, required),
290});
291
292impl Utxo {
293	/// Returns a `Utxo` with the `satisfaction_weight` estimate for a legacy P2PKH output.
294	pub fn new_p2pkh(outpoint: OutPoint, value: Amount, pubkey_hash: &PubkeyHash) -> Self {
295		let script_sig_size = 1 /* script_sig length */ +
296			1 /* OP_PUSH73 */ +
297			73 /* sig including sighash flag */ +
298			1 /* OP_PUSH33 */ +
299			33 /* pubkey */;
300		Self {
301			outpoint,
302			output: TxOut { value, script_pubkey: ScriptBuf::new_p2pkh(pubkey_hash) },
303			satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + 1, /* empty witness */
304		}
305	}
306
307	/// Returns a `Utxo` with the `satisfaction_weight` estimate for a P2WPKH nested in P2SH output.
308	pub fn new_nested_p2wpkh(outpoint: OutPoint, value: Amount, pubkey_hash: &WPubkeyHash) -> Self {
309		let script_sig_size = 1 /* script_sig length */ +
310			1 /* OP_0 */ +
311			1 /* OP_PUSH20 */ +
312			20 /* pubkey_hash */;
313		Self {
314			outpoint,
315			output: TxOut {
316				value,
317				script_pubkey: ScriptBuf::new_p2sh(
318					&ScriptBuf::new_p2wpkh(pubkey_hash).script_hash(),
319				),
320			},
321			satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64
322				+ P2WPKH_WITNESS_WEIGHT,
323		}
324	}
325
326	/// Returns a `Utxo` with the `satisfaction_weight` estimate for a SegWit v0 P2WPKH output.
327	pub fn new_v0_p2wpkh(outpoint: OutPoint, value: Amount, pubkey_hash: &WPubkeyHash) -> Self {
328		Self {
329			outpoint,
330			output: TxOut { value, script_pubkey: ScriptBuf::new_p2wpkh(pubkey_hash) },
331			satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT,
332		}
333	}
334}
335
336/// The result of a successful coin selection attempt for a transaction requiring additional UTXOs
337/// to cover its fees.
338#[derive(Clone, Debug)]
339pub struct CoinSelection {
340	/// The set of UTXOs (with at least 1 confirmation) to spend and use within a transaction
341	/// requiring additional fees.
342	pub confirmed_utxos: Vec<Utxo>,
343	/// An additional output tracking whether any change remained after coin selection. This output
344	/// should always have a value above dust for its given `script_pubkey`. It should not be
345	/// spent until the transaction it belongs to confirms to ensure mempool descendant limits are
346	/// not met. This implies no other party should be able to spend it except us.
347	pub change_output: Option<TxOut>,
348}
349
350/// An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
351/// sign for them. The coin selection method aims to mimic Bitcoin Core's `fundrawtransaction` RPC,
352/// which most wallets should be able to satisfy. Otherwise, consider implementing [`WalletSource`],
353/// which can provide a default implementation of this trait when used with [`Wallet`].
354///
355/// For a synchronous version of this trait, see [`sync::CoinSelectionSourceSync`].
356///
357/// This is not exported to bindings users as async is only supported in Rust.
358// Note that updates to documentation on this trait should be copied to the synchronous version.
359pub trait CoinSelectionSource {
360	/// Performs coin selection of a set of UTXOs, with at least 1 confirmation each, that are
361	/// available to spend. Implementations are free to pick their coin selection algorithm of
362	/// choice, as long as the following requirements are met:
363	///
364	/// 1. `must_spend` contains a set of [`Input`]s that must be included in the transaction
365	///    throughout coin selection, but must not be returned as part of the result.
366	/// 2. `must_pay_to` contains a set of [`TxOut`]s that must be included in the transaction
367	///    throughout coin selection. In some cases, like when funding an anchor transaction, this
368	///    set is empty. Implementations should ensure they handle this correctly on their end,
369	///    e.g., Bitcoin Core's `fundrawtransaction` RPC requires at least one output to be
370	///    provided, in which case a zero-value empty OP_RETURN output can be used instead.
371	/// 3. Enough inputs must be selected/contributed for the resulting transaction (including the
372	///    inputs and outputs noted above) to meet `target_feerate_sat_per_1000_weight`.
373	/// 4. The final transaction must have a weight smaller than `max_tx_weight`; if this
374	///    constraint can't be met, return an `Err`. In the case of counterparty-signed HTLC
375	///    transactions, we will remove a chunk of HTLCs and try your algorithm again. As for
376	///    anchor transactions, we will try your coin selection again with the same input-output
377	///    set when you call [`ChannelMonitor::rebroadcast_pending_claims`], as anchor transactions
378	///    cannot be downsized.
379	///
380	/// Implementations must take note that [`Input::satisfaction_weight`] only tracks the weight of
381	/// the input's `script_sig` and `witness`. Some wallets, like Bitcoin Core's, may require
382	/// providing the full input weight. Failing to do so may lead to underestimating fee bumps and
383	/// delaying block inclusion.
384	///
385	/// The `claim_id` must map to the set of external UTXOs assigned to the claim, such that they
386	/// can be re-used within new fee-bumped iterations of the original claiming transaction,
387	/// ensuring that claims don't double spend each other. If a specific `claim_id` has never had a
388	/// transaction associated with it, and all of the available UTXOs have already been assigned to
389	/// other claims, implementations must be willing to double spend their UTXOs. The choice of
390	/// which UTXOs to double spend is left to the implementation, but it must strive to keep the
391	/// set of other claims being double spent to a minimum.
392	///
393	/// [`ChannelMonitor::rebroadcast_pending_claims`]: crate::chain::channelmonitor::ChannelMonitor::rebroadcast_pending_claims
394	fn select_confirmed_utxos<'a>(
395		&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
396		target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
397	) -> AsyncResult<'a, CoinSelection, ()>;
398	/// Signs and provides the full witness for all inputs within the transaction known to the
399	/// trait (i.e., any provided via [`CoinSelectionSource::select_confirmed_utxos`]).
400	///
401	/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
402	/// unsigned transaction and then sign it with your wallet.
403	fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()>;
404}
405
406/// An alternative to [`CoinSelectionSource`] that can be implemented and used along [`Wallet`] to
407/// provide a default implementation to [`CoinSelectionSource`].
408///
409/// For a synchronous version of this trait, see [`sync::WalletSourceSync`].
410///
411/// This is not exported to bindings users as async is only supported in Rust.
412// Note that updates to documentation on this trait should be copied to the synchronous version.
413pub trait WalletSource {
414	/// Returns all UTXOs, with at least 1 confirmation each, that are available to spend.
415	fn list_confirmed_utxos<'a>(&'a self) -> AsyncResult<'a, Vec<Utxo>, ()>;
416	/// Returns a script to use for change above dust resulting from a successful coin selection
417	/// attempt.
418	fn get_change_script<'a>(&'a self) -> AsyncResult<'a, ScriptBuf, ()>;
419	/// Signs and provides the full [`TxIn::script_sig`] and [`TxIn::witness`] for all inputs within
420	/// the transaction known to the wallet (i.e., any provided via
421	/// [`WalletSource::list_confirmed_utxos`]).
422	///
423	/// If your wallet does not support signing PSBTs you can call `psbt.extract_tx()` to get the
424	/// unsigned transaction and then sign it with your wallet.
425	fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()>;
426}
427
428/// A wrapper over [`WalletSource`] that implements [`CoinSelectionSource`] by preferring UTXOs
429/// that would avoid conflicting double spends. If not enough UTXOs are available to do so,
430/// conflicting double spends may happen.
431///
432/// For a synchronous version of this wrapper, see [`sync::WalletSync`].
433///
434/// This is not exported to bindings users as async is only supported in Rust.
435// Note that updates to documentation on this struct should be copied to the synchronous version.
436pub struct Wallet<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend>
437where
438	W::Target: WalletSource + MaybeSend,
439	L::Target: Logger + MaybeSend,
440{
441	source: W,
442	logger: L,
443	// TODO: Do we care about cleaning this up once the UTXOs have a confirmed spend? We can do so
444	// by checking whether any UTXOs that exist in the map are no longer returned in
445	// `list_confirmed_utxos`.
446	locked_utxos: Mutex<HashMap<OutPoint, ClaimId>>,
447}
448
449impl<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend> Wallet<W, L>
450where
451	W::Target: WalletSource + MaybeSend,
452	L::Target: Logger + MaybeSend,
453{
454	/// Returns a new instance backed by the given [`WalletSource`] that serves as an implementation
455	/// of [`CoinSelectionSource`].
456	pub fn new(source: W, logger: L) -> Self {
457		Self { source, logger, locked_utxos: Mutex::new(new_hash_map()) }
458	}
459
460	/// Performs coin selection on the set of UTXOs obtained from
461	/// [`WalletSource::list_confirmed_utxos`]. Its algorithm can be described as "smallest
462	/// above-dust-after-spend first", with a slight twist: we may skip UTXOs that are above dust at
463	/// the target feerate after having spent them in a separate claim transaction if
464	/// `force_conflicting_utxo_spend` is unset to avoid producing conflicting transactions. If
465	/// `tolerate_high_network_feerates` is set, we'll attempt to spend UTXOs that contribute at
466	/// least 1 satoshi at the current feerate, otherwise, we'll only attempt to spend those which
467	/// contribute at least twice their fee.
468	async fn select_confirmed_utxos_internal(
469		&self, utxos: &[Utxo], claim_id: ClaimId, force_conflicting_utxo_spend: bool,
470		tolerate_high_network_feerates: bool, target_feerate_sat_per_1000_weight: u32,
471		preexisting_tx_weight: u64, input_amount_sat: Amount, target_amount_sat: Amount,
472		max_tx_weight: u64,
473	) -> Result<CoinSelection, ()> {
474		// P2WSH and P2TR outputs are both the heaviest-weight standard outputs at 34 bytes
475		let max_coin_selection_weight = max_tx_weight
476			.checked_sub(preexisting_tx_weight + P2WSH_TXOUT_WEIGHT)
477			.ok_or_else(|| {
478				log_debug!(
479					self.logger,
480					"max_tx_weight is too small to accommodate the preexisting tx weight plus a P2WSH/P2TR output"
481				);
482			})?;
483
484		let mut selected_amount;
485		let mut total_fees;
486		let mut selected_utxos;
487		{
488			let mut locked_utxos = self.locked_utxos.lock().unwrap();
489			let mut eligible_utxos = utxos
490				.iter()
491				.filter_map(|utxo| {
492					if let Some(utxo_claim_id) = locked_utxos.get(&utxo.outpoint) {
493						if *utxo_claim_id != claim_id && !force_conflicting_utxo_spend {
494							log_trace!(
495								self.logger,
496								"Skipping UTXO {} to prevent conflicting spend",
497								utxo.outpoint
498							);
499							return None;
500						}
501					}
502					let fee_to_spend_utxo = Amount::from_sat(fee_for_weight(
503						target_feerate_sat_per_1000_weight,
504						BASE_INPUT_WEIGHT + utxo.satisfaction_weight,
505					));
506					let should_spend = if tolerate_high_network_feerates {
507						utxo.output.value > fee_to_spend_utxo
508					} else {
509						utxo.output.value >= fee_to_spend_utxo * 2
510					};
511					if should_spend {
512						Some((utxo, fee_to_spend_utxo))
513					} else {
514						log_trace!(
515							self.logger,
516							"Skipping UTXO {} due to dust proximity after spend",
517							utxo.outpoint
518						);
519						None
520					}
521				})
522				.collect::<Vec<_>>();
523			eligible_utxos.sort_unstable_by_key(|(utxo, fee_to_spend_utxo)| {
524				utxo.output.value - *fee_to_spend_utxo
525			});
526
527			selected_amount = input_amount_sat;
528			total_fees = Amount::from_sat(fee_for_weight(
529				target_feerate_sat_per_1000_weight,
530				preexisting_tx_weight,
531			));
532			selected_utxos = VecDeque::new();
533			// Invariant: `selected_utxos_weight` is never greater than `max_coin_selection_weight`
534			let mut selected_utxos_weight = 0;
535			for (utxo, fee_to_spend_utxo) in eligible_utxos {
536				if selected_amount >= target_amount_sat + total_fees {
537					break;
538				}
539				// First skip any UTXOs with prohibitive satisfaction weights
540				if BASE_INPUT_WEIGHT + utxo.satisfaction_weight > max_coin_selection_weight {
541					continue;
542				}
543				// If adding this UTXO to `selected_utxos` would push us over the
544				// `max_coin_selection_weight`, remove UTXOs from the front to make room
545				// for this new UTXO.
546				while selected_utxos_weight + BASE_INPUT_WEIGHT + utxo.satisfaction_weight
547					> max_coin_selection_weight
548					&& !selected_utxos.is_empty()
549				{
550					let (smallest_value_after_spend_utxo, fee_to_spend_utxo): (Utxo, Amount) =
551						selected_utxos.pop_front().unwrap();
552					selected_amount -= smallest_value_after_spend_utxo.output.value;
553					total_fees -= fee_to_spend_utxo;
554					selected_utxos_weight -=
555						BASE_INPUT_WEIGHT + smallest_value_after_spend_utxo.satisfaction_weight;
556				}
557				selected_amount += utxo.output.value;
558				total_fees += fee_to_spend_utxo;
559				selected_utxos_weight += BASE_INPUT_WEIGHT + utxo.satisfaction_weight;
560				selected_utxos.push_back((utxo.clone(), fee_to_spend_utxo));
561			}
562			if selected_amount < target_amount_sat + total_fees {
563				log_debug!(
564					self.logger,
565					"Insufficient funds to meet target feerate {} sat/kW while remaining under {} WU",
566					target_feerate_sat_per_1000_weight,
567					max_coin_selection_weight,
568				);
569				return Err(());
570			}
571			// Once we've selected enough UTXOs to cover `target_amount_sat + total_fees`,
572			// we may be able to remove some small-value ones while still covering
573			// `target_amount_sat + total_fees`.
574			while !selected_utxos.is_empty()
575				&& selected_amount - selected_utxos.front().unwrap().0.output.value
576					>= target_amount_sat + total_fees - selected_utxos.front().unwrap().1
577			{
578				let (smallest_value_after_spend_utxo, fee_to_spend_utxo) =
579					selected_utxos.pop_front().unwrap();
580				selected_amount -= smallest_value_after_spend_utxo.output.value;
581				total_fees -= fee_to_spend_utxo;
582			}
583			for (utxo, _) in &selected_utxos {
584				locked_utxos.insert(utxo.outpoint, claim_id);
585			}
586		}
587
588		let remaining_amount = selected_amount - target_amount_sat - total_fees;
589		let change_script = self.source.get_change_script().await?;
590		let change_output_fee = fee_for_weight(
591			target_feerate_sat_per_1000_weight,
592			(8 /* value */ + change_script.consensus_encode(&mut sink()).unwrap() as u64)
593				* WITNESS_SCALE_FACTOR as u64,
594		);
595		let change_output_amount =
596			Amount::from_sat(remaining_amount.to_sat().saturating_sub(change_output_fee));
597		let change_output = if change_output_amount < change_script.minimal_non_dust() {
598			log_debug!(self.logger, "Coin selection attempt did not yield change output");
599			None
600		} else {
601			Some(TxOut { script_pubkey: change_script, value: change_output_amount })
602		};
603
604		Ok(CoinSelection {
605			confirmed_utxos: selected_utxos.into_iter().map(|(utxo, _)| utxo).collect(),
606			change_output,
607		})
608	}
609}
610
611impl<W: Deref + MaybeSync + MaybeSend, L: Deref + MaybeSync + MaybeSend> CoinSelectionSource
612	for Wallet<W, L>
613where
614	W::Target: WalletSource + MaybeSend + MaybeSync,
615	L::Target: Logger + MaybeSend + MaybeSync,
616{
617	fn select_confirmed_utxos<'a>(
618		&'a self, claim_id: ClaimId, must_spend: Vec<Input>, must_pay_to: &'a [TxOut],
619		target_feerate_sat_per_1000_weight: u32, max_tx_weight: u64,
620	) -> AsyncResult<'a, CoinSelection, ()> {
621		Box::pin(async move {
622			let utxos = self.source.list_confirmed_utxos().await?;
623			// TODO: Use fee estimation utils when we upgrade to bitcoin v0.30.0.
624			let total_output_size: u64 = must_pay_to
625				.iter()
626				.map(
627					|output| 8 /* value */ + 1 /* script len */ + output.script_pubkey.len() as u64,
628				)
629				.sum();
630			let total_satisfaction_weight: u64 =
631				must_spend.iter().map(|input| input.satisfaction_weight).sum();
632			let total_input_weight =
633				(BASE_INPUT_WEIGHT * must_spend.len() as u64) + total_satisfaction_weight;
634
635			let preexisting_tx_weight = SEGWIT_MARKER_FLAG_WEIGHT
636				+ total_input_weight
637				+ ((BASE_TX_SIZE + total_output_size) * WITNESS_SCALE_FACTOR as u64);
638			let input_amount_sat = must_spend.iter().map(|input| input.previous_utxo.value).sum();
639			let target_amount_sat = must_pay_to.iter().map(|output| output.value).sum();
640
641			let configs = [(false, false), (false, true), (true, false), (true, true)];
642			for (force_conflicting_utxo_spend, tolerate_high_network_feerates) in configs {
643				log_debug!(
644					self.logger,
645					"Attempting coin selection targeting {} sat/kW (force_conflicting_utxo_spend = {}, tolerate_high_network_feerates = {})",
646					target_feerate_sat_per_1000_weight,
647					force_conflicting_utxo_spend,
648					tolerate_high_network_feerates
649				);
650				let attempt = self
651					.select_confirmed_utxos_internal(
652						&utxos,
653						claim_id,
654						force_conflicting_utxo_spend,
655						tolerate_high_network_feerates,
656						target_feerate_sat_per_1000_weight,
657						preexisting_tx_weight,
658						input_amount_sat,
659						target_amount_sat,
660						max_tx_weight,
661					)
662					.await;
663				if attempt.is_ok() {
664					return attempt;
665				}
666			}
667			Err(())
668		})
669	}
670
671	fn sign_psbt<'a>(&'a self, psbt: Psbt) -> AsyncResult<'a, Transaction, ()> {
672		self.source.sign_psbt(psbt)
673	}
674}
675
676/// A handler for [`Event::BumpTransaction`] events that sources confirmed UTXOs from a
677/// [`CoinSelectionSource`] to fee bump transactions via Child-Pays-For-Parent (CPFP) or
678/// Replace-By-Fee (RBF).
679///
680/// For a synchronous version of this handler, see [`sync::BumpTransactionEventHandlerSync`].
681///
682/// This is not exported to bindings users as async is only supported in Rust.
683///
684/// [`Event::BumpTransaction`]: crate::events::Event::BumpTransaction
685// Note that updates to documentation on this struct should be copied to the synchronous version.
686pub struct BumpTransactionEventHandler<B: Deref, C: Deref, SP: Deref, L: Deref>
687where
688	B::Target: BroadcasterInterface,
689	C::Target: CoinSelectionSource,
690	SP::Target: SignerProvider,
691	L::Target: Logger,
692{
693	broadcaster: B,
694	utxo_source: C,
695	signer_provider: SP,
696	logger: L,
697	secp: Secp256k1<secp256k1::All>,
698}
699
700impl<B: Deref, C: Deref, SP: Deref, L: Deref> BumpTransactionEventHandler<B, C, SP, L>
701where
702	B::Target: BroadcasterInterface,
703	C::Target: CoinSelectionSource,
704	SP::Target: SignerProvider,
705	L::Target: Logger,
706{
707	/// Returns a new instance capable of handling [`Event::BumpTransaction`] events.
708	///
709	/// [`Event::BumpTransaction`]: crate::events::Event::BumpTransaction
710	pub fn new(broadcaster: B, utxo_source: C, signer_provider: SP, logger: L) -> Self {
711		Self { broadcaster, utxo_source, signer_provider, logger, secp: Secp256k1::new() }
712	}
713
714	/// Updates a transaction with the result of a successful coin selection attempt.
715	fn process_coin_selection(&self, tx: &mut Transaction, coin_selection: &CoinSelection) {
716		for utxo in coin_selection.confirmed_utxos.iter() {
717			tx.input.push(TxIn {
718				previous_output: utxo.outpoint,
719				script_sig: ScriptBuf::new(),
720				sequence: Sequence::ZERO,
721				witness: Witness::new(),
722			});
723		}
724		if let Some(change_output) = coin_selection.change_output.clone() {
725			tx.output.push(change_output);
726		} else if tx.output.is_empty() {
727			// We weren't provided a change output, likely because the input set was a perfect
728			// match, but we still need to have at least one output in the transaction for it to be
729			// considered standard. We choose to go with an empty OP_RETURN as it is the cheapest
730			// way to include a dummy output.
731			if tx.input.len() <= 1 {
732				// Transactions have to be at least 65 bytes in non-witness data, which we can run
733				// under if we have too few witness inputs.
734				log_debug!(self.logger, "Including large OP_RETURN output since an output is needed and a change output was not provided and the transaction is small");
735				debug_assert!(!tx.input.is_empty());
736				tx.output.push(TxOut {
737					value: Amount::ZERO,
738					// Minimum transaction size is 60 bytes, so we need a 5-byte script to get a
739					// 65 byte transaction. We do that as OP_RETURN <3 0 bytes, plus 1 byte len>.
740					script_pubkey: ScriptBuf::new_op_return(&[0, 0, 0]),
741				});
742				debug_assert_eq!(tx.base_size(), 65);
743			} else {
744				log_debug!(self.logger, "Including dummy OP_RETURN output since an output is needed and a change output was not provided");
745				tx.output.push(TxOut {
746					value: Amount::ZERO,
747					script_pubkey: ScriptBuf::new_op_return(&[]),
748				});
749			}
750		}
751	}
752
753	/// Handles a [`BumpTransactionEvent::ChannelClose`] event variant by producing a fully-signed
754	/// transaction spending an anchor output of the commitment transaction to bump its fee and
755	/// broadcasts them to the network as a package.
756	async fn handle_channel_close(
757		&self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32,
758		commitment_tx: &Transaction, commitment_tx_fee_sat: u64,
759		anchor_descriptor: &AnchorDescriptor,
760	) -> Result<(), ()> {
761		let channel_type = &anchor_descriptor
762			.channel_derivation_parameters
763			.transaction_parameters
764			.channel_type_features;
765		let anchor_input_witness_weight = if channel_type.supports_anchor_zero_fee_commitments() {
766			EMPTY_WITNESS_WEIGHT
767		} else {
768			ANCHOR_INPUT_WITNESS_WEIGHT
769		};
770
771		// First, check if the commitment transaction has sufficient fees on its own.
772		let commitment_tx_feerate_sat_per_1000_weight = compute_feerate_sat_per_1000_weight(
773			commitment_tx_fee_sat,
774			commitment_tx.weight().to_wu(),
775		);
776		if commitment_tx_feerate_sat_per_1000_weight >= package_target_feerate_sat_per_1000_weight {
777			log_debug!(self.logger, "Pre-signed commitment {} already has feerate {} sat/kW above required {} sat/kW, broadcasting.",
778				commitment_tx.compute_txid(), commitment_tx_feerate_sat_per_1000_weight,
779				package_target_feerate_sat_per_1000_weight);
780			self.broadcaster.broadcast_transactions(&[&commitment_tx]);
781			return Ok(());
782		}
783
784		// Our commitment transaction already has fees allocated to it, so we should take them into
785		// account. We do so by pretending the commitment transaction's fee and weight are part of
786		// the anchor input.
787		let mut anchor_utxo = anchor_descriptor.previous_utxo();
788		let commitment_tx_fee_sat = Amount::from_sat(commitment_tx_fee_sat);
789		let commitment_tx_weight = commitment_tx.weight().to_wu();
790		anchor_utxo.value += commitment_tx_fee_sat;
791		let starting_package_and_fixed_input_satisfaction_weight =
792			commitment_tx_weight + anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT;
793		let mut package_and_fixed_input_satisfaction_weight =
794			starting_package_and_fixed_input_satisfaction_weight;
795
796		loop {
797			let must_spend = vec![Input {
798				outpoint: anchor_descriptor.outpoint,
799				previous_utxo: anchor_utxo.clone(),
800				satisfaction_weight: package_and_fixed_input_satisfaction_weight,
801			}];
802			let must_spend_amount =
803				must_spend.iter().map(|input| input.previous_utxo.value).sum::<Amount>();
804
805			log_debug!(self.logger, "Performing coin selection for commitment package (commitment and anchor transaction) targeting {} sat/kW",
806				package_target_feerate_sat_per_1000_weight);
807			let coin_selection: CoinSelection = self
808				.utxo_source
809				.select_confirmed_utxos(
810					claim_id,
811					must_spend,
812					&[],
813					package_target_feerate_sat_per_1000_weight,
814					if channel_type.supports_anchor_zero_fee_commitments() {
815						TRUC_CHILD_MAX_WEIGHT
816					} else {
817						MAX_STANDARD_TX_WEIGHT as u64
818					}
819					// We added the commitment tx weight to the input satisfaction weight above, so
820					// increase the max_tx_weight by the same delta here.
821					+ commitment_tx_weight,
822				)
823				.await?;
824
825			let version = if channel_type.supports_anchor_zero_fee_commitments() {
826				Version::non_standard(3)
827			} else {
828				Version::TWO
829			};
830
831			let mut anchor_tx = Transaction {
832				version,
833				lock_time: LockTime::ZERO, // TODO: Use next best height.
834				input: vec![anchor_descriptor.unsigned_tx_input()],
835				output: vec![],
836			};
837
838			let input_satisfaction_weight: u64 =
839				coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum();
840			let total_satisfaction_weight =
841				anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT + input_satisfaction_weight;
842			let total_input_amount = must_spend_amount
843				+ coin_selection.confirmed_utxos.iter().map(|utxo| utxo.output.value).sum();
844
845			self.process_coin_selection(&mut anchor_tx, &coin_selection);
846			let anchor_txid = anchor_tx.compute_txid();
847
848			// construct psbt
849			let mut anchor_psbt = Psbt::from_unsigned_tx(anchor_tx).unwrap();
850			// add witness_utxo to anchor input
851			anchor_psbt.inputs[0].witness_utxo = Some(anchor_descriptor.previous_utxo());
852			// add witness_utxo to remaining inputs
853			for (idx, utxo) in coin_selection.confirmed_utxos.into_iter().enumerate() {
854				// add 1 to skip the anchor input
855				let index = idx + 1;
856				debug_assert_eq!(
857					anchor_psbt.unsigned_tx.input[index].previous_output,
858					utxo.outpoint
859				);
860				if utxo.output.script_pubkey.is_witness_program() {
861					anchor_psbt.inputs[index].witness_utxo = Some(utxo.output);
862				}
863			}
864
865			debug_assert_eq!(anchor_psbt.unsigned_tx.output.len(), 1);
866			let unsigned_tx_weight = anchor_psbt.unsigned_tx.weight().to_wu()
867				- (anchor_psbt.unsigned_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
868
869			let package_fee = total_input_amount
870				- anchor_psbt.unsigned_tx.output.iter().map(|output| output.value).sum();
871			let package_weight = unsigned_tx_weight + 2 /* wit marker */ + total_satisfaction_weight + commitment_tx.weight().to_wu();
872			if package_fee.to_sat() * 1000 / package_weight
873				< package_target_feerate_sat_per_1000_weight.into()
874			{
875				// On the first iteration of the loop, we may undershoot the target feerate because
876				// we had to add an OP_RETURN output in `process_coin_selection` which we didn't
877				// select sufficient coins for. Here we detect that case and go around again
878				// seeking additional weight.
879				if package_and_fixed_input_satisfaction_weight
880					== starting_package_and_fixed_input_satisfaction_weight
881				{
882					debug_assert!(
883						anchor_psbt.unsigned_tx.output[0].script_pubkey.is_op_return(),
884						"Coin selection failed to select sufficient coins for its change output"
885					);
886					package_and_fixed_input_satisfaction_weight +=
887						anchor_psbt.unsigned_tx.output[0].weight().to_wu();
888					continue;
889				} else {
890					debug_assert!(false, "Coin selection failed to select sufficient coins");
891				}
892			}
893
894			log_debug!(self.logger, "Signing anchor transaction {}", anchor_txid);
895			anchor_tx = self.utxo_source.sign_psbt(anchor_psbt).await?;
896
897			// No need to produce any witness to spend P2A anchors
898			if channel_type.supports_anchors_zero_fee_htlc_tx() {
899				let signer = self
900					.signer_provider
901					.derive_channel_signer(anchor_descriptor.channel_derivation_parameters.keys_id);
902				let channel_parameters =
903					&anchor_descriptor.channel_derivation_parameters.transaction_parameters;
904				let anchor_sig = signer.sign_holder_keyed_anchor_input(
905					channel_parameters,
906					&anchor_tx,
907					0,
908					&self.secp,
909				)?;
910				anchor_tx.input[0].witness = anchor_descriptor.tx_input_witness(&anchor_sig);
911			}
912
913			#[cfg(debug_assertions)]
914			{
915				let signed_tx_weight = anchor_tx.weight().to_wu();
916				let expected_signed_tx_weight =
917					unsigned_tx_weight + 2 /* wit marker */ + total_satisfaction_weight;
918				// Our estimate should be within a 1% error margin of the actual weight and we should
919				// never underestimate.
920				assert!(expected_signed_tx_weight >= signed_tx_weight);
921				assert!(expected_signed_tx_weight * 99 / 100 <= signed_tx_weight);
922
923				let expected_package_fee = Amount::from_sat(fee_for_weight(
924					package_target_feerate_sat_per_1000_weight,
925					signed_tx_weight + commitment_tx.weight().to_wu(),
926				));
927				// Our feerate should always be at least what we were seeking. It may overshoot if
928				// the coin selector burned funds to an OP_RETURN without a change output.
929				assert!(package_fee >= expected_package_fee);
930			}
931
932			#[cfg(debug_assertions)]
933			if channel_type.supports_anchor_zero_fee_commitments() {
934				assert!(commitment_tx.weight().to_wu() < TRUC_MAX_WEIGHT);
935				assert!(anchor_tx.weight().to_wu() < TRUC_CHILD_MAX_WEIGHT);
936			} else {
937				assert!(commitment_tx.weight().to_wu() < MAX_STANDARD_TX_WEIGHT as u64);
938				assert!(anchor_tx.weight().to_wu() < MAX_STANDARD_TX_WEIGHT as u64);
939			}
940
941			log_info!(
942				self.logger,
943				"Broadcasting anchor transaction {} to bump channel close with txid {}",
944				anchor_txid,
945				commitment_tx.compute_txid()
946			);
947			self.broadcaster.broadcast_transactions(&[&commitment_tx, &anchor_tx]);
948			return Ok(());
949		}
950	}
951
952	/// Handles a [`BumpTransactionEvent::HTLCResolution`] event variant by producing a
953	/// fully-signed, fee-bumped HTLC transaction that is broadcast to the network.
954	async fn handle_htlc_resolution(
955		&self, claim_id: ClaimId, target_feerate_sat_per_1000_weight: u32,
956		htlc_descriptors: &[HTLCDescriptor], tx_lock_time: LockTime,
957	) -> Result<(), ()> {
958		let channel_type = &htlc_descriptors[0]
959			.channel_derivation_parameters
960			.transaction_parameters
961			.channel_type_features;
962		let (htlc_success_witness_weight, htlc_timeout_witness_weight) =
963			if channel_type.supports_anchor_zero_fee_commitments() {
964				(
965					HTLC_SUCCESS_INPUT_P2A_ANCHOR_WITNESS_WEIGHT,
966					HTLC_TIMEOUT_INPUT_P2A_ANCHOR_WITNESS_WEIGHT,
967				)
968			} else if channel_type.supports_anchors_zero_fee_htlc_tx() {
969				(
970					HTLC_SUCCESS_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT,
971					HTLC_TIMEOUT_INPUT_KEYED_ANCHOR_WITNESS_WEIGHT,
972				)
973			} else {
974				panic!("channel type should be either zero-fee HTLCs, or zero-fee commitments");
975			};
976
977		let max_tx_weight = if channel_type.supports_anchor_zero_fee_commitments() {
978			// Cap the size of transactions claiming `HolderHTLCOutput` in 0FC channels.
979			// Otherwise, we could hit the max 10_000vB size limit on V3 transactions
980			// (BIP 431 rule 4).
981			TRUC_MAX_WEIGHT
982		} else {
983			// We should never hit this because HTLC-timeout transactions have a signed
984			// locktime, HTLC-success transactions do not, and we never aggregate
985			// packages with a signed locktime with packages that do not have a signed
986			// locktime.
987			// Hence in the worst case, we aggregate 483 success HTLC transactions,
988			// and 483 * 705 ~= 341_000, and 341_000 < 400_000.
989			MAX_STANDARD_TX_WEIGHT as u64
990		};
991		// A 1-input 1-output transaction, both p2wpkh is 438 WU.
992		// This is just an initial budget, we increase it further below in case the user can't satisfy it.
993		const USER_COINS_WEIGHT_BUDGET: u64 = 1000;
994
995		let mut broadcasted_htlcs = 0;
996		let mut batch_size = htlc_descriptors.len() - broadcasted_htlcs;
997		let mut utxo_id = claim_id;
998
999		while broadcasted_htlcs < htlc_descriptors.len() {
1000			let mut htlc_tx = Transaction {
1001				version: if channel_type.supports_anchor_zero_fee_commitments() {
1002					Version::non_standard(3)
1003				} else {
1004					Version::TWO
1005				},
1006				lock_time: tx_lock_time,
1007				input: vec![],
1008				output: vec![],
1009			};
1010			let mut must_spend = Vec::with_capacity(htlc_descriptors.len() - broadcasted_htlcs);
1011			let mut htlc_weight_sum = 0;
1012			for htlc_descriptor in
1013				&htlc_descriptors[broadcasted_htlcs..broadcasted_htlcs + batch_size]
1014			{
1015				let input_output_weight = if htlc_descriptor.preimage.is_some() {
1016					chan_utils::aggregated_htlc_success_input_output_pair_weight(channel_type)
1017				} else {
1018					chan_utils::aggregated_htlc_timeout_input_output_pair_weight(channel_type)
1019				};
1020				if htlc_weight_sum + input_output_weight >= max_tx_weight - USER_COINS_WEIGHT_BUDGET
1021				{
1022					break;
1023				}
1024				htlc_weight_sum += input_output_weight;
1025				let htlc_input = htlc_descriptor.unsigned_tx_input();
1026				must_spend.push(Input {
1027					outpoint: htlc_input.previous_output.clone(),
1028					previous_utxo: htlc_descriptor.previous_utxo(&self.secp),
1029					satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT
1030						+ if htlc_descriptor.preimage.is_some() {
1031							htlc_success_witness_weight
1032						} else {
1033							htlc_timeout_witness_weight
1034						},
1035				});
1036				htlc_tx.input.push(htlc_input);
1037				let htlc_output = htlc_descriptor.tx_output(&self.secp);
1038				htlc_tx.output.push(htlc_output);
1039			}
1040			batch_size = htlc_tx.input.len();
1041			let selected_htlcs =
1042				&htlc_descriptors[broadcasted_htlcs..broadcasted_htlcs + batch_size];
1043
1044			log_info!(
1045				self.logger,
1046				"Batch transaction assigned to UTXO id {} contains {} HTLCs: {}",
1047				log_bytes!(utxo_id.0),
1048				batch_size,
1049				log_iter!(selected_htlcs.iter().map(|d| d.outpoint()))
1050			);
1051
1052			log_debug!(
1053				self.logger,
1054				"Performing coin selection for HTLC transaction targeting {} sat/kW",
1055				target_feerate_sat_per_1000_weight
1056			);
1057
1058			#[cfg(debug_assertions)]
1059			let must_spend_satisfaction_weight =
1060				must_spend.iter().map(|input| input.satisfaction_weight).sum::<u64>();
1061			#[cfg(debug_assertions)]
1062			let must_spend_amount =
1063				must_spend.iter().map(|input| input.previous_utxo.value.to_sat()).sum::<u64>();
1064
1065			let coin_selection: CoinSelection = match self
1066				.utxo_source
1067				.select_confirmed_utxos(
1068					utxo_id,
1069					must_spend,
1070					&htlc_tx.output,
1071					target_feerate_sat_per_1000_weight,
1072					max_tx_weight,
1073				)
1074				.await
1075			{
1076				Ok(selection) => selection,
1077				Err(()) => {
1078					let pair_weight =
1079						chan_utils::aggregated_htlc_timeout_input_output_pair_weight(channel_type);
1080					let htlcs_to_remove =
1081						(USER_COINS_WEIGHT_BUDGET + pair_weight - 1) / pair_weight;
1082					batch_size = batch_size.checked_sub(htlcs_to_remove as usize).ok_or(())?;
1083					if batch_size == 0 {
1084						return Err(());
1085					}
1086					continue;
1087				},
1088			};
1089			broadcasted_htlcs += batch_size;
1090			batch_size = htlc_descriptors.len() - broadcasted_htlcs;
1091			utxo_id = claim_id.step_with_bytes(&broadcasted_htlcs.to_be_bytes());
1092
1093			#[cfg(debug_assertions)]
1094			let input_satisfaction_weight: u64 =
1095				coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum();
1096			#[cfg(debug_assertions)]
1097			let total_satisfaction_weight = must_spend_satisfaction_weight + input_satisfaction_weight;
1098			#[cfg(debug_assertions)]
1099			let input_value: u64 =
1100				coin_selection.confirmed_utxos.iter().map(|utxo| utxo.output.value.to_sat()).sum();
1101			#[cfg(debug_assertions)]
1102			let total_input_amount = must_spend_amount + input_value;
1103
1104			self.process_coin_selection(&mut htlc_tx, &coin_selection);
1105
1106			// construct psbt
1107			let mut htlc_psbt = Psbt::from_unsigned_tx(htlc_tx).unwrap();
1108			// add witness_utxo to htlc inputs
1109			for (i, htlc_descriptor) in selected_htlcs.iter().enumerate() {
1110				debug_assert_eq!(
1111					htlc_psbt.unsigned_tx.input[i].previous_output,
1112					htlc_descriptor.outpoint()
1113				);
1114				htlc_psbt.inputs[i].witness_utxo = Some(htlc_descriptor.previous_utxo(&self.secp));
1115			}
1116
1117			// add witness_utxo to remaining inputs
1118			for (idx, utxo) in coin_selection.confirmed_utxos.into_iter().enumerate() {
1119				// offset to skip the htlc inputs
1120				let index = idx + selected_htlcs.len();
1121				debug_assert_eq!(htlc_psbt.unsigned_tx.input[index].previous_output, utxo.outpoint);
1122				if utxo.output.script_pubkey.is_witness_program() {
1123					htlc_psbt.inputs[index].witness_utxo = Some(utxo.output);
1124				}
1125			}
1126
1127			#[cfg(debug_assertions)]
1128			let unsigned_tx_weight = htlc_psbt.unsigned_tx.weight().to_wu()
1129				- (htlc_psbt.unsigned_tx.input.len() as u64 * EMPTY_SCRIPT_SIG_WEIGHT);
1130
1131			log_debug!(
1132				self.logger,
1133				"Signing HTLC transaction {}",
1134				htlc_psbt.unsigned_tx.compute_txid()
1135			);
1136			htlc_tx = self.utxo_source.sign_psbt(htlc_psbt).await?;
1137
1138			let mut signers = BTreeMap::new();
1139			for (idx, htlc_descriptor) in selected_htlcs.iter().enumerate() {
1140				let keys_id = htlc_descriptor.channel_derivation_parameters.keys_id;
1141				let signer = signers
1142					.entry(keys_id)
1143					.or_insert_with(|| self.signer_provider.derive_channel_signer(keys_id));
1144				let htlc_sig = signer.sign_holder_htlc_transaction(
1145					&htlc_tx,
1146					idx,
1147					htlc_descriptor,
1148					&self.secp,
1149				)?;
1150				let witness_script = htlc_descriptor.witness_script(&self.secp);
1151				htlc_tx.input[idx].witness =
1152					htlc_descriptor.tx_input_witness(&htlc_sig, &witness_script);
1153			}
1154
1155			#[cfg(debug_assertions)]
1156			{
1157				let signed_tx_weight = htlc_tx.weight().to_wu();
1158				let expected_signed_tx_weight = unsigned_tx_weight + total_satisfaction_weight;
1159				// Our estimate should be within a 2% error margin of the actual weight and we should
1160				// never underestimate.
1161				assert!(expected_signed_tx_weight >= signed_tx_weight);
1162				assert!(expected_signed_tx_weight * 98 / 100 <= signed_tx_weight);
1163
1164				let expected_signed_tx_fee =
1165					fee_for_weight(target_feerate_sat_per_1000_weight, signed_tx_weight);
1166				let signed_tx_fee = total_input_amount
1167					- htlc_tx.output.iter().map(|output| output.value.to_sat()).sum::<u64>();
1168				// Our feerate should always be at least what we were seeking. It may overshoot if
1169				// the coin selector burned funds to an OP_RETURN without a change output.
1170				assert!(signed_tx_fee >= expected_signed_tx_fee);
1171			}
1172
1173			#[cfg(debug_assertions)]
1174			if channel_type.supports_anchor_zero_fee_commitments() {
1175				assert!(htlc_tx.weight().to_wu() < TRUC_MAX_WEIGHT);
1176			} else {
1177				assert!(htlc_tx.weight().to_wu() < MAX_STANDARD_TX_WEIGHT as u64);
1178			}
1179
1180			log_info!(self.logger, "Broadcasting {}", log_tx!(htlc_tx));
1181			self.broadcaster.broadcast_transactions(&[&htlc_tx]);
1182		}
1183
1184		Ok(())
1185	}
1186
1187	/// Handles all variants of [`BumpTransactionEvent`].
1188	pub async fn handle_event(&self, event: &BumpTransactionEvent) {
1189		match event {
1190			BumpTransactionEvent::ChannelClose {
1191				claim_id,
1192				package_target_feerate_sat_per_1000_weight,
1193				commitment_tx,
1194				commitment_tx_fee_satoshis,
1195				anchor_descriptor,
1196				..
1197			} => {
1198				log_info!(
1199					self.logger,
1200					"Handling channel close bump (claim_id = {}, commitment_txid = {})",
1201					log_bytes!(claim_id.0),
1202					commitment_tx.compute_txid()
1203				);
1204				self.handle_channel_close(
1205					*claim_id,
1206					*package_target_feerate_sat_per_1000_weight,
1207					commitment_tx,
1208					*commitment_tx_fee_satoshis,
1209					anchor_descriptor,
1210				)
1211				.await
1212				.unwrap_or_else(|_| {
1213					log_error!(
1214						self.logger,
1215						"Failed bumping commitment transaction fee for {}",
1216						commitment_tx.compute_txid()
1217					);
1218				});
1219			},
1220			BumpTransactionEvent::HTLCResolution {
1221				claim_id,
1222				target_feerate_sat_per_1000_weight,
1223				htlc_descriptors,
1224				tx_lock_time,
1225				..
1226			} => {
1227				log_info!(
1228					self.logger,
1229					"Handling HTLC bump (claim_id = {}, htlcs_to_claim = {})",
1230					log_bytes!(claim_id.0),
1231					log_iter!(htlc_descriptors.iter().map(|d| d.outpoint()))
1232				);
1233				self.handle_htlc_resolution(
1234					*claim_id,
1235					*target_feerate_sat_per_1000_weight,
1236					htlc_descriptors,
1237					*tx_lock_time,
1238				)
1239				.await
1240				.unwrap_or_else(|_| {
1241					log_error!(
1242						self.logger,
1243						"Failed bumping HTLC transaction fee for commitment {}",
1244						htlc_descriptors[0].commitment_txid
1245					);
1246				});
1247			},
1248		}
1249	}
1250}
1251
1252#[cfg(test)]
1253mod tests {
1254	use super::*;
1255
1256	use crate::events::bump_transaction::sync::{
1257		BumpTransactionEventHandlerSync, CoinSelectionSourceSync,
1258	};
1259	use crate::io::Cursor;
1260	use crate::ln::chan_utils::ChannelTransactionParameters;
1261	use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
1262	use crate::sign::KeysManager;
1263	use crate::types::features::ChannelTypeFeatures;
1264	use crate::util::ser::Readable;
1265	use crate::util::test_utils::{TestBroadcaster, TestLogger};
1266
1267	use bitcoin::hashes::Hash;
1268	use bitcoin::hex::FromHex;
1269	use bitcoin::{Network, ScriptBuf, Transaction, Txid};
1270
1271	struct TestCoinSelectionSource {
1272		// (commitment + anchor value, commitment + input weight, target feerate, result)
1273		expected_selects: Mutex<Vec<(u64, u64, u32, CoinSelection)>>,
1274	}
1275	impl CoinSelectionSourceSync for TestCoinSelectionSource {
1276		fn select_confirmed_utxos(
1277			&self, _claim_id: ClaimId, must_spend: Vec<Input>, _must_pay_to: &[TxOut],
1278			target_feerate_sat_per_1000_weight: u32, _max_tx_weight: u64,
1279		) -> Result<CoinSelection, ()> {
1280			let mut expected_selects = self.expected_selects.lock().unwrap();
1281			let (weight, value, feerate, res) = expected_selects.remove(0);
1282			assert_eq!(must_spend.len(), 1);
1283			assert_eq!(must_spend[0].satisfaction_weight, weight);
1284			assert_eq!(must_spend[0].previous_utxo.value.to_sat(), value);
1285			assert_eq!(target_feerate_sat_per_1000_weight, feerate);
1286			Ok(res)
1287		}
1288		fn sign_psbt(&self, psbt: Psbt) -> Result<Transaction, ()> {
1289			let mut tx = psbt.unsigned_tx;
1290			for input in tx.input.iter_mut() {
1291				if input.previous_output.txid != Txid::from_byte_array([44; 32]) {
1292					// Channel output, add a realistic size witness to make the assertions happy
1293					input.witness = Witness::from_slice(&[vec![42; 162]]);
1294				}
1295			}
1296			Ok(tx)
1297		}
1298	}
1299
1300	impl Drop for TestCoinSelectionSource {
1301		fn drop(&mut self) {
1302			assert!(self.expected_selects.lock().unwrap().is_empty());
1303		}
1304	}
1305
1306	#[test]
1307	fn test_op_return_under_funds() {
1308		// Test what happens if we have to select coins but the anchor output value itself suffices
1309		// to pay the required fee.
1310		//
1311		// This tests a case that occurred on mainnet (with the below transaction) where the target
1312		// feerate (of 868 sat/kW) was met by the anchor output's 330 sats alone. This caused the
1313		// use of an OP_RETURN which created a transaction which, at the time, was less than 64
1314		// bytes long (the current code generates a 65 byte transaction instead to meet
1315		// standardness rule). It also tests the handling of selection failure where we selected
1316		// coins which were insufficient once the OP_RETURN output was added, causing us to need to
1317		// select coins again with additional weight.
1318
1319		// Tx 18032ad172a5f28fa6e16392d6cc57ea47895781434ce15d03766cc47a955fb9
1320		let commitment_tx_bytes = Vec::<u8>::from_hex("02000000000101cc6b0a9dd84b52c07340fff6fab002fc37b4bdccfdce9f39c5ec8391a56b652907000000009b948b80044a01000000000000220020b4182433fdfdfbf894897c98f84d92cec815cee222755ffd000ae091c9dadc2d4a01000000000000220020f83f7dbf90e2de325b5bb6bab0ae370151278c6964739242b2e7ce0cb68a5d81cb4a02000000000022002024add256b3dccee772610caef82a601045ab6f98fd6d5df608cc756b891ccfe63ffa490000000000220020894bf32b37906a643625e87131897c3714c71b3ac9b161862c9aa6c8d468b4c70400473044022060abd347bff2cca0212b660e6addff792b3356bd4a1b5b26672dc2e694c3c5f002202b40b7e346b494a7b1d048b4ec33ba99c90a09ab48eb1df64ccdc768066c865c014730440220554d8361e04dc0ee178dcb23d2d23f53ec7a1ae4312a5be76bd9e83ab8981f3d0220501f23ffb18cb81ccea72d30252f88d5e69fd28ba4992803d03c00d06fa8899e0147522102817f6ce189ab7114f89e8d5df58cdbbaf272dc8e71b92982d47456a0b6a0ceee2102c9b4d2f24aca54f65e13f4c83e2a8d8e877e12d3c71a76e81f28a5cabc652aa352ae626c7620").unwrap();
1321		let commitment_tx: Transaction =
1322			Readable::read(&mut Cursor::new(&commitment_tx_bytes)).unwrap();
1323		let commitment_txid = commitment_tx.compute_txid();
1324		let total_commitment_weight =
1325			commitment_tx.weight().to_wu() + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT;
1326		let commitment_and_anchor_fee = 930 + 330;
1327		let op_return_weight =
1328			TxOut { value: Amount::ZERO, script_pubkey: ScriptBuf::new_op_return(&[0; 3]) }
1329				.weight()
1330				.to_wu();
1331
1332		let broadcaster = TestBroadcaster::new(Network::Testnet);
1333		let source = TestCoinSelectionSource {
1334			expected_selects: Mutex::new(vec![
1335				(
1336					total_commitment_weight,
1337					commitment_and_anchor_fee,
1338					868,
1339					CoinSelection { confirmed_utxos: Vec::new(), change_output: None },
1340				),
1341				(
1342					total_commitment_weight + op_return_weight,
1343					commitment_and_anchor_fee,
1344					868,
1345					CoinSelection {
1346						confirmed_utxos: vec![Utxo {
1347							outpoint: OutPoint { txid: Txid::from_byte_array([44; 32]), vout: 0 },
1348							output: TxOut {
1349								value: Amount::from_sat(200),
1350								script_pubkey: ScriptBuf::new(),
1351							},
1352							satisfaction_weight: 5, // Just the script_sig and witness lengths
1353						}],
1354						change_output: None,
1355					},
1356				),
1357			]),
1358		};
1359		let signer = KeysManager::new(&[42; 32], 42, 42, true);
1360		let logger = TestLogger::new();
1361		let handler = BumpTransactionEventHandlerSync::new(&broadcaster, &source, &signer, &logger);
1362
1363		let mut transaction_parameters = ChannelTransactionParameters::test_dummy(42_000_000);
1364		transaction_parameters.channel_type_features =
1365			ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
1366
1367		handler.handle_event(&BumpTransactionEvent::ChannelClose {
1368			channel_id: ChannelId([42; 32]),
1369			counterparty_node_id: PublicKey::from_slice(&[2; 33]).unwrap(),
1370			claim_id: ClaimId([42; 32]),
1371			package_target_feerate_sat_per_1000_weight: 868,
1372			commitment_tx_fee_satoshis: 930,
1373			commitment_tx,
1374			anchor_descriptor: AnchorDescriptor {
1375				channel_derivation_parameters: ChannelDerivationParameters {
1376					value_satoshis: 42_000_000,
1377					keys_id: [42; 32],
1378					transaction_parameters,
1379				},
1380				outpoint: OutPoint { txid: commitment_txid, vout: 0 },
1381				value: Amount::from_sat(ANCHOR_OUTPUT_VALUE_SATOSHI),
1382			},
1383			pending_htlcs: Vec::new(),
1384		});
1385	}
1386}