From 617a68098469fd3c450c85040b5a451c2cd55c06 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 17 Jan 2020 17:36:46 -0500 Subject: [PATCH] DRY-up list_channels by having a common lister that takes a filter --- lightning/src/ln/channelmanager.rs | 47 ++++++++---------------------- 1 file changed, 12 insertions(+), 35 deletions(-) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 3c13154c7..18d337202 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -680,14 +680,12 @@ impl ChannelManager { Ok(()) } - /// Gets the list of open channels, in random order. See ChannelDetail field documentation for - /// more information. - pub fn list_channels(&self) -> Vec { + fn list_channels_with_filter)) -> bool>(&self, f: F) -> Vec { let mut res = Vec::new(); { let channel_state = self.channel_state.lock().unwrap(); res.reserve(channel_state.by_id.len()); - for (channel_id, channel) in channel_state.by_id.iter() { + for (channel_id, channel) in channel_state.by_id.iter().filter(f) { let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat(); res.push(ChannelDetails { channel_id: (*channel_id).clone(), @@ -711,43 +709,22 @@ impl ChannelManager { res } + /// Gets the list of open channels, in random order. See ChannelDetail field documentation for + /// more information. + pub fn list_channels(&self) -> Vec { + self.list_channels_with_filter(|_| true) + } + /// Gets the list of usable channels, in random order. Useful as an argument to /// Router::get_route to ensure non-announced channels are used. /// /// These are guaranteed to have their is_live value set to true, see the documentation for /// ChannelDetails::is_live for more info on exactly what the criteria are. pub fn list_usable_channels(&self) -> Vec { - let mut res = Vec::new(); - { - let channel_state = self.channel_state.lock().unwrap(); - res.reserve(channel_state.by_id.len()); - for (channel_id, channel) in channel_state.by_id.iter() { - // Note we use is_live here instead of usable which leads to somewhat confused - // internal/external nomenclature, but that's ok cause that's probably what the user - // really wanted anyway. - if channel.is_live() { - let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat(); - res.push(ChannelDetails { - channel_id: (*channel_id).clone(), - short_channel_id: channel.get_short_channel_id(), - remote_network_id: channel.get_their_node_id(), - counterparty_features: InitFeatures::empty(), - channel_value_satoshis: channel.get_value_satoshis(), - inbound_capacity_msat, - outbound_capacity_msat, - user_id: channel.get_user_id(), - is_live: true, - }); - } - } - } - let per_peer_state = self.per_peer_state.read().unwrap(); - for chan in res.iter_mut() { - if let Some(peer_state) = per_peer_state.get(&chan.remote_network_id) { - chan.counterparty_features = peer_state.lock().unwrap().latest_features.clone(); - } - } - res + // Note we use is_live here instead of usable which leads to somewhat confused + // internal/external nomenclature, but that's ok cause that's probably what the user + // really wanted anyway. + self.list_channels_with_filter(|&(_, ref channel)| channel.is_live()) } /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs -- 2.39.5