From 250a1f6d65c5d27711d3abaf9c4e6f2c08750ae0 Mon Sep 17 00:00:00 2001 From: Leo Nash Date: Mon, 6 Jan 2025 17:48:49 +0000 Subject: [PATCH] Add `ChannelSigner::get_holder_anchor_input_witness_weight` --- lightning/src/events/bump_transaction.rs | 12 +++++++----- lightning/src/sign/mod.rs | 8 ++++++++ lightning/src/util/test_channel_signer.rs | 6 +++++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lightning/src/events/bump_transaction.rs b/lightning/src/events/bump_transaction.rs index 9dceef24a3b..ee4d6d34482 100644 --- a/lightning/src/events/bump_transaction.rs +++ b/lightning/src/events/bump_transaction.rs @@ -20,7 +20,7 @@ use crate::io_extras::sink; use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI; use crate::ln::types::ChannelId; use crate::ln::chan_utils; -use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, HTLCOutputInCommitment}; +use crate::ln::chan_utils::HTLCOutputInCommitment; use crate::prelude::*; use crate::sign::{ ChannelDerivationParameters, ChannelSigner, HTLCDescriptor, SignerProvider, P2WPKH_WITNESS_WEIGHT, @@ -608,6 +608,9 @@ where &self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32, commitment_tx: &Transaction, commitment_tx_fee_sat: u64, anchor_descriptor: &AnchorDescriptor, ) -> Result<(), ()> { + let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider); + let anchor_input_witness_weight = signer.get_holder_anchor_input_witness_weight(); + // Our commitment transaction already has fees allocated to it, so we should take them into // account. We do so by pretending the commitment transaction's fee and weight are part of // the anchor input. @@ -615,7 +618,7 @@ where let commitment_tx_fee_sat = Amount::from_sat(commitment_tx_fee_sat); anchor_utxo.value += commitment_tx_fee_sat; let starting_package_and_fixed_input_satisfaction_weight = - commitment_tx.weight().to_wu() + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT; + commitment_tx.weight().to_wu() + anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT; let mut package_and_fixed_input_satisfaction_weight = starting_package_and_fixed_input_satisfaction_weight; @@ -640,7 +643,7 @@ where output: vec![], }; - let total_satisfaction_weight = ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT + + let total_satisfaction_weight = anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT + coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum::(); let total_input_amount = must_spend_amount + coin_selection.confirmed_utxos.iter().map(|utxo| utxo.output.value).sum(); @@ -686,7 +689,6 @@ where log_debug!(self.logger, "Signing anchor transaction {}", anchor_txid); anchor_tx = self.utxo_source.sign_psbt(anchor_psbt)?; - let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider); anchor_tx = signer.spend_holder_anchor_input(&anchor_tx, 0, &self.secp)?; #[cfg(debug_assertions)] { @@ -857,7 +859,7 @@ mod tests { use super::*; use crate::io::Cursor; - use crate::ln::chan_utils::ChannelTransactionParameters; + use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, ChannelTransactionParameters}; use crate::util::ser::Readable; use crate::util::test_utils::{TestBroadcaster, TestLogger}; use crate::sign::KeysManager; diff --git a/lightning/src/sign/mod.rs b/lightning/src/sign/mod.rs index 868eb50baa6..d07f7b0d51b 100644 --- a/lightning/src/sign/mod.rs +++ b/lightning/src/sign/mod.rs @@ -45,6 +45,7 @@ use crate::ln::chan_utils::{ get_anchor_redeemscript, get_counterparty_payment_script, get_revokeable_redeemscript, make_funding_redeemscript, ChannelPublicKeys, ChannelTransactionParameters, ClosingTransaction, CommitmentTransaction, HTLCOutputInCommitment, HolderCommitmentTransaction, + ANCHOR_INPUT_WITNESS_WEIGHT, }; use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI; use crate::ln::channel_keys::{ @@ -969,6 +970,9 @@ pub trait ChannelSigner { fn spend_holder_anchor_input( &self, anchor_tx: &Transaction, input_idx: usize, secp_ctx: &Secp256k1, ) -> Result; + + /// Get the weight of the witness to spend the holder anchor input + fn get_holder_anchor_input_witness_weight(&self) -> u64; } /// Specifies the recipient of an invoice. @@ -1908,6 +1912,10 @@ impl ChannelSigner for InMemorySigner { tx.input[0].witness = witness; Ok(tx) } + + fn get_holder_anchor_input_witness_weight(&self) -> u64 { + ANCHOR_INPUT_WITNESS_WEIGHT + } } const MISSING_PARAMS_ERR: &'static str = diff --git a/lightning/src/util/test_channel_signer.rs b/lightning/src/util/test_channel_signer.rs index 87702912f8e..f4f4cfbd282 100644 --- a/lightning/src/util/test_channel_signer.rs +++ b/lightning/src/util/test_channel_signer.rs @@ -8,7 +8,7 @@ // licenses. use crate::ln::channel::{ANCHOR_OUTPUT_VALUE_SATOSHI, MIN_CHAN_DUST_LIMIT_SATOSHIS}; -use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction}; +use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction}; use crate::ln::channel_keys::{HtlcKey}; use crate::ln::msgs; use crate::types::payment::PaymentPreimage; @@ -371,6 +371,10 @@ impl ChannelSigner for TestChannelSigner { ) -> Result { self.inner.spend_holder_anchor_input(anchor_tx, input_idx, secp_ctx) } + + fn get_holder_anchor_input_witness_weight(&self) -> u64 { + ANCHOR_INPUT_WITNESS_WEIGHT + } } impl EcdsaChannelSigner for TestChannelSigner {