Skip to content

Commit f5ad64a

Browse files
committed
Yield BumpHTLCResolution events
1 parent 489de0e commit f5ad64a

File tree

2 files changed

+99
-5
lines changed

2 files changed

+99
-5
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 23 additions & 2 deletions
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};
@@ -2378,7 +2378,28 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23782378
pending_htlcs,
23792379
}));
23802380
},
2381-
_ => {},
2381+
ClaimEvent::BumpHTLC {
2382+
target_feerate_sat_per_1000_weight, tx_template, htlcs,
2383+
} => {
2384+
let mut htlc_descriptors = Vec::with_capacity(htlcs.len());
2385+
for htlc in htlcs {
2386+
htlc_descriptors.push(HTLCDescriptor {
2387+
channel_keys_id: self.channel_keys_id,
2388+
channel_value_satoshis: self.channel_value_satoshis,
2389+
per_commitment_number: htlc.per_commitment_number,
2390+
htlc: htlc.htlc,
2391+
preimage: htlc.preimage,
2392+
counterparty_base_htlc_key: htlc.counterparty_base_htlc_key,
2393+
counterparty_base_revocation_key: htlc.counterparty_base_revocation_key,
2394+
counterparty_sig: htlc.counterparty_sig,
2395+
});
2396+
}
2397+
ret.push(Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
2398+
target_feerate_sat_per_1000_weight,
2399+
tx_template,
2400+
htlc_descriptors,
2401+
}));
2402+
}
23822403
}
23832404
}
23842405
ret

lightning/src/util/events.rs

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ 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::ecdsa::Signature;
3739
use crate::io;
3840
use crate::prelude::*;
3941
use core::time::Duration;
@@ -228,6 +230,37 @@ pub struct AnchorDescriptor {
228230
pub outpoint: OutPoint,
229231
}
230232

