Skip to content

Commit ca07b7d

Browse files
committed
Yield BumpHTLCResolution events
1 parent 5715c85 commit ca07b7d

File tree

2 files changed

+87
-6
lines changed

2 files changed

+87
-6
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::chain::chaininterface::{BroadcasterInterface, FeeEstimator, LowerBoun
4444
use crate::chain::transaction::{OutPoint, TransactionData};
4545
use crate::chain::keysinterface::{SpendableOutputDescriptor, StaticPaymentOutputDescriptor, DelayedPaymentOutputDescriptor, Sign, KeysInterface};
4646
#[cfg(anchors)]
47-
use crate::chain::onchaintx::ClaimEvent;
47+
use crate::chain::onchaintx::{ClaimEvent, ExternalHTLCClaim};
4848
use crate::chain::onchaintx::OnchainTxHandler;
4949
use crate::chain::package::{CounterpartyOfferedHTLCOutput, CounterpartyReceivedHTLCOutput, HolderFundingOutput, HolderHTLCOutput, PackageSolvingData, PackageTemplate, RevokedOutput, RevokedHTLCOutput};
5050
use crate::chain::Filter;
@@ -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,31 @@ 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+
match htlc {
2380+
ExternalHTLCClaim::SecondStage {
2381+
amount, per_commitment_number, redeem_script, preimage, counterparty_sig
2382+
} => htlc_descriptors.push(HTLCDescriptor::SecondStage {
2383+
channel_keys_id: self.channel_keys_id,
2384+
channel_value_satoshis: self.channel_value_satoshis,
2385+
amount,
2386+
per_commitment_number,
2387+
redeem_script,
2388+
preimage,
2389+
counterparty_sig,
2390+
}),
2391+
}
2392+
}
2393+
ret.push(Event::BumpTransaction(BumpTransactionEvent::HTLCResolution {
2394+
target_feerate_sat_per_1000_weight,
2395+
tx_template,
2396+
htlc_descriptors,
2397+
}));
2398+
}
23752399
}
23762400
}
23772401
ret

