Skip to content

Commit 003a409

Browse files
committed
Move pending_offers_message to flows.rs
1 parent cb5d6d1 commit 003a409

File tree

3 files changed

+66
-82
lines changed

3 files changed

+66
-82
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,12 @@ use crate::ln::outbound_payment;
6464
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
6565
use crate::offers::invoice::Bolt12Invoice;
6666
use crate::offers::invoice::UnsignedBolt12Invoice;
67-
use crate::offers::invoice_request::InvoiceRequest;
6867
use crate::offers::nonce::Nonce;
69-
use crate::offers::parse::Bolt12SemanticError;
7068
use crate::offers::signer;
7169
#[cfg(async_payments)]
7270
use crate::offers::static_invoice::StaticInvoice;
7371
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
74-
use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
75-
use crate::onion_message::offers::OffersMessage;
72+
use crate::onion_message::messenger::{DefaultMessageRouter, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
7673
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
7774
use crate::sign::ecdsa::EcdsaChannelSigner;
7875
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
@@ -2113,8 +2110,6 @@ where
21132110
//
21142111
// Lock order tree:
21152112
//
2116-
// `pending_offers_messages`
2117-
//
21182113
// `pending_async_payments_messages`
21192114
//
21202115
// `total_consistency_lock`
@@ -2363,10 +2358,6 @@ where
23632358
event_persist_notifier: Notifier,
23642359
needs_persist_flag: AtomicBool,
23652360

2366-
#[cfg(not(any(test, feature = "_test_utils")))]
2367-
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
2368-
#[cfg(any(test, feature = "_test_utils"))]
2369-
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
23702361
pending_async_payments_messages: Mutex<Vec<(AsyncPaymentsMessage, MessageSendInstructions)>>,
23712362

23722363
/// Tracks the message events that are to be broadcasted when we are connected to some peer.
@@ -3229,7 +3220,6 @@ where
32293220
needs_persist_flag: AtomicBool::new(false),
32303221
funding_batch_states: Mutex::new(BTreeMap::new()),
32313222

3232-
pending_offers_messages: Mutex::new(Vec::new()),
32333223
pending_async_payments_messages: Mutex::new(Vec::new()),
32343224
pending_broadcast_messages: Mutex::new(Vec::new()),
32353225

@@ -9475,9 +9465,6 @@ impl Default for Bolt11InvoiceParameters {
94759465
///
94769466
/// [`OffersMessageFlow`]: crate::offers::flow::OffersMessageFlow
94779467
pub trait OffersMessageCommons {
9478-
/// Get pending offers messages
9479-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>>;
9480-
94819468
#[cfg(feature = "dnssec")]
94829469
/// Get pending DNS onion messages
94839470
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>>;
@@ -9575,13 +9562,6 @@ pub trait OffersMessageCommons {
95759562
/// Errors if the `MessageRouter` errors.
95769563
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()>;
95779564

9578-
/// Enqueue invoice request
9579-
fn enqueue_invoice_request(
9580-
&self,
9581-
invoice_request: InvoiceRequest,
9582-
reply_paths: Vec<BlindedMessagePath>,
9583-
) -> Result<(), Bolt12SemanticError>;
9584-
95859565
/// Get the current time determined by highest seen timestamp
95869566
fn get_current_blocktime(&self) -> Duration;
95879567

@@ -9621,10 +9601,6 @@ where
96219601
MR::Target: MessageRouter,
96229602
L::Target: Logger,
96239603
{
9624-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>> {
9625-
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
9626-
}
9627-
96289604
#[cfg(feature = "dnssec")]
96299605
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
96309606
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
@@ -9759,42 +9735,6 @@ where
97599735
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
97609736
}
97619737

9762-
fn enqueue_invoice_request(
9763-
&self,
9764-
invoice_request: InvoiceRequest,
9765-
reply_paths: Vec<BlindedMessagePath>,
9766-
) -> Result<(), Bolt12SemanticError> {
9767-
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
9768-
if !invoice_request.paths().is_empty() {
9769-
reply_paths
9770-
.iter()
9771-
.flat_map(|reply_path| invoice_request.paths().iter().map(move |path| (path, reply_path)))
9772-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
9773-
.for_each(|(path, reply_path)| {
9774-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9775-
destination: Destination::BlindedPath(path.clone()),
9776-
reply_path: reply_path.clone(),
9777-
};
9778-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9779-
pending_offers_messages.push((message, instructions));
9780-
});
9781-
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
9782-
for reply_path in reply_paths {
9783-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9784-
destination: Destination::Node(node_id),
9785-
reply_path,
9786-
};
9787-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9788-
pending_offers_messages.push((message, instructions));
9789-
}
9790-
} else {
9791-
debug_assert!(false);
9792-
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
9793-
}
9794-
9795-
Ok(())
9796-
}
9797-
97989738
fn get_current_blocktime(&self) -> Duration {
97999739
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
98009740
}
@@ -9832,13 +9772,6 @@ where
98329772
}
98339773
}
98349774

