Skip to content

Commit bf1d03b

Browse files
committed
f: Include Nonce in AwaitingInvoice for context recreation
1. The Nonce is sufficient for reconstructing the context, eliminating the need to pass it separately. 2. This change prevents potential errors where an incorrect context could be mistakenly passed within AwaitingInvoice.
1 parent 115fab4 commit bf1d03b

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8898,7 +8898,7 @@ macro_rules! create_refund_builder { ($self: ident, $builder: ty) => {
88988898
let expiration = StaleExpiration::AbsoluteTimeout(absolute_expiry);
88998899
$self.pending_outbound_payments
89008900
.add_new_awaiting_invoice(
8901-
payment_id, expiration, retry_strategy, max_total_routing_fee_msat, None, context
8901+
payment_id, expiration, retry_strategy, max_total_routing_fee_msat, None, nonce
89028902
)
89038903
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
89048904

@@ -9017,7 +9017,7 @@ where
90179017
let invoice_request = builder.build_and_sign()?;
90189018

90199019
let context = OffersContext::OutboundPayment { payment_id, nonce };
9020-
let reply_paths = self.create_blinded_paths(context.clone())
9020+
let reply_paths = self.create_blinded_paths(context)
90219021
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
90229022

90239023
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
@@ -9026,7 +9026,7 @@ where
90269026
self.pending_outbound_payments
90279027
.add_new_awaiting_invoice(
90289028
payment_id, expiration, retry_strategy, max_total_routing_fee_msat,
9029-
Some(invoice_request.clone()), context,
9029+
Some(invoice_request.clone()), nonce,
90309030
)
90319031
.map_err(|_| Bolt12SemanticError::DuplicatePaymentId)?;
90329032

lightning/src/ln/outbound_payment.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use crate::ln::onion_utils;
2424
use crate::ln::onion_utils::{DecodedOnionFailure, HTLCFailReason};
2525
use crate::offers::invoice::Bolt12Invoice;
2626
use crate::offers::invoice_request::InvoiceRequest;
27+
use crate::offers::nonce::Nonce;
2728
use crate::routing::router::{BlindedTail, InFlightHtlcs, Path, PaymentParameters, Route, RouteParameters, Router};
2829
use crate::sign::{EntropySource, NodeSigner, Recipient};
2930
use crate::util::errors::APIError;
@@ -58,7 +59,7 @@ pub(crate) enum PendingOutboundPayment {
5859
retry_strategy: Retry,
5960
max_total_routing_fee_msat: Option<u64>,
6061
invoice_request: Option<InvoiceRequest>,
61-
context: OffersContext,
62+
nonce: Option<Nonce>,
6263
},
6364
InvoiceReceived {
6465
payment_hash: PaymentHash,
@@ -1363,7 +1364,7 @@ impl OutboundPayments {
13631364
pub(super) fn add_new_awaiting_invoice(
13641365
&self, payment_id: PaymentId, expiration: StaleExpiration, retry_strategy: Retry,
13651366
max_total_routing_fee_msat: Option<u64>, invoice_request: Option<InvoiceRequest>,
1366-
context: OffersContext,
1367+
nonce: Nonce,
13671368
) -> Result<(), ()> {
13681369
let mut pending_outbounds = self.pending_outbound_payments.lock().unwrap();
13691370
match pending_outbounds.entry(payment_id) {
@@ -1374,7 +1375,7 @@ impl OutboundPayments {
13741375
retry_strategy,
13751376
max_total_routing_fee_msat,
13761377
invoice_request,
1377-
context,
1378+
nonce: Some(nonce),
13781379
});
13791380
self.awaiting_invoice.store(true, Ordering::Release);
13801381

@@ -1849,12 +1850,20 @@ impl OutboundPayments {
18491850
}
18501851

18511852
let mut pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
1852-
let invoice_requests = pending_outbound_payments.iter_mut()
1853-
.filter_map(|(_, payment)| match payment {
1854-
PendingOutboundPayment::AwaitingInvoice { invoice_request, context, ..} => {
1855-
invoice_request.take().map(|req| (context.clone(), req))
1853+
let invoice_requests = pending_outbound_payments
1854+
.iter_mut()
1855+
.filter_map(|(payment_id, payment)| {
1856+
if let PendingOutboundPayment::AwaitingInvoice {
1857+
invoice_request, nonce: Some(nonce), ..
1858+
} = payment {
1859+
let context = OffersContext::OutboundPayment {
1860+
payment_id: *payment_id,
1861+
nonce: *nonce,
1862+
};
1863+
invoice_request.take().map(|req| (context, req))
1864+
} else {
1865+
None
18561866
}
1857-
_ => None,
18581867
})
18591868
.collect();
18601869

@@ -1917,7 +1926,7 @@ impl_writeable_tlv_based_enum_upgradable!(PendingOutboundPayment,
19171926
(2, retry_strategy, required),
19181927
(4, max_total_routing_fee_msat, option),
19191928
(5, invoice_request, option),
1920-
(6, context, required),
1929+
(7, nonce, option),
19211930
},
19221931
(7, InvoiceReceived) => {
19231932
(0, payment_hash, required),
@@ -1933,7 +1942,6 @@ mod tests {
19331942

19341943
use core::time::Duration;
19351944

1936-
use crate::blinded_path::message::OffersContext;
19371945
use crate::blinded_path::EmptyNodeIdLookUp;
19381946
use crate::events::{Event, PathFailure, PaymentFailureReason};
19391947
use crate::ln::types::PaymentHash;
@@ -1943,6 +1951,7 @@ mod tests {
19431951
use crate::ln::outbound_payment::{Bolt12PaymentError, OutboundPayments, Retry, RetryableSendFailure, StaleExpiration};
19441952
#[cfg(feature = "std")]
19451953
use crate::offers::invoice::DEFAULT_RELATIVE_EXPIRY;
1954+
use crate::offers::nonce::Nonce;
19461955
use crate::offers::offer::OfferBuilder;
19471956
use crate::offers::test_utils::*;
19481957
use crate::routing::gossip::NetworkGraph;
@@ -2158,7 +2167,7 @@ mod tests {
21582167
assert!(!outbound_payments.has_pending_payments());
21592168
assert!(
21602169
outbound_payments.add_new_awaiting_invoice(
2161-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2170+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
21622171
).is_ok()
21632172
);
21642173
assert!(outbound_payments.has_pending_payments());
@@ -2184,14 +2193,14 @@ mod tests {
21842193

21852194
assert!(
21862195
outbound_payments.add_new_awaiting_invoice(
2187-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2196+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
21882197
).is_ok()
21892198
);
21902199
assert!(outbound_payments.has_pending_payments());
21912200

21922201
assert!(
21932202
outbound_payments.add_new_awaiting_invoice(
2194-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2203+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
21952204
).is_err()
21962205
);
21972206
}
@@ -2207,7 +2216,7 @@ mod tests {
22072216
assert!(!outbound_payments.has_pending_payments());
22082217
assert!(
22092218
outbound_payments.add_new_awaiting_invoice(
2210-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2219+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
22112220
).is_ok()
22122221
);
22132222
assert!(outbound_payments.has_pending_payments());
@@ -2233,14 +2242,14 @@ mod tests {
22332242

22342243
assert!(
22352244
outbound_payments.add_new_awaiting_invoice(
2236-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2245+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
22372246
).is_ok()
22382247
);
22392248
assert!(outbound_payments.has_pending_payments());
22402249

22412250
assert!(
22422251
outbound_payments.add_new_awaiting_invoice(
2243-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2252+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
22442253
).is_err()
22452254
);
22462255
}
@@ -2255,7 +2264,7 @@ mod tests {
22552264
assert!(!outbound_payments.has_pending_payments());
22562265
assert!(
22572266
outbound_payments.add_new_awaiting_invoice(
2258-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2267+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
22592268
).is_ok()
22602269
);
22612270
assert!(outbound_payments.has_pending_payments());
@@ -2289,7 +2298,7 @@ mod tests {
22892298

22902299
assert!(
22912300
outbound_payments.add_new_awaiting_invoice(
2292-
payment_id, expiration, Retry::Attempts(0), None, None, OffersContext::Unknown {}
2301+
payment_id, expiration, Retry::Attempts(0), None, None, Nonce::try_from(&[0u8; 16][..]).unwrap()
22932302
).is_ok()
22942303
);
22952304
assert!(outbound_payments.has_pending_payments());
@@ -2353,7 +2362,7 @@ mod tests {
23532362
assert!(
23542363
outbound_payments.add_new_awaiting_invoice(
23552364
payment_id, expiration, Retry::Attempts(0),
2356-
Some(invoice.amount_msats() / 100 + 50_000), None, OffersContext::Unknown {}
2365+
Some(invoice.amount_msats() / 100 + 50_000), None, Nonce::try_from(&[0u8; 16][..]).unwrap()
23572366
).is_ok()
23582367
);
23592368
assert!(outbound_payments.has_pending_payments());
@@ -2453,7 +2462,7 @@ mod tests {
24532462

24542463
assert!(
24552464
outbound_payments.add_new_awaiting_invoice(
2456-
payment_id, expiration, Retry::Attempts(0), Some(1234), None, OffersContext::Unknown {}
2465+
payment_id, expiration, Retry::Attempts(0), Some(1234), None, Nonce::try_from(&[0u8; 16][..]).unwrap()
24572466
).is_ok()
24582467
);
24592468
assert!(outbound_payments.has_pending_payments());

0 commit comments

Comments
 (0)