Replace WatchEventProvider with chain::Filter
[rust-lightning] / lightning / src / ln / channelmonitor.rs
index e93d1a4d583f41f4fd13e3433aac2b86bb61d332..fd6334d7029e7ffccf180e3d4a21634c5a3882f1 100644 (file)
@@ -43,6 +43,7 @@ use ln::chan_utils::{CounterpartyCommitmentSecrets, HTLCOutputInCommitment, Hold
 use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
 use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
 use chain;
+use chain::Filter;
 use chain::chaininterface::{ChainWatchedUtil, BroadcasterInterface, FeeEstimator};
 use chain::transaction::OutPoint;
 use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys};
@@ -196,25 +197,49 @@ impl_writeable!(HTLCUpdate, 0, { payment_hash, payment_preimage, source });
 ///
 /// [`chain::Watch`]: ../../chain/trait.Watch.html
 /// [`ChannelManager`]: ../channelmanager/struct.ChannelManager.html
-pub struct ChainMonitor<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref>
-       where T::Target: BroadcasterInterface,
+pub struct ChainMonitor<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref>
+       where C::Target: chain::Filter,
+        T::Target: BroadcasterInterface,
         F::Target: FeeEstimator,
         L::Target: Logger,
 {
        /// The monitors
        pub monitors: Mutex<HashMap<OutPoint, ChannelMonitor<ChanSigner>>>,
-       watch_events: Mutex<WatchEventQueue>,
+       watch_events: Mutex<WatchEventCache>,
+       chain_source: Option<C>,
        broadcaster: T,
        logger: L,
        fee_estimator: F
 }
 
