Skip to content

Commit ca505d5

Browse files
committed
Add retry_invoice_request_messages function
- Introduce retry_invoice_request_messages to handle retrying InvoiceRequest messages for PendingOutboundPayments. - This function retrieves the InvoiceRequest, uses a new reply_path, and enqueues the messages for retry. - Trigger this function when any ChannelMessageHandler function is invoked. - Ensures retries of InvoiceRequest messages if initial attempts fail due to connection loss, retrying when back online and receiving messages from peers.
1 parent 5b4ba4b commit ca505d5

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

lightning-net-tokio/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ mod tests {
658658
fn get_chain_hashes(&self) -> Option<Vec<ChainHash>> {
659659
Some(vec![ChainHash::using_genesis_block(Network::Testnet)])
660660
}
661+
fn retry_invoice_requests(&self) -> Result<(), ()> { Ok(()) }
661662
}
662663
impl MessageSendEventsProvider for MsgHandler {
663664
fn get_and_clear_pending_msg_events(&self) -> Vec<MessageSendEvent> {

lightning/src/ln/channelmanager.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9515,6 +9515,8 @@ where
95159515
L::Target: Logger,
95169516
{
95179517
fn handle_open_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::OpenChannel) {
9518+
9519+
let _ = self.retry_invoice_requests();
95189520
// Note that we never need to persist the updated ChannelManager for an inbound
95199521
// open_channel message - pre-funded channels are never written so there should be no
95209522
// change to the contents.
@@ -9533,12 +9535,14 @@ where
95339535
}
95349536

95359537
fn handle_open_channel_v2(&self, counterparty_node_id: &PublicKey, msg: &msgs::OpenChannelV2) {
9538+
let _ = self.retry_invoice_requests();
95369539
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
95379540
"Dual-funded channels not supported".to_owned(),
95389541
msg.common_fields.temporary_channel_id.clone())), *counterparty_node_id);
95399542
}
95409543

95419544
fn handle_accept_channel(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannel) {
9545+
let _ = self.retry_invoice_requests();
95429546
// Note that we never need to persist the updated ChannelManager for an inbound
95439547
// accept_channel message - pre-funded channels are never written so there should be no
95449548
// change to the contents.
@@ -9549,22 +9553,26 @@ where
95499553
}
95509554

95519555
fn handle_accept_channel_v2(&self, counterparty_node_id: &PublicKey, msg: &msgs::AcceptChannelV2) {
9556+
let _ = self.retry_invoice_requests();
95529557
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
95539558
"Dual-funded channels not supported".to_owned(),
95549559
msg.common_fields.temporary_channel_id.clone())), *counterparty_node_id);
95559560
}
95569561

95579562
fn handle_funding_created(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingCreated) {
9563+
let _ = self.retry_invoice_requests();
95589564
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
95599565
let _ = handle_error!(self, self.internal_funding_created(counterparty_node_id, msg), *counterparty_node_id);
95609566
}
95619567

95629568
fn handle_funding_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::FundingSigned) {
9569+
let _ = self.retry_invoice_requests();
95639570
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
95649571
let _ = handle_error!(self, self.internal_funding_signed(counterparty_node_id, msg), *counterparty_node_id);
95659572
}
95669573

95679574
fn handle_channel_ready(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReady) {
9575+
let _ = self.retry_invoice_requests();
95689576
// Note that we never need to persist the updated ChannelManager for an inbound
95699577
// channel_ready message - while the channel's state will change, any channel_ready message
95709578
// will ultimately be re-sent on startup and the `ChannelMonitor` won't be updated so we
@@ -9581,43 +9589,50 @@ where
95819589
}
95829590

95839591
fn handle_stfu(&self, counterparty_node_id: &PublicKey, msg: &msgs::Stfu) {
9592+
let _ = self.retry_invoice_requests();
95849593
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
95859594
"Quiescence not supported".to_owned(),
95869595
msg.channel_id.clone())), *counterparty_node_id);
95879596
}
95889597

