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;
23 pub mod chaininterface;
25 pub mod channelmonitor;
27 pub mod keysinterface;
29 /// An error when accessing the chain via [`Access`].
31 /// [`Access`]: trait.Access.html
33 pub enum AccessError {
34 /// The requested chain is unknown.
37 /// The requested transaction doesn't exist or hasn't confirmed.
41 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
43 pub trait Access: Send + Sync {
44 /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
45 /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
48 /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
49 fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
52 /// The `Listen` trait is used to be notified of when blocks have been connected or disconnected
55 /// Useful when needing to replay chain data upon startup or as new chain events occur.
57 /// Notifies the listener that a block was added at the given height.
58 fn block_connected(&mut self, block: &Block, height: u32);
60 /// Notifies the listener that a block was removed at the given height.
61 fn block_disconnected(&mut self, header: &BlockHeader, height: u32);
64 pub(crate) mod sealed {
66 /// Rustc currently isn't smart enough to figure out that two impls are not conflicting when they
67 /// both impl for `Deref` but with a different `Target`
68 /// (https://github.com/rust-lang/rust/issues/20400). Instead, we use the workaround suggested at
69 /// https://stackoverflow.com/questions/40392524/conflicting-trait-implementations-even-though-associated-types-differ/40408431#40408431
70 pub trait DerefListen {
71 fn block_connected(&self, block: &Block, height: u32);
72 fn block_disconnected(&self, header: &BlockHeader, height: u32);
74 impl<T: Deref, U: Deref> DerefListen for (T, U) where T::Target: DerefListen, U::Target: DerefListen {
75 fn block_connected(&self, block: &Block, height: u32) {
76 Deref::deref(&self.0).block_connected(block, height);
77 Deref::deref(&self.1).block_connected(block, height);
80 fn block_disconnected(&self, header: &BlockHeader, height: u32) {
81 Deref::deref(&self.0).block_disconnected(header, height);
82 Deref::deref(&self.1).block_disconnected(header, height);
86 impl<T: Deref> Listen for T where T::Target: sealed::DerefListen {
87 fn block_connected(&mut self, block: &Block, height: u32) {
88 sealed::DerefListen::block_connected(Deref::deref(self), block, height);
90 fn block_disconnected(&mut self, header: &BlockHeader, height: u32) {
91 sealed::DerefListen::block_disconnected(Deref::deref(self), header, height);
96 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
97 /// blocks are connected and disconnected.
99 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
100 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
101 /// channel state changes and HTLCs are resolved. See method documentation for specific
104 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
105 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
106 /// without taking any further action such as persisting the current state.
108 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
109 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
110 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
111 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
112 /// multiple instances.
114 /// [`ChannelMonitor`]: channelmonitor/struct.ChannelMonitor.html
115 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
116 /// [`PermanentFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.PermanentFailure
117 pub trait Watch<ChannelSigner: Sign>: Send + Sync {
118 /// Watches a channel identified by `funding_txo` using `monitor`.
120 /// Implementations are responsible for watching the chain for the funding transaction along
121 /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
122 /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
124 /// [`get_outputs_to_watch`]: channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
125 /// [`block_connected`]: channelmonitor/struct.ChannelMonitor.html#method.block_connected
126 /// [`block_disconnected`]: channelmonitor/struct.ChannelMonitor.html#method.block_disconnected
127 fn watch_channel(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
129 /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
131 /// Implementations must call [`update_monitor`] with the given update. See
132 /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
134 /// [`update_monitor`]: channelmonitor/struct.ChannelMonitor.html#method.update_monitor
135 /// [`ChannelMonitorUpdateErr`]: channelmonitor/enum.ChannelMonitorUpdateErr.html
136 fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
138 /// Returns any monitor events since the last call. Subsequent calls must only return new
140 fn release_pending_monitor_events(&self) -> Vec<MonitorEvent>;
143 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
146 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
147 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
148 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
149 /// receiving full blocks from a chain source, any further filtering is unnecessary.
151 /// After an output has been registered, subsequent block retrievals from the chain source must not
152 /// exclude any transactions matching the new criteria nor any in-block descendants of such
155 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
156 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
157 /// processed later. Then, in order to block until the data has been processed, any `Watch`
158 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
160 /// [`Watch`]: trait.Watch.html
161 /// [`TemporaryFailure`]: channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.TemporaryFailure
162 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
163 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
164 pub trait Filter: Send + Sync {
165 /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
166 /// a spending condition.
167 fn register_tx(&self, txid: &Txid, script_pubkey: &Script);
169 /// Registers interest in spends of a transaction output identified by `outpoint` having
170 /// `script_pubkey` as the spending condition.
171 fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);