Skip to content

Commit 8170c84

Browse files
committed
Yield BumpHTLCResolution events
1 parent c17ce2e commit 8170c84

File tree

3 files changed

+168
-10
lines changed

3 files changed

+168
-10
lines changed

lightning/src/chain/channelmonitor.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable,
5353
use crate::util::byte_utils;
5454
use crate::util::events::Event;
5555
#[cfg(anchors)]
56-
use crate::util::events::{AnchorDescriptor, BumpTransactionEvent};
56+
use crate::util::events::{AnchorDescriptor, HTLCDescriptor, BumpTransactionEvent};
5757

5858
use crate::prelude::*;
5959
use core::{cmp, mem};
@@ -2425,7 +2425,27 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
24252425
pending_htlcs,
24262426
}));
24272427
},
2428-
_ => {},
2428+
ClaimEvent::BumpHTLC {
2429+
target_feerate_sat_per_1000_weight, htlcs,
2430+
} => {
2431+
let mut htlc_descriptors = Vec::with_capacity(htlcs.len());
2432+
for htlc in htlcs {
2433+
htlc_descriptors.push(HTLCDescriptor {
2434+
channel_keys_id: self.channel_keys_id,
2435+
channel_value_satoshis: self.channel_value_satoshis,
2436+
channel_parameters: self.onchain_tx_handler.channel_transaction_parameters.clone(),
2437+
commitment_txid: htlc.commitment_txid,
2438+
per_commitment_number: htlc.per_commitment_number,
2439+
htlc: htlc.htlc,
2440+
preimage: htlc.preimage,
2441+
counterparty_sig: htlc.counterparty_sig,
2442+
});
2443+
}
2444+
ret.push(Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
2445+
target_feerate_sat_per_1000_weight,
2446+
htlc_descriptors,
2447+
}));
2448+
}
24292449
}
24302450
}
24312451
ret

lightning/src/ln/chan_utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl_writeable_tlv_based!(TxCreationKeys, {
462462
});
463463

