@@ -20,10 +20,7 @@ use crate::io_extras::sink;
2020use crate :: ln:: channel:: ANCHOR_OUTPUT_VALUE_SATOSHI ;
2121use crate :: ln:: types:: ChannelId ;
2222use 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 } ;
2724use crate :: prelude:: * ;
2825use 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
0 commit comments