95899598
#[cfg(splicing)]
95909599
fn handle_splice(&self, counterparty_node_id: &PublicKey, msg: &msgs::Splice) {
9600+
let _ = self.retry_invoice_requests();
95919601
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
95929602
"Splicing not supported".to_owned(),
95939603
msg.channel_id.clone())), *counterparty_node_id);
95949604
}
95959605

95969606
#[cfg(splicing)]
95979607
fn handle_splice_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceAck) {
9608+
let _ = self.retry_invoice_requests();
95989609
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
95999610
"Splicing not supported (splice_ack)".to_owned(),
96009611
msg.channel_id.clone())), *counterparty_node_id);
96019612
}
96029613

96039614
#[cfg(splicing)]
96049615
fn handle_splice_locked(&self, counterparty_node_id: &PublicKey, msg: &msgs::SpliceLocked) {
9616+
let _ = self.retry_invoice_requests();
96059617
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
96069618
"Splicing not supported (splice_locked)".to_owned(),
96079619
msg.channel_id.clone())), *counterparty_node_id);
96089620
}
96099621

96109622
fn handle_shutdown(&self, counterparty_node_id: &PublicKey, msg: &msgs::Shutdown) {
9623+
let _ = self.retry_invoice_requests();
96119624
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
96129625
let _ = handle_error!(self, self.internal_shutdown(counterparty_node_id, msg), *counterparty_node_id);
96139626
}
96149627

96159628
fn handle_closing_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::ClosingSigned) {
9629+
let _ = self.retry_invoice_requests();
96169630
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
96179631
let _ = handle_error!(self, self.internal_closing_signed(counterparty_node_id, msg), *counterparty_node_id);
96189632
}
96199633

96209634
fn handle_update_add_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateAddHTLC) {
9635+
let _ = self.retry_invoice_requests();
96219636
// Note that we never need to persist the updated ChannelManager for an inbound
96229637
// update_add_htlc message - the message itself doesn't change our channel state only the
96239638
// `commitment_signed` message afterwards will.
@@ -9634,11 +9649,13 @@ where
96349649
}
96359650

96369651
fn handle_update_fulfill_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFulfillHTLC) {
9652+
let _ = self.retry_invoice_requests();
96379653
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
96389654
let _ = handle_error!(self, self.internal_update_fulfill_htlc(counterparty_node_id, msg), *counterparty_node_id);
96399655
}
96409656

96419657
fn handle_update_fail_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailHTLC) {
9658+
let _ = self.retry_invoice_requests();
96429659
// Note that we never need to persist the updated ChannelManager for an inbound
96439660
// update_fail_htlc message - the message itself doesn't change our channel state only the
96449661
// `commitment_signed` message afterwards will.
@@ -9655,6 +9672,7 @@ where
96559672
}
96569673

96579674
fn handle_update_fail_malformed_htlc(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFailMalformedHTLC) {
9675+
let _ = self.retry_invoice_requests();
96589676
// Note that we never need to persist the updated ChannelManager for an inbound
96599677
// update_fail_malformed_htlc message - the message itself doesn't change our channel state
96609678
// only the `commitment_signed` message afterwards will.
@@ -9671,16 +9689,19 @@ where
96719689
}
96729690

96739691
fn handle_commitment_signed(&self, counterparty_node_id: &PublicKey, msg: &msgs::CommitmentSigned) {
9692+
let _ = self.retry_invoice_requests();
96749693
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
96759694
let _ = handle_error!(self, self.internal_commitment_signed(counterparty_node_id, msg), *counterparty_node_id);
96769695
}
96779696

96789697
fn handle_revoke_and_ack(&self, counterparty_node_id: &PublicKey, msg: &msgs::RevokeAndACK) {
9698+
let _ = self.retry_invoice_requests();
96799699
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
96809700
let _ = handle_error!(self, self.internal_revoke_and_ack(counterparty_node_id, msg), *counterparty_node_id);
96819701
}
96829702

