Replace ManyChannelMonitor with chain::Watch
[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 ln::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateErr, MonitorEvent};
19
20 pub mod chaininterface;
21 pub mod transaction;
22 pub mod keysinterface;
23
24 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
25 /// UTXOs.
26 pub trait Access: Send + Sync {
27         /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
28         /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
29         /// is unknown.
30         ///
31         /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
32         fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
33 }
34
35 /// An error when accessing the chain via [`Access`].
36 ///
37 /// [`Access`]: trait.Access.html
38 #[derive(Clone)]
39 pub enum AccessError {
40         /// The requested chain is unknown.
41         UnknownChain,
42
43         /// The requested transaction doesn't exist or hasn't confirmed.
44         UnknownTx,
45 }
46
47 /// The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
48 /// blocks are connected and disconnected.
49 ///
50 /// Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
51 /// responsible for maintaining a set of monitors such that they can be updated accordingly as
52 /// channel state changes and HTLCs are resolved. See method documentation for specific
53 /// requirements.
54 ///
55 /// Implementations **must** ensure that updates are successfully applied and persisted upon method
56 /// completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
57 /// without taking any further action such as persisting the current state.
58 ///
59 /// If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
60 /// backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
61 /// could result in a revoked transaction being broadcast, allowing the counterparty to claim all
62 /// funds in the channel. See [`ChannelMonitorUpdateErr`] for more details about how to handle
63 /// multiple instances.
64 ///
65 /// [`ChannelMonitor`]: ../ln/channelmonitor/struct.ChannelMonitor.html
66 /// [`ChannelMonitorUpdateErr`]: ../ln/channelmonitor/enum.ChannelMonitorUpdateErr.html
67 /// [`PermanentFailure`]: ../ln/channelmonitor/enum.ChannelMonitorUpdateErr.html#variant.PermanentFailure
68 pub trait Watch: Send + Sync {
69         /// Keys needed by monitors for creating and signing transactions.
70         type Keys: ChannelKeys;
71
72         /// Watches a channel identified by `funding_txo` using `monitor`.
73         ///
74         /// Implementations are responsible for watching the chain for the funding transaction along
75         /// with spends of its output and any outputs returned by [`get_outputs_to_watch`]. In practice,
76         /// this means calling [`block_connected`] and [`block_disconnected`] on the monitor and
77         /// including all such transactions that meet this criteria.
78         ///
79         /// [`get_outputs_to_watch`]: ../ln/channelmonitor/struct.ChannelMonitor.html#method.get_outputs_to_watch
80         /// [`block_connected`]: ../ln/channelmonitor/struct.ChannelMonitor.html#method.block_connected
81         /// [`block_disconnected`]: ../ln/channelmonitor/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`]: ../ln/channelmonitor/struct.ChannelMonitor.html#method.update_monitor
90         /// [`ChannelMonitorUpdateErr`]: ../ln/channelmonitor/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 /// An interface for providing [`WatchEvent`]s.
99 ///
100 /// [`WatchEvent`]: enum.WatchEvent.html
101 pub trait WatchEventProvider {
102         /// Releases events produced since the last call. Subsequent calls must only return new events.
103         fn release_pending_watch_events(&self) -> Vec<WatchEvent>;
104 }
105
106 /// An event indicating on-chain activity to watch for pertaining to a channel.
107 pub enum WatchEvent {
108         /// Watch for a transaction with `txid` and having an output with `script_pubkey` as a spending
109         /// condition.
110         WatchTransaction {
111                 /// Identifier of the transaction.
112                 txid: Txid,
113
114                 /// Spending condition for an output of the transaction.
115                 script_pubkey: Script,
116         },
117         /// Watch for spends of a transaction output identified by `outpoint` having `script_pubkey` as
118         /// the spending condition.
119         WatchOutput {
120                 /// Identifier for the output.
121                 outpoint: OutPoint,
122
123                 /// Spending condition for the output.
124                 script_pubkey: Script,
125         }
126 }