use bitcoin::blockdata::block::{Block, BlockHeader}; use bitcoin::blockdata::transaction::Transaction; use bitcoin::blockdata::script::Script; use bitcoin::util::hash::Sha256dHash; use util::logger::Logger; use std::sync::{Mutex,Weak,MutexGuard,Arc}; use std::sync::atomic::{AtomicUsize, Ordering}; /// An interface to request notification of certain scripts as they appear the /// chain. /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're /// called from inside the library in response to ChainListener events, P2P events, or timer /// events). pub trait ChainWatchInterface: Sync + Send { /// Provides a scriptPubKey which much be watched for. fn install_watch_script(&self, script_pub_key: &Script); /// Provides an outpoint which must be watched for, providing any transactions which spend the /// given outpoint. fn install_watch_outpoint(&self, outpoint: (Sha256dHash, u32), out_script: &Script); /// Indicates that a listener needs to see all transactions. fn watch_all_txn(&self); fn register_listener(&self, listener: Weak); //TODO: unregister } /// An interface to send a transaction to the Bitcoin network. pub trait BroadcasterInterface: Sync + Send { /// Sends a transaction out to (hopefully) be mined. fn broadcast_transaction(&self, tx: &Transaction); } /// A trait indicating a desire to listen for events from the chain pub trait ChainListener: Sync + Send { /// Notifies a listener that a block was connected. /// Note that if a new script/transaction is watched during a block_connected call, the block /// *must* be re-scanned with the new script/transaction and block_connected should be called /// again with the same header and (at least) the new transactions. /// This also means those counting confirmations using block_connected callbacks should watch /// for duplicate headers and not count them towards confirmations! fn block_connected(&self, header: &BlockHeader, height: u32, txn_matched: &[&Transaction], indexes_of_txn_matched: &[u32]); /// Notifies a listener that a block was disconnected. /// Unlike block_connected, this *must* never be called twice for the same disconnect event. fn block_disconnected(&self, header: &BlockHeader); } pub enum ConfirmationTarget { Background, Normal, HighPriority, } /// A trait which should be implemented to provide feerate information on a number of time /// horizons. /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're /// called from inside the library in response to ChainListener events, P2P events, or timer /// events). pub trait FeeEstimator: Sync + Send { /// Gets estimated satoshis of fee required per 1000 Weight-Units. This translates to: /// * satoshis-per-byte * 250 /// * ceil(satoshis-per-kbyte / 4) /// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs /// don't put us below 1 satoshi-per-byte). fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64; } /// Utility to capture some common parts of ChainWatchInterface implementors. /// Keeping a local copy of this in a ChainWatchInterface implementor is likely useful. pub struct ChainWatchInterfaceUtil { watched: Mutex<(Vec