Wake `background-processor` from `ChainMonitor` on new blocks 2024-04-notify-bp-on-blocks
authorMatt Corallo <git@bluematt.me>
Tue, 30 Apr 2024 21:30:26 +0000 (21:30 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 1 May 2024 19:42:24 +0000 (19:42 +0000)
When we receive a new block we may generate
`Event::SpendableOutputs` in `ChannelMonitor`s which then need to
be processed by the background processor. While it will do so
eventually when its normal loop goes around, this may cause user
tests to be delayed in finding events, so we should notify the BP
immediately to wake it on new blocks.

We implement that here, unconditionally notifying the
`background-processor` whenever we receive a new block or confirmed
transactions.

lightning/src/chain/chainmonitor.rs

index 81ba30ffb83ed964ca4fe13901be979073e2e6ad..8e567087054b4a0b11e5bc495cc074343e398b6f 100644 (file)
@@ -296,6 +296,8 @@ pub struct ChainMonitor<ChannelSigner: WriteableEcdsaChannelSigner, C: Deref, T:
        /// The best block height seen, used as a proxy for the passage of time.
        highest_chain_height: AtomicUsize,
 
+       /// A [`Notifier`] used to wake up the background processor in case we have any [`Event`]s for
+       /// it to give to users (or [`MonitorEvent`]s for `ChannelManager` to process).
        event_notifier: Notifier,
 }
 
@@ -738,6 +740,8 @@ where
                        monitor.block_connected(
                                header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &self.logger)
                });
+               // Assume we may have some new events and wake the event processor
+               self.event_notifier.notify();
        }
 
        fn block_disconnected(&self, header: &Header, height: u32) {
@@ -765,6 +769,8 @@ where
                        monitor.transactions_confirmed(
                                header, txdata, height, &*self.broadcaster, &*self.fee_estimator, &self.logger)
                });
+               // Assume we may have some new events and wake the event processor
+               self.event_notifier.notify();
        }
 
        fn transaction_unconfirmed(&self, txid: &Txid) {
@@ -785,6 +791,8 @@ where
                                header, height, &*self.broadcaster, &*self.fee_estimator, &self.logger
                        )
                });
+               // Assume we may have some new events and wake the event processor
+               self.event_notifier.notify();
        }
 
        fn get_relevant_txids(&self) -> Vec<(Txid, u32, Option<BlockHash>)> {