]> git.bitcoin.ninja Git - rust-lightning/commitdiff
DRY-up list_channels by having a common lister that takes a filter 2019-12-features-in-routes
authorMatt Corallo <git@bluematt.me>
Fri, 17 Jan 2020 22:36:46 +0000 (17:36 -0500)
committerMatt Corallo <git@bluematt.me>
Tue, 21 Jan 2020 20:09:12 +0000 (15:09 -0500)
lightning/src/ln/channelmanager.rs

index 3c13154c7234abcfc3532098f2306b963c9ff7bf..18d3372024a3354838f188f109e9c5b8bcee3899 100644 (file)
@@ -680,14 +680,12 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
                Ok(())
        }
 
-       /// Gets the list of open channels, in random order. See ChannelDetail field documentation for
-       /// more information.
-       pub fn list_channels(&self) -> Vec<ChannelDetails> {
+       fn list_channels_with_filter<F: FnMut(&(&[u8; 32], &Channel<ChanSigner>)) -> bool>(&self, f: F) -> Vec<ChannelDetails> {
                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<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
                res
        }
 
+       /// Gets the list of open channels, in random order. See ChannelDetail field documentation for
+       /// more information.
+       pub fn list_channels(&self) -> Vec<ChannelDetails> {
+               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<ChannelDetails> {
-               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