X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fchain%2Fchaininterface.rs;fp=src%2Fchain%2Fchaininterface.rs;h=5ff720c32e5fed57d8c0ad23e20c3180c675674e;hb=6c07555cad1d8500dca3fd11ab736d4e5c84f8c2;hp=78a40099c35d69687962d33c88b058c06399e6ba;hpb=68d0fcd12ec933b1d23a3588cdd922624d8d7594;p=rust-lightning diff --git a/src/chain/chaininterface.rs b/src/chain/chaininterface.rs index 78a40099..5ff720c3 100644 --- a/src/chain/chaininterface.rs +++ b/src/chain/chaininterface.rs @@ -5,9 +5,12 @@ use bitcoin::blockdata::constants::genesis_block; use bitcoin::util::hash::Sha256dHash; use bitcoin::network::constants::Network; use bitcoin::network::serialize::BitcoinHash; + use util::logger::Logger; + use std::sync::{Mutex,Weak,MutexGuard,Arc}; use std::sync::atomic::{AtomicUsize, Ordering}; +use std::collections::HashSet; /// Used to give chain error details upstream pub enum ChainError { @@ -25,8 +28,8 @@ pub enum ChainError { /// 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 a txid/random-scriptPubKey-in-the-tx which much be watched for. + fn install_watch_tx(&self, txid: &Sha256dHash, script_pub_key: &Script); /// Provides an outpoint which must be watched for, providing any transactions which spend the /// given outpoint. @@ -54,9 +57,11 @@ pub trait BroadcasterInterface: Sync + Send { /// 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. + /// Note that if a new transaction/outpoint is watched during a block_connected call, the block + /// *must* be re-scanned with the new transaction/outpoints and block_connected should be + /// called again with the same header and (at least) the new transactions. + /// Note that if non-new transaction/outpoints may be registered during a call, a second call + /// *must not* happen. /// 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]); @@ -85,11 +90,98 @@ pub trait FeeEstimator: Sync + Send { fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u64; } +/// Utility for tracking registered txn/outpoints and checking for matches +pub struct ChainWatchedUtil { + watch_all: bool, + + // We are more conservative in matching during testing to ensure everything matches *exactly*, + // even though during normal runtime we take more optimized match approaches... + #[cfg(test)] + watched_txn: HashSet<(Sha256dHash, Script)>, + #[cfg(not(test))] + watched_txn: HashSet