-/// Utility for tracking registered txn/outpoints and checking for matches
-#[cfg_attr(test, derive(PartialEq))]
-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<(Txid, Script)>,
- #[cfg(not(test))]
- watched_txn: HashSet<Script>,
-
- watched_outpoints: HashSet<(Txid, u32)>,
-}
-
-impl ChainWatchedUtil {
- /// Constructs an empty (watches nothing) ChainWatchedUtil
- pub fn new() -> Self {
- Self {
- watch_all: false,
- watched_txn: HashSet::new(),
- watched_outpoints: HashSet::new(),
- }
- }
-
- /// Registers a tx for monitoring, returning true if it was a new tx and false if we'd already
- /// been watching for it.
- pub fn register_tx(&mut self, txid: &Txid, script_pub_key: &Script) -> bool {
- if self.watch_all { return false; }
- #[cfg(test)]
- {
- self.watched_txn.insert((txid.clone(), script_pub_key.clone()))
- }
- #[cfg(not(test))]
- {
- let _tx_unused = txid; // It's used in cfg(test), though
- self.watched_txn.insert(script_pub_key.clone())
- }
- }
-
- /// Registers an outpoint for monitoring, returning true if it was a new outpoint and false if
- /// we'd already been watching for it
- pub fn register_outpoint(&mut self, outpoint: (Txid, u32), _script_pub_key: &Script) -> bool {
- if self.watch_all { return false; }
- self.watched_outpoints.insert(outpoint)
- }
-
- /// Sets us to match all transactions, returning true if this is a new setting and false if
- /// we'd already been set to match everything.
- pub fn watch_all(&mut self) -> bool {
- if self.watch_all { return false; }
- self.watch_all = true;
- true
- }
-
- /// Checks if a given transaction matches the current filter.
- pub fn does_match_tx(&self, tx: &Transaction) -> bool {
- if self.watch_all {
- return true;
- }
- for out in tx.output.iter() {
- #[cfg(test)]
- for &(ref txid, ref script) in self.watched_txn.iter() {
- if *script == out.script_pubkey {
- if tx.txid() == *txid {
- return true;
- }
- }
- }
- #[cfg(not(test))]
- for script in self.watched_txn.iter() {
- if *script == out.script_pubkey {
- return true;
- }
- }
- }
- for input in tx.input.iter() {
- for outpoint in self.watched_outpoints.iter() {
- let &(outpoint_hash, outpoint_index) = outpoint;
- if outpoint_hash == input.previous_output.txid && outpoint_index == input.previous_output.vout {
- return true;
- }
- }
- }
- false
- }
-}
-
-/// BlockNotifierArc is useful when you need a BlockNotifier that points to ChainListeners with
-/// static lifetimes, e.g. when you're using lightning-net-tokio (since tokio::spawn requires
-/// parameters with static lifetimes). Other times you can afford a reference, which is more
-/// efficient, in which case BlockNotifierRef is a more appropriate type. Defining these type
-/// aliases prevents issues such as overly long function definitions.
-pub type BlockNotifierArc<C> = Arc<BlockNotifier<'static, Arc<ChainListener>, C>>;
-
-/// BlockNotifierRef is useful when you want a BlockNotifier that points to ChainListeners
-/// with nonstatic lifetimes. This is useful for when static lifetimes are not needed. Nonstatic
-/// lifetimes are more efficient but less flexible, and should be used by default unless static
-/// lifetimes are required, e.g. when you're using lightning-net-tokio (since tokio::spawn
-/// requires parameters with static lifetimes), in which case BlockNotifierArc is a more
-/// appropriate type. Defining these type aliases for common usages prevents issues such as
-/// overly long function definitions.
-pub type BlockNotifierRef<'a, C> = BlockNotifier<'a, &'a ChainListener, C>;
-
-/// Utility for notifying listeners about new blocks, and handling block rescans if new watch
-/// data is registered.