Cache HTLC per_commitment_point in descriptor
[rust-lightning] / lightning / src / events / bump_transaction.rs
index ffc5456f832796da82831389892a51c6b0287daf..0e3152d7b0a4c848c2fe5a4a6f07cd37f83db5cd 100644 (file)
@@ -7,23 +7,25 @@
 // You may not use this file except in accordance with one or both of these
 // licenses.
 
-//! Utitilies for bumping transactions originating from [`super::Event`]s.
+//! Utilities for bumping transactions originating from [`Event`]s.
+//!
+//! [`Event`]: crate::events::Event
 
 use core::convert::TryInto;
 use core::ops::Deref;
 
 use crate::chain::chaininterface::BroadcasterInterface;
 use crate::chain::ClaimId;
-use crate::sign::{ChannelSigner, EcdsaChannelSigner, SignerProvider};
 use crate::io_extras::sink;
-use crate::ln::PaymentPreimage;
 use crate::ln::chan_utils;
 use crate::ln::chan_utils::{
        ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT,
        HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, ChannelTransactionParameters, HTLCOutputInCommitment
 };
-use crate::events::Event;
-use crate::prelude::HashMap;
+use crate::ln::features::ChannelTypeFeatures;
+use crate::ln::PaymentPreimage;
+use crate::prelude::*;
+use crate::sign::{ChannelSigner, EcdsaChannelSigner, SignerProvider};
 use crate::sync::Mutex;
 use crate::util::logger::Logger;
 
@@ -33,7 +35,6 @@ use bitcoin::consensus::Encodable;
 use bitcoin::secp256k1;
 use bitcoin::secp256k1::{PublicKey, Secp256k1};
 use bitcoin::secp256k1::ecdsa::Signature;
-use crate::ln::features::ChannelTypeFeatures;
 
 const EMPTY_SCRIPT_SIG_WEIGHT: u64 = 1 /* empty script_sig */ * WITNESS_SCALE_FACTOR as u64;
 
