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 pub enum AccessError {
30 /// The requested chain is unknown.
33 /// The requested transaction doesn't exist or hasn't confirmed.
37 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
39 pub trait Access: Send + Sync {
40 /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
41 /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
44 /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
45 fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
48 /// The `Listen` trait is used to be notified of when blocks have been connected or disconnected
51 /// Useful when needing to replay chain data upon startup or as new chain events occur.
53 /// Notifies the listener that a block was added at the given height.
54 fn block_connected(&self, block: &Block, height: u32);
56 /// Notifies the listener that a block was removed at the given height.
57 fn block_disconnected(&self, header: &BlockHeader, height: u32);
60 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
61 /// blocks are connected and disconnected.
63 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
64 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
65 /// channel state changes and HTLCs are resolved. See method documentation for specific
68 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
69 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
70 /// without taking any further action such as persisting the current state.
72 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
73 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
74 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
75 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
76 /// multiple instances.
78 /// [`ChannelMonitor`]: channelmonitor::ChannelMonitor
79 /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
80 /// [`PermanentFailure`]: channelmonitor::ChannelMonitorUpdateErr::PermanentFailure
81 pub trait Watch<ChannelSigner: Sign>: Send + Sync {
82 /// Watches a channel identified by `funding_txo` using `monitor`.
84 /// Implementations are responsible for watching the chain for the funding transaction along
85 /// with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
86 /// calling [`block_connected`] and [`block_disconnected`] on the monitor.
88 /// [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
89 /// [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
90 /// [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
91 fn watch_channel(&self, funding_txo: OutPoint, monitor: ChannelMonitor<ChannelSigner>) -> Result<(), ChannelMonitorUpdateErr>;
93 /// Updates a channel identified by `funding_txo` by applying `update` to its monitor.
95 /// Implementations must call [`update_monitor`] with the given update. See
96 /// [`ChannelMonitorUpdateErr`] for invariants around returning an error.
98 /// [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
99 /// [`ChannelMonitorUpdateErr`]: channelmonitor::ChannelMonitorUpdateErr
100 fn update_channel(&self, funding_txo: OutPoint, update: ChannelMonitorUpdate) -> Result<(), ChannelMonitorUpdateErr>;
102 /// Returns any monitor events since the last call. Subsequent calls must only return new
104 fn release_pending_monitor_events(&self) -> Vec<MonitorEvent>;
107 /// The `Filter` trait defines behavior for indicating chain activity of interest pertaining to
110 /// This is useful in order to have a [`Watch`] implementation convey to a chain source which
111 /// transactions to be notified of. Notification may take the form of pre-filtering blocks or, in
112 /// the case of [BIP 157]/[BIP 158], only fetching a block if the compact filter matches. If
113 /// receiving full blocks from a chain source, any further filtering is unnecessary.
115 /// After an output has been registered, subsequent block retrievals from the chain source must not
116 /// exclude any transactions matching the new criteria nor any in-block descendants of such
119 /// Note that use as part of a [`Watch`] implementation involves reentrancy. Therefore, the `Filter`
120 /// should not block on I/O. Implementations should instead queue the newly monitored data to be
121 /// processed later. Then, in order to block until the data has been processed, any [`Watch`]
122 /// invocation that has called the `Filter` must return [`TemporaryFailure`].
124 /// [`TemporaryFailure`]: channelmonitor::ChannelMonitorUpdateErr::TemporaryFailure
125 /// [BIP 157]: https://github.com/bitcoin/bips/blob/master/bip-0157.mediawiki
126 /// [BIP 158]: https://github.com/bitcoin/bips/blob/master/bip-0158.mediawiki
127 pub trait Filter: Send + Sync {
128 /// Registers interest in a transaction with `txid` and having an output with `script_pubkey` as
129 /// a spending condition.
130 fn register_tx(&self, txid: &Txid, script_pubkey: &Script);
132 /// Registers interest in spends of a transaction output identified by `outpoint` having
133 /// `script_pubkey` as the spending condition.
134 fn register_output(&self, outpoint: &OutPoint, script_pubkey: &Script);
137 impl<T: Listen> Listen for std::ops::Deref<Target = T> {
138 fn block_connected(&self, block: &Block, height: u32) {
139 (**self).block_connected(block, height);
142 fn block_disconnected(&self, header: &BlockHeader, height: u32) {
143 (**self).block_disconnected(header, height);
147 impl<T: std::ops::Deref, U: std::ops::Deref> Listen for (T, U)
152 fn block_connected(&self, block: &Block, height: u32) {
153 self.0.block_connected(block, height);
154 self.1.block_connected(block, height);
157 fn block_disconnected(&self, header: &BlockHeader, height: u32) {
158 self.0.block_disconnected(header, height);
159 self.1.block_disconnected(header, height);