+
+ /// 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<Balance> {
+ let mut ret = Vec::new();
+ let monitor_states = self.monitors.read().unwrap();
+ for (_, monitor_state) in monitor_states.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_state.monitor.get_claimable_balances());
+ }
+ ret
+ }
+
+ /// Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
+ /// such [`ChannelMonitor`] is currently being monitored for.
+ ///
+ /// Note that the result holds a mutex over our monitor set, and should not be held
+ /// indefinitely.
+ pub fn get_monitor(&self, funding_txo: OutPoint) -> Result<LockedChannelMonitor<'_, ChannelSigner>, ()> {
+ let lock = self.monitors.read().unwrap();
+ if lock.get(&funding_txo).is_some() {
+ Ok(LockedChannelMonitor { lock, funding_txo })
+ } else {
+ Err(())
+ }
+ }
+
+ /// Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
+ ///
+ /// Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
+ /// monitoring for on-chain state resolutions.
+ pub fn list_monitors(&self) -> Vec<OutPoint> {
+ self.monitors.read().unwrap().keys().map(|outpoint| *outpoint).collect()
+ }
+
+ #[cfg(test)]
+ pub fn remove_monitor(&self, funding_txo: &OutPoint) -> ChannelMonitor<ChannelSigner> {
+ self.monitors.write().unwrap().remove(funding_txo).unwrap().monitor
+ }
+
+ #[cfg(any(test, feature = "fuzztarget", feature = "_test_utils"))]
+ pub fn get_and_clear_pending_events(&self) -> Vec<events::Event> {
+ use util::events::EventsProvider;
+ let events = core::cell::RefCell::new(Vec::new());
+ let event_handler = |event: &events::Event| events.borrow_mut().push(event.clone());
+ self.process_pending_events(&event_handler);
+ events.into_inner()
+ }
+}
+
+impl<ChannelSigner: Sign, C: Deref, T: Deref, F: Deref, L: Deref, P: Deref>
+chain::Listen for ChainMonitor<ChannelSigner, C, T, F, L, P>
+where
+ C::Target: chain::Filter,
+ T::Target: BroadcasterInterface,
+ F::Target: FeeEstimator,
+ L::Target: Logger,
+ P::Target: Persist<ChannelSigner>,
+{
+ fn block_connected(&self, block: &Block, height: u32) {
+ let header = &block.header;
+ let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
+ log_debug!(self.logger, "New best block {} at height {} provided via block_connected", header.block_hash(), height);
+ self.process_chain_data(header, &txdata, |monitor, txdata| {
+ monitor.block_connected(
+ header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
+ });
+ }
+
+ fn block_disconnected(&self, header: &BlockHeader, height: u32) {
+ let monitor_states = self.monitors.read().unwrap();
+ log_debug!(self.logger, "Latest block {} at height {} removed via block_disconnected", header.block_hash(), height);
+ for monitor_state in monitor_states.values() {
+ monitor_state.monitor.block_disconnected(
+ header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
+ }
+ }