1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Structs and traits which allow other parts of rust-lightning to interact with the blockchain.
12 use bitcoin::blockdata::script::Script;
13 use bitcoin::blockdata::transaction::TxOut;
14 use bitcoin::hash_types::{BlockHash, Txid};
16 use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
17 use chain::keysinterface::Sign;
18 use chain::transaction::OutPoint;
20 pub mod chaininterface;
22 pub mod channelmonitor;
24 pub mod keysinterface;
26 /// An error when accessing the chain via [`Access`].
28 /// [`Access`]: trait.Access.html
30 pub enum AccessError {
31 /// The requested chain is unknown.
34 /// The requested transaction doesn't exist or hasn't confirmed.
38 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
40 pub trait Access: Send + Sync {
41 /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
42 /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
45 /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
46 fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
49 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
50 /// blocks are connected and disconnected.
52 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
53 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
54 /// channel state changes and HTLCs are resolved. See method documentation for specific
57 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
58 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
59 /// without taking any further action such as persisting the current state.
61 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
62 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
63 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
64 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
65 /// multiple instances.
67 /// [`ChannelMonitor`]: channelmonitor/struct.ChannelMonitor.html
68 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
69 /// [`PermanentFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.PermanentFailure
70 pub trait Watch<ChannelSigner: Sign>: Send + Sync {
71 /// Watches a channel identified by `funding_txo` using `monitor`.
73 /// Implementations are responsible for watching the chain for the funding transaction along
74 /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
75 /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
77 /// [`get_outputs_to_watch`]: channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
78 /// [`block_connected`]: channelmonitor/struct.ChannelMonitor.html#method.block_connected
79 /// [`block_disconnected`]: channelmonitor/struct.ChannelMonitor.html#method.block_disconnected
80 fn watch_channel(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
82 /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
84 /// Implementations must call [`update_monitor`] with the given update. See
85 /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
87 /// [`update_monitor`]: channelmonitor/struct.ChannelMonitor.html#method.update_monitor
88 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
89 fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
91 /// Returns any monitor events since the last call. Subsequent calls must only return new
93 fn release_pending_monitor_events(&self) -> Vec<MonitorEvent>;
96 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
99 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
100 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
101 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
102 /// receiving full blocks from a chain source, any further filtering is unnecessary.
104 /// After an output has been registered, subsequent block retrievals from the chain source must not
105 /// exclude any transactions matching the new criteria nor any in-block descendants of such
108 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
109 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
110 /// processed later. Then, in order to block until the data has been processed, any `Watch`
111 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
113 /// [`Watch`]: trait.Watch.html
114 /// [`TemporaryFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.TemporaryFailure
115 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
116 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
117 pub trait Filter: Send + Sync {
118 /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
119 /// a spending condition.
120 fn register_tx(&self, txid: &Txid, script_pubkey: &Script);
122 /// Registers interest in spends of a transaction output identified by `outpoint` having
123 /// `script_pubkey` as the spending condition.
124 fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);