-struct WatchEventQueue {
+struct WatchEventCache {
        watched: ChainWatchedUtil,
-       events: Vec<chain::WatchEvent>,
+       events: Vec<WatchEvent>,
 }
 
-impl WatchEventQueue {
+/// An event indicating on-chain activity to watch for pertaining to a channel.
+enum WatchEvent {
+       /// Watch for a transaction with `txid` and having an output with `script_pubkey` as a spending
+       /// condition.
+       WatchTransaction {
+               /// Identifier of the transaction.
+               txid: Txid,
+
+               /// Spending condition for an output of the transaction.
+               script_pubkey: Script,
+       },
+       /// Watch for spends of a transaction output identified by `outpoint` having `script_pubkey` as
+       /// the spending condition.
+       WatchOutput {
+               /// Identifier for the output.
+               outpoint: OutPoint,
+
+               /// Spending condition for the output.
+               script_pubkey: Script,
+       }
+}
+
+impl WatchEventCache {
        fn new() -> Self {
                Self {
                        watched: ChainWatchedUtil::new(),
@@ -224,7 +249,7 @@ impl WatchEventQueue {
 
        fn watch_tx(&mut self, txid: &Txid, script_pubkey: &Script) {
                if self.watched.register_tx(txid, script_pubkey) {
-                       self.events.push(chain::WatchEvent::WatchTransaction {
+                       self.events.push(WatchEvent::WatchTransaction {
                                txid: *txid,
                                script_pubkey: script_pubkey.clone()
                        });
@@ -234,7 +259,7 @@ impl WatchEventQueue {
        fn watch_output(&mut self, outpoint: (&Txid, usize), script_pubkey: &Script) {
                let (txid, index) = outpoint;
                if self.watched.register_outpoint((*txid, index as u32), script_pubkey) {
-                       self.events.push(chain::WatchEvent::WatchOutput {
+                       self.events.push(WatchEvent::WatchOutput {
                                outpoint: OutPoint {
                                        txid: *txid,
                                        index: index as u16,
@@ -244,10 +269,24 @@ impl WatchEventQueue {
                }
        }
 
-       fn dequeue_events(&mut self) -> Vec<chain::WatchEvent> {
-               let mut pending_events = Vec::with_capacity(self.events.len());
-               pending_events.append(&mut self.events);
-               pending_events
+       fn flush_events<C: Deref>(&mut self, chain_source: &Option<C>) -> bool where C::Target: chain::Filter {
+               let num_events = self.events.len();
+               match chain_source {
+                       &None => self.events.clear(),
+                       &Some(ref chain_source) => {
+                               for event in self.events.drain(..) {
+                                       match event {
+                                               WatchEvent::WatchTransaction { txid, script_pubkey } => {
+                                                       chain_source.register_tx(txid, script_pubkey)
+                                               },
+                                               WatchEvent::WatchOutput { outpoint, script_pubkey } => {
+                                                       chain_source.register_output(outpoint, script_pubkey)
+                                               },
+                                       }
+                               }
+                       }
+               }
+               num_events > 0
        }
 
        fn filter_block<'a>(&self, txdata: &[(usize, &'a Transaction)]) -> Vec<(usize, &'a Transaction)> {
@@ -270,8 +309,9 @@ impl WatchEventQueue {
        }
 }
 
-impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSigner, T, F, L>
-       where T::Target: BroadcasterInterface,
+impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSigner, C, T, F, L>
+       where C::Target: chain::Filter,
+             T::Target: BroadcasterInterface,
              F::Target: FeeEstimator,
              L::Target: Logger,
 {
@@ -280,9 +320,15 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
        /// [`ChannelMonitor::block_connected`] for details. Any HTLCs that were resolved on chain will
        /// be returned by [`chain::Watch::release_pending_monitor_events`].
        ///
+       /// Calls back to [`chain::Filter`] if any monitor indicated new outputs to watch, returning
+       /// `true` if so. Subsequent calls must not exclude any transactions matching the new outputs
+       /// nor any in-block descendants of such transactions. It is not necessary to re-fetch the block
+       /// to obtain updated `txdata`.
+       ///
        /// [`ChannelMonitor::block_connected`]: struct.ChannelMonitor.html#method.block_connected
        /// [`chain::Watch::release_pending_monitor_events`]: ../../chain/trait.Watch.html#tymethod.release_pending_monitor_events
-       pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) {
+       /// [`chain::Filter`]: ../../chain/trait.Filter.html
+       pub fn block_connected(&self, header: &BlockHeader, txdata: &[(usize, &Transaction)], height: u32) -> bool {
                let mut watch_events = self.watch_events.lock().unwrap();
                let matched_txn = watch_events.filter_block(txdata);
                {
@@ -297,6 +343,7 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
                                }
                        }
                }
+               watch_events.flush_events(&self.chain_source)
        }
 
        /// Dispatches to per-channel monitors, which are responsible for updating their on-chain view
@@ -312,17 +359,26 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
        }
 }
 
-impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSigner, T, F, L>
-       where T::Target: BroadcasterInterface,
+impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSigner, C, T, F, L>
+       where C::Target: chain::Filter,
+             T::Target: BroadcasterInterface,
              F::Target: FeeEstimator,
              L::Target: Logger,
 {
-       /// Creates a new object which can be used to monitor several channels given the chain
-       /// interface with which to register to receive notifications.
-       pub fn new(broadcaster: T, logger: L, feeest: F) -> Self {
+       /// Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
+       ///
+       /// When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
+       /// will call back to it indicating transactions and outputs of interest. This allows clients to
+       /// pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
+       /// always need to fetch full blocks absent another means for determining which blocks contain
+       /// transactions relevant to the watched channels.
+       ///
+       /// [`chain::Filter`]: ../../chain/trait.Filter.html
+       pub fn new(chain_source: Option<C>, broadcaster: T, logger: L, feeest: F) -> Self {
                Self {
                        monitors: Mutex::new(HashMap::new()),
-                       watch_events: Mutex::new(WatchEventQueue::new()),
+                       watch_events: Mutex::new(WatchEventCache::new()),
+                       chain_source,
                        broadcaster,
                        logger,
                        fee_estimator: feeest,
@@ -330,6 +386,10 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
        }
 
        /// Adds the monitor that watches the channel referred to by the given outpoint.
+       ///
+       /// Calls back to [`chain::Filter`] with the funding transaction and outputs to watch.
+       ///
+       /// [`chain::Filter`]: ../../chain/trait.Filter.html
        fn add_monitor(&self, outpoint: OutPoint, monitor: ChannelMonitor<ChanSigner>) -> Result<(), MonitorUpdateError> {
                let mut watch_events = self.watch_events.lock().unwrap();
                let mut monitors = self.monitors.lock().unwrap();
@@ -349,6 +409,7 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
                        }
                }
                entry.insert(monitor);
+               watch_events.flush_events(&self.chain_source);
                Ok(())
        }
 
@@ -365,8 +426,9 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> ChainMonitor<ChanSig
        }
 }
 
-impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send> chain::Watch for ChainMonitor<ChanSigner, T, F, L>
-       where T::Target: BroadcasterInterface,
+impl<ChanSigner: ChannelKeys, C: Deref + Sync + Send, T: Deref + Sync + Send, F: Deref + Sync + Send, L: Deref + Sync + Send> chain::Watch for ChainMonitor<ChanSigner, C, T, F, L>
+       where C::Target: chain::Filter,
+             T::Target: BroadcasterInterface,
              F::Target: FeeEstimator,
              L::Target: Logger,
 {
@@ -395,8 +457,9 @@ impl<ChanSigner: ChannelKeys, T: Deref + Sync + Send, F: Deref + Sync + Send, L:
        }
 }
 
-impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> events::EventsProvider for ChainMonitor<ChanSigner, T, F, L>
-       where T::Target: BroadcasterInterface,
+impl<ChanSigner: ChannelKeys, C: Deref, T: Deref, F: Deref, L: Deref> events::EventsProvider for ChainMonitor<ChanSigner, C, T, F, L>
+       where C::Target: chain::Filter,
+             T::Target: BroadcasterInterface,
              F::Target: FeeEstimator,
              L::Target: Logger,
 {
@@ -409,16 +472,6 @@ impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> events::EventsProvid
        }
 }
 
-impl<ChanSigner: ChannelKeys, T: Deref, F: Deref, L: Deref> chain::WatchEventProvider for ChainMonitor<ChanSigner, T, F, L>
-       where T::Target: BroadcasterInterface,
-             F::Target: FeeEstimator,
-             L::Target: Logger,
-{
-       fn release_pending_watch_events(&self) -> Vec<chain::WatchEvent> {
-               self.watch_events.lock().unwrap().dequeue_events()
-       }
-}
-
 /// If an HTLC expires within this many blocks, don't try to claim it in a shared transaction,
 /// instead claiming it in its own individual transaction.
 pub(crate) const CLTV_SHARED_CLAIM_BUFFER: u32 = 12;