Skip to content

Commit 037d161

Browse files
committed
Yield BumpHTLCResolution events
1 parent 2e74643 commit 037d161

File tree

2 files changed

+103
-6
lines changed

2 files changed

+103
-6
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};
@@ -2371,7 +2371,28 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
23712371
pending_htlcs,
23722372
}));
23732373
},
2374-
_ => {},
2374+
ClaimEvent::BumpHTLC {
2375+
target_feerate_sat_per_1000_weight, tx_template, htlcs,
2376+
} => {
2377+
let mut htlc_descriptors = Vec::with_capacity(htlcs.len());
2378+
for htlc in htlcs {
2379+
htlc_descriptors.push(HTLCDescriptor {
2380+
channel_keys_id: self.channel_keys_id,
2381+
channel_value_satoshis: self.channel_value_satoshis,
2382+
per_commitment_number: htlc.per_commitment_number,
2383+
htlc: htlc.htlc,
2384+
preimage: htlc.preimage,
2385+
counterparty_base_htlc_key: htlc.counterparty_base_htlc_key,
2386+
counterparty_base_revocation_key: htlc.counterparty_base_revocation_key,
2387+
counterparty_sig: htlc.counterparty_sig,
2388+
});
2389+
}
2390+
ret.push(Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
2391+
target_feerate_sat_per_1000_weight,
2392+
tx_template,
2393+
htlc_descriptors,
2394+
}));
2395+
}
23752396
}
23762397
}
23772398
ret

lightning/src/util/events.rs

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! few other things.
1616
1717
use crate::chain::keysinterface::SpendableOutputDescriptor;
18+
#[cfg(anchors)]
1819
use crate::ln::chan_utils::HTLCOutputInCommitment;
1920
use crate::ln::channelmanager::PaymentId;
2021
use crate::ln::channel::FUNDING_CONF_DEADLINE_BLOCKS;
@@ -26,11 +27,15 @@ use crate::routing::gossip::NetworkUpdate;
2627
use crate::util::ser::{BigSize, FixedLengthReader, Writeable, Writer, MaybeReadable, Readable, VecReadWrapper, VecWriteWrapper};
2728
use crate::routing::router::{RouteHop, RouteParameters};
2829

29-
use bitcoin::{PackedLockTime, Transaction, OutPoint};
30+
#[cfg(anchors)]
31+
use bitcoin::OutPoint;
32+
use bitcoin::{PackedLockTime, Transaction};
3033
use bitcoin::blockdata::script::Script;
3134
use bitcoin::hashes::Hash;
3235
use bitcoin::hashes::sha256::Hash as Sha256;
3336
use bitcoin::secp256k1::PublicKey;
37+
#[cfg(anchors)]
38+
use bitcoin::secp256k1::ecdsa::Signature;
3439
use crate::io;
3540
use crate::prelude::*;
3641
use core::time::Duration;
@@ -225,6 +230,37 @@ pub struct AnchorDescriptor {
225230
pub outpoint: OutPoint,
226231
}
227232

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+
228264
#[cfg(anchors)]
229265
/// Represents the different types of transactions, originating from LDK, to be bumped.
230266
#[derive(Clone, Debug)]
@@ -244,7 +280,10 @@ pub enum BumpTransactionEvent {
244280
/// The consumer should be able to sign for any of the additional inputs included within the
245281
/// child anchor transaction. To sign its anchor input, an [`InMemorySigner`] should be
246282
/// re-derived through [`KeysManager::derive_channel_keys`] with the help of
247-
/// [`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.
248287
///
249288
/// It is possible to receive more than one instance of this event if a valid child anchor
250289
/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
@@ -265,6 +304,8 @@ pub enum BumpTransactionEvent {
265304
///
266305
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
267306
/// [`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
268309
ChannelClose {
269310
/// The target feerate that the transaction package, which consists of the commitment
270311
/// transaction and the to-be-crafted child anchor transaction, must meet.
@@ -283,6 +324,40 @@ pub enum BumpTransactionEvent {
283324
/// commitment transaction confirms.
284325
pending_htlcs: Vec<HTLCOutputInCommitment>,
285326
},
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+
},
286361
}
287362

288363
/// An Event which you should probably take some action in response to.
@@ -853,9 +928,10 @@ impl Writeable for Event {
853928
&Event::BumpTransaction(ref event)=> {
854929
27u8.write(writer)?;
855930
match event {
856-
// We never write the ChannelClose events as they'll be replayed upon restarting
857-
// anyway if the commitment transaction remains unconfirmed.
931+
// We never write the ChannelClose|HTLCResolution events as they'll be replayed
932+
// upon restarting anyway if they remain unresolved.
858933
BumpTransactionEvent::ChannelClose { .. } => {}
934+
BumpTransactionEvent::HTLCResolution { .. } => {}
859935
}
860936
}
861937
// Note that, going forward, all new events must only write data inside of

0 commit comments

Comments
 (0)