Remove ChainListener
[rust-lightning] / lightning / src / chain / chaininterface.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 //! Traits and utility impls which allow other parts of rust-lightning to interact with the
11 //! blockchain.
12 //!
13 //! Includes traits for monitoring and receiving notifications of new blocks and block
14 //! disconnections, transaction broadcasting, and feerate information requests.
15
16 use bitcoin::blockdata::transaction::Transaction;
17 use bitcoin::blockdata::script::Script;
18 use bitcoin::hash_types::Txid;
19
20 use std::collections::HashSet;
21
22 /// An interface to send a transaction to the Bitcoin network.
23 pub trait BroadcasterInterface: Sync + Send {
24         /// Sends a transaction out to (hopefully) be mined.
25         fn broadcast_transaction(&self, tx: &Transaction);
26 }
27
28 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
29 /// estimation.
30 pub enum ConfirmationTarget {
31         /// We are happy with this transaction confirming slowly when feerate drops some.
32         Background,
33         /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
34         Normal,
35         /// We'd like this transaction to confirm in the next few blocks.
36         HighPriority,
37 }
38
39 /// A trait which should be implemented to provide feerate information on a number of time
40 /// horizons.
41 ///
42 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
43 /// called from inside the library in response to chain events, P2P events, or timer events).
44 pub trait FeeEstimator: Sync + Send {
45         /// Gets estimated satoshis of fee required per 1000 Weight-Units.
46         ///
47         /// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs
48         /// don't put us below 1 satoshi-per-byte).
49         ///
50         /// This translates to:
51         ///  * satoshis-per-byte * 250
52         ///  * ceil(satoshis-per-kbyte / 4)
53         fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32;
54 }
55
56 /// Minimum relay fee as required by bitcoin network mempool policy.
57 pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
58
59 /// Utility for tracking registered txn/outpoints and checking for matches
60 #[cfg_attr(test, derive(PartialEq))]
61 pub struct ChainWatchedUtil {
62         watch_all: bool,
63
64         // We are more conservative in matching during testing to ensure everything matches *exactly*,
65         // even though during normal runtime we take more optimized match approaches...
66         #[cfg(test)]
67         watched_txn: HashSet<(Txid, Script)>,
68         #[cfg(not(test))]
69         watched_txn: HashSet<Script>,
70
71         watched_outpoints: HashSet<(Txid, u32)>,
72 }
73
74 impl ChainWatchedUtil {
75         /// Constructs an empty (watches nothing) ChainWatchedUtil
76         pub fn new() -> Self {
77                 Self {
78                         watch_all: false,
79                         watched_txn: HashSet::new(),
80                         watched_outpoints: HashSet::new(),
81                 }
82         }
83
84         /// Registers a tx for monitoring, returning true if it was a new tx and false if we'd already
85         /// been watching for it.
86         pub fn register_tx(&mut self, txid: &Txid, script_pub_key: &Script) -> bool {
87                 if self.watch_all { return false; }
88                 #[cfg(test)]
89                 {
90                         self.watched_txn.insert((txid.clone(), script_pub_key.clone()))
91                 }
92                 #[cfg(not(test))]
93                 {
94                         let _tx_unused = txid; // It's used in cfg(test), though
95                         self.watched_txn.insert(script_pub_key.clone())
96                 }
97         }
98
99         /// Registers an outpoint for monitoring, returning true if it was a new outpoint and false if
100         /// we'd already been watching for it
101         pub fn register_outpoint(&mut self, outpoint: (Txid, u32), _script_pub_key: &Script) -> bool {
102                 if self.watch_all { return false; }
103                 self.watched_outpoints.insert(outpoint)
104         }
105
106         /// Sets us to match all transactions, returning true if this is a new setting and false if
107         /// we'd already been set to match everything.
108         pub fn watch_all(&mut self) -> bool {
109                 if self.watch_all { return false; }
110                 self.watch_all = true;
111                 true
112         }
113
114         /// Checks if a given transaction matches the current filter.
115         pub fn does_match_tx(&self, tx: &Transaction) -> bool {
116                 if self.watch_all {
117                         return true;
118                 }
119                 for out in tx.output.iter() {
120                         #[cfg(test)]
121                         for &(ref txid, ref script) in self.watched_txn.iter() {
122                                 if *script == out.script_pubkey {
123                                         if tx.txid() == *txid {
124                                                 return true;
125                                         }
126                                 }
127                         }
128                         #[cfg(not(test))]
129                         for script in self.watched_txn.iter() {
130                                 if *script == out.script_pubkey {
131                                         return true;
132                                 }
133                         }
134                 }
135                 for input in tx.input.iter() {
136                         for outpoint in self.watched_outpoints.iter() {
137                                 let &(outpoint_hash, outpoint_index) = outpoint;
138                                 if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
139                                         return true;
140                                 }
141                         }
142                 }
143                 false
144         }
145 }