Skip to content

Commit ac9ade3

Browse files
TheBlueMattAntoine Riard
authored andcommitted
Return refs from build_commitment_transaction, removing clone()s
1 parent 936ba31 commit ac9ade3

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/ln/channel.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ impl Channel {
751751
/// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
752752
/// which peer generated this transaction and "to whom" this transaction flows.
753753
#[inline]
754-
fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, Vec<(HTLCOutputInCommitment, Option<HTLCSource>)>) {
754+
fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) {
755755
let obscured_commitment_transaction_number = self.get_commitment_transaction_number_obscure_factor() ^ (INITIAL_COMMITMENT_NUMBER - commitment_number);
756756

757757
let txins = {
@@ -765,7 +765,7 @@ impl Channel {
765765
ins
766766
};
767767

768-
let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<HTLCSource>)>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
768+
let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<&HTLCSource>)>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
769769

770770
let dust_limit_satoshis = if local { self.our_dust_limit_satoshis } else { self.their_dust_limit_satoshis };
771771
let mut remote_htlc_total_msat = 0;
@@ -830,7 +830,7 @@ impl Channel {
830830
};
831831

832832
if include {
833-
add_htlc_output!(htlc, true, Some(htlc.source.clone()));
833+
add_htlc_output!(htlc, true, Some(&htlc.source));
834834
local_htlc_total_msat += htlc.amount_msat;
835835
} else {
836836
match htlc.state {
@@ -901,7 +901,7 @@ impl Channel {
901901
transaction_utils::sort_outputs(&mut txouts);
902902

903903
let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
904-
let mut htlcs_used: Vec<(HTLCOutputInCommitment, Option<HTLCSource>)> = Vec::with_capacity(txouts.len());
904+
let mut htlcs_used: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(txouts.len());
905905
for (idx, out) in txouts.drain(..).enumerate() {
906906
outputs.push(out.0);
907907
if let Some(mut out_htlc) = out.1 {
@@ -1668,7 +1668,11 @@ impl Channel {
16681668
self.feerate_per_kw
16691669
};
16701670

1671-
let mut local_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
1671+
let mut local_commitment_tx = {
1672+
let mut commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
1673+
let htlcs_cloned: Vec<_> = commitment_tx.1.drain(..).map(|htlc_pair| (htlc_pair.0, htlc_pair.1.map(|source| (*source).clone()))).collect();
1674+
(commitment_tx.0, htlcs_cloned)
1675+
};
16721676
let local_commitment_txid = local_commitment_tx.0.txid();
16731677
let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
16741678
secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer");
@@ -1692,19 +1696,19 @@ impl Channel {
16921696
new_local_commitment_txn.push(local_commitment_tx.0.clone());
16931697

16941698
let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.1.len());
1695-
for (idx, &(ref htlc, ref htlc_source)) in local_commitment_tx.1.iter().enumerate() {
1696-
let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, htlc, true, &local_keys, feerate_per_kw);
1699+
for (idx, (htlc, htlc_source)) in local_commitment_tx.1.drain(..).enumerate() {
1700+
let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, &htlc, true, &local_keys, feerate_per_kw);
16971701
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
16981702
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
16991703
secp_check!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx signature from peer");
17001704
let htlc_sig = if htlc.offered {
1701-
let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, htlc, &local_keys)?;
1705+
let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, &htlc, &local_keys)?;
17021706
new_local_commitment_txn.push(htlc_tx);
17031707
htlc_sig
17041708
} else {
1705-
self.create_htlc_tx_signature(&htlc_tx, htlc, &local_keys)?.1
1709+
self.create_htlc_tx_signature(&htlc_tx, &htlc, &local_keys)?.1
17061710
};
1707-
htlcs_and_sigs.push(((*htlc).clone(), msg.htlc_signatures[idx], htlc_sig, (*htlc_source).clone()));
1711+
htlcs_and_sigs.push((htlc, msg.htlc_signatures[idx], htlc_sig, htlc_source));
17081712
}
17091713

17101714
let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
@@ -3232,7 +3236,7 @@ impl Channel {
32323236
}
32333237

32343238
let remote_keys = self.build_remote_transaction_keys()?;
3235-
let remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true, feerate_per_kw);
3239+
let mut remote_commitment_tx = self.build_commitment_transaction(self.cur_remote_commitment_transaction_number, &remote_keys, false, true, feerate_per_kw);
32363240
let remote_commitment_txid = remote_commitment_tx.0.txid();
32373241
let remote_sighash = Message::from_slice(&bip143::SighashComponents::new(&remote_commitment_tx.0).sighash_all(&remote_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
32383242
let our_sig = self.secp_ctx.sign(&remote_sighash, &self.local_keys.funding_key);
@@ -3247,11 +3251,12 @@ impl Channel {
32473251
htlc_sigs.push(self.secp_ctx.sign(&htlc_sighash, &our_htlc_key));
32483252
}
32493253

3254+
let htlcs_no_ref = remote_commitment_tx.1.drain(..).map(|htlc_pair| (htlc_pair.0, htlc_pair.1.map(|source| source.clone()))).collect();
32503255
Ok((msgs::CommitmentSigned {
32513256
channel_id: self.channel_id,
32523257
signature: our_sig,
32533258
htlc_signatures: htlc_sigs,
3254-
}, remote_commitment_tx))
3259+
}, (remote_commitment_tx.0, htlcs_no_ref)))
32553260
}
32563261

