Skip to content

Commit 03da11e

Browse files
committed
Introduce InvoiceRequest as a field in AwaitingInvoice.
This will be used to recreate InvoiceRequest messages for retries for the payments that are still awaiting receiving an invoice.
1 parent bd3cc00 commit 03da11e

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8635,7 +8635,7 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
86358635
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
86368636
$self.pending_outbound_payments
86378637
.add_new_awaiting_invoice(
8638-
payment_id, expiration, retry_strategy, max_total_routing_fee_msat,
8638+
payment_id, expiration, retry_strategy, max_total_routing_fee_msat, None
86398639
)
86408640
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
86418641

@@ -8752,7 +8752,7 @@ where
87528752
let expiration = StaleExpiration::TimerTicks(1);
87538753
self.pending_outbound_payments
87548754
.add_new_awaiting_invoice(
8755-
payment_id, expiration, retry_strategy, max_total_routing_fee_msat
8755+
payment_id, expiration, retry_strategy, max_total_routing_fee_msat, Some(invoice_request.clone())
87568756
)
87578757
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
87588758

lightning/src/ln/outbound_payment.rs

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use bitcoin::hashes::Hash;
1313
use bitcoin::hashes::sha256::Hash as Sha256;
1414
use bitcoin::secp256k1::{self, Secp256k1, SecretKey};
1515

