Skip to content

Commit 4b5c491

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 7218ae2 commit 4b5c491

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
@@ -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 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 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: 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
@@ -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 message_received(&self) {}
390392
}
391393

392394
impl Deref for ErroringMessageHandler {
@@ -1623,6 +1625,8 @@ impl<Descriptor: SocketDescriptor, CM: Deref, RM: Deref, OM: Deref, L: Deref, CM
16231625
let their_node_id = peer_lock.their_node_id.clone().expect("We know the peer's public key by the time we receive messages").0;
16241626
let logger = WithContext::from(&self.logger, Some(their_node_id), None, None);
16251627

1628+
self.message_handler.chan_handler.message_received();
1629+
16261630
let message = match self.do_handle_message_holding_peer_lock(peer_lock, message, &their_node_id, &logger)? {
16271631
Some(processed_message) => processed_message,
16281632
None => return Ok(None),

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 message_received(&self) {}
939941
}
940942

941943
impl events::MessageSendEventsProvider for TestChannelMessageHandler {

0 commit comments

Comments
 (0)