32573262
/// Adds a pending outbound HTLC to this channel, and creates a signed commitment transaction
@@ -3960,11 +3965,15 @@ mod tests {
39603965
let htlc_basepoint = PublicKey::from_secret_key(&secp_ctx, &chan.local_keys.htlc_base_key);
39613966
let keys = TxCreationKeys::new(&secp_ctx, &per_commitment_point, &delayed_payment_base, &htlc_basepoint, &chan.their_revocation_basepoint.unwrap(), &chan.their_payment_basepoint.unwrap(), &chan.their_htlc_basepoint.unwrap()).unwrap();
39623967

3963-
let mut unsigned_tx: (Transaction, Vec<(HTLCOutputInCommitment, Option<HTLCSource>)>);
3968+
let mut unsigned_tx: (Transaction, Vec<HTLCOutputInCommitment>);
39643969

39653970
macro_rules! test_commitment {
39663971
( $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr) => {
3967-
unsigned_tx = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
3972+
unsigned_tx = {
3973+
let mut tx = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
3974+
let htlcs = tx.1.drain(..).map(|htlc_pair| htlc_pair.0).collect();
3975+
(tx.0, htlcs)
3976+
};
39683977
let their_signature = Signature::from_der(&secp_ctx, &hex::decode($their_sig_hex).unwrap()[..]).unwrap();
39693978
let sighash = Message::from_slice(&bip143::SighashComponents::new(&unsigned_tx.0).sighash_all(&unsigned_tx.0.input[0], &chan.get_funding_redeemscript(), chan.channel_value_satoshis)[..]).unwrap();
39703979
secp_ctx.verify(&sighash, &their_signature, &chan.their_funding_pubkey.unwrap()).unwrap();
@@ -3980,7 +3989,7 @@ mod tests {
39803989
( $htlc_idx: expr, $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr ) => {
39813990
let remote_signature = Signature::from_der(&secp_ctx, &hex::decode($their_sig_hex).unwrap()[..]).unwrap();
39823991

3983-
let (ref htlc, _) = unsigned_tx.1[$htlc_idx];
3992+
let ref htlc = unsigned_tx.1[$htlc_idx];
39843993
let mut htlc_tx = chan.build_htlc_transaction(&unsigned_tx.0.txid(), &htlc, true, &keys, chan.feerate_per_kw);
39853994
let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &keys);
39863995
let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();

0 commit comments

Comments
 (0)