Skip to content

Commit 1c4a496

Browse files
committed
Remove unnecessary allocation in send_commitment_no_state_update
The logging for loop in `send_commitment_no_state_update` only iterates over the non-dust HTLCs. Thus we do not need to create a `Vec<HTLCOutputInCommitment>` that includes both dust and non-dust HTLCs, we can grab the `Vec<HTLCOutputInCommitment>` that holds only non-dust HTLCs directly from `CommitmentTransaction`.
1 parent 2bcdd84 commit 1c4a496

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6833,7 +6833,7 @@ impl<SP: Deref> FundedChannel<SP> where
68336833
log_trace!(logger, "Regenerating latest commitment update in channel {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
68346834
&self.context.channel_id(), if update_fee.is_some() { " update_fee," } else { "" },
68356835
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
6836-
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger).map(|(cu, _)| cu) {
6836+
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger) {
68376837
if self.context.signer_pending_commitment_update {
68386838
log_trace!(logger, "Commitment update generated: clearing signer_pending_commitment_update");
68396839
self.context.signer_pending_commitment_update = false;
@@ -8747,7 +8747,7 @@ impl<SP: Deref> FundedChannel<SP> where
87478747

87488748
/// Only fails in case of signer rejection. Used for channel_reestablish commitment_signed
87498749
/// generation when we shouldn't change HTLC/channel state.
8750-
fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<(msgs::CommitmentSigned, (Txid, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>)), ChannelError> where L::Target: Logger {
8750+
fn send_commitment_no_state_update<L: Deref>(&self, logger: &L) -> Result<msgs::CommitmentSigned, ChannelError> where L::Target: Logger {
87518751
// Get the fee tests from `build_commitment_no_state_update`
87528752
#[cfg(any(test, fuzzing))]
87538753
self.build_commitment_no_state_update(logger);
@@ -8760,11 +8760,6 @@ impl<SP: Deref> FundedChannel<SP> where
87608760
let (signature, htlc_signatures);
87618761

87628762
{
8763-
let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
8764-
for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
8765-
htlcs.push(htlc);
8766-
}
8767-
87688763
let res = ecdsa.sign_counterparty_commitment(
87698764
&self.funding.channel_transaction_parameters,
87708765
&commitment_stats.tx,
@@ -8781,7 +8776,8 @@ impl<SP: Deref> FundedChannel<SP> where
87818776
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
87828777

87838778
let counterparty_keys = commitment_stats.tx.trust().keys();
8784-
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8779+
debug_assert_eq!(htlc_signatures.len(), commitment_stats.tx.htlcs().len());
8780+
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
87858781
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
87868782
encode::serialize_hex(&chan_utils::build_htlc_transaction(&counterparty_commitment_txid, commitment_stats.feerate_per_kw, self.funding.get_holder_selected_contest_delay(), htlc, &self.context.channel_type, &counterparty_keys.broadcaster_delayed_payment_key, &counterparty_keys.revocation_key)),
87878783
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
@@ -8790,14 +8786,14 @@ impl<SP: Deref> FundedChannel<SP> where
87908786
}
87918787
}
87928788

8793-
Ok((msgs::CommitmentSigned {
8789+
Ok(msgs::CommitmentSigned {
87948790
channel_id: self.context.channel_id,
87958791
signature,
87968792
htlc_signatures,
87978793
batch: None,
87988794
#[cfg(taproot)]
87998795
partial_signature_with_nonce: None,
8800-
}, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
8796+
})
88018797
},
88028798
// TODO (taproot|arik)
88038799
#[cfg(taproot)]
@@ -11888,14 +11884,8 @@ mod tests {
1188811884
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $opt_anchors: expr, {
1188911885
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
1189011886
} ) => { {
11891-
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11892-
let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11893-
11894-
let htlcs = commitment_stats.htlcs_included.drain(..)
11895-
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
11896-
.collect();
11897-
(commitment_stats.tx, htlcs)
11898-
};
11887+
let commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11888+
let commitment_tx = commitment_stats.tx;
1189911889
let trusted_tx = commitment_tx.trust();
1190011890
let unsigned_tx = trusted_tx.built_transaction();
1190111891
let redeemscript = chan.funding.get_funding_redeemscript();
@@ -11910,10 +11900,10 @@ mod tests {
1191011900
counterparty_htlc_sigs.clear(); // Don't warn about excess mut for no-HTLC calls
1191111901
$({
1191211902
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
11913-
per_htlc.push((htlcs[$htlc_idx].clone(), Some(remote_signature)));
11903+
per_htlc.push((commitment_tx.htlcs()[$htlc_idx].clone(), Some(remote_signature)));
1191411904
counterparty_htlc_sigs.push(remote_signature);
1191511905
})*
11916-
assert_eq!(htlcs.len(), per_htlc.len());
11906+
assert_eq!(commitment_tx.htlcs().len(), per_htlc.len());
1191711907

1191811908
let holder_commitment_tx = HolderCommitmentTransaction::new(
1191911909
commitment_tx.clone(),
@@ -11936,7 +11926,7 @@ mod tests {
1193611926
log_trace!(logger, "verifying htlc {}", $htlc_idx);
1193711927
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
1193811928

11939-
let ref htlc = htlcs[$htlc_idx];
11929+
let ref htlc = commitment_tx.htlcs()[$htlc_idx];
1194011930
let keys = commitment_tx.trust().keys();
1194111931
let mut htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.context.feerate_per_kw,
1194211932
chan.funding.get_counterparty_selected_contest_delay().unwrap(),

0 commit comments

Comments
 (0)