16+
use crate::offers::invoice_request::InvoiceRequest;
1617
use crate::sign::{EntropySource, NodeSigner, Recipient};
1718
use crate::events::{self, PaymentFailureReason};
1819
use crate::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
@@ -50,6 +51,7 @@ pub(crate) enum PendingOutboundPayment {
5051
expiration: StaleExpiration,
5152
retry_strategy: Retry,
5253
max_total_routing_fee_msat: Option<u64>,
54+
invoice_request: Option<InvoiceRequest>,
5355
},
5456
InvoiceReceived {
5557
payment_hash: PaymentHash,
@@ -1291,7 +1293,7 @@ impl OutboundPayments {
12911293

12921294
pub(super) fn add_new_awaiting_invoice(
12931295
&self, payment_id: PaymentId, expiration: StaleExpiration, retry_strategy: Retry,
1294-
max_total_routing_fee_msat: Option<u64>
1296+
max_total_routing_fee_msat: Option<u64>, invoice_request: Option<InvoiceRequest>
12951297
) -> Result<(), ()> {
12961298
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
12971299
match pending_outbounds.entry(payment_id) {
@@ -1301,6 +1303,7 @@ impl OutboundPayments {
13011303
expiration,
13021304
retry_strategy,
13031305
max_total_routing_fee_msat,
1306+
invoice_request,
13041307
});
13051308

13061309
Ok(())
@@ -1766,6 +1769,20 @@ impl OutboundPayments {
17661769
pub fn clear_pending_payments(&self) {
17671770
self.pending_outbound_payments.lock().unwrap().clear()
17681771
}
1772+
1773+
pub fn get_invoice_request_awaiting_invoice(&self) -> Vec<InvoiceRequest> {
1774+
let pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
1775+
1776+
pending_outbound_payments.iter().filter_map(
1777+
|(_, payment)| {
1778+
if let PendingOutboundPayment::AwaitingInvoice { invoice_request, .. } = payment {
1779+
invoice_request.clone()
1780+
} else {
1781+
None
1782+
}
1783+
}
1784+
).collect()
1785+
}
17691786
}
17701787

17711788
/// Returns whether a payment with the given [`PaymentHash`] and [`PaymentId`] is, in fact, a
@@ -1821,6 +1838,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
18211838
(0, expiration, required),
18221839
(2, retry_strategy, required),
18231840
(4, max_total_routing_fee_msat, option),
1841+
(6, invoice_request, option),
18241842
},
18251843
(7, InvoiceReceived) => {
18261844
(0, payment_hash, required),
@@ -2058,7 +2076,7 @@ mod tests {
20582076
assert!(!outbound_payments.has_pending_payments());
20592077
assert!(
20602078
outbound_payments.add_new_awaiting_invoice(
2061-
payment_id, expiration, Retry::Attempts(0), None
2079+
payment_id, expiration, Retry::Attempts(0), None, None
20622080
).is_ok()
20632081
);
20642082
assert!(outbound_payments.has_pending_payments());
@@ -2084,14 +2102,14 @@ mod tests {
20842102

20852103
assert!(
20862104
outbound_payments.add_new_awaiting_invoice(
2087-
payment_id, expiration, Retry::Attempts(0), None
2105+
payment_id, expiration, Retry::Attempts(0), None, None
20882106
).is_ok()
20892107
);
20902108
assert!(outbound_payments.has_pending_payments());
20912109

20922110
assert!(
20932111
outbound_payments.add_new_awaiting_invoice(
2094-
payment_id, expiration, Retry::Attempts(0), None
2112+
payment_id, expiration, Retry::Attempts(0), None, None
20952113
).is_err()
20962114
);
20972115
}
@@ -2107,7 +2125,7 @@ mod tests {
21072125
assert!(!outbound_payments.has_pending_payments());
21082126
assert!(
21092127
outbound_payments.add_new_awaiting_invoice(
2110-
payment_id, expiration, Retry::Attempts(0), None
2128+
payment_id, expiration, Retry::Attempts(0), None, None
21112129
).is_ok()
21122130
);
21132131
assert!(outbound_payments.has_pending_payments());
@@ -2133,14 +2151,14 @@ mod tests {
21332151

21342152
assert!(
21352153
outbound_payments.add_new_awaiting_invoice(
2136-
payment_id, expiration, Retry::Attempts(0), None
2154+
payment_id, expiration, Retry::Attempts(0), None, None
21372155
).is_ok()
21382156
);
21392157
assert!(outbound_payments.has_pending_payments());
21402158

21412159
assert!(
21422160
outbound_payments.add_new_awaiting_invoice(
2143-
payment_id, expiration, Retry::Attempts(0), None
2161+
payment_id, expiration, Retry::Attempts(0), None, None
21442162
).is_err()
21452163
);
21462164
}
@@ -2155,7 +2173,7 @@ mod tests {
21552173
assert!(!outbound_payments.has_pending_payments());
21562174
assert!(
21572175
outbound_payments.add_new_awaiting_invoice(
2158-
payment_id, expiration, Retry::Attempts(0), None
2176+
payment_id, expiration, Retry::Attempts(0), None, None
21592177
).is_ok()
21602178
);
21612179
assert!(outbound_payments.has_pending_payments());
@@ -2188,7 +2206,7 @@ mod tests {
21882206

21892207
assert!(
21902208
outbound_payments.add_new_awaiting_invoice(
2191-
payment_id, expiration, Retry::Attempts(0), None
2209+
payment_id, expiration, Retry::Attempts(0), None, None
21922210
).is_ok()
21932211
);
21942212
assert!(outbound_payments.has_pending_payments());
@@ -2250,7 +2268,7 @@ mod tests {
22502268
assert!(
22512269
outbound_payments.add_new_awaiting_invoice(
22522270
payment_id, expiration, Retry::Attempts(0),
2253-
Some(invoice.amount_msats() / 100 + 50_000)
2271+
Some(invoice.amount_msats() / 100 + 50_000), None
22542272
).is_ok()
22552273
);
22562274
assert!(outbound_payments.has_pending_payments());
@@ -2347,7 +2365,7 @@ mod tests {
23472365

23482366
assert!(
23492367
outbound_payments.add_new_awaiting_invoice(
2350-
payment_id, expiration, Retry::Attempts(0), Some(1234)
2368+
payment_id, expiration, Retry::Attempts(0), Some(1234), None
23512369
).is_ok()
23522370
);
23532371
assert!(outbound_payments.has_pending_payments());

lightning/src/offers/invoice_request.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,13 @@ impl Writeable for InvoiceRequestContents {
10091009
}
10101010
}
10111011

1012+
impl Readable for InvoiceRequest<> {
1013+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
1014+
let bytes: WithoutLength<Vec<u8>> = Readable::read(reader)?;
1015+
Self::try_from(bytes.0).map_err(|_| DecodeError::InvalidValue)
1016+
}
1017+
}
1018+
10121019
/// Valid type range for invoice_request TLV records.
10131020
pub(super) const INVOICE_REQUEST_TYPES: core::ops::Range<u64> = 80..160;
10141021

0 commit comments

Comments
 (0)