Skip to content

Commit 7eeb226

Browse files
committed
Introduce handle_message_received in ChannelMessageHandler
- Introduce the `handle_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 more robust against sudden connection drops where the initial attempt to send the message might have failed.
1 parent 3b7d74c commit 7eeb226

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ mod tests {
787787
fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
788788
Some(vec![ChainHash::using_genesis_block(Network::Testnet)])
789789
}
790+
fn handle_message_received(&self) {}
790791
}
791792
impl MessageSendEventsProvider for MsgHandler {
792793
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
@@ -10746,6 +10746,39 @@ where
1074610746
"Dual-funded channels not supported".to_owned(),
1074710747
msg.channel_id.clone())), *counterparty_node_id);
1074810748
}
10749+
10750+
fn handle_message_received(&self) {
10751+
for (payment_id, retryable_invoice_request) in self
10752+
.pending_outbound_payments
10753+
.release_invoice_requests_awaiting_invoice()
10754+
{
10755+
let RetryableInvoiceRequest { invoice_request, nonce } = retryable_invoice_request;
10756+
let hmac = payment_id.hmac_for_offer_payment(nonce, &self.inbound_payment_key);
10757+
let context = OffersContext::OutboundPayment {
10758+
payment_id,
10759+
nonce,
10760+
hmac: Some(hmac)
10761+
};
10762+
match self.create_blinded_paths(context) {
10763+
Ok(reply_paths) => match self.enqueue_invoice_request(invoice_request, reply_paths) {
10764+
Ok(_) => {}
10765+
Err(_) => {
10766+
log_warn!(self.logger,
10767+
"Retry failed for an invoice request with payment_id: {}",
10768+
payment_id
10769+
);
10770+
}
10771+
},
10772+
Err(_) => {
10773+
log_warn!(self.logger,
10774+
"Retry failed for an invoice request with payment_id: {}. \
10775+
Reason: router could not find a blinded path to include as the reply path",
10776+
payment_id
10777+
);
10778+
}
10779+
}
10780+
}
10781+
}
1074910782
}
1075010783

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

lightning/src/ln/msgs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,10 @@ 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+
/// Handles behavior which is triggered when any message from any peer for any handler
1610+
/// is received.
1611+
fn handle_message_received(&self);
16081612
}
16091613

16101614
/// 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
@@ -387,6 +387,8 @@ impl ChannelMessageHandler for ErroringMessageHandler {
387387
fn handle_tx_abort(&self, their_node_id: &PublicKey, msg: &msgs::TxAbort) {
388388
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
389389
}
390+
391+
fn handle_message_received(&self) {}
390392
}
391393

392394
impl Deref for ErroringMessageHandler {
@@ -1750,6 +1752,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
17501752
log_trace!(logger, "Received message {:?} from {}", message, log_pubkey!(their_node_id));
17511753
}
17521754

1755+
self.message_handler.chan_handler.handle_message_received();
1756+
17531757
let mut should_forward = None;
17541758

17551759
match message {

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,8 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
936936
fn handle_tx_abort(&self, _their_node_id: &PublicKey, msg: &msgs::TxAbort) {
937937
self.received_msg(wire::Message::TxAbort(msg.clone()));
938938
}
939+
940+
fn handle_message_received(&self) {}
939941
}
940942

941943
impl events::MessageSendEventsProvider for TestChannelMessageHandler {

0 commit comments

Comments
 (0)