@@ -1105,7 +1105,7 @@ impl_writeable_tlv_based!(HolderCommitmentTransaction, {
11051105
11061106impl HolderCommitmentTransaction {
11071107 #[ cfg( test) ]
1108- pub fn dummy ( htlcs : & mut Vec < ( HTLCOutputInCommitment , ( ) ) > ) -> Self {
1108+ pub fn dummy ( nondust_htlcs : & mut Vec < HTLCOutputInCommitment > ) -> Self {
11091109 let secp_ctx = Secp256k1 :: new ( ) ;
11101110 let dummy_key = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
11111111 let dummy_sig = sign ( & secp_ctx, & secp256k1:: Message :: from_digest ( [ 42 ; 32 ] ) , & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
@@ -1133,11 +1133,11 @@ impl HolderCommitmentTransaction {
11331133 channel_type_features : ChannelTypeFeatures :: only_static_remote_key ( ) ,
11341134 } ;
11351135 let mut counterparty_htlc_sigs = Vec :: new ( ) ;
1136- for _ in 0 ..htlcs . len ( ) {
1136+ for _ in 0 ..nondust_htlcs . len ( ) {
11371137 counterparty_htlc_sigs. push ( dummy_sig) ;
11381138 }
1139- let inner = CommitmentTransaction :: new_with_auxiliary_htlc_data ( 0 , 0 , 0 , dummy_key. clone ( ) , dummy_key. clone ( ) , keys, 0 , htlcs , & channel_parameters. as_counterparty_broadcastable ( ) ) ;
1140- htlcs . sort_by_key ( |htlc| htlc. 0 . transaction_output_index ) ;
1139+ let inner = CommitmentTransaction :: new ( 0 , 0 , 0 , dummy_key. clone ( ) , dummy_key. clone ( ) , keys, 0 , nondust_htlcs . iter_mut ( ) , & channel_parameters. as_counterparty_broadcastable ( ) ) ;
1140+ nondust_htlcs . sort_by_key ( |htlc| htlc. transaction_output_index ) ;
11411141 HolderCommitmentTransaction {
11421142 inner,
11431143 counterparty_sig : dummy_sig,
@@ -1440,18 +1440,15 @@ impl CommitmentTransaction {
14401440 ///
14411441 /// Populates HTLCOutputInCommitment.transaction_output_index in htlcs_with_aux.
14421442 ///
1443- /// The generic T allows the caller to match the HTLC output index with auxiliary data.
1444- /// This auxiliary data is not stored in this object.
1445- ///
14461443 /// Only include HTLCs that are above the dust limit for the channel.
14471444 ///
14481445 /// This is not exported to bindings users due to the generic though we likely should expose a version without
1449- pub fn new_with_auxiliary_htlc_data < T > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , broadcaster_funding_key : PublicKey , countersignatory_funding_key : PublicKey , keys : TxCreationKeys , feerate_per_kw : u32 , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters ) -> CommitmentTransaction {
1446+ pub fn new < ' a > ( commitment_number : u64 , to_broadcaster_value_sat : u64 , to_countersignatory_value_sat : u64 , broadcaster_funding_key : PublicKey , countersignatory_funding_key : PublicKey , keys : TxCreationKeys , feerate_per_kw : u32 , nondust_htlcs : impl Iterator < Item = & ' a mut HTLCOutputInCommitment > , channel_parameters : & DirectedChannelTransactionParameters ) -> CommitmentTransaction {
14501447 let to_broadcaster_value_sat = Amount :: from_sat ( to_broadcaster_value_sat) ;
14511448 let to_countersignatory_value_sat = Amount :: from_sat ( to_countersignatory_value_sat) ;
14521449
14531450 // Sort outputs and populate output indices while keeping track of the auxiliary data
1454- let ( outputs, htlcs ) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux , channel_parameters, & broadcaster_funding_key, & countersignatory_funding_key) . unwrap ( ) ;
1451+ let ( outputs, nondust_htlcs ) = Self :: internal_build_outputs ( & keys, to_broadcaster_value_sat, to_countersignatory_value_sat, nondust_htlcs , channel_parameters, & broadcaster_funding_key, & countersignatory_funding_key) . unwrap ( ) ;
14551452
14561453 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
14571454 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
@@ -1462,7 +1459,7 @@ impl CommitmentTransaction {
14621459 to_countersignatory_value_sat,
14631460 to_broadcaster_delay : Some ( channel_parameters. contest_delay ( ) ) ,
14641461 feerate_per_kw,
1465- htlcs,
1462+ htlcs : nondust_htlcs ,
14661463 channel_type_features : channel_parameters. channel_type_features ( ) . clone ( ) ,
14671464 keys,
14681465 built : BuiltCommitmentTransaction {
@@ -1483,8 +1480,8 @@ impl CommitmentTransaction {
14831480 fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < BuiltCommitmentTransaction , ( ) > {
14841481 let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
14851482
1486- let mut htlcs_with_aux = self . htlcs . iter ( ) . map ( |h| ( h . clone ( ) , ( ) ) ) . collect ( ) ;
1487- let ( outputs, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , & mut htlcs_with_aux , channel_parameters, broadcaster_funding_key, countersignatory_funding_key) ?;
1483+ let mut nondust_htlcs = self . htlcs . clone ( ) ;
1484+ let ( outputs, _) = Self :: internal_build_outputs ( keys, self . to_broadcaster_value_sat , self . to_countersignatory_value_sat , nondust_htlcs . iter_mut ( ) , channel_parameters, broadcaster_funding_key, countersignatory_funding_key) ?;
14881485
14891486 let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
14901487 let txid = transaction. compute_txid ( ) ;
@@ -1508,12 +1505,24 @@ impl CommitmentTransaction {
15081505 // - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
15091506 // caller needs to have sorted together with the HTLCs so it can keep track of the output index
15101507 // - building of a bitcoin transaction during a verify() call, in which case T is just ()
1511- fn internal_build_outputs < T > ( keys : & TxCreationKeys , to_broadcaster_value_sat : Amount , to_countersignatory_value_sat : Amount , htlcs_with_aux : & mut Vec < ( HTLCOutputInCommitment , T ) > , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < ( Vec < TxOut > , Vec < HTLCOutputInCommitment > ) , ( ) > {
1508+ fn internal_build_outputs < ' a > ( keys : & TxCreationKeys , to_broadcaster_value_sat : Amount , to_countersignatory_value_sat : Amount , nondust_htlcs : impl Iterator < Item = & ' a mut HTLCOutputInCommitment > , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < ( Vec < TxOut > , Vec < HTLCOutputInCommitment > ) , ( ) > {
15121509 let countersignatory_pubkeys = channel_parameters. countersignatory_pubkeys ( ) ;
15131510 let contest_delay = channel_parameters. contest_delay ( ) ;
15141511
15151512 let mut txouts: Vec < ( TxOut , Option < & mut HTLCOutputInCommitment > ) > = Vec :: new ( ) ;
15161513
1514+ for htlc in nondust_htlcs {
1515+ let script = get_htlc_redeemscript ( & htlc, & channel_parameters. channel_type_features ( ) , & keys) ;
1516+ let txout = TxOut {
1517+ script_pubkey : script. to_p2wsh ( ) ,
1518+ value : htlc. to_bitcoin_amount ( ) ,
1519+ } ;
1520+ txouts. push ( ( txout, Some ( htlc) ) ) ;
1521+ }
1522+ // This removes the need for the `ExactSizeIterator` bound on `nondust_htlcs`
1523+ let mut htlcs = Vec :: with_capacity ( txouts. len ( ) ) ;
1524+ let htlcs_empty = txouts. is_empty ( ) ;
1525+
15171526 if to_countersignatory_value_sat > Amount :: ZERO {
15181527 let script = if channel_parameters. channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
15191528 get_to_countersignatory_with_anchors_redeemscript ( & countersignatory_pubkeys. payment_point ) . to_p2wsh ( )
@@ -1545,7 +1554,7 @@ impl CommitmentTransaction {
15451554 }
15461555
15471556 if channel_parameters. channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
1548- if to_broadcaster_value_sat > Amount :: ZERO || !htlcs_with_aux . is_empty ( ) {
1557+ if to_broadcaster_value_sat > Amount :: ZERO || !htlcs_empty {
15491558 let anchor_script = get_anchor_redeemscript ( broadcaster_funding_key) ;
15501559 txouts. push ( (
15511560 TxOut {
@@ -1556,7 +1565,7 @@ impl CommitmentTransaction {
15561565 ) ) ;
15571566 }
15581567
1559- if to_countersignatory_value_sat > Amount :: ZERO || !htlcs_with_aux . is_empty ( ) {
1568+ if to_countersignatory_value_sat > Amount :: ZERO || !htlcs_empty {
15601569 let anchor_script = get_anchor_redeemscript ( countersignatory_funding_key) ;
15611570 txouts. push ( (
15621571 TxOut {
@@ -1568,16 +1577,6 @@ impl CommitmentTransaction {
15681577 }
15691578 }
15701579
1571- let mut htlcs = Vec :: with_capacity ( htlcs_with_aux. len ( ) ) ;
1572- for ( htlc, _) in htlcs_with_aux {
1573- let script = get_htlc_redeemscript ( & htlc, & channel_parameters. channel_type_features ( ) , & keys) ;
1574- let txout = TxOut {
1575- script_pubkey : script. to_p2wsh ( ) ,
1576- value : htlc. to_bitcoin_amount ( ) ,
1577- } ;
1578- txouts. push ( ( txout, Some ( htlc) ) ) ;
1579- }
1580-
15811580 // Sort output in BIP-69 order (amount, scriptPubkey). Tie-breaks based on HTLC
15821581 // CLTV expiration height.
15831582 sort_outputs ( & mut txouts, |a, b| {
@@ -1915,7 +1914,7 @@ mod tests {
19151914 counterparty_funding_pubkey : PublicKey ,
19161915 keys : TxCreationKeys ,
19171916 feerate_per_kw : u32 ,
1918- htlcs_with_aux : Vec < ( HTLCOutputInCommitment , ( ) ) > ,
1917+ nondust_htlcs : Vec < HTLCOutputInCommitment > ,
19191918 channel_parameters : ChannelTransactionParameters ,
19201919 counterparty_pubkeys : ChannelPublicKeys ,
19211920 }
@@ -1943,27 +1942,27 @@ mod tests {
19431942 funding_outpoint : Some ( chain:: transaction:: OutPoint { txid : Txid :: all_zeros ( ) , index : 0 } ) ,
19441943 channel_type_features : ChannelTypeFeatures :: only_static_remote_key ( ) ,
19451944 } ;
1946- let htlcs_with_aux = Vec :: new ( ) ;
1945+ let nondust_htlcs = Vec :: new ( ) ;
19471946
19481947 Self {
19491948 commitment_number : 0 ,
19501949 holder_funding_pubkey : holder_pubkeys. funding_pubkey ,
19511950 counterparty_funding_pubkey : counterparty_pubkeys. funding_pubkey ,
19521951 keys,
19531952 feerate_per_kw : 1 ,
1954- htlcs_with_aux ,
1953+ nondust_htlcs ,
19551954 channel_parameters,
19561955 counterparty_pubkeys,
19571956 }
19581957 }
19591958
19601959 fn build ( & mut self , to_broadcaster_sats : u64 , to_countersignatory_sats : u64 ) -> CommitmentTransaction {
1961- CommitmentTransaction :: new_with_auxiliary_htlc_data (
1960+ CommitmentTransaction :: new (
19621961 self . commitment_number , to_broadcaster_sats, to_countersignatory_sats,
19631962 self . holder_funding_pubkey . clone ( ) ,
19641963 self . counterparty_funding_pubkey . clone ( ) ,
19651964 self . keys . clone ( ) , self . feerate_per_kw ,
1966- & mut self . htlcs_with_aux , & self . channel_parameters . as_holder_broadcastable ( )
1965+ self . nondust_htlcs . iter_mut ( ) , & self . channel_parameters . as_holder_broadcastable ( )
19671966 )
19681967 }
19691968 }
@@ -2009,7 +2008,7 @@ mod tests {
20092008
20102009 // Generate broadcaster output and received and offered HTLC outputs, w/o anchors
20112010 builder. channel_parameters . channel_type_features = ChannelTypeFeatures :: only_static_remote_key ( ) ;
2012- builder. htlcs_with_aux = vec ! [ ( received_htlc. clone( ) , ( ) ) , ( offered_htlc. clone( ) , ( ) ) ] ;
2011+ builder. nondust_htlcs = vec ! [ received_htlc. clone( ) , offered_htlc. clone( ) ] ;
20132012 let tx = builder. build ( 3000 , 0 ) ;
20142013 let keys = & builder. keys . clone ( ) ;
20152014 assert_eq ! ( tx. built. transaction. output. len( ) , 3 ) ;
@@ -2022,7 +2021,7 @@ mod tests {
20222021
20232022 // Generate broadcaster output and received and offered HTLC outputs, with anchors
20242023 builder. channel_parameters . channel_type_features = ChannelTypeFeatures :: anchors_zero_htlc_fee_and_dependencies ( ) ;
2025- builder. htlcs_with_aux = vec ! [ ( received_htlc. clone( ) , ( ) ) , ( offered_htlc. clone( ) , ( ) ) ] ;
2024+ builder. nondust_htlcs = vec ! [ received_htlc. clone( ) , offered_htlc. clone( ) ] ;
20262025 let tx = builder. build ( 3000 , 0 ) ;
20272026 assert_eq ! ( tx. built. transaction. output. len( ) , 5 ) ;
20282027 assert_eq ! ( tx. built. transaction. output[ 2 ] . script_pubkey, get_htlc_redeemscript( & received_htlc, & ChannelTypeFeatures :: anchors_zero_htlc_fee_and_dependencies( ) , & keys) . to_p2wsh( ) ) ;
0 commit comments