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 std::collections::{HashMap, hash_map};
-use std::sync::RwLock;
+use sync::RwLock;
use core::ops::Deref;
/// An implementation of [`chain::Watch`] for monitoring channels.
}
}
+ /// 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 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<events::Event> {
use util::events::EventsProvider;
let events = core::cell::RefCell::new(Vec::new());
- let event_handler = |event| events.borrow_mut().push(event);
+ let event_handler = |event: &events::Event| events.borrow_mut().push(event.clone());
self.process_pending_events(&event_handler);
events.into_inner()
}
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 monitors = self.monitors.read().unwrap();
+ log_debug!(self.logger, "Latest block {} at height {} removed via block_disconnected", header.block_hash(), height);
for monitor in monitors.values() {
monitor.block_disconnected(
header, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
P::Target: channelmonitor::Persist<ChannelSigner>,
{
fn transactions_confirmed(&self, header: &BlockHeader, txdata: &TransactionData, height: u32) {
+ log_debug!(self.logger, "{} provided transactions confirmed at height {} in block {}", txdata.len(), height, header.block_hash());
self.process_chain_data(header, txdata, |monitor, txdata| {
monitor.transactions_confirmed(
header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &*self.logger)
}
fn transaction_unconfirmed(&self, txid: &Txid) {
+ log_debug!(self.logger, "Transaction {} reorganized out of chain", txid);
let monitors = self.monitors.read().unwrap();
for monitor in monitors.values() {
monitor.transaction_unconfirmed(txid, &*self.broadcaster, &*self.fee_estimator, &*self.logger);
}
fn best_block_updated(&self, header: &BlockHeader, height: u32) {
+ log_debug!(self.logger, "New best block {} at height {} provided via best_block_updated", header.block_hash(), height);
self.process_chain_data(header, &[], |monitor, txdata| {
// While in practice there shouldn't be any recursive calls when given empty txdata,
// it's still possible if a chain::Filter implementation returns a transaction.
pending_events.append(&mut monitor.get_and_clear_pending_events());
}
for event in pending_events.drain(..) {
- handler.handle_event(event);
+ handler.handle_event(&event);
}
}
}