]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Introduce message_received in ChannelMessageHandler
authorshaavan <shaavan.github@gmail.com>
Mon, 10 Jun 2024 11:46:08 +0000 (17:16 +0530)
committershaavan <shaavan.github@gmail.com>
Thu, 12 Sep 2024 13:22:48 +0000 (18:52 +0530)
- Introduce the `message_received` function to manage the
  behavior when a message is received from any peer.
- This function is used within `ChannelManager` to retry `InvoiceRequest`
  messages if we haven't received the corresponding invoice yet.
- This change makes the offer communication robust against sudden
  connection drops where the initial attempt to send the message
  might have failed.

lightning-net-tokio/src/lib.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/msgs.rs
lightning/src/ln/peer_handler.rs
lightning/src/util/test_utils.rs

index 4306e192180c8cf61e5d6ed634fc33590485c03f..89ac7a52ec2d21688617c119ef4588678120efe4 100644 (file)
@@ -786,6 +786,7 @@ mod tests {
                fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
                        Some(vec![ChainHash::using_genesis_block(Network::Testnet)])
                }
+               fn message_received(&self) {}
        }
        impl MessageSendEventsProvider for MsgHandler {
                fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {
index 609effd7c4909edf79d7aaea044e5345fb784b52..5f8dc1e5541fa1689caf22276b28bf9624d6a9c8 100644 (file)
@@ -10824,6 +10824,39 @@ where
                        "Dual-funded channels not supported".to_owned(),
                        msg.channel_id.clone())), counterparty_node_id);
        }
+
+       fn message_received(&self) {
+               for (payment_id, retryable_invoice_request) in self
+                       .pending_outbound_payments
+                       .release_invoice_requests_awaiting_invoice()
+               {
+                       let RetryableInvoiceRequest { invoice_request, nonce } = retryable_invoice_request;
+                       let hmac = payment_id.hmac_for_offer_payment(nonce, &self.inbound_payment_key);
+                       let context = OffersContext::OutboundPayment {
+                               payment_id,
+                               nonce,
+                               hmac: Some(hmac)
+                       };
+                       match self.create_blinded_paths(context) {
+                               Ok(reply_paths) => match self.enqueue_invoice_request(invoice_request, reply_paths) {
+                                       Ok(_) => {}
+                                       Err(_) => {
+                                               log_warn!(self.logger,
+                                                       "Retry failed for an invoice request with payment_id: {}",
+                                                       payment_id
+                                               );
+                                       }
+                               },
+                               Err(_) => {
+                                       log_warn!(self.logger,
+                                               "Retry failed for an invoice request with payment_id: {}. \
+                                                       Reason: router could not find a blinded path to include as the reply path",
+                                               payment_id
+                                       );
+                               }
+                       }
+               }
+       }
 }
 
 impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>
index 72646bf5d4af9689784915e8153c2d0a9c563f7e..5c536a09dc4b79ee326d712d8a1498f0d2daa619 100644 (file)
@@ -1605,6 +1605,14 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
        /// If it's `None`, then no particular network chain hash compatibility will be enforced when
        /// connecting to peers.
        fn get_chain_hashes(&self) -> Option<Vec<ChainHash>>;
+
+       /// Indicates that a message was received from any peer for any handler.
+       /// Called before the message is passed to the appropriate handler.
+       /// Useful for indicating that a network connection is active.
+       ///
+       /// Note: Since this function is called frequently, it should be as
+       /// efficient as possible for its intended purpose.
+       fn message_received(&self);
 }
 
 /// A trait to describe an object which can receive routing messages.
index fa26600250e0897dbef9a79c45a79d89e0d38fca..3c0d724ad9423a2b18da59b734dd3e08d4bef287 100644 (file)
@@ -388,6 +388,8 @@ impl ChannelMessageHandler for ErroringMessageHandler {
        fn handle_tx_abort(&self, their_node_id: PublicKey, msg: &msgs::TxAbort) {
                ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
        }
+
+       fn message_received(&self) {}
 }
 
 impl Deref for ErroringMessageHandler {
@@ -1616,6 +1618,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
                let their_node_id = peer_lock.their_node_id.expect("We know the peer's public key by the time we receive messages").0;
                let logger = WithContext::from(&self.logger, Some(their_node_id), None, None);
 
+               self.message_handler.chan_handler.message_received();
+               
                let message = match self.do_handle_message_holding_peer_lock(peer_lock, message, their_node_id, &logger)? {
                        Some(processed_message) => processed_message,
                        None => return Ok(None),
index e41ba359b3b8d25bf6d181c2cd8587f90f4b56cf..a15584c9178e3d75b9428ef1c41fbce36a195c1d 100644 (file)
@@ -942,6 +942,8 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
        fn handle_tx_abort(&self, _their_node_id: PublicKey, msg: &msgs::TxAbort) {
                self.received_msg(wire::Message::TxAbort(msg.clone()));
        }
+
+       fn message_received(&self) {}
 }
 
 impl events::MessageSendEventsProvider for TestChannelMessageHandler {