@@ -1105,7 +1105,7 @@ impl_writeable_tlv_based!(HolderCommitmentTransaction, {
1105
1105
1106
1106
impl HolderCommitmentTransaction {
1107
1107
#[ cfg( test) ]
1108
- pub fn dummy ( htlcs : & mut Vec < ( HTLCOutputInCommitment , ( ) ) > ) -> Self {
1108
+ pub fn dummy ( nondust_htlcs : & mut Vec < HTLCOutputInCommitment > ) -> Self {
1109
1109
let secp_ctx = Secp256k1 :: new ( ) ;
1110
1110
let dummy_key = PublicKey :: from_secret_key ( & secp_ctx, & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
1111
1111
let dummy_sig = sign ( & secp_ctx, & secp256k1:: Message :: from_digest ( [ 42 ; 32 ] ) , & SecretKey :: from_slice ( & [ 42 ; 32 ] ) . unwrap ( ) ) ;
@@ -1133,11 +1133,11 @@ impl HolderCommitmentTransaction {
1133
1133
channel_type_features : ChannelTypeFeatures :: only_static_remote_key ( ) ,
1134
1134
} ;
1135
1135
let mut counterparty_htlc_sigs = Vec :: new ( ) ;
1136
- for _ in 0 ..htlcs . len ( ) {
1136
+ for _ in 0 ..nondust_htlcs . len ( ) {
1137
1137
counterparty_htlc_sigs. push ( dummy_sig) ;
1138
1138
}
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 ) ;
1141
1141
HolderCommitmentTransaction {
1142
1142
inner,
1143
1143
counterparty_sig : dummy_sig,
@@ -1440,18 +1440,15 @@ impl CommitmentTransaction {
1440
1440
///
1441
1441
/// Populates HTLCOutputInCommitment.transaction_output_index in htlcs_with_aux.
1442
1442
///
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
- ///
1446
1443
/// Only include HTLCs that are above the dust limit for the channel.
1447
1444
///
1448
1445
/// 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 {
1450
1447
let to_broadcaster_value_sat = Amount :: from_sat ( to_broadcaster_value_sat) ;
1451
1448
let to_countersignatory_value_sat = Amount :: from_sat ( to_countersignatory_value_sat) ;
1452
1449
1453
1450
// 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 ( ) ;
1455
1452
1456
1453
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( commitment_number, channel_parameters) ;
1457
1454
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
@@ -1462,7 +1459,7 @@ impl CommitmentTransaction {
1462
1459
to_countersignatory_value_sat,
1463
1460
to_broadcaster_delay : Some ( channel_parameters. contest_delay ( ) ) ,
1464
1461
feerate_per_kw,
1465
- htlcs,
1462
+ htlcs : nondust_htlcs ,
1466
1463
channel_type_features : channel_parameters. channel_type_features ( ) . clone ( ) ,
1467
1464
keys,
1468
1465
built : BuiltCommitmentTransaction {
@@ -1483,8 +1480,8 @@ impl CommitmentTransaction {
1483
1480
fn internal_rebuild_transaction ( & self , keys : & TxCreationKeys , channel_parameters : & DirectedChannelTransactionParameters , broadcaster_funding_key : & PublicKey , countersignatory_funding_key : & PublicKey ) -> Result < BuiltCommitmentTransaction , ( ) > {
1484
1481
let ( obscured_commitment_transaction_number, txins) = Self :: internal_build_inputs ( self . commitment_number , channel_parameters) ;
1485
1482
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) ?;
1488
1485
1489
1486
let transaction = Self :: make_transaction ( obscured_commitment_transaction_number, txins, outputs) ;
1490
1487
let txid = transaction. compute_txid ( ) ;
@@ -1508,12 +1505,24 @@ impl CommitmentTransaction {
1508
1505
// - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
1509
1506
// caller needs to have sorted together with the HTLCs so it can keep track of the output index
1510
1507
// - 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 > ) , ( ) > {
1512
1509
let countersignatory_pubkeys = channel_parameters. countersignatory_pubkeys ( ) ;
1513
1510
let contest_delay = channel_parameters. contest_delay ( ) ;
1514
1511
1515
1512
let mut txouts: Vec < ( TxOut , Option < & mut HTLCOutputInCommitment > ) > = Vec :: new ( ) ;
1516
1513
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
+
1517
1526
if to_countersignatory_value_sat > Amount :: ZERO {
1518
1527
let script = if channel_parameters. channel_type_features ( ) . supports_anchors_zero_fee_htlc_tx ( ) {
1519
1528
get_to_countersignatory_with_anchors_redeemscript ( & countersignatory_pubkeys. payment_point ) . to_p2wsh ( )
@@ -1545,7 +1554,7 @@ impl CommitmentTransaction {
1545
1554
}
1546
1555
1547
1556
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 {
1549
1558
let anchor_script = get_anchor_redeemscript ( broadcaster_funding_key) ;
1550
1559
txouts. push ( (
1551
1560
TxOut {
@@ -1556,7 +1565,7 @@ impl CommitmentTransaction {
1556
1565
) ) ;
1557
1566
}
1558
1567
1559
- if to_countersignatory_value_sat > Amount :: ZERO || !htlcs_with_aux . is_empty ( ) {
1568
+ if to_countersignatory_value_sat > Amount :: ZERO || !htlcs_empty {
1560
1569
let anchor_script = get_anchor_redeemscript ( countersignatory_funding_key) ;
1561
1570
txouts. push ( (
1562
1571
TxOut {
@@ -1568,16 +1577,6 @@ impl CommitmentTransaction {
1568
1577
}
1569
1578
}
1570
1579
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
-
1581
1580
// Sort output in BIP-69 order (amount, scriptPubkey). Tie-breaks based on HTLC
1582
1581
// CLTV expiration height.
1583
1582
sort_outputs ( & mut txouts, |a, b| {
@@ -1915,7 +1914,7 @@ mod tests {
1915
1914
counterparty_funding_pubkey : PublicKey ,
1916
1915
keys : TxCreationKeys ,
1917
1916
feerate_per_kw : u32 ,
1918
- htlcs_with_aux : Vec < ( HTLCOutputInCommitment , ( ) ) > ,
1917
+ nondust_htlcs : Vec < HTLCOutputInCommitment > ,
1919
1918
channel_parameters : ChannelTransactionParameters ,
1920
1919
counterparty_pubkeys : ChannelPublicKeys ,
1921
1920
}
@@ -1943,27 +1942,27 @@ mod tests {
1943
1942
funding_outpoint : Some ( chain:: transaction:: OutPoint { txid : Txid :: all_zeros ( ) , index : 0 } ) ,
1944
1943
channel_type_features : ChannelTypeFeatures :: only_static_remote_key ( ) ,
1945
1944
} ;
1946
- let htlcs_with_aux = Vec :: new ( ) ;
1945
+ let nondust_htlcs = Vec :: new ( ) ;
1947
1946
1948
1947
Self {
1949
1948
commitment_number : 0 ,
1950
1949
holder_funding_pubkey : holder_pubkeys. funding_pubkey ,
1951
1950
counterparty_funding_pubkey : counterparty_pubkeys. funding_pubkey ,
1952
1951
keys,
1953
1952
feerate_per_kw : 1 ,
1954
- htlcs_with_aux ,
1953
+ nondust_htlcs ,
1955
1954
channel_parameters,
1956
1955
counterparty_pubkeys,
1957
1956
}
1958
1957
}
1959
1958
1960
1959
fn build ( & mut self , to_broadcaster_sats : u64 , to_countersignatory_sats : u64 ) -> CommitmentTransaction {
1961
- CommitmentTransaction :: new_with_auxiliary_htlc_data (
1960
+ CommitmentTransaction :: new (
1962
1961
self . commitment_number , to_broadcaster_sats, to_countersignatory_sats,
1963
1962
self . holder_funding_pubkey . clone ( ) ,
1964
1963
self . counterparty_funding_pubkey . clone ( ) ,
1965
1964
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 ( )
1967
1966
)
1968
1967
}
1969
1968
}
@@ -2009,7 +2008,7 @@ mod tests {
2009
2008
2010
2009
// Generate broadcaster output and received and offered HTLC outputs, w/o anchors
2011
2010
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( ) ] ;
2013
2012
let tx = builder. build ( 3000 , 0 ) ;
2014
2013
let keys = & builder. keys . clone ( ) ;
2015
2014
assert_eq ! ( tx. built. transaction. output. len( ) , 3 ) ;
@@ -2022,7 +2021,7 @@ mod tests {
2022
2021
2023
2022
// Generate broadcaster output and received and offered HTLC outputs, with anchors
2024
2023
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( ) ] ;
2026
2025
let tx = builder. build ( 3000 , 0 ) ;
2027
2026
assert_eq ! ( tx. built. transaction. output. len( ) , 5 ) ;
2028
2027
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