A little bit of iteration instead of Vecs, though not complete 2020-08-667-test
authorMatt Corallo <git@bluematt.me>
Tue, 11 Aug 2020 19:34:35 +0000 (15:34 -0400)
committerMatt Corallo <git@bluematt.me>
Tue, 11 Aug 2020 19:34:35 +0000 (15:34 -0400)
lightning/src/ln/channel.rs
lightning/src/ln/channelmonitor.rs

index 1112e89e3bd556ae53c83b8521a23d175e777dac..509ceba0333aa307afae16ac3ff8a389c5e1f780 100644 (file)
@@ -858,9 +858,8 @@ impl<ChanSigner: ChannelKeys> Channel<ChanSigner> {
                }
 
                let local_htlc_outputs = local_outputs.iter().collect();
-               let prev_remote_outputs = prev_remote_htlc_outputs.iter().collect();
-               let remote_outputs = [curr_remote_outputs, prev_remote_outputs].concat();
-               ChannelMonitor::<ChanSigner>::would_broadcast_at_height_given_htlcs(local_htlc_outputs, remote_outputs, height, &self.payment_preimages, logger)
+               let prev_remote_outputs = prev_remote_htlc_outputs.iter();
+               ChannelMonitor::<ChanSigner>::would_broadcast_at_height_given_htlcs(local_htlc_outputs, curr_remote_outputs.iter().map(|ref a| **a).chain(prev_remote_outputs), height, &self.payment_preimages, logger)
        }
 
        // Utilities to build transactions:
index 3b8bf46b614633d1c7728d45ff1b70982a76c7fe..7742298e0a426961b777a034163c2b0f3806243b 100644 (file)
@@ -2006,23 +2006,27 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
        fn would_broadcast_at_height<L: Deref>(&self, height: u32, logger: &L) -> bool where L::Target: Logger {
                let local_outputs: Vec<&HTLCOutputInCommitment> = self.current_local_commitment_tx.htlc_outputs
                        .iter().map(|&(ref a, _, _)| a).collect();
-               let mut prev_remote_outputs = Vec::new();
-               if let Some(ref txid) = self.prev_remote_commitment_txid {
+
+               let dummy_vec = Vec::new();
+               let map_fn = |&(ref a, _)| a;
+
+               let prev_remote_outputs = if let Some(ref txid) = self.prev_remote_commitment_txid {
                        if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
-                               prev_remote_outputs = htlc_outputs.iter().map(|&(ref a, _)| a).collect();
-                       }
-               }
-               let mut curr_remote_outputs = Vec::new();
-               if let Some(ref txid) = self.current_remote_commitment_txid {
+                               htlc_outputs.iter().map(map_fn)
+                       } else { dummy_vec.iter().map(map_fn) }
+               } else { dummy_vec.iter().map(map_fn) };
+
+               let curr_remote_outputs = if let Some(ref txid) = self.current_remote_commitment_txid {
                        if let Some(ref htlc_outputs) = self.remote_claimable_outpoints.get(txid) {
-                               curr_remote_outputs = htlc_outputs.iter().map(|&(ref a, _)| a).collect()
-                       }
-               }
-               let remote_outputs = [curr_remote_outputs, prev_remote_outputs].concat();
-               ChannelMonitor::<ChanSigner>::would_broadcast_at_height_given_htlcs(local_outputs, remote_outputs, height, &self.payment_preimages, logger)
+                               htlc_outputs.iter().map(map_fn)
+                       } else { dummy_vec.iter().map(map_fn) }
+               } else { dummy_vec.iter().map(map_fn) };
+
+               ChannelMonitor::<ChanSigner>::would_broadcast_at_height_given_htlcs(local_outputs, curr_remote_outputs.chain(prev_remote_outputs), height, &self.payment_preimages, logger)
        }
 
-       pub(super) fn would_broadcast_at_height_given_htlcs<L: Deref>(local_htlc_outputs: Vec<&HTLCOutputInCommitment>, remote_htlc_outputs: Vec<&HTLCOutputInCommitment>, height: u32, preimages: &HashMap<PaymentHash, PaymentPreimage>, logger: &L) -> bool where L::Target: Logger {
+       pub(super) fn would_broadcast_at_height_given_htlcs<'a, L: Deref, I1: Iterator<Item=&'a HTLCOutputInCommitment>>
+                       (local_htlc_outputs: Vec<&HTLCOutputInCommitment>, remote_htlc_outputs: I1, height: u32, preimages: &HashMap<PaymentHash, PaymentPreimage>, logger: &L) -> bool where L::Target: Logger {
                // We need to consider all HTLCs which are:
                //  * in any unrevoked remote commitment transaction, as they could broadcast said
                //    transactions and we'd end up in a race, or