Skip to content

Commit b2ce5e8

Browse files
committed
fixup! less copying
1 parent 3c35f8d commit b2ce5e8

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,9 @@ impl CommitmentTransaction {
768768
// Also keeps track of auxiliary HTLC data and returns it along with the mutated and sorted HTLCs.
769769
// This allows the caller to match the HTLC output index with the auxiliary data.
770770
// This auxiliary data is not stored in this object.
771-
pub(crate) fn new_with_auxiliary_htlc_data<T: Copy>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<secp256k1::All>) -> (CommitmentTransaction, Vec<(HTLCOutputInCommitment, T)>) {
771+
pub(crate) fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs: Vec<HTLCOutputInCommitment>, aux: Vec<T>, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<secp256k1::All>) -> (CommitmentTransaction, Vec<(HTLCOutputInCommitment, T)>) {
772772
// Sort outputs and populate output indices while keeping track of the auxiliary data
773-
let mut txouts = Self::do_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, &htlcs_with_aux, channel_parameters, &secp_ctx).unwrap();
773+
let mut txouts = Self::do_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, &htlcs, aux, channel_parameters, &secp_ctx).unwrap();
774774
let mut result_htlcs_with_aux = Vec::new();
775775
let mut htlcs = Vec::new();
776776
for (idx, mut out) in txouts.drain(..).enumerate() {
@@ -822,13 +822,14 @@ impl CommitmentTransaction {
822822
}
823823

824824
fn build_outputs<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<Vec<(TxOut, Script)>, ()> {
825-
let htlcs = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
826-
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &htlcs, channel_parameters, secp_ctx)?;
825+
let mut aux = Vec::with_capacity(self.htlcs.len());
826+
aux.resize(self.htlcs.len(), ());
827+
let mut txouts = Self::do_build_outputs(&self.keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &self.htlcs, aux, channel_parameters, secp_ctx)?;
827828
let outs = txouts.drain(..).map(|(out, (s, _))| (out, s)).collect();
828829
Ok(outs)
829830
}
830831

831-
fn do_build_outputs<T: Copy, S: secp256k1::Signing + secp256k1::Verification>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs: &Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<S>) -> Result<Vec<(TxOut, (Script, Option<(HTLCOutputInCommitment, T)>))>, ()> {
832+
fn do_build_outputs<T, S: secp256k1::Signing + secp256k1::Verification>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs: &Vec<HTLCOutputInCommitment>, aux: Vec<T>, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<S>) -> Result<Vec<(TxOut, (Script, Option<(HTLCOutputInCommitment, T)>))>, ()> {
832833
let (broadcaster_pubkeys, countersignatory_pubkeys) = channel_parameters.pubkeys();
833834
let contest_delay = channel_parameters.contest_delay();
834835

@@ -873,13 +874,13 @@ impl CommitmentTransaction {
873874
));
874875
}
875876

876-
for (htlc, t) in htlcs {
877+
for (htlc, t) in htlcs.iter().zip(aux) {
877878
let script = chan_utils::get_htlc_redeemscript(&htlc, &keys);
878879
let txout = TxOut {
879880
script_pubkey: script.to_v0_p2wsh(),
880881
value: htlc.amount_msat / 1000,
881882
};
882-
txouts.push((txout, (script, Some((htlc.clone(), *t)))));
883+
txouts.push((txout, (script, Some((htlc.clone(), t)))));
883884
}
884885

885886
// Sort output in BIP-69 order (amount, scriptPubkey). Tie-breaks based on HTLC

lightning/src/ln/channel.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,9 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
839839
#[inline]
840840
fn build_commitment_transaction<L: Deref>(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u32, logger: &L) -> (CommitmentTransaction, usize, Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)>) where L::Target: Logger {
841841
let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
842-
let mut included_non_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len());
842+
let num_htlcs = self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len();
843+
let mut included_non_dust_htlcs: Vec<HTLCOutputInCommitment> = Vec::with_capacity(num_htlcs);
844+
let mut included_non_dust_sources: Vec<Option<&HTLCSource>> = Vec::with_capacity(num_htlcs);
843845

844846
let broadcaster_dust_limit_satoshis = if local { self.holder_dust_limit_satoshis } else { self.counterparty_dust_limit_satoshis };
845847
let mut remote_htlc_total_msat = 0;
@@ -866,7 +868,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
866868
let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
867869
if $htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + (feerate_per_kw as u64 * HTLC_TIMEOUT_TX_WEIGHT / 1000) {
868870
log_trace!(logger, " ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
869-
included_non_dust_htlcs.push((htlc_in_tx, $source));
871+
included_non_dust_htlcs.push(htlc_in_tx);
872+
included_non_dust_sources.push($source);
870873
} else {
871874
log_trace!(logger, " ...including {} {} dust HTLC {} (hash {}) with value {} due to dust limit", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
872875
included_dust_htlcs.push((htlc_in_tx, $source));
@@ -875,7 +878,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
875878
let htlc_in_tx = get_htlc_in_commitment!($htlc, false);
876879
if $htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + (feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000) {
877880
log_trace!(logger, " ...including {} {} HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
878-
included_non_dust_htlcs.push((htlc_in_tx, $source));
881+
included_non_dust_htlcs.push(htlc_in_tx);
882+
included_non_dust_sources.push($source);
879883
} else {
880884
log_trace!(logger, " ...including {} {} dust HTLC {} (hash {}) with value {}", if $outbound { "outbound" } else { "inbound" }, $state_name, $htlc.htlc_id, log_bytes!($htlc.payment_hash.0), $htlc.amount_msat);
881885
included_dust_htlcs.push((htlc_in_tx, $source));
@@ -985,21 +989,22 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
985989
value_to_b = 0;
986990
}
987991

988-
let count = included_non_dust_htlcs.len();
992+
let num_nondust_htlcs = included_non_dust_htlcs.len();
989993

990994
let (info, mut htlcs_included) = CommitmentTransaction::new_with_auxiliary_htlc_data(commitment_number,
991995
value_to_a as u64,
992996
value_to_b as u64,
993997
keys.clone(),
994998
feerate_per_kw,
995999
included_non_dust_htlcs,
1000+
included_non_dust_sources,
9961001
&self.get_channel_parameters(local),
9971002
&self.secp_ctx
9981003
);
9991004

10001005
htlcs_included.append(&mut included_dust_htlcs);
10011006

1002-
(info, count, htlcs_included)
1007+
(info, num_nondust_htlcs, htlcs_included)
10031008
}
10041009

10051010
#[inline]

0 commit comments

Comments
 (0)