Skip to content

Commit 32da8df

Browse files
committed
Add new sub-module for BumpTransactionEvent
Its accompanying event handler will also live here.
1 parent 3d85e59 commit 32da8df

File tree

2 files changed

+233
-225
lines changed

2 files changed

+233
-225
lines changed
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
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+
use crate::ln::PaymentPreimage;
11+
use crate::ln::chan_utils;
12+
use crate::ln::chan_utils::{ChannelTransactionParameters, HTLCOutputInCommitment};
13+
14+
use bitcoin::{OutPoint, Script, Transaction, Txid, TxIn, TxOut, Witness};
15+
use bitcoin::secp256k1;
16+
use bitcoin::secp256k1::{PublicKey, Secp256k1};
17+
use bitcoin::secp256k1::ecdsa::Signature;
18+
19+
/// A descriptor used to sign for a commitment transaction's anchor output.
20+
#[derive(Clone, Debug, PartialEq, Eq)]
21+
pub struct AnchorDescriptor {
22+
/// A unique identifier used along with `channel_value_satoshis` to re-derive the
23+
/// [`InMemorySigner`] required to sign `input`.
24+
///
25+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
26+
pub channel_keys_id: [u8; 32],
27+
/// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
28+
/// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
29+
/// `input`.
30+
///
31+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
32+
pub channel_value_satoshis: u64,
33+
/// The transaction input's outpoint corresponding to the commitment transaction's anchor
34+
/// output.
35+
pub outpoint: OutPoint,
36+
}
37+
38+
/// A descriptor used to sign for a commitment transaction's HTLC output.
39+
#[derive(Clone, Debug, PartialEq, Eq)]
40+
pub struct HTLCDescriptor {
41+
/// A unique identifier used along with `channel_value_satoshis` to re-derive the
42+
/// [`InMemorySigner`] required to sign `input`.
43+
///
44+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
45+
pub channel_keys_id: [u8; 32],
46+
/// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
47+
/// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
48+
/// `input`.
49+
///
50+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
51+
pub channel_value_satoshis: u64,
52+
/// The necessary channel parameters that need to be provided to the re-derived
53+
/// [`InMemorySigner`] through [`ChannelSigner::provide_channel_parameters`].
54+
///
55+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
56+
/// [`ChannelSigner::provide_channel_parameters`]: crate::chain::keysinterface::ChannelSigner::provide_channel_parameters
57+
pub channel_parameters: ChannelTransactionParameters,
58+
/// The txid of the commitment transaction in which the HTLC output lives.
59+
pub commitment_txid: Txid,
60+
/// The number of the commitment transaction in which the HTLC output lives.
61+
pub per_commitment_number: u64,
62+
/// The details of the HTLC as it appears in the commitment transaction.
63+
pub htlc: HTLCOutputInCommitment,
64+
/// The preimage, if `Some`, to claim the HTLC output with. If `None`, the timeout path must be
65+
/// taken.
66+
pub preimage: Option<PaymentPreimage>,
67+
/// The counterparty's signature required to spend the HTLC output.
68+
pub counterparty_sig: Signature
69+
}
70+
71+
impl HTLCDescriptor {
72+
/// Returns the unsigned transaction input spending the HTLC output in the commitment
73+
/// transaction.
74+
pub fn unsigned_tx_input(&self) -> TxIn {
75+
chan_utils::build_htlc_input(&self.commitment_txid, &self.htlc, true /* opt_anchors */)
76+
}
77+
78+
/// Returns the delayed output created as a result of spending the HTLC output in the commitment
79+
/// transaction.
80+
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(
81+
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
82+
) -> TxOut {
83+
let channel_params = self.channel_parameters.as_holder_broadcastable();
84+
let broadcaster_keys = channel_params.broadcaster_pubkeys();
85+
let counterparty_keys = channel_params.countersignatory_pubkeys();
86+
let broadcaster_delayed_key = chan_utils::derive_public_key(
87+
secp, per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
88+
);
89+
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
90+
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
91+
);
92+
chan_utils::build_htlc_output(
93+
0 /* feerate_per_kw */, channel_params.contest_delay(), &self.htlc, true /* opt_anchors */,
94+
false /* use_non_zero_fee_anchors */, &broadcaster_delayed_key, &counterparty_revocation_key
95+
)
96+
}
97+
98+
/// Returns the witness script of the HTLC output in the commitment transaction.
99+
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(
100+
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
101+
) -> Script {
102+
let channel_params = self.channel_parameters.as_holder_broadcastable();
103+
let broadcaster_keys = channel_params.broadcaster_pubkeys();
104+
let counterparty_keys = channel_params.countersignatory_pubkeys();
105+
let broadcaster_htlc_key = chan_utils::derive_public_key(
106+
secp, per_commitment_point, &broadcaster_keys.htlc_basepoint
107+
);
108+
let counterparty_htlc_key = chan_utils::derive_public_key(
109+
secp, per_commitment_point, &counterparty_keys.htlc_basepoint
110+
);
111+
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
112+
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
113+
);
114+
chan_utils::get_htlc_redeemscript_with_explicit_keys(
115+
&self.htlc, true /* opt_anchors */, &broadcaster_htlc_key, &counterparty_htlc_key,
116+
&counterparty_revocation_key,
117+
)
118+
}
119+
120+
/// Returns the fully signed witness required to spend the HTLC output in the commitment
121+
/// transaction.
122+
pub fn tx_input_witness(&self, signature: &Signature, witness_script: &Script) -> Witness {
123+
chan_utils::build_htlc_input_witness(
124+
signature, &self.counterparty_sig, &self.preimage, witness_script, true /* opt_anchors */
125+
)
126+
}
127+
}
128+
129+
/// Represents the different types of transactions, originating from LDK, to be bumped.
130+
#[derive(Clone, Debug, PartialEq, Eq)]
131+
pub enum BumpTransactionEvent {
132+
/// Indicates that a channel featuring anchor outputs is to be closed by broadcasting the local
133+
/// commitment transaction. Since commitment transactions have a static feerate pre-agreed upon,
134+
/// they may need additional fees to be attached through a child transaction using the popular
135+
/// [Child-Pays-For-Parent](https://bitcoinops.org/en/topics/cpfp) fee bumping technique. This
136+
/// child transaction must include the anchor input described within `anchor_descriptor` along
137+
/// with additional inputs to meet the target feerate. Failure to meet the target feerate
138+
/// decreases the confirmation odds of the transaction package (which includes the commitment
139+
/// and child anchor transactions), possibly resulting in a loss of funds. Once the transaction
140+
/// is constructed, it must be fully signed for and broadcast by the consumer of the event
141+
/// along with the `commitment_tx` enclosed. Note that the `commitment_tx` must always be
142+
/// broadcast first, as the child anchor transaction depends on it.
143+
///
144+
/// The consumer should be able to sign for any of the additional inputs included within the
145+
/// child anchor transaction. To sign its anchor input, an [`InMemorySigner`] should be
146+
/// re-derived through [`KeysManager::derive_channel_keys`] with the help of
147+
/// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`]. The
148+
/// anchor input signature can be computed with [`EcdsaChannelSigner::sign_holder_anchor_input`],
149+
/// which can then be provided to [`build_anchor_input_witness`] along with the `funding_pubkey`
150+
/// to obtain the full witness required to spend.
151+
///
152+
/// It is possible to receive more than one instance of this event if a valid child anchor
153+
/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
154+
/// be taken by the consumer of the event to ensure any future iterations of the child anchor
155+
/// transaction adhere to the [Replace-By-Fee
156+
/// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
157+
/// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
158+
/// these events is not user-controlled, users may ignore/drop the event if they are no longer
159+
/// able to commit external confirmed funds to the child anchor transaction.
160+
///
161+
/// The set of `pending_htlcs` on the commitment transaction to be broadcast can be inspected to
162+
/// determine whether a significant portion of the channel's funds are allocated to HTLCs,
163+
/// enabling users to make their own decisions regarding the importance of the commitment
164+
/// transaction's confirmation. Note that this is not required, but simply exists as an option
165+
/// for users to override LDK's behavior. On commitments with no HTLCs (indicated by those with
166+
/// an empty `pending_htlcs`), confirmation of the commitment transaction can be considered to
167+
/// be not urgent.
168+
///
169+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
170+
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
171+
/// [`EcdsaChannelSigner::sign_holder_anchor_input`]: crate::chain::keysinterface::EcdsaChannelSigner::sign_holder_anchor_input
172+
/// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
173+
ChannelClose {
174+
/// The target feerate that the transaction package, which consists of the commitment
175+
/// transaction and the to-be-crafted child anchor transaction, must meet.
176+
package_target_feerate_sat_per_1000_weight: u32,
177+
/// The channel's commitment transaction to bump the fee of. This transaction should be
178+
/// broadcast along with the anchor transaction constructed as a result of consuming this
179+
/// event.
180+
commitment_tx: Transaction,
181+
/// The absolute fee in satoshis of the commitment transaction. This can be used along the
182+
/// with weight of the commitment transaction to determine its feerate.
183+
commitment_tx_fee_satoshis: u64,
184+
/// The descriptor to sign the anchor input of the anchor transaction constructed as a
185+
/// result of consuming this event.
186+
anchor_descriptor: AnchorDescriptor,
187+
/// The set of pending HTLCs on the commitment transaction that need to be resolved once the
188+
/// commitment transaction confirms.
189+
pending_htlcs: Vec<HTLCOutputInCommitment>,
190+
},
191+
/// Indicates that a channel featuring anchor outputs has unilaterally closed on-chain by a
192+
/// holder commitment transaction and its HTLC(s) need to be resolved on-chain. With the
193+
/// zero-HTLC-transaction-fee variant of anchor outputs, the pre-signed HTLC
194+
/// transactions have a zero fee, thus requiring additional inputs and/or outputs to be attached
195+
/// for a timely confirmation within the chain. These additional inputs and/or outputs must be
196+
/// appended to the resulting HTLC transaction to meet the target feerate. Failure to meet the
197+
/// target feerate decreases the confirmation odds of the transaction, possibly resulting in a
198+
/// loss of funds. Once the transaction meets the target feerate, it must be signed for and
199+
/// broadcast by the consumer of the event.
200+
///
201+
/// The consumer should be able to sign for any of the non-HTLC inputs added to the resulting
202+
/// HTLC transaction. To sign HTLC inputs, an [`InMemorySigner`] should be re-derived through
203+
/// [`KeysManager::derive_channel_keys`] with the help of `channel_keys_id` and
204+
/// `channel_value_satoshis`. Each HTLC input's signature can be computed with
205+
/// [`EcdsaChannelSigner::sign_holder_htlc_transaction`], which can then be provided to
206+
/// [`HTLCDescriptor::tx_input_witness`] to obtain the fully signed witness required to spend.
207+
///
208+
/// It is possible to receive more than one instance of this event if a valid HTLC transaction
209+
/// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
210+
/// the consumer of the event to ensure any future iterations of the HTLC transaction adhere to
211+
/// the [Replace-By-Fee
212+
/// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
213+
/// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
214+
/// these events is not user-controlled, users may ignore/drop the event if either they are no
215+
/// longer able to commit external confirmed funds to the HTLC transaction or the fee committed
216+
/// to the HTLC transaction is greater in value than the HTLCs being claimed.
217+
///
218+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
219+
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
220+
/// [`EcdsaChannelSigner::sign_holder_htlc_transaction`]: crate::chain::keysinterface::EcdsaChannelSigner::sign_holder_htlc_transaction
221+
/// [`HTLCDescriptor::tx_input_witness`]: HTLCDescriptor::tx_input_witness
222+
HTLCResolution {
223+
/// The target feerate that the resulting HTLC transaction must meet.
224+
target_feerate_sat_per_1000_weight: u32,
225+
/// The set of pending HTLCs on the confirmed commitment that need to be claimed, preferably
226+
/// by the same transaction.
227+
htlc_descriptors: Vec<HTLCDescriptor>,
228+
tx_lock_time: PackedLockTime,
229+
},
230+
}

0 commit comments

Comments
 (0)