Replace ChainWatchInterface in NetGraphMsgHandler
[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::transaction::OutPoint;
17
18 pub mod chaininterface;
19 pub mod transaction;
20 pub mod keysinterface;
21
22 /// The `Access` trait defines behavior for accessing chain data and state, such as blocks and
23 /// UTXOs.
24 pub trait Access: Send + Sync {
25         /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
26         /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
27         /// is unknown.
28         ///
29         /// [`short_channel_id`]: https://github.com/lightningnetwork/lightning-rfc/blob/master/07-routing-gossip.md#definition-of-short_channel_id
30         fn get_utxo(&self, genesis_hash: &BlockHash, short_channel_id: u64) -> Result<TxOut, AccessError>;
31 }
32
33 /// An error when accessing the chain via [`Access`].
34 ///
35 /// [`Access`]: trait.Access.html
36 #[derive(Clone)]
37 pub enum AccessError {
38         /// The requested chain is unknown.
39         UnknownChain,
40
41         /// The requested transaction doesn't exist or hasn't confirmed.
42         UnknownTx,
43 }
44
45 /// An interface for providing [`WatchEvent`]s.
46 ///
47 /// [`WatchEvent`]: enum.WatchEvent.html
48 pub trait WatchEventProvider {
49         /// Releases events produced since the last call. Subsequent calls must only return new events.
50         fn release_pending_watch_events(&self) -> Vec<WatchEvent>;
51 }
52
53 /// An event indicating on-chain activity to watch for pertaining to a channel.
54 pub enum WatchEvent {
55         /// Watch for a transaction with `txid` and having an output with `script_pubkey` as a spending
56         /// condition.
57         WatchTransaction {
58                 /// Identifier of the transaction.
59                 txid: Txid,
60
61                 /// Spending condition for an output of the transaction.
62                 script_pubkey: Script,
63         },
64         /// Watch for spends of a transaction output identified by `outpoint` having `script_pubkey` as
65         /// the spending condition.
66         WatchOutput {
67                 /// Identifier for the output.
68                 outpoint: OutPoint,
69
70                 /// Spending condition for the output.
71                 script_pubkey: Script,
72         }
73 }