96839703
fn handle_update_fee(&self, counterparty_node_id: &PublicKey, msg: &msgs::UpdateFee) {
9704+
let _ = self.retry_invoice_requests();
96849705
// Note that we never need to persist the updated ChannelManager for an inbound
96859706
// update_fee message - the message itself doesn't change our channel state only the
96869707
// `commitment_signed` message afterwards will.
@@ -9697,11 +9718,13 @@ where
96979718
}
96989719

96999720
fn handle_announcement_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) {
9721+
let _ = self.retry_invoice_requests();
97009722
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
97019723
let _ = handle_error!(self, self.internal_announcement_signatures(counterparty_node_id, msg), *counterparty_node_id);
97029724
}
97039725

97049726
fn handle_channel_update(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelUpdate) {
9727+
let _ = self.retry_invoice_requests();
97059728
PersistenceNotifierGuard::optionally_notify(self, || {
97069729
if let Ok(persist) = handle_error!(self, self.internal_channel_update(counterparty_node_id, msg), *counterparty_node_id) {
97079730
persist
@@ -9712,6 +9735,7 @@ where
97129735
}
97139736

97149737
fn handle_channel_reestablish(&self, counterparty_node_id: &PublicKey, msg: &msgs::ChannelReestablish) {
9738+
let _ = self.retry_invoice_requests();
97159739
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(self, || {
97169740
let res = self.internal_channel_reestablish(counterparty_node_id, msg);
97179741
let persist = match &res {
@@ -9725,6 +9749,7 @@ where
97259749
}
97269750

97279751
fn peer_disconnected(&self, counterparty_node_id: &PublicKey) {
9752+
let _ = self.retry_invoice_requests();
97289753
let _persistence_guard = PersistenceNotifierGuard::optionally_notify(
97299754
self, || NotifyOption::SkipPersistHandleEvents);
97309755
let mut failed_channels = Vec::new();
@@ -9847,6 +9872,7 @@ where
98479872
}
98489873

98499874
fn peer_connected(&self, counterparty_node_id: &PublicKey, init_msg: &msgs::Init, inbound: bool) -> Result<(), ()> {
9875+
let _ = self.retry_invoice_requests();
98509876
let logger = WithContext::from(&self.logger, Some(*counterparty_node_id), None, None);
98519877
if !init_msg.features.supports_static_remote_key() {
98529878
log_debug!(logger, "Peer {} does not support static remote key, disconnecting", log_pubkey!(counterparty_node_id));
@@ -9961,6 +9987,7 @@ where
99619987
}
99629988

99639989
fn handle_error(&self, counterparty_node_id: &PublicKey, msg: &msgs::ErrorMessage) {
9990+
let _ = self.retry_invoice_requests();
99649991
match &msg.data as &str {
99659992
"cannot co-op close channel w/ active htlcs"|
99669993
"link failed to shutdown" =>
@@ -10080,58 +10107,82 @@ where
1008010107
}
1008110108

1008210109
fn handle_tx_add_input(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAddInput) {
10110+
let _ = self.retry_invoice_requests();
1008310111
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1008410112
"Dual-funded channels not supported".to_owned(),
1008510113
msg.channel_id.clone())), *counterparty_node_id);
1008610114
}
1008710115

1008810116
fn handle_tx_add_output(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAddOutput) {
10117+
let _ = self.retry_invoice_requests();
1008910118
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1009010119
"Dual-funded channels not supported".to_owned(),
1009110120
msg.channel_id.clone())), *counterparty_node_id);
1009210121
}
1009310122

1009410123
fn handle_tx_remove_input(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxRemoveInput) {
10124+
let _ = self.retry_invoice_requests();
1009510125
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1009610126
"Dual-funded channels not supported".to_owned(),
1009710127
msg.channel_id.clone())), *counterparty_node_id);
1009810128
}
1009910129

1010010130
fn handle_tx_remove_output(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxRemoveOutput) {
10131+
let _ = self.retry_invoice_requests();
1010110132
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1010210133
"Dual-funded channels not supported".to_owned(),
1010310134
msg.channel_id.clone())), *counterparty_node_id);
1010410135
}
1010510136

