From: Matt Corallo Date: Wed, 25 Aug 2021 20:13:01 +0000 (+0000) Subject: Add an accessor to `ChainMonitor` to get the claimable balances X-Git-Tag: v0.0.101~19^2~1 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=a675886e101d1cfb79117a040af88e7f3f7727d0;p=rust-lightning Add an accessor to `ChainMonitor` to get the claimable balances The common user desire is to get the set of claimable balances for all non-closed channels. In order to do so, they really want to just ask their `ChainMonitor` for the set of balances, which they can do here by passing the `ChannelManager::list_channels` output to `ChainMonitor::get_claimable_balances`. --- diff --git a/lightning/src/chain/chainmonitor.rs b/lightning/src/chain/chainmonitor.rs index 8969427a..f8fe6bf6 100644 --- a/lightning/src/chain/chainmonitor.rs +++ b/lightning/src/chain/chainmonitor.rs @@ -30,12 +30,13 @@ use chain; use chain::{Filter, WatchedOutput}; use chain::chaininterface::{BroadcasterInterface, FeeEstimator}; use chain::channelmonitor; -use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent, Persist, TransactionOutputs}; +use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, Balance, MonitorEvent, Persist, TransactionOutputs}; use chain::transaction::{OutPoint, TransactionData}; use chain::keysinterface::Sign; use util::logger::Logger; use util::events; use util::events::EventHandler; +use ln::channelmanager::ChannelDetails; use prelude::*; use sync::RwLock; @@ -140,6 +141,31 @@ where C::Target: chain::Filter, } } + /// Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or + /// claims which are awaiting confirmation. + /// + /// Includes the balances from each [`ChannelMonitor`] *except* those included in + /// `ignored_channels`, allowing you to filter out balances from channels which are still open + /// (and whose balance should likely be pulled from the [`ChannelDetails`]). + /// + /// See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for + /// inclusion in the return value. + pub fn get_claimable_balances(&self, ignored_channels: &[ChannelDetails]) -> Vec { + let mut ret = Vec::new(); + let monitors = self.monitors.read().unwrap(); + for (_, monitor) in monitors.iter().filter(|(funding_outpoint, _)| { + for chan in ignored_channels { + if chan.funding_txo.as_ref() == Some(funding_outpoint) { + return false; + } + } + true + }) { + ret.append(&mut monitor.get_claimable_balances()); + } + ret + } + #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))] pub fn get_and_clear_pending_events(&self) -> Vec { use util::events::EventsProvider;