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