233+
#[cfg(anchors)]
234+
/// A descriptor used to sign for a commitment transaction's HTLC output.
235+
#[derive(Clone, Debug)]
236+
pub struct HTLCDescriptor {
237+
/// A unique identifier used along with `channel_value_satoshis` to re-derive the
238+
/// [`InMemorySigner`] required to sign `input`.
239+
///
240+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
241+
pub channel_keys_id: [u8; 32],
242+
/// The value in satoshis of the channel we're attempting to spend the anchor output of. This is
243+
/// used along with `channel_keys_id` to re-derive the [`InMemorySigner`] required to sign
244+
/// `input`.
245+
///
246+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
247+
pub channel_value_satoshis: u64,
248+
/// The number of the commitment transaction in which the HTLC output lives.
249+
pub per_commitment_number: u64,
250+
/// The details of the HTLC as it appears in a commitment transaction.
251+
pub htlc: HTLCOutputInCommitment,
252+
/// The preimage, if one exists, to claim the HTLC output with.
253+
pub preimage: Option<PaymentPreimage>,
254+
/// The counterparty's base HTLC key of the channel. This is required to reconstruct the HTLC
255+
/// output's witness script.
256+
pub counterparty_base_htlc_key: PublicKey,
257+
/// The counterparty's base revocation key of the channel. This is required to reconstruct the
258+
/// HTLC output's witness script.
259+
pub counterparty_base_revocation_key: PublicKey,
260+
/// The counterparty's signature required to spend the HTLC output.
261+
pub counterparty_sig: Signature
262+
}
263+
231264
#[cfg(anchors)]
232265
/// Represents the different types of transactions, originating from LDK, to be bumped.
233266
#[derive(Clone, Debug)]
@@ -247,7 +280,10 @@ pub enum BumpTransactionEvent {
247280
/// The consumer should be able to sign for any of the additional inputs included within the
248281
/// child anchor transaction. To sign its anchor input, an [`InMemorySigner`] should be
249282
/// re-derived through [`KeysManager::derive_channel_keys`] with the help of
250-
/// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`].
283+
/// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`]. The
284+
/// anchor input signature can be computed with [`InMemorySigner::sign_holder_anchor_input`],
285+
/// which can then be provided to [`build_anchor_input_witness`] along with the `funding_pubkey`
286+
/// to obtain the full witness required to spend.
251287
///
252288
/// It is possible to receive more than one instance of this event if a valid child anchor
253289
/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
@@ -268,6 +304,8 @@ pub enum BumpTransactionEvent {
268304
///
269305
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
270306
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
307+
/// [`InMemorySigner::sign_holder_anchor_input`]: crate::chain::keysinterface::InMemorySigner::sign_holder_anchor_input
308+
/// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
271309
ChannelClose {
272310
/// The target feerate that the transaction package, which consists of the commitment
273311
/// transaction and the to-be-crafted child anchor transaction, must meet.
@@ -286,6 +324,40 @@ pub enum BumpTransactionEvent {
286324
/// commitment transaction confirms.
287325
pending_htlcs: Vec<HTLCOutputInCommitment>,
288326
},
327+
/// Indicates that a channel featuring anchor outputs has HTLC(s) that need to be resolved
328+
/// onchain. With the zero-HTLC-transaction-fee variant of anchor outputs, the pre-signed HTLC
329+
/// transactions have a zero fee, thus requiring additional inputs and/or outputs to be attached
330+
/// for a timely confirmation within the chain. These additional inputs and/or outputs must be
331+
/// appended to the enclosed `tx_template` to meet the target feerate. Failure to meet the
332+
/// target feerate decreases the confirmation odds of the transaction, possibly resulting in a
333+
/// loss of funds. Once the transaction is amended to meet the target feerate, it must be signed
334+
/// for and broadcast by the consumer of the event.
335+
///
336+
/// The consumer should be able to sign for any of the additional inputs added to `tx_template`.
337+
/// To sign HTLC inputs, an [`InMemorySigner`] should be re-derived through
338+
/// [`KeysManager::derive_channel_keys`] with the help of `channel_keys_id` and
339+
/// `channel_value_satoshis`. Each HTLC input's signature can be computed with
340+
/// [`InMemorySigner::sign_holder_htlc_transaction`], which can then be provided to
341+
/// [`build_htlc_input_witness`] along with the enclosed [`HTLCDescriptor`] to obtain the full
342+
/// witness required to spend.
343+
///
344+
/// It is possible to receive more than one instance of this event if a valid HTLC transaction
345+
/// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
346+
/// the consumer of the event to ensure any future iterations of the HTLC transaction adhere to
347+
/// the [Replace-By-Fee rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
348+
/// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
349+
/// these events is not user-controlled, users may ignore/drop the event if they are no longer
350+
/// able to commit external confirmed funds to the HTLC transaction.
351+
///
352+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
353+
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
354+
/// [`InMemorySigner::sign_holder_htlc_transaction`]: crate::chain::keysinterface::InMemorySigner::sign_holder_htlc_transaction
355+
/// [`build_htlc_input_witness`]: crate::ln::chan_utils::build_htlc_input_witness
356+
HTLCResolution {
357+
target_feerate_sat_per_1000_weight: u32,
358+
tx_template: Transaction,
359+
htlc_descriptors: Vec<HTLCDescriptor>,
360+
},
289361
}
290362

291363
/// An Event which you should probably take some action in response to.
@@ -890,9 +962,10 @@ impl Writeable for Event {
890962
&Event::BumpTransaction(ref event)=> {
891963
27u8.write(writer)?;
892964
match event {
893-
// We never write the ChannelClose events as they'll be replayed upon restarting
894-
// anyway if the commitment transaction remains unconfirmed.
965+
// We never write the ChannelClose|HTLCResolution events as they'll be replayed
966+
// upon restarting anyway if they remain unresolved.
895967
BumpTransactionEvent::ChannelClose { .. } => {}
968+
BumpTransactionEvent::HTLCResolution { .. } => {}
896969
}
897970
}
898971
&Event::ChannelReady { ref channel_id, ref user_channel_id, ref counterparty_node_id, ref channel_type } => {

0 commit comments

Comments
 (0)