Skip to content

Commit 305ddb4

Browse files
committed
Remove unnecessary allocation in send_commitment_no_state_update
1 parent 5208a28 commit 305ddb4

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

lightning/src/ln/channel.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6731,7 +6731,7 @@ impl<SP: Deref> FundedChannel<SP> where
67316731
log_trace!(logger, "Regenerating latest commitment update in channel {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
67326732
&self.context.channel_id(), if update_fee.is_some() { " update_fee," } else { "" },
67336733
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
6734-
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger).map(|(cu, _)| cu) {
6734+
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger) {
67356735
if self.context.signer_pending_commitment_update {
67366736
log_trace!(logger, "Commitment update generated: clearing signer_pending_commitment_update");
67376737
self.context.signer_pending_commitment_update = false;
@@ -8508,7 +8508,7 @@ impl<SP: Deref> FundedChannel<SP> where
85088508

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

85238523
{
8524-
let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
8525-
for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
8526-
htlcs.push(htlc);
8527-
}
8528-
85298524
let res = ecdsa.sign_counterparty_commitment(
85308525
&self.funding.channel_transaction_parameters,
85318526
&commitment_stats.tx,
@@ -8542,7 +8537,7 @@ impl<SP: Deref> FundedChannel<SP> where
85428537
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
85438538

85448539
let counterparty_keys = commitment_stats.tx.trust().keys();
8545-
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8540+
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
85468541
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
85478542
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)),
85488543
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
@@ -8551,14 +8546,14 @@ impl<SP: Deref> FundedChannel<SP> where
85518546
}
85528547
}
85538548

8554-
Ok((msgs::CommitmentSigned {
8549+
Ok(msgs::CommitmentSigned {
85558550
channel_id: self.context.channel_id,
85568551
signature,
85578552
htlc_signatures,
85588553
batch: None,
85598554
#[cfg(taproot)]
85608555
partial_signature_with_nonce: None,
8561-
}, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
8556+
})
85628557
},
85638558
// TODO (taproot|arik)
85648559
#[cfg(taproot)]
@@ -11609,14 +11604,8 @@ mod tests {
1160911604
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $opt_anchors: expr, {
1161011605
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
1161111606
} ) => { {
11612-
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11613-
let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11614-
11615-
let htlcs = commitment_stats.htlcs_included.drain(..)
11616-
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
11617-
.collect();
11618-
(commitment_stats.tx, htlcs)
11619-
};
11607+
let commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11608+
let commitment_tx = commitment_stats.tx;
1162011609
let trusted_tx = commitment_tx.trust();
1162111610
let unsigned_tx = trusted_tx.built_transaction();
1162211611
let redeemscript = chan.funding.get_funding_redeemscript();
@@ -11631,10 +11620,10 @@ mod tests {
1163111620
counterparty_htlc_sigs.clear(); // Don't warn about excess mut for no-HTLC calls
1163211621
$({
1163311622
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
11634-
per_htlc.push((htlcs[$htlc_idx].clone(), Some(remote_signature)));
11623+
per_htlc.push((commitment_tx.htlcs()[$htlc_idx].clone(), Some(remote_signature)));
1163511624
counterparty_htlc_sigs.push(remote_signature);
1163611625
})*
11637-
assert_eq!(htlcs.len(), per_htlc.len());
11626+
assert_eq!(commitment_tx.htlcs().len(), per_htlc.len());
1163811627

1163911628
let holder_commitment_tx = HolderCommitmentTransaction::new(
1164011629
commitment_tx.clone(),
@@ -11657,7 +11646,7 @@ mod tests {
1165711646
log_trace!(logger, "verifying htlc {}", $htlc_idx);
1165811647
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
1165911648

11660-
let ref htlc = htlcs[$htlc_idx];
11649+
let ref htlc = commitment_tx.htlcs()[$htlc_idx];
1166111650
let keys = commitment_tx.trust().keys();
1166211651
let mut htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.context.feerate_per_kw,
1166311652
chan.funding.get_counterparty_selected_contest_delay().unwrap(),

0 commit comments

Comments
 (0)