Skip to content

Commit 5cccee5

Browse files
committed
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.
1 parent 1881f35 commit 5cccee5

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ mod tests {
786786
fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
787787
Some(vec![ChainHash::using_genesis_block(Network::Testnet)])
788788
}
789+
fn message_received(&self) {}
789790
}
790791
impl MessageSendEventsProvider for MsgHandler {
791792
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {

lightning/src/ln/channelmanager.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10824,6 +10824,39 @@ where
1082410824
"Dual-funded channels not supported".to_owned(),
1082510825
msg.channel_id.clone())), counterparty_node_id);
1082610826
}
10827+
10828+
fn message_received(&self) {
10829+
for (payment_id, retryable_invoice_request) in self
10830+
.pending_outbound_payments
10831+
.release_invoice_requests_awaiting_invoice()
10832+
{
10833+
let RetryableInvoiceRequest { invoice_request, nonce } = retryable_invoice_request;
10834+
let hmac = payment_id.hmac_for_offer_payment(nonce, &self.inbound_payment_key);
10835+
let context = OffersContext::OutboundPayment {
10836+
payment_id,
10837+
nonce,
10838+
hmac: Some(hmac)
10839+
};
10840+
match self.create_blinded_paths(context) {
10841+
Ok(reply_paths) => match self.enqueue_invoice_request(invoice_request, reply_paths) {
10842+
Ok(_) => {}
10843+
Err(_) => {
10844+
log_warn!(self.logger,
10845+
"Retry failed for an invoice request with payment_id: {}",
10846+
payment_id
10847+
);
10848+
}
10849+
},
10850+
Err(_) => {
10851+
log_warn!(self.logger,
10852+
"Retry failed for an invoice request with payment_id: {}. \
10853+
Reason: router could not find a blinded path to include as the reply path",
10854+
payment_id
10855+
);
10856+
}
10857+
}
10858+
}
10859+
}
1082710860
}
1082810861

1082910862
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, L: Deref>

lightning/src/ln/msgs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,14 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
16051605
/// If it's `None`, then no particular network chain hash compatibility will be enforced when
16061606
/// connecting to peers.
16071607
fn get_chain_hashes(&self) -> Option<Vec<ChainHash>>;
1608+
1609+
/// Indicates that a message was received from any peer for any handler.
1610+
/// Called before the message is passed to the appropriate handler.
1611+
/// Useful for indicating that a network connection is active.
1612+
///
1613+
/// Note: Since this function is called frequently, it should be as
1614+
/// efficient as possible for its intended purpose.
1615+
fn message_received(&self);
16081616
}
16091617

16101618
/// A trait to describe an object which can receive routing messages.

lightning/src/ln/peer_handler.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ impl ChannelMessageHandler for ErroringMessageHandler {
388388
fn handle_tx_abort(&self, their_node_id: PublicKey, msg: &msgs::TxAbort) {
389389
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
390390
}
391+
392+
fn message_received(&self) {}
391393
}
392394

393395
impl Deref for ErroringMessageHandler {
@@ -1616,6 +1618,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
16161618
let their_node_id = peer_lock.their_node_id.expect("We know the peer's public key by the time we receive messages").0;
16171619
let logger = WithContext::from(&self.logger, Some(their_node_id), None, None);
16181620

1621+
self.message_handler.chan_handler.message_received();
1622+
16191623
let message = match self.do_handle_message_holding_peer_lock(peer_lock, message, their_node_id, &logger)? {
16201624
Some(processed_message) => processed_message,
16211625
None => return Ok(None),

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,8 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
942942
fn handle_tx_abort(&self, _their_node_id: PublicKey, msg: &msgs::TxAbort) {
943943
self.received_msg(wire::Message::TxAbort(msg.clone()));
944944
}
945+
946+
fn message_received(&self) {}
945947
}
946948

947949
impl events::MessageSendEventsProvider for TestChannelMessageHandler {

0 commit comments

Comments
 (0)