From: shaavan Date: Mon, 10 Jun 2024 11:46:08 +0000 (+0530) Subject: Introduce message_received in ChannelMessageHandler X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=5cccee565f622604fdd08e106ffff4dccf1a77b5;p=rust-lightning Introduce message_received in ChannelMessageHandler - 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. --- diff --git a/lightning-net-tokio/src/lib.rs b/lightning-net-tokio/src/lib.rs index 4306e1921..89ac7a52e 100644 --- a/lightning-net-tokio/src/lib.rs +++ b/lightning-net-tokio/src/lib.rs @@ -786,6 +786,7 @@ mod tests { fn get_chain_hashes(&self) -> Option> { 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 { diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 609effd7c..5f8dc1e55 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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 diff --git a/lightning/src/ln/msgs.rs b/lightning/src/ln/msgs.rs index 72646bf5d..5c536a09d 100644 --- a/lightning/src/ln/msgs.rs +++ b/lightning/src/ln/msgs.rs @@ -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>; + + /// 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. diff --git a/lightning/src/ln/peer_handler.rs b/lightning/src/ln/peer_handler.rs index fa2660025..3c0d724ad 100644 --- a/lightning/src/ln/peer_handler.rs +++ b/lightning/src/ln/peer_handler.rs @@ -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 processed_message, None => return Ok(None), diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index e41ba359b..a15584c91 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -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 {