]> git.bitcoin.ninja Git - rust-lightning/commitdiff
refactor output sorting
authorDevrandom <c1.devrandom@niftybox.net>
Wed, 15 Jan 2020 18:33:38 +0000 (10:33 -0800)
committerDevrandom <c1.devrandom@niftybox.net>
Wed, 15 Jan 2020 18:33:38 +0000 (10:33 -0800)
lightning/src/ln/channel.rs
lightning/src/util/transaction_utils.rs

index 6b6e52275ff14c6e9752c2a76ceb5a85ea59cfc0..dcdde52a5fe46b0cbc791e540206373f86ad59cf 100644 (file)
@@ -814,7 +814,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        ins
                };
 
-               let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<&HTLCSource>)>, Script)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
+               let mut txouts: Vec<(TxOut, (Option<(HTLCOutputInCommitment, Option<&HTLCSource>)>, Script))> =
+                       Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
                let mut included_dust_htlcs: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::new();
 
                let dust_limit_satoshis = if local { self.our_dust_limit_satoshis } else { self.their_dust_limit_satoshis };
@@ -846,7 +847,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                txouts.push((TxOut {
                                                        script_pubkey: script.to_v0_p2wsh(),
                                                        value: $htlc.amount_msat / 1000
-                                               }, Some((htlc_in_tx, $source)), script));
+                                               }, (Some((htlc_in_tx, $source)), script)));
                                        } else {
                                                log_trace!(self, "   ...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);
                                                included_dust_htlcs.push((htlc_in_tx, $source));
@@ -859,7 +860,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                                                txouts.push((TxOut { // "received HTLC output"
                                                        script_pubkey: script.to_v0_p2wsh(),
                                                        value: $htlc.amount_msat / 1000
-                                               }, Some((htlc_in_tx, $source)), script));
+                                               }, (Some((htlc_in_tx, $source)), script)));
                                        } else {
                                                log_trace!(self, "   ...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);
                                                included_dust_htlcs.push((htlc_in_tx, $source));
@@ -965,7 +966,7 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        txouts.push((TxOut {
                                script_pubkey: script.to_v0_p2wsh(),
                                value: value_to_a as u64
-                       }, None, script));
+                       }, (None, script)));
                }
 
                if value_to_b >= (dust_limit_satoshis as i64) {
@@ -978,10 +979,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                        txouts.push((TxOut {
                                script_pubkey: script,
                                value: value_to_b as u64
-                       }, None, redeem_script));
+                       }, (None, redeem_script)));
                }
 
-               transaction_utils::sort_outputs2(&mut txouts, |a, b| {
+               transaction_utils::sort_outputs(&mut txouts, |(a, _), (b, _)| {
                        if let &Some(ref a_htlc) = a {
                                if let &Some(ref b_htlc) = b {
                                        a_htlc.0.cltv_expiry.cmp(&b_htlc.0.cltv_expiry)
@@ -998,10 +999,10 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
                let mut scripts: Vec<Script> = Vec::with_capacity(txouts.len());
                let mut htlcs_included: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(txouts.len() + included_dust_htlcs.len());
-               for (idx, mut out) in txouts.drain(..).enumerate() {
-                       outputs.push(out.0);
-                       scripts.push(out.2);
-                       if let Some((mut htlc, source_option)) = out.1.take() {
+               for (idx, (out, (mut htlc_data, script))) in txouts.drain(..).enumerate() {
+                       outputs.push(out);
+                       scripts.push(script);
+                       if let Some((mut htlc, source_option)) = htlc_data.take() {
                                htlc.transaction_output_index = Some(idx as u32);
                                htlcs_included.push((htlc, source_option));
                        }
index 95d14236eeb3f34e1b3dba5031fa4f8a4c095e11..a7c1e6bc6f78a48e15b69cf4081f0b88534b30e3 100644 (file)
@@ -12,16 +12,6 @@ pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>
        });
 }
 
-pub fn sort_outputs2<T, T1, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T, T1)>, tie_breaker: C) {
-       outputs.sort_unstable_by(|a, b| {
-               a.0.value.cmp(&b.0.value).then_with(|| {
-                       a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
-                               tie_breaker(&a.1, &b.1)
-                       })
-               })
-       });
-}
-
 #[cfg(test)]
 mod tests {
        use super::*;