Skip to content

Commit db7df42

Browse files
committed
Add ChannelSigner::get_holder_anchor_input_witness_weight
1 parent 9aeb2bd commit db7df42

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::io_extras::sink;
2020
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
2121
use crate::ln::types::ChannelId;
2222
use crate::ln::chan_utils;
23-
use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, HTLCOutputInCommitment};
23+
use crate::ln::chan_utils::HTLCOutputInCommitment;
2424
use crate::prelude::*;
2525
use crate::sign::{
2626
ChannelDerivationParameters, ChannelSigner, HTLCDescriptor, SignerProvider, P2WPKH_WITNESS_WEIGHT,
@@ -608,14 +608,17 @@ where
608608
&self, claim_id: ClaimId, package_target_feerate_sat_per_1000_weight: u32,
609609
commitment_tx: &Transaction, commitment_tx_fee_sat: u64, anchor_descriptor: &AnchorDescriptor,
610610
) -> Result<(), ()> {
611+
let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider);
612+
let anchor_input_witness_weight = signer.get_holder_anchor_input_witness_weight();
613+
611614
// Our commitment transaction already has fees allocated to it, so we should take them into
612615
// account. We do so by pretending the commitment transaction's fee and weight are part of
613616
// the anchor input.
614617
let mut anchor_utxo = anchor_descriptor.previous_utxo();
615618
let commitment_tx_fee_sat = Amount::from_sat(commitment_tx_fee_sat);
616619
anchor_utxo.value += commitment_tx_fee_sat;
617620
let starting_package_and_fixed_input_satisfaction_weight =
618-
commitment_tx.weight().to_wu() + ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT;
621+
commitment_tx.weight().to_wu() + anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT;
619622
let mut package_and_fixed_input_satisfaction_weight =
620623
starting_package_and_fixed_input_satisfaction_weight;
621624

@@ -640,7 +643,7 @@ where
640643
output: vec![],
641644
};
642645

643-
let total_satisfaction_weight = ANCHOR_INPUT_WITNESS_WEIGHT + EMPTY_SCRIPT_SIG_WEIGHT +
646+
let total_satisfaction_weight = anchor_input_witness_weight + EMPTY_SCRIPT_SIG_WEIGHT +
644647
coin_selection.confirmed_utxos.iter().map(|utxo| utxo.satisfaction_weight).sum::<u64>();
645648
let total_input_amount = must_spend_amount +
646649
coin_selection.confirmed_utxos.iter().map(|utxo| utxo.output.value).sum();
@@ -686,7 +689,6 @@ where
686689
log_debug!(self.logger, "Signing anchor transaction {}", anchor_txid);
687690
anchor_tx = self.utxo_source.sign_psbt(anchor_psbt)?;
688691

689-
let signer = anchor_descriptor.derive_channel_signer(&self.signer_provider);
690692
anchor_tx = signer.spend_holder_anchor_input(&anchor_tx, 0, &self.secp)?;
691693

692694
#[cfg(debug_assertions)] {
@@ -857,7 +859,7 @@ mod tests {
857859
use super::*;
858860

859861
use crate::io::Cursor;
860-
use crate::ln::chan_utils::ChannelTransactionParameters;
862+
use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, ChannelTransactionParameters};
861863
use crate::util::ser::Readable;
862864
use crate::util::test_utils::{TestBroadcaster, TestLogger};
863865
use crate::sign::KeysManager;

lightning/src/sign/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use crate::ln::chan_utils::{
4949
get_anchor_redeemscript, get_counterparty_payment_script, get_revokeable_redeemscript,
5050
make_funding_redeemscript, ChannelPublicKeys, ChannelTransactionParameters, ClosingTransaction,
5151
CommitmentTransaction, HTLCOutputInCommitment, HolderCommitmentTransaction,
52+
ANCHOR_INPUT_WITNESS_WEIGHT,
5253
};
5354
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
5455
use crate::ln::channel_keys::{
@@ -964,6 +965,9 @@ pub trait ChannelSigner {
964965
fn spend_holder_anchor_input(
965966
&self, anchor_tx: &Transaction, input_idx: usize, secp_ctx: &Secp256k1<secp256k1::All>,
966967
) -> Result<Transaction, ()>;
968+
969+
/// Get the weight of the witness to spend the holder anchor input
970+
fn get_holder_anchor_input_witness_weight(&self) -> u64;
967971
}
968972

969973
/// Specifies the recipient of an invoice.
@@ -1851,6 +1855,10 @@ impl ChannelSigner for InMemorySigner {
18511855
tx.input[input_idx].witness = witness;
18521856
Ok(tx)
18531857
}
1858+
1859+
fn get_holder_anchor_input_witness_weight(&self) -> u64 {
1860+
ANCHOR_INPUT_WITNESS_WEIGHT
1861+
}
18541862
}
18551863

18561864
const MISSING_PARAMS_ERR: &'static str =

lightning/src/util/test_channel_signer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// licenses.
99

1010
use crate::ln::channel::{ANCHOR_OUTPUT_VALUE_SATOSHI, MIN_CHAN_DUST_LIMIT_SATOSHIS};
11-
use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction};
11+
use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction};
1212
use crate::ln::channel_keys::{HtlcKey};
1313
use crate::ln::msgs;
1414
use crate::types::payment::PaymentPreimage;
@@ -353,6 +353,10 @@ impl ChannelSigner for TestChannelSigner {
353353
) -> Result<Transaction, ()> {
354354
self.inner.spend_holder_anchor_input(anchor_tx, input_idx, secp_ctx)
355355
}
356+
357+
fn get_holder_anchor_input_witness_weight(&self) -> u64 {
358+
ANCHOR_INPUT_WITNESS_WEIGHT
359+
}
356360
}
357361

358362
impl EcdsaChannelSigner for TestChannelSigner {

0 commit comments

Comments
 (0)