Skip to content

Commit 592f17d

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 1023d0b commit 592f17d

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
@@ -6831,7 +6831,7 @@ impl<SP: Deref> FundedChannel<SP> where
68316831
log_trace!(logger, "Regenerating latest commitment update in channel {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
68326832
&self.context.channel_id(), if update_fee.is_some() { " update_fee," } else { "" },
68336833
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
6834-
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger).map(|(cu, _)| cu) {
6834+
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger) {
68356835
if self.context.signer_pending_commitment_update {
68366836
log_trace!(logger, "Commitment update generated: clearing signer_pending_commitment_update");
68376837
self.context.signer_pending_commitment_update = false;
@@ -8745,7 +8745,7 @@ impl<SP: Deref> FundedChannel<SP> where
87458745

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

87608760
{
8761-
let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
8762-
for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
8763-
htlcs.push(htlc);
8764-
}
8765-
87668761
let res = ecdsa.sign_counterparty_commitment(
87678762
&self.funding.channel_transaction_parameters,
87688763
&commitment_stats.tx,
@@ -8779,7 +8774,8 @@ impl<SP: Deref> FundedChannel<SP> where
87798774
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
87808775

87818776
let counterparty_keys = commitment_stats.tx.trust().keys();
8782-
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8777+
debug_assert_eq!(htlc_signatures.len(), commitment_stats.tx.htlcs().len());
8778+
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
87838779
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
87848780
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)),
87858781
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
@@ -8788,14 +8784,14 @@ impl<SP: Deref> FundedChannel<SP> where
87888784
}
87898785
}
87908786

8791-
Ok((msgs::CommitmentSigned {
8787+
Ok(msgs::CommitmentSigned {
87928788
channel_id: self.context.channel_id,
87938789
signature,
87948790
htlc_signatures,
87958791
batch: None,
87968792
#[cfg(taproot)]
87978793
partial_signature_with_nonce: None,
8798-
}, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
8794+
})
87998795
},
88008796
// TODO (taproot|arik)
88018797
#[cfg(taproot)]
@@ -11886,14 +11882,8 @@ mod tests {
1188611882
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $opt_anchors: expr, {
1188711883
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
1188811884
} ) => { {
11889-
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11890-
let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11891-
11892-
let htlcs = commitment_stats.htlcs_included.drain(..)
11893-
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
11894-
.collect();
11895-
(commitment_stats.tx, htlcs)
11896-
};
11885+
let commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11886+
let commitment_tx = commitment_stats.tx;
1189711887
let trusted_tx = commitment_tx.trust();
1189811888
let unsigned_tx = trusted_tx.built_transaction();
1189911889
let redeemscript = chan.funding.get_funding_redeemscript();
@@ -11908,10 +11898,10 @@ mod tests {
1190811898
counterparty_htlc_sigs.clear(); // Don't warn about excess mut for no-HTLC calls
1190911899
$({
1191011900
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
11911-
per_htlc.push((htlcs[$htlc_idx].clone(), Some(remote_signature)));
11901+
per_htlc.push((commitment_tx.htlcs()[$htlc_idx].clone(), Some(remote_signature)));
1191211902
counterparty_htlc_sigs.push(remote_signature);
1191311903
})*
11914-
assert_eq!(htlcs.len(), per_htlc.len());
11904+
assert_eq!(commitment_tx.htlcs().len(), per_htlc.len());
1191511905

1191611906
let holder_commitment_tx = HolderCommitmentTransaction::new(
1191711907
commitment_tx.clone(),
@@ -11934,7 +11924,7 @@ mod tests {
1193411924
log_trace!(logger, "verifying htlc {}", $htlc_idx);
1193511925
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
1193611926

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

0 commit comments

Comments
 (0)