9835-
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
9836-
/// along different paths.
9837-
/// Sending multiple requests increases the chances of successful delivery in case some
9838-
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
9839-
/// even if multiple invoices are received.
9840-
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
9841-
98429775
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref> ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
98439776
where
98449777
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -13174,7 +13107,6 @@ where
1317413107

1317513108
funding_batch_states: Mutex::new(BTreeMap::new()),
1317613109

13177-
pending_offers_messages: Mutex::new(Vec::new()),
1317813110
pending_async_payments_messages: Mutex::new(Vec::new()),
1317913111

1318013112
pending_broadcast_messages: Mutex::new(Vec::new()),

lightning/src/ln/offers_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,7 @@ fn fails_authentication_when_handling_invoice_request() {
14031403
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
14041404

14051405
connect_peers(david, alice);
1406-
match &mut david.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1406+
match &mut david.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
14071407
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
14081408
*destination = Destination::Node(alice_id),
14091409
_ => panic!(),
@@ -1428,7 +1428,7 @@ fn fails_authentication_when_handling_invoice_request() {
14281428
.unwrap();
14291429
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
14301430

1431-
match &mut david.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1431+
match &mut david.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
14321432
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
14331433
*destination = Destination::BlindedPath(invalid_path),
14341434
_ => panic!(),
@@ -1508,7 +1508,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
15081508

15091509
// Don't send the invoice request, but grab its reply path to use with a different request.
15101510
let invalid_reply_path = {
1511-
let mut pending_offers_messages = david.node.pending_offers_messages.lock().unwrap();
1511+
let mut pending_offers_messages = david.offers_handler.pending_offers_messages.lock().unwrap();
15121512
let pending_invoice_request = pending_offers_messages.pop().unwrap();
15131513
pending_offers_messages.clear();
15141514
match pending_invoice_request.1 {
@@ -1525,7 +1525,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
15251525
// Swap out the reply path to force authentication to fail when handling the invoice since it
15261526
// will be sent over the wrong blinded path.
15271527
{
1528-
let mut pending_offers_messages = david.node.pending_offers_messages.lock().unwrap();
1528+
let mut pending_offers_messages = david.offers_handler.pending_offers_messages.lock().unwrap();
15291529
let mut pending_invoice_request = pending_offers_messages.first_mut().unwrap();
15301530
match &mut pending_invoice_request.1 {
15311531
MessageSendInstructions::WithSpecifiedReplyPath { reply_path, .. } =>
@@ -1612,7 +1612,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
16121612
let expected_invoice = alice.offers_handler.request_refund_payment(&refund).unwrap();
16131613

16141614
connect_peers(david, alice);
1615-
match &mut alice.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1615+
match &mut alice.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
16161616
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
16171617
*destination = Destination::Node(david_id),
16181618
_ => panic!(),
@@ -1643,7 +1643,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
16431643

16441644
let expected_invoice = alice.offers_handler.request_refund_payment(&refund).unwrap();
16451645

1646-
match &mut alice.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1646+
match &mut alice.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
16471647
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
16481648
*destination = Destination::BlindedPath(invalid_path),
16491649
_ => panic!(),
@@ -2234,7 +2234,7 @@ fn fails_paying_invoice_with_unknown_required_features() {
22342234
destination: Destination::BlindedPath(reply_path),
22352235
};
22362236
let message = OffersMessage::Invoice(invoice);
2237-
alice.node.pending_offers_messages.lock().unwrap().push((message, instructions));
2237+
alice.offers_handler.pending_offers_messages.lock().unwrap().push((message, instructions));
22382238

22392239
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
22402240
charlie.onion_messenger.handle_onion_message(alice_id, &onion_message);

lightning/src/offers/flow.rs

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::blinded_path::message::{BlindedMessagePath, MessageContext, OffersCon
2121
use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
2222
use crate::events::{Event, PaymentFailureReason};
2323
use crate::ln::channelmanager::{
24-
Bolt12PaymentError, OffersMessageCommons, PaymentId, Verification, OFFERS_MESSAGE_REQUEST_LIMIT,
24+
Bolt12PaymentError, OffersMessageCommons, PaymentId, Verification,
2525
};
2626
use crate::ln::outbound_payment::{Retry, RetryableInvoiceRequest, StaleExpiration};
2727
use crate::onion_message::dns_resolution::HumanReadableName;
@@ -416,6 +416,11 @@ where
416416

417417
message_router: MR,
418418

419+
#[cfg(not(any(test, feature = "_test_utils")))]
420+
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
421+
#[cfg(any(test, feature = "_test_utils"))]
422+
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
423+
419424
#[cfg(feature = "_test_utils")]
420425
/// In testing, it is useful be able to forge a name -> offer mapping so that we can pay an
421426
/// offer generated in the test.
@@ -443,9 +448,13 @@ where
443448

444449
Self {
445450
secp_ctx,
451+
entropy_source,
452+
446453
commons,
454+
447455
message_router,
448-
entropy_source,
456+
457+
pending_offers_messages: Mutex::new(Vec::new()),
449458
#[cfg(feature = "_test_utils")]
450459
testing_dnssec_proof_offer_resolution_override: Mutex::new(new_hash_map()),
451460
logger,
@@ -473,6 +482,13 @@ where
473482
/// [`Refund`]: crate::offers::refund
474483
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
475484

485+
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
486+
/// along different paths.
487+
/// Sending multiple requests increases the chances of successful delivery in case some
488+
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
489+
/// even if multiple invoices are received.
490+
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
491+
476492
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
477493
where
478494
ES::Target: EntropySource,
@@ -531,6 +547,42 @@ where
531547
)
532548
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
533549
}
550+
551+
fn enqueue_invoice_request(
552+
&self, invoice_request: InvoiceRequest, reply_paths: Vec<BlindedMessagePath>,
553+
) -> Result<(), Bolt12SemanticError> {
554+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
555+
if !invoice_request.paths().is_empty() {
556+
reply_paths
557+
.iter()
558+
.flat_map(|reply_path| {
559+
invoice_request.paths().iter().map(move |path| (path, reply_path))
560+
})
561+
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
562+
.for_each(|(path, reply_path)| {
563+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
564+
destination: Destination::BlindedPath(path.clone()),
565+
reply_path: reply_path.clone(),
566+
};
567+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
568+
pending_offers_messages.push((message, instructions));
569+
});
570+
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
571+
for reply_path in reply_paths {
572+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
573+
destination: Destination::Node(node_id),
574+
reply_path,
575+
};
576+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
577+
pending_offers_messages.push((message, instructions));
578+
}
579+
} else {
580+
debug_assert!(false);
581+
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
582+
}
583+
584+
Ok(())
585+
}
534586
}
535587

536588
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
@@ -587,7 +639,7 @@ where
587639

588640
create_pending_payment(&invoice_request, nonce)?;
589641

590-
self.commons.enqueue_invoice_request(invoice_request, reply_paths)
642+
self.enqueue_invoice_request(invoice_request, reply_paths)
591643
}
592644
}
593645

@@ -862,7 +914,7 @@ where
862914
});
863915
match self.commons.create_blinded_paths(context) {
864916
Ok(reply_paths) => {
865-
match self.commons.enqueue_invoice_request(invoice_request, reply_paths) {
917+
match self.enqueue_invoice_request(invoice_request, reply_paths) {
866918
Ok(_) => {},
867919
Err(_) => {
868920
log_warn!(
@@ -886,7 +938,7 @@ where
886938
}
887939

888940
fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> {
889-
core::mem::take(&mut self.commons.get_pending_offers_messages())
941+
core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
890942
}
891943
}
892944

@@ -1218,7 +1270,7 @@ where
12181270
.create_blinded_paths(context)
12191271
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
12201272

1221-
let mut pending_offers_messages = self.commons.get_pending_offers_messages();
1273+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
12221274
if refund.paths().is_empty() {
12231275
for reply_path in reply_paths {
12241276
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {

0 commit comments

Comments
 (0)