Skip to content

Commit f6328c4

Browse files
committed
DRY-up list_channels by having a common lister that takes a filter
1 parent c946bbb commit f6328c4

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

lightning/src/ln/channelmanager.rs

+12-35
Original file line numberDiff line numberDiff line change
@@ -680,14 +680,12 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
680680
Ok(())
681681
}
682682

683-
/// Gets the list of open channels, in random order. See ChannelDetail field documentation for
684-
/// more information.
685-
pub fn list_channels(&self) -> Vec<ChannelDetails> {
683+
fn list_channels_with_filter<F: FnMut(&(&[u8; 32], &Channel<ChanSigner>)) -> bool>(&self, f: F) -> Vec<ChannelDetails> {
686684
let mut res = Vec::new();
687685
{
688686
let channel_state = self.channel_state.lock().unwrap();
689687
res.reserve(channel_state.by_id.len());
690-
for (channel_id, channel) in channel_state.by_id.iter() {
688+
for (channel_id, channel) in channel_state.by_id.iter().filter(f) {
691689
let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
692690
res.push(ChannelDetails {
693691
channel_id: (*channel_id).clone(),
@@ -711,43 +709,22 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
711709
res
712710
}
713711

712+
/// Gets the list of open channels, in random order. See ChannelDetail field documentation for
713+
/// more information.
714+
pub fn list_channels(&self) -> Vec<ChannelDetails> {
715+
self.list_channels_with_filter(|_| true)
716+
}
717+
714718
/// Gets the list of usable channels, in random order. Useful as an argument to
715719
/// Router::get_route to ensure non-announced channels are used.
716720
///
717721
/// These are guaranteed to have their is_live value set to true, see the documentation for
718722
/// ChannelDetails::is_live for more info on exactly what the criteria are.
719723
pub fn list_usable_channels(&self) -> Vec<ChannelDetails> {
720-
let mut res = Vec::new();
721-
{
722-
let channel_state = self.channel_state.lock().unwrap();
723-
res.reserve(channel_state.by_id.len());
724-
for (channel_id, channel) in channel_state.by_id.iter() {
725-
// Note we use is_live here instead of usable which leads to somewhat confused
726-
// internal/external nomenclature, but that's ok cause that's probably what the user
727-
// really wanted anyway.
728-
if channel.is_live() {
729-
let (inbound_capacity_msat, outbound_capacity_msat) = channel.get_inbound_outbound_available_balance_msat();
730-
res.push(ChannelDetails {
731-
channel_id: (*channel_id).clone(),
732-
short_channel_id: channel.get_short_channel_id(),
733-
remote_network_id: channel.get_their_node_id(),
734-
counterparty_features: InitFeatures::empty(),
735-
channel_value_satoshis: channel.get_value_satoshis(),
736-
inbound_capacity_msat,
737-
outbound_capacity_msat,
738-
user_id: channel.get_user_id(),
739-
is_live: true,
740-
});
741-
}
742-
}
743-
}
744-
let per_peer_state = self.per_peer_state.read().unwrap();
745-
for chan in res.iter_mut() {
746-
if let Some(peer_state) = per_peer_state.get(&chan.remote_network_id) {
747-
chan.counterparty_features = peer_state.lock().unwrap().latest_features.clone();
748-
}
749-
}
750-
res
724+
// Note we use is_live here instead of usable which leads to somewhat confused
725+
// internal/external nomenclature, but that's ok cause that's probably what the user
726+
// really wanted anyway.
727+
self.list_channels_with_filter(|(_, channel)| channel.is_live())
751728
}
752729

753730
/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs

0 commit comments

Comments
 (0)