@@ -736,8 +736,8 @@ impl HolderCommitmentTransaction {
736
736
counterparty_parameters : Some ( CounterpartyChannelTransactionParameters { pubkeys : channel_pubkeys. clone ( ) , selected_contest_delay : 0 } ) ,
737
737
funding_outpoint : Some ( chain:: transaction:: OutPoint { txid : Default :: default ( ) , index : 0 } )
738
738
} ;
739
- let aux : Vec < ( ) > = Vec :: new ( ) ;
740
- let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , keys, 0 , Vec :: new ( ) , aux , & channel_parameters. as_counterparty_broadcastable ( ) ) . 0 ;
739
+ let mut htlcs_with_aux : Vec < ( _ , ( ) ) > = Vec :: new ( ) ;
740
+ let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , keys, 0 , & mut htlcs_with_aux , & channel_parameters. as_counterparty_broadcastable ( ) ) ;
741
741
HolderCommitmentTransaction {
742
742
inner,
743
743
counterparty_sig : dummy_sig,
@@ -887,32 +887,20 @@ impl_writeable!(CommitmentTransaction, 0, {
887
887
impl CommitmentTransaction {
888
888
/// Construct an object of the class while assigning transaction output indices to HTLCs.
889
889
///
890
- /// Also keeps track of auxiliary HTLC data and returns it along with the mutated and sorted HTLCs.
891
- /// This allows the caller to match the HTLC output index with the auxiliary data.
890
+ /// Populates HTLCOutputInCommitment.transaction_output_index in htlcs_with_aux.
891
+ ///
892
+ /// The generic T allows the caller to match the HTLC output index with auxiliary data.
892
893
/// This auxiliary data is not stored in this object.
893
894
///
894
895
/// Only include HTLCs that are above the dust limit for the channel.
895
- ///
896
- /// Panics if the length of htlcs and aux are different.
897
- pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs : Vec < HTLCOutputInCommitment > , aux : Vec < T > , channel_parameters : & DirectedChannelTransactionParameters ) -> ( CommitmentTransaction , Vec < ( HTLCOutputInCommitment , T ) > ) {
896
+ pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> CommitmentTransaction {
898
897
// Sort outputs and populate output indices while keeping track of the auxiliary data
899
- let mut txouts = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, & htlcs, aux, channel_parameters) . unwrap ( ) ;
900
- let mut result_htlcs_with_aux = Vec :: new ( ) ;
901
- let mut htlcs = Vec :: new ( ) ;
902
- let mut outputs = Vec :: new ( ) ;
903
- for ( idx, mut out) in txouts. drain ( ..) . enumerate ( ) {
904
- if let Some ( mut htlc) = ( out. 1 ) . 1 . take ( ) {
905
- htlc. 0 . transaction_output_index = Some ( idx as u32 ) ;
906
- result_htlcs_with_aux. push ( ( htlc. 0 . clone ( ) , htlc. 1 ) ) ;
907
- htlcs. push ( htlc. 0 ) ;
908
- }
909
- outputs. push ( out. 0 ) ;
910
- }
898
+ let ( outputs, htlcs, _) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters) . unwrap ( ) ;
911
899
912
900
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
913
901
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
914
902
let txid = transaction. txid ( ) ;
915
- let info = CommitmentTransaction {
903
+ CommitmentTransaction {
916
904
commitment_number,
917
905
to_broadcaster_value_sat,
918
906
to_countersignatory_value_sat,
@@ -923,44 +911,32 @@ impl CommitmentTransaction {
923
911
transaction,
924
912
txid
925
913
} ,
926
- } ;
927
- ( info, result_htlcs_with_aux)
914
+ }
928
915
}
929
916
930
917
#[ cfg( feature = "_test_utils" ) ]
931
- /// Re-build the Bitcoin transaction.
932
- ///
933
- /// This function ignores the pre-built transaction and the pre-derived TxCreationKeys
934
- /// for use in an environment, such as an external signer, where we don't trust these to be
935
- /// derived by the Lightning node.
918
+ /// Get the transaction output scripts.
936
919
///
937
- /// The second return value are the scripts that were built for debugging purposes, since
938
- /// only the hash is present in the actual transaction.
939
- pub fn rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < ( BuiltCommitmentTransaction , Vec < Script > ) , ( ) > {
940
- self . internal_rebuild_transaction ( keys, channel_parameters)
920
+ /// For debugging purposes, since only the hash is present in the actual transaction.
921
+ pub fn get_output_scripts ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < Vec < Script > , ( ) > {
922
+ let mut htlcs_with_aux = self . htlcs . iter ( ) . map ( |h| ( h. clone ( ) , ( ) ) ) . collect ( ) ;
923
+ let ( _, _, scripts) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & mut htlcs_with_aux, channel_parameters) ?;
924
+ Ok ( scripts)
941
925
}
942
926
943
- fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < ( BuiltCommitmentTransaction , Vec < Script > ) , ( ) > {
927
+ fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < BuiltCommitmentTransaction , ( ) > {
944
928
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
945
929
946
- let mut aux = Vec :: with_capacity ( self . htlcs . len ( ) ) ;
947
- aux. resize ( self . htlcs . len ( ) , ( ) ) ;
948
- let mut txouts = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & self . htlcs , aux, channel_parameters) ?;
949
-
950
- let mut outputs = Vec :: with_capacity ( txouts. len ( ) ) ;
951
- let mut scripts = Vec :: with_capacity ( txouts. len ( ) ) ;
952
- for ( txout, ( script, _) ) in txouts. drain ( ..) {
953
- outputs. push ( txout) ;
954
- scripts. push ( script) ;
955
- }
930
+ let mut htlcs_with_aux = self . htlcs . iter ( ) . map ( |h| ( h. clone ( ) , ( ) ) ) . collect ( ) ;
931
+ let ( outputs, _, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & mut htlcs_with_aux, channel_parameters) ?;
956
932
957
933
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
958
934
let txid = transaction. txid ( ) ;
959
935
let built_transaction = BuiltCommitmentTransaction {
960
936
transaction,
961
937
txid
962
938
} ;
963
- Ok ( ( built_transaction, scripts ) )
939
+ Ok ( built_transaction)
964
940
}
965
941
966
942
fn make_transaction ( obscured_commitment_transaction_number : u64 , txins : Vec < TxIn > , outputs : Vec < TxOut > ) -> Transaction {
@@ -976,12 +952,11 @@ impl CommitmentTransaction {
976
952
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
977
953
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
978
954
// - building of a bitcoin transaction (build -> build_outputs), in which case T is just ()
979
- fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , htlcs : & Vec < HTLCOutputInCommitment > , aux : Vec < T > , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < Vec < ( TxOut , ( Script , Option < ( HTLCOutputInCommitment , T ) > ) ) > , ( ) > {
980
- assert_eq ! ( htlcs. len( ) , aux. len( ) , "htlcs vs aux length mismatch" ) ;
955
+ fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> Result < ( Vec < TxOut > , Vec < HTLCOutputInCommitment > , Vec < Script > ) , ( ) > {
981
956
let countersignatory_pubkeys = channel_parameters. countersignatory_pubkeys ( ) ;
982
957
let contest_delay = channel_parameters. contest_delay ( ) ;
983
958
984
- let mut txouts: Vec < ( TxOut , ( Script , Option < ( HTLCOutputInCommitment , T ) > ) ) > = Vec :: new ( ) ;
959
+ let mut txouts: Vec < ( TxOut , ( Script , Option < & mut HTLCOutputInCommitment > ) ) > = Vec :: new ( ) ;
985
960
986
961
if to_countersignatory_value_sat > 0 {
987
962
let script = script_for_p2wpkh ( & countersignatory_pubkeys. payment_point ) ;
@@ -1009,29 +984,43 @@ impl CommitmentTransaction {
1009
984
) ) ;
1010
985
}
1011
986
1012
- for ( htlc, t) in htlcs. iter ( ) . zip ( aux) {
987
+ let mut htlcs = Vec :: with_capacity ( htlcs_with_aux. len ( ) ) ;
988
+ for ( htlc, _) in htlcs_with_aux {
1013
989
let script = chan_utils:: get_htlc_redeemscript ( & htlc, & keys) ;
1014
990
let txout = TxOut {
1015
991
script_pubkey : script. to_v0_p2wsh ( ) ,
1016
992
value : htlc. amount_msat / 1000 ,
1017
993
} ;
1018
- txouts. push ( ( txout, ( script, Some ( ( htlc. clone ( ) , t ) ) ) ) ) ;
994
+ txouts. push ( ( txout, ( script, Some ( htlc) ) ) ) ;
1019
995
}
1020
996
1021
997
// Sort output in BIP-69 order (amount, scriptPubkey). Tie-breaks based on HTLC
1022
998
// CLTV expiration height.
1023
999
sort_outputs ( & mut txouts, |a, b| {
1024
1000
if let & ( _, Some ( ref a_htlcout) ) = a {
1025
1001
if let & ( _, Some ( ref b_htlcout) ) = b {
1026
- a_htlcout. 0 . cltv_expiry . cmp ( & b_htlcout. 0 . cltv_expiry )
1002
+ a_htlcout. cltv_expiry . cmp ( & b_htlcout. cltv_expiry )
1027
1003
} else {
1028
1004
cmp:: Ordering :: Equal
1029
1005
}
1030
1006
} else {
1031
1007
cmp:: Ordering :: Equal
1032
1008
}
1033
1009
} ) ;
1034
- Ok ( txouts)
1010
+
1011
+ let mut outputs = Vec :: with_capacity ( txouts. len ( ) ) ;
1012
+ let mut scripts = Vec :: new ( ) ;
1013
+ scripts. clear ( ) ; // Prevent mut warning
1014
+ for ( idx, out) in txouts. drain ( ..) . enumerate ( ) {
1015
+ if let Some ( htlc) = ( out. 1 ) . 1 {
1016
+ htlc. transaction_output_index = Some ( idx as u32 ) ;
1017
+ htlcs. push ( htlc. clone ( ) ) ;
1018
+ }
1019
+ outputs. push ( out. 0 ) ;
1020
+ #[ cfg( feature = "_test_utils" ) ]
1021
+ scripts. push ( ( out. 1 ) . 0 ) ;
1022
+ }
1023
+ Ok ( ( outputs, htlcs, scripts) )
1035
1024
}
1036
1025
1037
1026
fn internal_build_inputs ( commitment_number : u64 , channel_parameters : & DirectedChannelTransactionParameters ) -> ( u64 , Vec < TxIn > ) {
@@ -1116,7 +1105,7 @@ impl CommitmentTransaction {
1116
1105
if keys != self . keys {
1117
1106
return Err ( ( ) ) ;
1118
1107
}
1119
- let ( tx , _ ) = self . internal_rebuild_transaction ( & keys, channel_parameters) ?;
1108
+ let tx = self . internal_rebuild_transaction ( & keys, channel_parameters) ?;
1120
1109
if self . built . transaction != tx. transaction || self . built . txid != tx. txid {
1121
1110
return Err ( ( ) ) ;
1122
1111
}
0 commit comments