1 // This file is Copyright its original authors, visible in version control
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
10 //! Traits and utility impls which allow other parts of rust-lightning to interact with the
13 //! Includes traits for monitoring and receiving notifications of new blocks and block
14 //! disconnections, transaction broadcasting, and feerate information requests.
16 use bitcoin::blockdata::transaction::Transaction;
17 use bitcoin::blockdata::script::Script;
18 use bitcoin::hash_types::Txid;
20 use std::collections::HashSet;
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);
28 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
30 pub enum ConfirmationTarget {
31 /// We are happy with this transaction confirming slowly when feerate drops some.
33 /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
35 /// We'd like this transaction to confirm in the next few blocks.
39 /// A trait which should be implemented to provide feerate information on a number of time
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.
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).
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;
56 /// Minimum relay fee as required by bitcoin network mempool policy.
57 pub const MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = 4000;
59 /// Utility for tracking registered txn/outpoints and checking for matches
60 #[cfg_attr(test, derive(PartialEq))]
61 pub struct ChainWatchedUtil {
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...
67 watched_txn: HashSet<(Txid, Script)>,
69 watched_txn: HashSet<Script>,
71 watched_outpoints: HashSet<(Txid, u32)>,
74 impl ChainWatchedUtil {
75 /// Constructs an empty (watches nothing) ChainWatchedUtil
76 pub fn new() -> Self {
79 watched_txn: HashSet::new(),
80 watched_outpoints: HashSet::new(),
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; }
90 self.watched_txn.insert((txid.clone(), script_pub_key.clone()))
94 let _tx_unused = txid; // It's used in cfg(test), though
95 self.watched_txn.insert(script_pub_key.clone())
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)
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;
114 /// Checks if a given transaction matches the current filter.
115 pub fn does_match_tx(&self, tx: &Transaction) -> bool {
119 for out in tx.output.iter() {
121 for &(ref txid, ref script) in self.watched_txn.iter() {
122 if *script == out.script_pubkey {
123 if tx.txid() == *txid {
129 for script in self.watched_txn.iter() {
130 if *script == out.script_pubkey {
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 {