Skip to content

Commit d35fa33

Browse files
committed
Ask ChannelSigner the weight of the htlc tx input
1 parent 4c23a7a commit d35fa33

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

lightning/src/events/bump_transaction.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +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::{
24-
ANCHOR_INPUT_WITNESS_WEIGHT, HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT,
25-
HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT, HTLCOutputInCommitment
26-
};
23+
use crate::ln::chan_utils::{ANCHOR_INPUT_WITNESS_WEIGHT, HTLCOutputInCommitment};
2724
use crate::prelude::*;
2825
use crate::sign::{
2926
ChannelDerivationParameters, ChannelSigner, HTLCDescriptor, SignerProvider, P2WPKH_WITNESS_WEIGHT,
@@ -730,24 +727,24 @@ where
730727
let mut must_spend = Vec::with_capacity(htlc_descriptors.len());
731728
let mut signers_and_revokeable_spks = BTreeMap::new();
732729
for htlc_descriptor in htlc_descriptors {
730+
// TODO: avoid having mutable references flying around
731+
let (_, revokeable_spk, witness_weight) = signers_and_revokeable_spks.entry(htlc_descriptor.channel_derivation_parameters.keys_id)
732+
.or_insert_with(|| {
733+
let signer = htlc_descriptor.derive_channel_signer(&self.signer_provider);
734+
let revokeable_spk = signer.get_revokeable_spk(true, htlc_descriptor.per_commitment_number, &htlc_descriptor.per_commitment_point, &self.secp);
735+
// TODO: cache this, it does not change throughout the lifetime of the channel
736+
let witness_weight = signer.get_holder_htlc_transaction_witness_weight(htlc_descriptor.preimage.is_some());
737+
(signer, revokeable_spk, witness_weight)
738+
});
739+
733740
let htlc_input = htlc_descriptor.unsigned_tx_input();
734741
must_spend.push(Input {
735742
outpoint: htlc_input.previous_output.clone(),
736743
previous_utxo: htlc_descriptor.previous_utxo(&self.secp),
737-
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + if htlc_descriptor.preimage.is_some() {
738-
HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT
739-
} else {
740-
HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT
741-
},
744+
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + *witness_weight,
742745
});
743746
htlc_tx.input.push(htlc_input);
744-
let revokeable_spk = signers_and_revokeable_spks.entry(htlc_descriptor.channel_derivation_parameters.keys_id)
745-
.or_insert_with(|| {
746-
let signer = htlc_descriptor.derive_channel_signer(&self.signer_provider);
747-
let revokeable_spk = signer.get_revokeable_spk(true, htlc_descriptor.per_commitment_number, &htlc_descriptor.per_commitment_point, &self.secp);
748-
(signer, revokeable_spk)
749-
}).1.clone();
750-
let htlc_output = htlc_descriptor.tx_output(revokeable_spk);
747+
let htlc_output = htlc_descriptor.tx_output(revokeable_spk.clone());
751748
htlc_tx.output.push(htlc_output);
752749
}
753750

lightning/src/sign/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,10 @@ pub trait ChannelSigner {
948948
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
949949
secp_ctx: &Secp256k1<secp256k1::All>,
950950
) -> Result<Transaction, ()>;
951+
952+
/// Gets the weight of the witness of the input that spends the htlc output of a
953+
/// holder commitment transaction
954+
fn get_holder_htlc_transaction_witness_weight(&self, offered: bool) -> u64;
951955
}
952956

953957
/// Specifies the recipient of an invoice.
@@ -1800,6 +1804,14 @@ impl ChannelSigner for InMemorySigner {
18001804
signed_tx.input[input].witness = witness;
18011805
Ok(signed_tx)
18021806
}
1807+
1808+
fn get_holder_htlc_transaction_witness_weight(&self, offered: bool) -> u64 {
1809+
if offered {
1810+
chan_utils::HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT
1811+
} else {
1812+
chan_utils::HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT
1813+
}
1814+
}
18031815
}
18041816

18051817
const MISSING_PARAMS_ERR: &'static str =

lightning/src/util/test_channel_signer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ impl ChannelSigner for TestChannelSigner {
350350
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
351351
}
352352

353+
fn get_holder_htlc_transaction_witness_weight(&self, offered: bool) -> u64 {
354+
self.inner.get_holder_htlc_transaction_witness_weight(offered)
355+
}
353356
}
354357

355358
impl EcdsaChannelSigner for TestChannelSigner {

0 commit comments

Comments
 (0)