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::block::{Block, BlockHeader};
13 use bitcoin::blockdata::script::Script;
14 use bitcoin::blockdata::transaction::TxOut;
15 use bitcoin::hash_types::{BlockHash, Txid};
17 use chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
18 use chain::keysinterface::Sign;
19 use chain::transaction::OutPoint;
21 pub mod chaininterface;
23 pub mod channelmonitor;
25 pub mod keysinterface;
27 /// An error when accessing the chain via [`Access`].
29 /// [`Access`]: trait.Access.html
31 pub enum AccessError {
32 /// The requested chain is unknown.
35 /// The requested transaction doesn't exist or hasn't confirmed.
39 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
41 pub trait Access: Send + Sync {
42 /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
43 /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
46 /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
47 fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
50 /// Adaptor used for notifying when blocks have been connected or disconnected from the chain.
52 /// Used when needing to replay chain data upon startup or as new chain events occur.
53 pub trait ChainListener {
54 /// Notifies the listener that a block was added at the given height.
55 fn block_connected(&self, block: &Block, height: u32);
57 /// Notifies the listener that a block was removed at the given height.
58 fn block_disconnected(&self, header: &BlockHeader, height: u32);
61 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
62 /// blocks are connected and disconnected.
64 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
65 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
66 /// channel state changes and HTLCs are resolved. See method documentation for specific
69 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
70 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
71 /// without taking any further action such as persisting the current state.
73 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
74 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
75 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
76 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
77 /// multiple instances.
79 /// [`ChannelMonitor`]: channelmonitor/struct.ChannelMonitor.html
80 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
81 /// [`PermanentFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.PermanentFailure
82 pub trait Watch<ChannelSigner: Sign>: Send + Sync {
83 /// Watches a channel identified by `funding_txo` using `monitor`.
85 /// Implementations are responsible for watching the chain for the funding transaction along
86 /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
87 /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
89 /// [`get_outputs_to_watch`]: channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
90 /// [`block_connected`]: channelmonitor/struct.ChannelMonitor.html#method.block_connected
91 /// [`block_disconnected`]: channelmonitor/struct.ChannelMonitor.html#method.block_disconnected
92 fn watch_channel(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
94 /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
96 /// Implementations must call [`update_monitor`] with the given update. See
97 /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
99 /// [`update_monitor`]: channelmonitor/struct.ChannelMonitor.html#method.update_monitor
100 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
101 fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
103 /// Returns any monitor events since the last call. Subsequent calls must only return new
105 fn release_pending_monitor_events(&self) -> Vec<MonitorEvent>;
108 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
111 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
112 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
113 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
114 /// receiving full blocks from a chain source, any further filtering is unnecessary.
116 /// After an output has been registered, subsequent block retrievals from the chain source must not
117 /// exclude any transactions matching the new criteria nor any in-block descendants of such
120 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
121 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
122 /// processed later. Then, in order to block until the data has been processed, any `Watch`
123 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
125 /// [`Watch`]: trait.Watch.html
126 /// [`TemporaryFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.TemporaryFailure
127 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
128 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
129 pub trait Filter: Send + Sync {
130 /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
131 /// a spending condition.
132 fn register_tx(&self, txid: &Txid, script_pubkey: &Script);
134 /// Registers interest in spends of a transaction output identified by `outpoint` having
135 /// `script_pubkey` as the spending condition.
136 fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
139 impl<T: ChainListener> ChainListener for std::ops::Deref<Target = T> {
140 fn block_connected(&self, block: &Block, height: u32) {
141 (**self).block_connected(block, height);
144 fn block_disconnected(&self, header: &BlockHeader, height: u32) {
145 (**self).block_disconnected(header, height);
149 impl<T: std::ops::Deref, U: std::ops::Deref> ChainListener for (T, U)
151 T::Target: ChainListener,
152 U::Target: ChainListener,
154 fn block_connected(&self, block: &Block, height: u32) {
155 self.0.block_connected(block, height);
156 self.1.block_connected(block, height);
159 fn block_disconnected(&self, header: &BlockHeader, height: u32) {
160 self.0.block_disconnected(header, height);
161 self.1.block_disconnected(header, height);