lightning/src/util/events.rs

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use bitcoin::blockdata::script::Script;
3131
use bitcoin::hashes::Hash;
3232
use bitcoin::hashes::sha256::Hash as Sha256;
3333
use bitcoin::secp256k1::PublicKey;
34+
#[cfg(anchors)]
35+
use bitcoin::secp256k1::ecdsa::Signature;
3436
use crate::io;
3537
use crate::prelude::*;
3638
use core::time::Duration;
@@ -244,7 +246,10 @@ pub enum BumpTransactionEvent {
244246
/// The consumer should be able to sign for any of the additional inputs included within the
245247
/// child anchor transaction. To sign its anchor input, an [`InMemorySigner`] should be
246248
/// re-derived through [`KeysManager::derive_channel_keys`] with the help of
247-
/// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`].
249+
/// [`AnchorDescriptor::channel_keys_id`] and [`AnchorDescriptor::channel_value_satoshis`]. The
250+
/// anchor input signature can be computed with [`InMemorySigner::sign_holder_anchor_input`],
251+
/// which can then be provided to [`build_anchor_input_witness`] along with the `funding_pubkey`
252+
/// to obtain the full witness required to spend.
248253
///
249254
/// It is possible to receive more than one instance of this event if a valid child anchor
250255
/// transaction is never broadcast or is but not with a sufficient fee to be mined. Care should
@@ -265,6 +270,8 @@ pub enum BumpTransactionEvent {
265270
///
266271
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
267272
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
273+
/// [`InMemorySigner::sign_holder_anchor_input`]: crate::chain::keysinterface::InMemorySigner::sign_holder_anchor_input
274+
/// [`build_anchor_input_witness`]: crate::ln::chan_utils::build_anchor_input_witness
268275
ChannelClose {
269276
/// The target feerate that the transaction package, which consists of the commitment
270277
/// transaction and the to-be-crafted child anchor transaction, must meet.
@@ -283,6 +290,55 @@ pub enum BumpTransactionEvent {
283290
/// commitment transaction confirms.
284291
pending_htlcs: Vec<HTLCOutputInCommitment>,
285292
},
293+
/// Indicates that a channel featuring anchor outputs has HTLC(s) that need to be resolved
294+
/// onchain. With the zero-HTLC-transaction-fee variant of anchor outputs, the pre-signed HTLC
295+
/// transactions have a zero fee, thus requiring additional inputs and/or outputs to be attached
296+
/// for a timely confirmation within the chain. These additional inputs and/or outputs must be
297+
/// appended to the enclosed `tx_template` to meet the target feerate. Consumers must ensure the
298+
/// order of pre-existing inputs and outputs within `tx_template` are not changed, as it is
299+
/// crucial to the validity of the transaction itself. Failure to meet the target feerate
300+
/// decreases the confirmation odds of the transaction, possinly resulting in a loss of funds.
301+
/// Once the transaction is amended to meet the target feerate, it must be signed for and
302+
/// broadcast by the consumer of the event.
303+
///
304+
/// The consumer should be able to sign for any of the additional inputs added to `tx_template`.
305+
/// To sign HTLC inputs, an [`InMemorySigner`] should be re-derived through
306+
/// [`KeysManager::derive_channel_keys`] with the help of `channel_keys_id` and
307+
/// `AnchorDescriptor::channel_value_satoshis`. Each HTLC input's signature can be computed
308+
/// with [`InMemorySigner::sign_holder_htlc_transaction`], which can then be provided to
309+
/// [`build_htlc_input_witness`] along with the enclosed [`HTLCDescriptor`] to obtain the full
310+
/// witness required to spend.
311+
///
312+
/// It is possible to receive more than one instance of this event if a valid HTLC transaction
313+
/// is never broadcast or is but not with a sufficient fee to be mined. Care should be taken by
314+
/// the consumer of the event to ensure any future iterations of the HTLC transaction adhere to
315+
/// the [Replace-By-Fee rules](https://github.com/bitcoin/bitcoin/blob/master/doc/policy/mempool-replacements.md)
316+
/// for fee bumps to be accepted into the mempool, and eventually the chain. As the frequency of
317+
/// these events is not user-controlled, users may ignore/drop the event if they are no longer
318+
/// able to commit external confirmed funds to the HTLC transaction.
319+
///
320+
/// [`InMemorySigner`]: crate::chain::keysinterface::InMemorySigner
321+
/// [`KeysManager::derive_channel_keys`]: crate::chain::keysinterface::KeysManager::derive_channel_keys
322+
/// [`InMemorySigner::sign_holder_htlc_transaction`]: crate::chain::keysinterface::InMemorySigner::sign_holder_htlc_transaction
323+
/// [`build_htlc_input_witness`]: crate::ln::chan_utils::build_htlc_input_witness
324+
HTLCResolution {
325+
target_feerate_sat_per_1000_weight: u32,
326+
tx_template: Transaction,
327+
htlc_descriptors: Vec<HTLCDescriptor>,
328+
},
329+
}
330+
331+
#[derive(Clone, Debug)]
332+
pub enum HTLCDescriptor {
333+
SecondStage {
334+
channel_keys_id: [u8; 32],
335+
channel_value_satoshis: u64,
336+
amount: u64,
337+
per_commitment_number: u64,
338+
redeem_script: Script,
339+
preimage: Option<PaymentPreimage>,
340+
counterparty_sig: Signature
341+
}
286342
}
287343

288344
/// An Event which you should probably take some action in response to.
@@ -853,9 +909,10 @@ impl Writeable for Event {
853909
&Event::BumpTransaction(ref event)=> {
854910
27u8.write(writer)?;
855911
match event {
856-
// We never write the ChannelClose events as they'll be replayed upon restarting
857-
// anyway if the commitment transaction remains unconfirmed.
912+
// We never write the ChannelClose|HTLCResolution events as they'll be replayed
913+
// upon restarting anyway if they remain unresolved.
858914
BumpTransactionEvent::ChannelClose { .. } => {}
915+
BumpTransactionEvent::HTLCResolution { .. } => {}
859916
}
860917
}
861918
// Note that, going forward, all new events must only write data inside of

0 commit comments

Comments
 (0)