Skip to content

Commit 95566c3

Browse files
committed
Add an accessor to ChainMonitor to fet 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`.
1 parent afd83d4 commit 95566c3

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

lightning/src/chain/chainmonitor.rs

+25-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ use chain;
3030
use chain::{Filter, WatchedOutput};
3131
use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
3232
use chain::channelmonitor;
33-
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent, Persist, TransactionOutputs};
33+
use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, ClaimableBalance, MonitorEvent, Persist, TransactionOutputs};
3434
use chain::transaction::{OutPoint, TransactionData};
3535
use chain::keysinterface::Sign;
3636
use util::logger::Logger;
3737
use util::events;
3838
use util::events::EventHandler;
39+
use ln::channelmanager::ChannelDetails;
3940

4041
use prelude::*;
4142
use sync::RwLock;
@@ -140,6 +141,29 @@ where C::Target: chain::Filter,
140141
}
141142
}
142143

144+
/// Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or
145+
/// claims which are awaiting confirmation.
146+
///
147+
/// Includes the balances from each [`ChannelMonitor`] *except* those included in
148+
/// `ignored_channels`, allowing you to filter for only channels which are not still open (and
149+
/// who's balance should likely be pulled from the [`ChannelDetails`].
150+
///
151+
/// See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
152+
/// inclusion in the return value.
153+
pub fn get_claimable_balances(&self, ignored_channels: &[ChannelDetails]) -> Vec<ClaimableBalance> {
154+
let mut ret = Vec::new();
155+
let monitors = self.monitors.read().unwrap();
156+
'monitor_iter: for (funding_outpoint, monitor) in monitors.iter() {
157+
for chan in ignored_channels {
158+
if chan.funding_txo.as_ref() == Some(funding_outpoint) {
159+
continue 'monitor_iter;
160+
}
161+
}
162+
ret.append(&mut monitor.get_claimable_balances());
163+
}
164+
ret
165+
}
166+
143167
#[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
144168
pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
145169
use util::events::EventsProvider;

0 commit comments

Comments
 (0)