Skip to content

Commit 61fd4eb

Browse files
committed
Remove unnecessary allocation in send_commitment_no_state_update
1 parent 5ae1430 commit 61fd4eb

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
@@ -6764,7 +6764,7 @@ impl<SP: Deref> FundedChannel<SP> where
67646764
log_trace!(logger, "Regenerating latest commitment update in channel {} with{} {} update_adds, {} update_fulfills, {} update_fails, and {} update_fail_malformeds",
67656765
&self.context.channel_id(), if update_fee.is_some() { " update_fee," } else { "" },
67666766
update_add_htlcs.len(), update_fulfill_htlcs.len(), update_fail_htlcs.len(), update_fail_malformed_htlcs.len());
6767-
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger).map(|(cu, _)| cu) {
6767+
let commitment_signed = if let Ok(update) = self.send_commitment_no_state_update(logger) {
67686768
if self.context.signer_pending_commitment_update {
67696769
log_trace!(logger, "Commitment update generated: clearing signer_pending_commitment_update");
67706770
self.context.signer_pending_commitment_update = false;
@@ -8545,7 +8545,7 @@ impl<SP: Deref> FundedChannel<SP> where
85458545

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

85608560
{
8561-
let mut htlcs = Vec::with_capacity(commitment_stats.htlcs_included.len());
8562-
for &(ref htlc, _) in commitment_stats.htlcs_included.iter() {
8563-
htlcs.push(htlc);
8564-
}
8565-
85668561
let res = ecdsa.sign_counterparty_commitment(
85678562
&self.funding.channel_transaction_parameters,
85688563
&commitment_stats.tx,
@@ -8579,7 +8574,7 @@ impl<SP: Deref> FundedChannel<SP> where
85798574
log_bytes!(signature.serialize_compact()[..]), &self.context.channel_id());
85808575

85818576
let counterparty_keys = commitment_stats.tx.trust().keys();
8582-
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(htlcs) {
8577+
for (ref htlc_sig, ref htlc) in htlc_signatures.iter().zip(commitment_stats.tx.htlcs()) {
85838578
log_trace!(logger, "Signed remote HTLC tx {} with redeemscript {} with pubkey {} -> {} in channel {}",
85848579
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)),
85858580
encode::serialize_hex(&chan_utils::get_htlc_redeemscript(&htlc, &self.context.channel_type, &counterparty_keys)),
@@ -8588,14 +8583,14 @@ impl<SP: Deref> FundedChannel<SP> where
85888583
}
85898584
}
85908585

8591-
Ok((msgs::CommitmentSigned {
8586+
Ok(msgs::CommitmentSigned {
85928587
channel_id: self.context.channel_id,
85938588
signature,
85948589
htlc_signatures,
85958590
batch: None,
85968591
#[cfg(taproot)]
85978592
partial_signature_with_nonce: None,
8598-
}, (counterparty_commitment_txid, commitment_stats.htlcs_included)))
8593+
})
85998594
},
86008595
// TODO (taproot|arik)
86018596
#[cfg(taproot)]
@@ -11649,14 +11644,8 @@ mod tests {
1164911644
( $counterparty_sig_hex: expr, $sig_hex: expr, $tx_hex: expr, $opt_anchors: expr, {
1165011645
$( { $htlc_idx: expr, $counterparty_htlc_sig_hex: expr, $htlc_sig_hex: expr, $htlc_tx_hex: expr } ), *
1165111646
} ) => { {
11652-
let (commitment_tx, htlcs): (_, Vec<HTLCOutputInCommitment>) = {
11653-
let mut commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11654-
11655-
let htlcs = commitment_stats.htlcs_included.drain(..)
11656-
.filter_map(|(htlc, _)| if htlc.transaction_output_index.is_some() { Some(htlc) } else { None })
11657-
.collect();
11658-
(commitment_stats.tx, htlcs)
11659-
};
11647+
let commitment_stats = chan.context.build_commitment_transaction(&chan.funding, 0xffffffffffff - 42, &per_commitment_point, true, false, &logger);
11648+
let commitment_tx = commitment_stats.tx;
1166011649
let trusted_tx = commitment_tx.trust();
1166111650
let unsigned_tx = trusted_tx.built_transaction();
1166211651
let redeemscript = chan.funding.get_funding_redeemscript();
@@ -11671,10 +11660,10 @@ mod tests {
1167111660
counterparty_htlc_sigs.clear(); // Don't warn about excess mut for no-HTLC calls
1167211661
$({
1167311662
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
11674-
per_htlc.push((htlcs[$htlc_idx].clone(), Some(remote_signature)));
11663+
per_htlc.push((commitment_tx.htlcs()[$htlc_idx].clone(), Some(remote_signature)));
1167511664
counterparty_htlc_sigs.push(remote_signature);
1167611665
})*
11677-
assert_eq!(htlcs.len(), per_htlc.len());
11666+
assert_eq!(commitment_tx.htlcs().len(), per_htlc.len());
1167811667

1167911668
let holder_commitment_tx = HolderCommitmentTransaction::new(
1168011669
commitment_tx.clone(),
@@ -11697,7 +11686,7 @@ mod tests {
1169711686
log_trace!(logger, "verifying htlc {}", $htlc_idx);
1169811687
let remote_signature = Signature::from_der(&<Vec<u8>>::from_hex($counterparty_htlc_sig_hex).unwrap()[..]).unwrap();
1169911688

11700-
let ref htlc = htlcs[$htlc_idx];
11689+
let ref htlc = commitment_tx.htlcs()[$htlc_idx];
1170111690
let keys = commitment_tx.trust().keys();
1170211691
let mut htlc_tx = chan_utils::build_htlc_transaction(&unsigned_tx.txid, chan.context.feerate_per_kw,
1170311692
chan.funding.get_counterparty_selected_contest_delay().unwrap(),

0 commit comments

Comments
 (0)