1010610137
fn handle_tx_complete(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxComplete) {
10138+
let _ = self.retry_invoice_requests();
1010710139
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1010810140
"Dual-funded channels not supported".to_owned(),
1010910141
msg.channel_id.clone())), *counterparty_node_id);
1011010142
}
1011110143

1011210144
fn handle_tx_signatures(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxSignatures) {
10145+
let _ = self.retry_invoice_requests();
1011310146
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1011410147
"Dual-funded channels not supported".to_owned(),
1011510148
msg.channel_id.clone())), *counterparty_node_id);
1011610149
}
1011710150

1011810151
fn handle_tx_init_rbf(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxInitRbf) {
10152+
let _ = self.retry_invoice_requests();
1011910153
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1012010154
"Dual-funded channels not supported".to_owned(),
1012110155
msg.channel_id.clone())), *counterparty_node_id);
1012210156
}
1012310157

1012410158
fn handle_tx_ack_rbf(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAckRbf) {
10159+
let _ = self.retry_invoice_requests();
1012510160
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1012610161
"Dual-funded channels not supported".to_owned(),
1012710162
msg.channel_id.clone())), *counterparty_node_id);
1012810163
}
1012910164

1013010165
fn handle_tx_abort(&self, counterparty_node_id: &PublicKey, msg: &msgs::TxAbort) {
10166+
let _ = self.retry_invoice_requests();
1013110167
let _: Result<(), _> = handle_error!(self, Err(MsgHandleErrInternal::send_err_msg_no_close(
1013210168
"Dual-funded channels not supported".to_owned(),
1013310169
msg.channel_id.clone())), *counterparty_node_id);
1013410170
}
10171+
10172+
fn retry_invoice_requests(&self) -> Result<(), ()> {
10173+
let invoice_requests = self.pending_outbound_payments.get_invoice_request_awaiting_invoice();
10174+
if invoice_requests.is_empty() { return Ok(());}
10175+
10176+
let reply_path = self.create_blinded_path()?;
10177+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
10178+
10179+
for invoice_request in invoice_requests {
10180+
pending_offers_messages.extend(self.create_invoice_request_messages(
10181+
invoice_request, reply_path.clone()).map_err(|_| ())?);
10182+
}
10183+
10184+
Ok(())
10185+
}
1013510186
}
1013610187

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

lightning/src/ln/msgs.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,6 +1550,13 @@ pub trait ChannelMessageHandler : MessageSendEventsProvider {
15501550
/// If it's `None`, then no particular network chain hash compatibility will be enforced when
15511551
/// connecting to peers.
15521552
fn get_chain_hashes(&self) -> Option<Vec<ChainHash>>;
1553+
1554+
/// Retries the [`InvoiceRequest`] message for pending outbound payments
1555+
/// that are still awaiting a [`Bolt12Invoice`].
1556+
///
1557+
/// [`InvoiceRequest`]: crate::offers::invoice_request::InvoiceRequest
1558+
/// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
1559+
fn retry_invoice_requests(&self) -> Result<(), ()>;
15531560
}
15541561

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

lightning/src/ln/peer_handler.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ impl ChannelMessageHandler for ErroringMessageHandler {
379379
fn handle_tx_abort(&self, their_node_id: &PublicKey, msg: &msgs::TxAbort) {
380380
ErroringMessageHandler::push_error(self, their_node_id, msg.channel_id);
381381
}
382+
383+
fn retry_invoice_requests(&self) -> Result<(), ()> { Ok(()) }
382384
}
383385

384386
impl Deref for ErroringMessageHandler {

lightning/src/util/test_utils.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ impl msgs::ChannelMessageHandler for TestChannelMessageHandler {
907907
fn handle_tx_abort(&self, _their_node_id: &PublicKey, msg: &msgs::TxAbort) {
908908
self.received_msg(wire::Message::TxAbort(msg.clone()));
909909
}
910+
911+
fn retry_invoice_requests(&self) -> Result<(), ()> { Ok(()) }
910912
}
911913

912914
impl events::MessageSendEventsProvider for TestChannelMessageHandler {

0 commit comments

Comments
 (0)