464464
/// One counterparty's public keys which do not change over the life of a channel.
465-
#[derive(Clone, PartialEq, Eq)]
465+
#[derive(Clone, Debug, PartialEq, Eq)]
466466
pub struct ChannelPublicKeys {
467467
/// The public key which is used to sign all commitment transactions, as it appears in the
468468
/// on-chain channel lock-in 2-of-2 multisig output.
@@ -811,7 +811,7 @@ pub fn build_anchor_input_witness(funding_key: &PublicKey, funding_sig: &Signatu
811811
///
812812
/// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters
813813
/// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions.
814-
#[derive(Clone, PartialEq)]
814+
#[derive(Clone, Debug, PartialEq)]
815815
pub struct ChannelTransactionParameters {
816816
/// Holder public keys
817817
pub holder_pubkeys: ChannelPublicKeys,
@@ -835,7 +835,7 @@ pub struct ChannelTransactionParameters {
835835
}
836836

837837
/// Late-bound per-channel counterparty data used to build transactions.
838-
#[derive(Clone, PartialEq)]
838+
#[derive(Clone, Debug, PartialEq)]
839839
pub struct CounterpartyChannelTransactionParameters {
840840
/// Counter-party public keys
841841
pub pubkeys: ChannelPublicKeys,

lightning/src/util/events.rs

+143-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
1717
use crate::chain::keysinterface::SpendableOutputDescriptor;
1818
#[cfg(anchors)]
19-
use crate::ln::chan_utils::HTLCOutputInCommitment;
19+
use crate::ln::chan_utils::{self, ChannelTransactionParameters, HTLCOutputInCommitment};
2020
use crate::ln::channelmanager::{InterceptId, PaymentId};
2121
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
2222
use crate::ln::features::ChannelTypeFeatures;
@@ -29,11 +29,15 @@ use crate::routing::router::{RouteHop, RouteParameters};
2929

3030
use bitcoin::{PackedLockTime, Transaction};
3131
#[cfg(anchors)]
32-
use bitcoin::OutPoint;
32+
use bitcoin::{OutPoint, Txid, TxIn, TxOut, Witness};
3333
use bitcoin::blockdata::script::Script;
3434
use bitcoin::hashes::Hash;
3535
use bitcoin::hashes::sha256::Hash as Sha256;
3636
use bitcoin::secp256k1::PublicKey;
37+
#[cfg(anchors)]
38+
use bitcoin::secp256k1::{self, Secp256k1};
39+
#[cfg(anchors)]
40+
use bitcoin::secp256k1::ecdsa::Signature;
3741
use crate::io;
3842
use crate::prelude::*;
3943
use core::time::Duration;
@@ -237,6 +241,99 @@ pub struct AnchorDescriptor {
237241
pub outpoint: OutPoint,
238242
}
239243

244+
#[cfg(anchors)]
245+
/// A descriptor used to sign for a commitment transaction's HTLC output.
246+
#[derive(Clone, Debug)]
247+
pub struct HTLCDescriptor {
248+
/// A unique identifier used along with `channel_value_satoshis` to re-derive the
249+
/// [`InMemorySigner`] required to sign `input`.
250+
///
251+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
252+
pub channel_keys_id: [u8; 32],
253+
/// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
254+
/// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
255+
/// `input`.
256+
///
257+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
258+
pub channel_value_satoshis: u64,
259+
/// The necessary channel parameters that need to be provided to the re-derived
260+
/// [`InMemorySigner`] through [`BaseSign::ready_channel`].
261+
///
262+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
263+
/// [`BaseSign::ready_channel`]: crate::chain::keysinterface::BaseSign::ready_channel
264+
pub channel_parameters: ChannelTransactionParameters,
265+
/// The txid of the commitment transaction in which the HTLC output lives.
266+
pub commitment_txid: Txid,
267+
/// The number of the commitment transaction in which the HTLC output lives.
268+
pub per_commitment_number: u64,
269+
/// The details of the HTLC as it appears in the commitment transaction.
270+
pub htlc: HTLCOutputInCommitment,
271+
/// The preimage, if `Some`, to claim the HTLC output with. If `None`, the timeout path must be
272+
/// taken.
273+
pub preimage: Option<PaymentPreimage>,
274+
/// The counterparty's signature required to spend the HTLC output.
275+
pub counterparty_sig: Signature
276+
}
277+
278+
#[cfg(anchors)]
279+
impl HTLCDescriptor {
280+
/// Returns the unsigned transaction input spending the HTLC output in the commitment
281+
/// transaction.
282+
pub fn unsigned_tx_input(&self) -> TxIn {
283+
chan_utils::build_htlc_input(&self.commitment_txid, &self.htlc, true /* opt_anchors */)
284+
}
285+
286+
/// Returns the delayed output created as a result of spending the HTLC output in the commitment
287+
/// transaction.
288+
pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(
289+
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
290+
) -> TxOut {
291+
let channel_params = self.channel_parameters.as_holder_broadcastable();
292+
let broadcaster_keys = channel_params.broadcaster_pubkeys();
293+
let counterparty_keys = channel_params.countersignatory_pubkeys();
294+
let broadcaster_delayed_key = chan_utils::derive_public_key(
295+
secp, per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
296+
);
297+
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
298+
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
299+
);
300+
chan_utils::build_htlc_output(
301+
0 /* feerate_per_kw */, channel_params.contest_delay(), &self.htlc, true /* opt_anchors */,
302+
false /* use_non_zero_fee_anchors */, &broadcaster_delayed_key, &counterparty_revocation_key
303+
)
304+
}
305+
306+
/// Returns the witness script of the HTLC output in the commitment transaction.
307+
pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(
308+
&self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
309+
) -> Script {
310+
let channel_params = self.channel_parameters.as_holder_broadcastable();
311+
let broadcaster_keys = channel_params.broadcaster_pubkeys();
312+
let counterparty_keys = channel_params.countersignatory_pubkeys();
313+
let broadcaster_htlc_key = chan_utils::derive_public_key(
314+
secp, per_commitment_point, &broadcaster_keys.htlc_basepoint
315+
);
316+
let counterparty_htlc_key = chan_utils::derive_public_key(
317+
secp, per_commitment_point, &counterparty_keys.htlc_basepoint
318+
);
319+
let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
320+
secp, per_commitment_point, &counterparty_keys.revocation_basepoint
321+
);
322+
chan_utils::get_htlc_redeemscript_with_explicit_keys(
323+
&self.htlc, true /* opt_anchors */, &broadcaster_htlc_key, &counterparty_htlc_key,
324+
&counterparty_revocation_key,
325+
)
326+
}
327+
328+
/// Returns the fully signed witness required to spend the HTLC output in the commitment
329+
/// transaction.
330+
pub fn tx_input_witness(&self, signature: &Signature, witness_script: &Script) -> Witness {
331+
chan_utils::build_htlc_input_witness(
332+
signature, &self.counterparty_sig, &self.preimage, witness_script, true /* opt_anchors */
333+
)
334+
}
335+
}
336+
240337
#[cfg(anchors)]
241338
/// Represents the different types of transactions, originating from LDK, to be bumped.
242339
#[derive(Clone, Debug)]
@@ -256,7 +353,10 @@ pub enum BumpTransactionEvent {
256353
/// The consumer should be able to sign for any of the additional inputs included within the
257354
/// child anchor transaction. To sign its anchor input, an [`InMemorySigner`] should be
258355
/// re-derived through [`KeysManager::derive_channel_keys`] with the help of
259-
/// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`].
356+
/// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`]. The
357+
/// anchor input signature can be computed with [`BaseSign::sign_holder_anchor_input`],
358+
/// which can then be provided to [`build_anchor_input_witness`] along with the `funding_pubkey`
359+
/// to obtain the full witness required to spend.
260360
///
261361
/// It is possible to receive more than one instance of this event if a valid child anchor
262362
/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
@@ -277,6 +377,8 @@ pub enum BumpTransactionEvent {
277377
///
278378
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
279379
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
380+
/// [`BaseSign::sign_holder_anchor_input`]: crate::chain::keysinterface::BaseSign::sign_holder_anchor_input
381+
/// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
280382
ChannelClose {
281383
/// The target feerate that the transaction package, which consists of the commitment
282384
/// transaction and the to-be-crafted child anchor transaction, must meet.
@@ -295,6 +397,41 @@ pub enum BumpTransactionEvent {
295397
/// commitment transaction confirms.
296398
pending_htlcs: Vec<HTLCOutputInCommitment>,
297399
},
400+
/// Indicates that a channel featuring anchor outputs has unilaterally closed on-chain by a
401+
/// holder commitment transaction and its HTLC(s) need to be resolved on-chain. With the
402+
/// zero-HTLC-transaction-fee variant of anchor outputs, the pre-signed HTLC
403+
/// transactions have a zero fee, thus requiring additional inputs and/or outputs to be attached
404+
/// for a timely confirmation within the chain. These additional inputs and/or outputs must be
405+
/// appended to the resulting HTLC transaction to meet the target feerate. Failure to meet the
406+
/// target feerate decreases the confirmation odds of the transaction, possibly resulting in a
407+
/// loss of funds. Once the transaction meets the target feerate, it must be signed for and
408+
/// broadcast by the consumer of the event.
409+
///
410+
/// The consumer should be able to sign for any of the non-HTLC inputs added to the resulting
411+
/// HTLC transaction. To sign HTLC inputs, an [`InMemorySigner`] should be re-derived through
412+
/// [`KeysManager::derive_channel_keys`] with the help of `channel_keys_id` and
413+
/// `channel_value_satoshis`. Each HTLC input's signature can be computed with
414+
/// [`BaseSign::sign_holder_htlc_transaction`], which can then be provided to
415+
/// [`HTLCDescriptor::tx_input_witness`] to obtain the fully signed witness required to spend.
416+
///
417+
/// It is possible to receive more than one instance of this event if a valid HTLC transaction
418+
/// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
419+
/// the consumer of the event to ensure any future iterations of the HTLC transaction adhere to
420+
/// the [Replace-By-Fee
421+
/// rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
422+
/// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
423+
/// these events is not user-controlled, users may ignore/drop the event if either they are no
424+
/// longer able to commit external confirmed funds to the HTLC transaction or the fee committed
425+
/// to the HTLC transaction is greater in value than the HTLCs being claimed.
426+
///
427+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
428+
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
429+
/// [`BaseSign::sign_holder_htlc_transaction`]: crate::chain::keysinterface::BaseSign::sign_holder_htlc_transaction
430+
/// [`HTLCDescriptor::tx_input_witness`]: HTLCDescriptor::tx_input_witness
431+
HTLCResolution {
432+
target_feerate_sat_per_1000_weight: u32,
433+
htlc_descriptors: Vec<HTLCDescriptor>,
434+
},
298435
}
299436

300437
/// Will be used in [`Event::HTLCIntercepted`] to identify the next hop in the HTLC's path.
@@ -983,9 +1120,10 @@ impl Writeable for Event {
9831120
&Event::BumpTransaction(ref event)=> {
9841121
27u8.write(writer)?;
9851122
match event {
986-
// We never write the ChannelClose events as they'll be replayed upon restarting
987-
// anyway if the commitment transaction remains unconfirmed.
1123+
// We never write the ChannelClose|HTLCResolution events as they'll be replayed
1124+
// upon restarting anyway if they remain unresolved.
9881125
BumpTransactionEvent::ChannelClose { .. } => {}
1126+
BumpTransactionEvent::HTLCResolution { .. } => {}
9891127
}
9901128
}
9911129
&Event::ChannelReady { ref channel_id, ref user_channel_id, ref counterparty_node_id, ref channel_type } => {

0 commit comments

Comments
 (0)