Skip to content

Commit 05fdbfa

Browse files
committed
Ask ChannelSigner the weight of the htlc tx input
1 parent 96dee21 commit 05fdbfa

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
@@ -946,6 +946,10 @@ pub trait ChannelSigner {
946946
&self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor,
947947
secp_ctx: &Secp256k1<secp256k1::All>,
948948
) -> Result<Transaction, ()>;
949+
950+
/// Gets the weight of the witness of the input that spends the htlc output of a
951+
/// holder commitment transaction
952+
fn get_holder_htlc_transaction_witness_weight(&self, offered: bool) -> u64;
949953
}
950954

951955
/// Specifies the recipient of an invoice.
@@ -1768,6 +1772,14 @@ impl ChannelSigner for InMemorySigner {
17681772
signed_tx.input[input].witness = witness;
17691773
Ok(signed_tx)
17701774
}
1775+
1776+
fn get_holder_htlc_transaction_witness_weight(&self, offered: bool) -> u64 {
1777+
if offered {
1778+
chan_utils::HTLC_SUCCESS_INPUT_ANCHOR_WITNESS_WEIGHT
1779+
} else {
1780+
chan_utils::HTLC_TIMEOUT_INPUT_ANCHOR_WITNESS_WEIGHT
1781+
}
1782+
}
17711783
}
17721784

17731785
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
@@ -336,6 +336,9 @@ impl ChannelSigner for TestChannelSigner {
336336
Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap())
337337
}
338338

339+
fn get_holder_htlc_transaction_witness_weight(&self, offered: bool) -> u64 {
340+
self.inner.get_holder_htlc_transaction_witness_weight(offered)
341+
}
339342
}
340343

341344
impl EcdsaChannelSigner for TestChannelSigner {

0 commit comments

Comments
 (0)