Struct ChainMonitor
pub struct ChainMonitor<ChannelSigner, C, T, F, L, P>where
ChannelSigner: EcdsaChannelSigner,
C: Deref,
T: Deref,
F: Deref,
L: Deref,
P: Deref,
<C as Deref>::Target: Filter,
<T as Deref>::Target: BroadcasterInterface,
<F as Deref>::Target: FeeEstimator,
<L as Deref>::Target: Logger,
<P as Deref>::Target: Persist<ChannelSigner>,{ /* private fields */ }
Expand description
An implementation of chain::Watch
for monitoring channels.
Connected and disconnected blocks must be provided to ChainMonitor
as documented by
chain::Watch
. May be used in conjunction with ChannelManager
to monitor channels locally
or used independently to monitor channels remotely. See the module-level documentation for
details.
Note that ChainMonitor
should regularly trigger rebroadcasts/fee bumps of pending claims from
a force-closed channel. This is crucial in preventing certain classes of pinning attacks,
detecting substantial mempool feerate changes between blocks, and ensuring reliability if
broadcasting fails. We recommend invoking this every 30 seconds, or lower if running in an
environment with spotty connections, like on mobile.
Implementations§
§impl<ChannelSigner, C, T, F, L, P> ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> ChainMonitor<ChannelSigner, C, T, F, L, P>
pub fn new(
chain_source: Option<C>,
broadcaster: T,
logger: L,
feeest: F,
persister: P,
) -> ChainMonitor<ChannelSigner, C, T, F, L, P>
pub fn new( chain_source: Option<C>, broadcaster: T, logger: L, feeest: F, persister: P, ) -> ChainMonitor<ChannelSigner, C, T, F, L, P>
Creates a new ChainMonitor
used to watch on-chain activity pertaining to channels.
When an optional chain source implementing chain::Filter
is provided, the chain monitor
will call back to it indicating transactions and outputs of interest. This allows clients to
pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
always need to fetch full blocks absent another means for determining which blocks contain
transactions relevant to the watched channels.
pub fn get_claimable_balances(
&self,
ignored_channels: &[&ChannelDetails],
) -> Vec<Balance>
pub fn get_claimable_balances( &self, ignored_channels: &[&ChannelDetails], ) -> Vec<Balance>
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
.
See ChannelMonitor::get_claimable_balances
for more details on the exact criteria for
inclusion in the return value.
pub fn get_monitor(
&self,
funding_txo: OutPoint,
) -> Result<LockedChannelMonitor<'_, ChannelSigner>, ()>
pub fn get_monitor( &self, funding_txo: OutPoint, ) -> Result<LockedChannelMonitor<'_, ChannelSigner>, ()>
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 list_monitors(&self) -> Vec<(OutPoint, ChannelId)>
pub fn list_monitors(&self) -> Vec<(OutPoint, ChannelId)>
Lists the funding outpoint and channel ID 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_pending_monitor_updates(&self) -> HashMap<OutPoint, Vec<u64>>
pub fn list_pending_monitor_updates(&self) -> HashMap<OutPoint, Vec<u64>>
Lists the pending updates for each ChannelMonitor
(by OutPoint
being monitored).
Each Vec<u64>
contains update_id
s from ChannelMonitor::get_latest_update_id
for updates
that have not yet been fully persisted. Note that if a full monitor is persisted all the pending
monitor updates must be individually marked completed by calling ChainMonitor::channel_monitor_updated
.
pub fn channel_monitor_updated(
&self,
funding_txo: OutPoint,
completed_update_id: u64,
) -> Result<(), APIError>
pub fn channel_monitor_updated( &self, funding_txo: OutPoint, completed_update_id: u64, ) -> Result<(), APIError>
Indicates the persistence of a ChannelMonitor
has completed after
ChannelMonitorUpdateStatus::InProgress
was returned from an update operation.
Thus, the anticipated use is, at a high level:
- This
ChainMonitor
callsPersist::update_persisted_channel
which stores the update to disk and begins updating any remote (e.g. watchtower/backup) copies, returningChannelMonitorUpdateStatus::InProgress
, - once all remote copies are updated, you call this function with
ChannelMonitor::get_latest_update_id
orChannelMonitorUpdate::update_id
as thecompleted_update_id
, and once all pending updates have completed the channel will be re-enabled.
It is only necessary to call ChainMonitor::channel_monitor_updated
when you return ChannelMonitorUpdateStatus::InProgress
from Persist
and either:
- A new
ChannelMonitor
was added inPersist::persist_new_channel
, or - A
ChannelMonitorUpdate
was provided as part ofPersist::update_persisted_channel
. Note that we don’t care about calls toPersist::update_persisted_channel
where noChannelMonitorUpdate
was provided.
Returns an APIError::APIMisuseError
if funding_txo
does not match any currently
registered ChannelMonitor
s.
pub async fn process_pending_events_async<Future, H>(&self, handler: H)
pub async fn process_pending_events_async<Future, H>(&self, handler: H)
Processes any events asynchronously in the order they were generated since the last call using the given event handler.
See the trait-level documentation of EventsProvider
for requirements.
pub fn get_update_future(&self) -> Future ⓘ
pub fn get_update_future(&self) -> Future ⓘ
Gets a Future
that completes when an event is available either via
chain::Watch::release_pending_monitor_events
or
EventsProvider::process_pending_events
.
Note that callbacks registered on the Future
MUST NOT call back into this
ChainMonitor
and should instead register actions to be taken later.
pub fn rebroadcast_pending_claims(&self)
pub fn rebroadcast_pending_claims(&self)
Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is crucial in preventing certain classes of pinning attacks, detecting substantial mempool feerate changes between blocks, and ensuring reliability if broadcasting fails. We recommend invoking this every 30 seconds, or lower if running in an environment with spotty connections, like on mobile.
pub fn signer_unblocked(&self, monitor_opt: Option<OutPoint>)
pub fn signer_unblocked(&self, monitor_opt: Option<OutPoint>)
Triggers rebroadcasts of pending claims from force-closed channels after a transaction signature generation failure.
monitor_opt
can be used as a filter to only trigger them for a specific channel monitor.
pub fn archive_fully_resolved_channel_monitors(&self)
pub fn archive_fully_resolved_channel_monitors(&self)
Archives fully resolved channel monitors by calling Persist::archive_persisted_channel
.
This is useful for pruning fully resolved monitors from the monitor set and primary storage so they are not kept in memory and reloaded on restart.
Should be called occasionally (once every handful of blocks or on startup).
Depending on the implementation of Persist::archive_persisted_channel
the monitor
data could be moved to an archive location or removed entirely.
Trait Implementations§
§impl<ChannelSigner, C, T, F, L, P> Confirm for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> Confirm for ChainMonitor<ChannelSigner, C, T, F, L, P>
§fn transactions_confirmed(
&self,
header: &Header,
txdata: &[(usize, &Transaction)],
height: u32,
)
fn transactions_confirmed( &self, header: &Header, txdata: &[(usize, &Transaction)], height: u32, )
§fn transaction_unconfirmed(&self, txid: &Txid)
fn transaction_unconfirmed(&self, txid: &Txid)
§fn best_block_updated(&self, header: &Header, height: u32)
fn best_block_updated(&self, header: &Header, height: u32)
§impl<ChannelSigner, C, T, F, L, P> EventsProvider for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> EventsProvider for ChainMonitor<ChannelSigner, C, T, F, L, P>
§fn process_pending_events<H>(&self, handler: H)
fn process_pending_events<H>(&self, handler: H)
Processes SpendableOutputs
events produced from each ChannelMonitor
upon maturity.
For channels featuring anchor outputs, this method will also process BumpTransaction
events produced from each ChannelMonitor
while there is a balance to claim onchain
within each channel. As the confirmation of a commitment transaction may be critical to the
safety of funds, we recommend invoking this every 30 seconds, or lower if running in an
environment with spotty connections, like on mobile.
An EventHandler
may safely call back to the provider, though this shouldn’t be needed in
order to handle these events.
§impl<ChannelSigner, C, T, F, L, P> Listen for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> Listen for ChainMonitor<ChannelSigner, C, T, F, L, P>
§fn filtered_block_connected(
&self,
header: &Header,
txdata: &[(usize, &Transaction)],
height: u32,
)
fn filtered_block_connected( &self, header: &Header, txdata: &[(usize, &Transaction)], height: u32, )
§fn block_disconnected(&self, header: &Header, height: u32)
fn block_disconnected(&self, header: &Header, height: u32)
§fn block_connected(&self, block: &Block, height: u32)
fn block_connected(&self, block: &Block, height: u32)
§impl<ChannelSigner, C, T, F, L, P> Watch<ChannelSigner> for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> Watch<ChannelSigner> for ChainMonitor<ChannelSigner, C, T, F, L, P>
§fn watch_channel(
&self,
funding_outpoint: OutPoint,
monitor: ChannelMonitor<ChannelSigner>,
) -> Result<ChannelMonitorUpdateStatus, ()>
fn watch_channel( &self, funding_outpoint: OutPoint, monitor: ChannelMonitor<ChannelSigner>, ) -> Result<ChannelMonitorUpdateStatus, ()>
§fn update_channel(
&self,
funding_txo: OutPoint,
update: &ChannelMonitorUpdate,
) -> ChannelMonitorUpdateStatus
fn update_channel( &self, funding_txo: OutPoint, update: &ChannelMonitorUpdate, ) -> ChannelMonitorUpdateStatus
§fn release_pending_monitor_events(
&self,
) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)>
fn release_pending_monitor_events( &self, ) -> Vec<(OutPoint, ChannelId, Vec<MonitorEvent>, Option<PublicKey>)>
Auto Trait Implementations§
impl<ChannelSigner, C, T, F, L, P> !Freeze for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> RefUnwindSafe for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> Send for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> Sync for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> Unpin for ChainMonitor<ChannelSigner, C, T, F, L, P>
impl<ChannelSigner, C, T, F, L, P> UnwindSafe for ChainMonitor<ChannelSigner, C, T, F, L, P>
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request