1 //! Traits and utility impls which allow other parts of rust-lightning to interact with the
4 //! Includes traits for monitoring and receiving notifications of new blocks and block
5 //! disconnections, transaction broadcasting, and feerate information requests.
7 use bitcoin::blockdata::block::{Block, BlockHeader};
8 use bitcoin::blockdata::transaction::Transaction;
9 use bitcoin::blockdata::script::Script;
10 use bitcoin::blockdata::constants::genesis_block;
11 use bitcoin::util::hash::BitcoinHash;
12 use bitcoin_hashes::sha256d::Hash as Sha256dHash;
13 use bitcoin::network::constants::Network;
15 use util::logger::Logger;
17 use std::sync::{Mutex, MutexGuard, Arc};
18 use std::sync::atomic::{AtomicUsize, Ordering};
19 use std::collections::HashSet;
21 use std::marker::PhantomData;
24 /// Used to give chain error details upstream
27 /// Client doesn't support UTXO lookup (but the chain hash matches our genesis block hash)
29 /// Chain isn't the one watched
31 /// Tx doesn't exist or is unconfirmed
35 /// An interface to request notification of certain scripts as they appear the
38 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
39 /// called from inside the library in response to ChainListener events, P2P events, or timer
41 pub trait ChainWatchInterface: Sync + Send {
42 /// Provides a txid/random-scriptPubKey-in-the-tx which much be watched for.
43 fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script);
45 /// Provides an outpoint which must be watched for, providing any transactions which spend the
47 fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script);
49 /// Indicates that a listener needs to see all transactions.
50 fn watch_all_txn(&self);
52 /// Gets the script and value in satoshis for a given unspent transaction output given a
53 /// short_channel_id (aka unspent_tx_output_identier). For BTC/tBTC channels the top three
54 /// bytes are the block height, the next 3 the transaction index within the block, and the
55 /// final two the output within the transaction.
56 fn get_chain_utxo(&self, genesis_hash: Sha256dHash, unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError>;
58 /// Gets the list of transactions and transaction indices that the ChainWatchInterface is
60 fn filter_block<'a>(&self, block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>);
62 /// Returns a usize that changes when the ChainWatchInterface's watched data is modified.
63 /// Users of `filter_block` should pre-save a copy of `reentered`'s return value and use it to
64 /// determine whether they need to re-filter a given block.
65 fn reentered(&self) -> usize;
68 /// An interface to send a transaction to the Bitcoin network.
69 pub trait BroadcasterInterface: Sync + Send {
70 /// Sends a transaction out to (hopefully) be mined.
71 fn broadcast_transaction(&self, tx: &Transaction);
74 /// A trait indicating a desire to listen for events from the chain
75 pub trait ChainListener: Sync + Send {
76 /// Notifies a listener that a block was connected.
78 /// The txn_matched array should be set to references to transactions which matched the
79 /// relevant installed watch outpoints/txn, or the full set of transactions in the block.
81 /// Note that if txn_matched includes only matched transactions, and a new
82 /// transaction/outpoint is watched during a block_connected call, the block *must* be
83 /// re-scanned with the new transaction/outpoints and block_connected should be called
84 /// again with the same header and (at least) the new transactions.
86 /// Note that if non-new transaction/outpoints are be registered during a call, a second call
87 /// *must not* happen.
89 /// This also means those counting confirmations using block_connected callbacks should watch
90 /// for duplicate headers and not count them towards confirmations!
91 fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]);
92 /// Notifies a listener that a block was disconnected.
93 /// Unlike block_connected, this *must* never be called twice for the same disconnect event.
94 /// Height must be the one of the block which was disconnected (not new height of the best chain)
95 fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32);
98 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
100 pub enum ConfirmationTarget {
101 /// We are happy with this transaction confirming slowly when feerate drops some.
103 /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
105 /// We'd like this transaction to confirm in the next few blocks.
109 /// A trait which should be implemented to provide feerate information on a number of time
112 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
113 /// called from inside the library in response to ChainListener events, P2P events, or timer
115 pub trait FeeEstimator: Sync + Send {
116 /// Gets estimated satoshis of fee required per 1000 Weight-Units.
118 /// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs
119 /// don't put us below 1 satoshi-per-byte).
121 /// This translates to:
122 /// * satoshis-per-byte * 250
123 /// * ceil(satoshis-per-kbyte / 4)
124 fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64;
127 /// Minimum relay fee as required by bitcoin network mempool policy.
128 pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
130 /// Utility for tracking registered txn/outpoints and checking for matches
131 #[cfg_attr(test, derive(PartialEq))]
132 pub struct ChainWatchedUtil {
135 // We are more conservative in matching during testing to ensure everything matches *exactly*,
136 // even though during normal runtime we take more optimized match approaches...
138 watched_txn: HashSet<(Sha256dHash, Script)>,
140 watched_txn: HashSet<Script>,
142 watched_outpoints: HashSet<(Sha256dHash, u32)>,
145 impl ChainWatchedUtil {
146 /// Constructs an empty (watches nothing) ChainWatchedUtil
147 pub fn new() -> Self {
150 watched_txn: HashSet::new(),
151 watched_outpoints: HashSet::new(),
155 /// Registers a tx for monitoring, returning true if it was a new tx and false if we'd already
156 /// been watching for it.
157 pub fn register_tx(&mut self, txid: &Sha256dHash, script_pub_key: &Script) -> bool {
158 if self.watch_all { return false; }
161 self.watched_txn.insert((txid.clone(), script_pub_key.clone()))
165 let _tx_unused = txid; // It's used in cfg(test), though
166 self.watched_txn.insert(script_pub_key.clone())
170 /// Registers an outpoint for monitoring, returning true if it was a new outpoint and false if
171 /// we'd already been watching for it
172 pub fn register_outpoint(&mut self, outpoint: (Sha256dHash, u32), _script_pub_key: &Script) -> bool {
173 if self.watch_all { return false; }
174 self.watched_outpoints.insert(outpoint)
177 /// Sets us to match all transactions, returning true if this is a new setting and false if
178 /// we'd already been set to match everything.
179 pub fn watch_all(&mut self) -> bool {
180 if self.watch_all { return false; }
181 self.watch_all = true;
185 /// Checks if a given transaction matches the current filter.
186 pub fn does_match_tx(&self, tx: &Transaction) -> bool {
190 for out in tx.output.iter() {
192 for &(ref txid, ref script) in self.watched_txn.iter() {
193 if *script == out.script_pubkey {
194 if tx.txid() == *txid {
200 for script in self.watched_txn.iter() {
201 if *script == out.script_pubkey {
206 for input in tx.input.iter() {
207 for outpoint in self.watched_outpoints.iter() {
208 let &(outpoint_hash, outpoint_index) = outpoint;
209 if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
218 /// BlockNotifierArc is useful when you need a BlockNotifier that points to ChainListeners with
219 /// static lifetimes, e.g. when you're using lightning-net-tokio (since tokio::spawn requires
220 /// parameters with static lifetimes). Other times you can afford a reference, which is more
221 /// efficient, in which case BlockNotifierRef is a more appropriate type. Defining these type
222 /// aliases prevents issues such as overly long function definitions.
223 pub type BlockNotifierArc = Arc<BlockNotifier<'static, Arc<ChainListener>>>;
225 /// BlockNotifierRef is useful when you want a BlockNotifier that points to ChainListeners
226 /// with nonstatic lifetimes. This is useful for when static lifetimes are not needed. Nonstatic
227 /// lifetimes are more efficient but less flexible, and should be used by default unless static
228 /// lifetimes are required, e.g. when you're using lightning-net-tokio (since tokio::spawn
229 /// requires parameters with static lifetimes), in which case BlockNotifierArc is a more
230 /// appropriate type. Defining these type aliases for common usages prevents issues such as
231 /// overly long function definitions.
232 pub type BlockNotifierRef<'a> = BlockNotifier<'a, &'a ChainListener>;
234 /// Utility for notifying listeners about new blocks, and handling block rescans if new watch
235 /// data is registered.
237 /// Rather than using a plain BlockNotifier, it is preferable to use either a BlockNotifierArc
238 /// or a BlockNotifierRef for conciseness. See their documentation for more details, but essentially
239 /// you should default to using a BlockNotifierRef, and use a BlockNotifierArc instead when you
240 /// require ChainListeners with static lifetimes, such as when you're using lightning-net-tokio.
241 pub struct BlockNotifier<'a, CL: Deref<Target = ChainListener + 'a> + 'a> {
242 listeners: Mutex<Vec<CL>>,
243 chain_monitor: Arc<ChainWatchInterface>,
244 phantom: PhantomData<&'a ()>,
247 impl<'a, CL: Deref<Target = ChainListener + 'a> + 'a> BlockNotifier<'a, CL> {
248 /// Constructs a new BlockNotifier without any listeners.
249 pub fn new(chain_monitor: Arc<ChainWatchInterface>) -> BlockNotifier<'a, CL> {
251 listeners: Mutex::new(Vec::new()),
253 phantom: PhantomData,
257 /// Register the given listener to receive events.
258 pub fn register_listener(&self, listener: CL) {
259 let mut vec = self.listeners.lock().unwrap();
262 /// Unregister the given listener to no longer
265 /// If the same listener is registered multiple times, unregistering
266 /// will remove ALL occurrences of that listener. Comparison is done using
267 /// the pointer returned by the Deref trait implementation.
268 pub fn unregister_listener(&self, listener: CL) {
269 let mut vec = self.listeners.lock().unwrap();
270 // item is a ref to an abstract thing that dereferences to a ChainListener,
271 // so dereference it twice to get the ChainListener itself
272 vec.retain(|item | !ptr::eq(&(**item), &(*listener)));
275 /// Notify listeners that a block was connected given a full, unfiltered block.
277 /// Handles re-scanning the block and calling block_connected again if listeners register new
278 /// watch data during the callbacks for you (see ChainListener::block_connected for more info).
279 pub fn block_connected<'b>(&self, block: &'b Block, height: u32) {
280 let mut reentered = true;
282 let (matched, matched_index) = self.chain_monitor.filter_block(block);
283 reentered = self.block_connected_checked(&block.header, height, matched.as_slice(), matched_index.as_slice());
287 /// Notify listeners that a block was connected, given pre-filtered list of transactions in the
288 /// block which matched the filter (probably using does_match_tx).
290 /// Returns true if notified listeners registered additional watch data (implying that the
291 /// block must be re-scanned and this function called again prior to further block_connected
292 /// calls, see ChainListener::block_connected for more info).
293 pub fn block_connected_checked(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]) -> bool {
294 let last_seen = self.chain_monitor.reentered();
296 let listeners = self.listeners.lock().unwrap();
297 for listener in listeners.iter() {
298 listener.block_connected(header, height, txn_matched, indexes_of_txn_matched);
300 return last_seen != self.chain_monitor.reentered();
303 /// Notify listeners that a block was disconnected.
304 pub fn block_disconnected(&self, header: &BlockHeader, disconnected_height: u32) {
305 let listeners = self.listeners.lock().unwrap();
306 for listener in listeners.iter() {
307 listener.block_disconnected(&header, disconnected_height);
312 /// Utility to capture some common parts of ChainWatchInterface implementors.
314 /// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful.
315 pub struct ChainWatchInterfaceUtil {
317 watched: Mutex<ChainWatchedUtil>,
318 reentered: AtomicUsize,
322 // We only expose PartialEq in test since its somewhat unclear exactly what it should do and we're
323 // only comparing a subset of fields (essentially just checking that the set of things we're
324 // watching is the same).
326 impl PartialEq for ChainWatchInterfaceUtil {
327 fn eq(&self, o: &Self) -> bool {
328 self.network == o.network &&
329 *self.watched.lock().unwrap() == *o.watched.lock().unwrap()
333 /// Register listener
334 impl ChainWatchInterface for ChainWatchInterfaceUtil {
335 fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script) {
336 let mut watched = self.watched.lock().unwrap();
337 if watched.register_tx(txid, script_pub_key) {
338 self.reentered.fetch_add(1, Ordering::Relaxed);
342 fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script) {
343 let mut watched = self.watched.lock().unwrap();
344 if watched.register_outpoint(outpoint, out_script) {
345 self.reentered.fetch_add(1, Ordering::Relaxed);
349 fn watch_all_txn(&self) {
350 let mut watched = self.watched.lock().unwrap();
351 if watched.watch_all() {
352 self.reentered.fetch_add(1, Ordering::Relaxed);
356 fn get_chain_utxo(&self, genesis_hash: Sha256dHash, _unspent_tx_output_identifier: u64) -> Result<(Script, u64), ChainError> {
357 if genesis_hash != genesis_block(self.network).header.bitcoin_hash() {
358 return Err(ChainError::NotWatched);
360 Err(ChainError::NotSupported)
363 fn filter_block<'a>(&self, block: &'a Block) -> (Vec<&'a Transaction>, Vec<u32>) {
364 let mut matched = Vec::new();
365 let mut matched_index = Vec::new();
367 let watched = self.watched.lock().unwrap();
368 for (index, transaction) in block.txdata.iter().enumerate() {
369 if self.does_match_tx_unguarded(transaction, &watched) {
370 matched.push(transaction);
371 matched_index.push(index as u32);
375 (matched, matched_index)
378 fn reentered(&self) -> usize {
379 self.reentered.load(Ordering::Relaxed)
383 impl ChainWatchInterfaceUtil {
384 /// Creates a new ChainWatchInterfaceUtil for the given network
385 pub fn new(network: Network, logger: Arc<Logger>) -> ChainWatchInterfaceUtil {
386 ChainWatchInterfaceUtil {
388 watched: Mutex::new(ChainWatchedUtil::new()),
389 reentered: AtomicUsize::new(1),
394 /// Checks if a given transaction matches the current filter.
395 pub fn does_match_tx(&self, tx: &Transaction) -> bool {
396 let watched = self.watched.lock().unwrap();
397 self.does_match_tx_unguarded (tx, &watched)
400 fn does_match_tx_unguarded(&self, tx: &Transaction, watched: &MutexGuard<ChainWatchedUtil>) -> bool {
401 watched.does_match_tx(tx)
407 use ln::functional_test_utils::{create_chanmon_cfgs, create_node_cfgs};
408 use super::{BlockNotifier, ChainListener};
412 fn register_listener_test() {
413 let chanmon_cfgs = create_chanmon_cfgs(1);
414 let node_cfgs = create_node_cfgs(1, &chanmon_cfgs);
415 let block_notifier = BlockNotifier::new(node_cfgs[0].chain_monitor.clone());
416 assert_eq!(block_notifier.listeners.lock().unwrap().len(), 0);
417 let listener = &node_cfgs[0].chan_monitor.simple_monitor as &ChainListener;
418 block_notifier.register_listener(listener);
419 let vec = block_notifier.listeners.lock().unwrap();
420 assert_eq!(vec.len(), 1);
421 let item = vec.first().clone().unwrap();
422 assert!(ptr::eq(&(**item), &(*listener)));
426 fn unregister_single_listener_test() {
427 let chanmon_cfgs = create_chanmon_cfgs(2);
428 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
429 let block_notifier = BlockNotifier::new(node_cfgs[0].chain_monitor.clone());
430 let listener1 = &node_cfgs[0].chan_monitor.simple_monitor as &ChainListener;
431 let listener2 = &node_cfgs[1].chan_monitor.simple_monitor as &ChainListener;
432 block_notifier.register_listener(listener1);
433 block_notifier.register_listener(listener2);
434 let vec = block_notifier.listeners.lock().unwrap();
435 assert_eq!(vec.len(), 2);
437 block_notifier.unregister_listener(listener1);
438 let vec = block_notifier.listeners.lock().unwrap();
439 assert_eq!(vec.len(), 1);
440 let item = vec.first().clone().unwrap();
441 assert!(ptr::eq(&(**item), &(*listener2)));
445 fn unregister_single_listener_ref_test() {
446 let chanmon_cfgs = create_chanmon_cfgs(2);
447 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
448 let block_notifier = BlockNotifier::new(node_cfgs[0].chain_monitor.clone());
449 block_notifier.register_listener(&node_cfgs[0].chan_monitor.simple_monitor as &ChainListener);
450 block_notifier.register_listener(&node_cfgs[1].chan_monitor.simple_monitor as &ChainListener);
451 let vec = block_notifier.listeners.lock().unwrap();
452 assert_eq!(vec.len(), 2);
454 block_notifier.unregister_listener(&node_cfgs[0].chan_monitor.simple_monitor);
455 let vec = block_notifier.listeners.lock().unwrap();
456 assert_eq!(vec.len(), 1);
457 let item = vec.first().clone().unwrap();
458 assert!(ptr::eq(&(**item), &(*&node_cfgs[1].chan_monitor.simple_monitor)));
462 fn unregister_multiple_of_the_same_listeners_test() {
463 let chanmon_cfgs = create_chanmon_cfgs(2);
464 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
465 let block_notifier = BlockNotifier::new(node_cfgs[0].chain_monitor.clone());
466 let listener1 = &node_cfgs[0].chan_monitor.simple_monitor as &ChainListener;
467 let listener2 = &node_cfgs[1].chan_monitor.simple_monitor as &ChainListener;
468 block_notifier.register_listener(listener1);
469 block_notifier.register_listener(listener1);
470 block_notifier.register_listener(listener2);
471 let vec = block_notifier.listeners.lock().unwrap();
472 assert_eq!(vec.len(), 3);
474 block_notifier.unregister_listener(listener1);
475 let vec = block_notifier.listeners.lock().unwrap();
476 assert_eq!(vec.len(), 1);
477 let item = vec.first().clone().unwrap();
478 assert!(ptr::eq(&(**item), &(*listener2)));