From: Matt Corallo Date: Tue, 11 Aug 2020 19:34:35 +0000 (-0400) Subject: A little bit of iteration instead of Vecs, though not complete X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=refs%2Fheads%2F2020-08-667-test;p=rust-lightning A little bit of iteration instead of Vecs, though not complete --- diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 1112e89e3..509ceba03 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -858,9 +858,8 @@ impl Channel { } 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::::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::::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: diff --git a/lightning/src/ln/channelmonitor.rs b/lightning/src/ln/channelmonitor.rs index 3b8bf46b6..7742298e0 100644 --- a/lightning/src/ln/channelmonitor.rs +++ b/lightning/src/ln/channelmonitor.rs @@ -2006,23 +2006,27 @@ impl ChannelMonitor { fn would_broadcast_at_height(&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::::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::::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(local_htlc_outputs: Vec<&HTLCOutputInCommitment>, remote_htlc_outputs: Vec<&HTLCOutputInCommitment>, height: u32, preimages: &HashMap, logger: &L) -> bool where L::Target: Logger { + pub(super) fn would_broadcast_at_height_given_htlcs<'a, L: Deref, I1: Iterator> + (local_htlc_outputs: Vec<&HTLCOutputInCommitment>, remote_htlc_outputs: I1, height: u32, preimages: &HashMap, 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