@@ -92,6 +93,12 @@ pub struct HTLCDescriptor {
        pub commitment_txid: Txid,
        /// The number of the commitment transaction in which the HTLC output lives.
        pub per_commitment_number: u64,
+       /// The key tweak corresponding to the number of the commitment transaction in which the HTLC
+       /// output lives. This tweak is applied to all the basepoints for both parties in the channel to
+       /// arrive at unique keys per commitment.
+       ///
+       /// See <https://github.com/lightning/bolts/blob/master/03-transactions.md#keys> for more info.
+       pub per_commitment_point: PublicKey,
        /// The details of the HTLC as it appears in the commitment transaction.
        pub htlc: HTLCOutputInCommitment,
        /// The preimage, if `Some`, to claim the HTLC output with. If `None`, the timeout path must be
@@ -110,17 +117,15 @@ impl HTLCDescriptor {
 
        /// Returns the delayed output created as a result of spending the HTLC output in the commitment
        /// transaction.
-       pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(
-               &self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
-       ) -> TxOut {
+       pub fn tx_output<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> TxOut {
                let channel_params = self.channel_parameters.as_holder_broadcastable();
                let broadcaster_keys = channel_params.broadcaster_pubkeys();
                let counterparty_keys = channel_params.countersignatory_pubkeys();
                let broadcaster_delayed_key = chan_utils::derive_public_key(
-                       secp, per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
+                       secp, &self.per_commitment_point, &broadcaster_keys.delayed_payment_basepoint
                );
                let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
-                       secp, per_commitment_point, &counterparty_keys.revocation_basepoint
+                       secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
                );
                chan_utils::build_htlc_output(
                        0 /* feerate_per_kw */, channel_params.contest_delay(), &self.htlc,
@@ -129,20 +134,18 @@ impl HTLCDescriptor {
        }
 
        /// Returns the witness script of the HTLC output in the commitment transaction.
-       pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(
-               &self, per_commitment_point: &PublicKey, secp: &Secp256k1<C>
-       ) -> Script {
+       pub fn witness_script<C: secp256k1::Signing + secp256k1::Verification>(&self, secp: &Secp256k1<C>) -> Script {
                let channel_params = self.channel_parameters.as_holder_broadcastable();
                let broadcaster_keys = channel_params.broadcaster_pubkeys();
                let counterparty_keys = channel_params.countersignatory_pubkeys();
                let broadcaster_htlc_key = chan_utils::derive_public_key(
-                       secp, per_commitment_point, &broadcaster_keys.htlc_basepoint
+                       secp, &self.per_commitment_point, &broadcaster_keys.htlc_basepoint
                );
                let counterparty_htlc_key = chan_utils::derive_public_key(
-                       secp, per_commitment_point, &counterparty_keys.htlc_basepoint
+                       secp, &self.per_commitment_point, &counterparty_keys.htlc_basepoint
                );
                let counterparty_revocation_key = chan_utils::derive_public_revocation_key(
-                       secp, per_commitment_point, &counterparty_keys.revocation_basepoint
+                       secp, &self.per_commitment_point, &counterparty_keys.revocation_basepoint
                );
                chan_utils::get_htlc_redeemscript_with_explicit_keys(
                        &self.htlc, &ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(), &broadcaster_htlc_key, &counterparty_htlc_key,
@@ -360,12 +363,12 @@ impl Utxo {
 pub struct CoinSelection {
        /// The set of UTXOs (with at least 1 confirmation) to spend and use within a transaction
        /// requiring additional fees.
-       confirmed_utxos: Vec<Utxo>,
+       pub confirmed_utxos: Vec<Utxo>,
        /// An additional output tracking whether any change remained after coin selection. This output
        /// should always have a value above dust for its given `script_pubkey`. It should not be
        /// spent until the transaction it belongs to confirms to ensure mempool descendant limits are
        /// not met. This implies no other party should be able to spend it except us.
-       change_output: Option<TxOut>,
+       pub change_output: Option<TxOut>,
 }
 
 /// An abstraction over a bitcoin wallet that can perform coin selection over a set of UTXOs and can
@@ -553,6 +556,8 @@ impl<W: Deref> CoinSelectionSource for Wallet<W> where W::Target: WalletSource {
 /// A handler for [`Event::BumpTransaction`] events that sources confirmed UTXOs from a
 /// [`CoinSelectionSource`] to fee bump transactions via Child-Pays-For-Parent (CPFP) or
 /// Replace-By-Fee (RBF).
+///
+/// [`Event::BumpTransaction`]: crate::events::Event::BumpTransaction
 pub struct BumpTransactionEventHandler<B: Deref, C: Deref, SP: Deref, L: Deref>
 where
        B::Target: BroadcasterInterface,
@@ -575,6 +580,8 @@ where
        L::Target: Logger,
 {
        /// Returns a new instance capable of handling [`Event::BumpTransaction`] events.
+       ///
+       /// [`Event::BumpTransaction`]: crate::events::Event::BumpTransaction
        pub fn new(broadcaster: B, utxo_source: C, signer_provider: SP, logger: L) -> Self {
                Self {
                        broadcaster,
@@ -691,15 +698,12 @@ where
                let mut signers = HashMap::new();
                let mut must_spend = Vec::with_capacity(htlc_descriptors.len());
                for htlc_descriptor in htlc_descriptors {
-                       let signer = signers.entry(htlc_descriptor.channel_keys_id)
+                       signers.entry(htlc_descriptor.channel_keys_id)
                                .or_insert_with(||
                                        self.signer_provider.derive_channel_signer(
                                                htlc_descriptor.channel_value_satoshis, htlc_descriptor.channel_keys_id,
                                        )
                                );
-                       let per_commitment_point = signer.get_per_commitment_point(
-                               htlc_descriptor.per_commitment_number, &self.secp
-                       );
 
                        let htlc_input = htlc_descriptor.unsigned_tx_input();
                        must_spend.push(Input {
@@ -711,7 +715,7 @@ where
                                },
                        });
                        tx.input.push(htlc_input);
-                       let htlc_output = htlc_descriptor.tx_output(&per_commitment_point, &self.secp);
+                       let htlc_output = htlc_descriptor.tx_output(&self.secp);
                        tx.output.push(htlc_output);
                }
 
@@ -738,10 +742,7 @@ where
                        let htlc_sig = signer.sign_holder_htlc_transaction(
                                &htlc_tx, idx, htlc_descriptor, &self.secp
                        )?;
-                       let per_commitment_point = signer.get_per_commitment_point(
-                               htlc_descriptor.per_commitment_number, &self.secp
-                       );
-                       let witness_script = htlc_descriptor.witness_script(&per_commitment_point, &self.secp);
+                       let witness_script = htlc_descriptor.witness_script(&self.secp);
                        htlc_tx.input[idx].witness = htlc_descriptor.tx_input_witness(&htlc_sig, &witness_script);
                }
 
@@ -749,13 +750,8 @@ where
                Ok(())
        }
 
-       /// Handles all variants of [`BumpTransactionEvent`], immediately returning otherwise.
-       pub fn handle_event(&self, event: &Event) {
-               let event = if let Event::BumpTransaction(event) = event {
-                       event
-               } else {
-                       return;
-               };
+       /// Handles all variants of [`BumpTransactionEvent`].
+       pub fn handle_event(&self, event: &BumpTransactionEvent) {
                match event {
                        BumpTransactionEvent::ChannelClose {
                                claim_id, package_target_feerate_sat_per_1000_weight, commitment_tx,