Skip to content

Commit c7ee105

Browse files
committed
Move pending_offers_message to flows.rs
1 parent 40ebff2 commit c7ee105

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
@@ -66,15 +66,12 @@ use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, Retr
6666
use crate::ln::wire::Encode;
6767
use crate::offers::invoice::Bolt12Invoice;
6868
use crate::offers::invoice::UnsignedBolt12Invoice;
69-
use crate::offers::invoice_request::InvoiceRequest;
7069
use crate::offers::nonce::Nonce;
71-
use crate::offers::parse::Bolt12SemanticError;
7270
use crate::offers::signer;
7371
#[cfg(async_payments)]
7472
use crate::offers::static_invoice::StaticInvoice;
7573
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
76-
use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
77-
use crate::onion_message::offers::OffersMessage;
74+
use crate::onion_message::messenger::{DefaultMessageRouter, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
7875
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
7976
use crate::sign::ecdsa::EcdsaChannelSigner;
8077
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
@@ -2106,8 +2103,6 @@ where
21062103
//
21072104
// Lock order tree:
21082105
//
2109-
// `pending_offers_messages`
2110-
//
21112106
// `pending_async_payments_messages`
21122107
//
21132108
// `total_consistency_lock`
@@ -2356,10 +2351,6 @@ where
23562351
event_persist_notifier: Notifier,
23572352
needs_persist_flag: AtomicBool,
23582353

2359-
#[cfg(not(any(test, feature = "_test_utils")))]
2360-
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
2361-
#[cfg(any(test, feature = "_test_utils"))]
2362-
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
23632354
pending_async_payments_messages: Mutex<Vec<(AsyncPaymentsMessage, MessageSendInstructions)>>,
23642355

23652356
/// Tracks the message events that are to be broadcasted when we are connected to some peer.
@@ -3218,7 +3209,6 @@ where
32183209
needs_persist_flag: AtomicBool::new(false),
32193210
funding_batch_states: Mutex::new(BTreeMap::new()),
32203211

3221-
pending_offers_messages: Mutex::new(Vec::new()),
32223212
pending_async_payments_messages: Mutex::new(Vec::new()),
32233213
pending_broadcast_messages: Mutex::new(Vec::new()),
32243214

@@ -9164,9 +9154,6 @@ impl Default for Bolt11InvoiceParameters {
91649154
///
91659155
/// [`OffersMessageFlow`]: crate::offers::flow::OffersMessageFlow
91669156
pub trait OffersMessageCommons {
9167-
/// Get pending offers messages
9168-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>>;
9169-
91709157
#[cfg(feature = "dnssec")]
91719158
/// Get pending DNS onion messages
91729159
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>>;
@@ -9264,13 +9251,6 @@ pub trait OffersMessageCommons {
92649251
/// Errors if the `MessageRouter` errors.
92659252
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()>;
92669253

9267-
/// Enqueue invoice request
9268-
fn enqueue_invoice_request(
9269-
&self,
9270-
invoice_request: InvoiceRequest,
9271-
reply_paths: Vec<BlindedMessagePath>,
9272-
) -> Result<(), Bolt12SemanticError>;
9273-
92749254
/// Get the current time determined by highest seen timestamp
92759255
fn get_current_blocktime(&self) -> Duration;
92769256

@@ -9310,10 +9290,6 @@ where
93109290
MR::Target: MessageRouter,
93119291
L::Target: Logger,
93129292
{
9313-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>> {
9314-
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
9315-
}
9316-
93179293
#[cfg(feature = "dnssec")]
93189294
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
93199295
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
@@ -9448,42 +9424,6 @@ where
94489424
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
94499425
}
94509426

9451-
fn enqueue_invoice_request(
9452-
&self,
9453-
invoice_request: InvoiceRequest,
9454-
reply_paths: Vec<BlindedMessagePath>,
9455-
) -> Result<(), Bolt12SemanticError> {
9456-
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
9457-
if !invoice_request.paths().is_empty() {
9458-
reply_paths
9459-
.iter()
9460-
.flat_map(|reply_path| invoice_request.paths().iter().map(move |path| (path, reply_path)))
9461-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
9462-
.for_each(|(path, reply_path)| {
9463-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9464-
destination: Destination::BlindedPath(path.clone()),
9465-
reply_path: reply_path.clone(),
9466-
};
9467-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9468-
pending_offers_messages.push((message, instructions));
9469-
});
9470-
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
9471-
for reply_path in reply_paths {
9472-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9473-
destination: Destination::Node(node_id),
9474-
reply_path,
9475-
};
9476-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9477-
pending_offers_messages.push((message, instructions));
9478-
}
9479-
} else {
9480-
debug_assert!(false);
9481-
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
9482-
}
9483-
9484-
Ok(())
9485-
}
9486-
94879427
fn get_current_blocktime(&self) -> Duration {
94889428
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
94899429
}
@@ -9521,13 +9461,6 @@ where
95219461
}
95229462
}
95239463

9524-
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
9525-
/// along different paths.
9526-
/// Sending multiple requests increases the chances of successful delivery in case some
9527-
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
9528-
/// even if multiple invoices are received.
9529-
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
9530-
95319464
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>
95329465
where
95339466
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -12844,7 +12777,6 @@ where
1284412777

1284512778
funding_batch_states: Mutex::new(BTreeMap::new()),
1284612779

12847-
pending_offers_messages: Mutex::new(Vec::new()),
1284812780
pending_async_payments_messages: Mutex::new(Vec::new()),
1284912781

1285012782
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;
@@ -414,6 +414,11 @@ where
414414

415415
message_router: MR,
416416

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

442447
Self {
443448
secp_ctx,
449+
entropy_source,
450+
444451
commons,
452+
445453
message_router,
446-
entropy_source,
454+
455+
pending_offers_messages: Mutex::new(Vec::new()),
447456
#[cfg(feature = "_test_utils")]
448457
testing_dnssec_proof_offer_resolution_override: Mutex::new(new_hash_map()),
449458
logger,
@@ -471,6 +480,13 @@ where
471480
/// [`Refund`]: crate::offers::refund
472481
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
473482

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

534586
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
@@ -585,7 +637,7 @@ where
585637

586638
create_pending_payment(&invoice_request, nonce)?;
587639

588-
self.commons.enqueue_invoice_request(invoice_request, reply_paths)
640+
self.enqueue_invoice_request(invoice_request, reply_paths)
589641
}
590642
}
591643

@@ -860,7 +912,7 @@ where
860912
});
861913
match self.commons.create_blinded_paths(context) {
862914
Ok(reply_paths) => {
863-
match self.commons.enqueue_invoice_request(invoice_request, reply_paths) {
915+
match self.enqueue_invoice_request(invoice_request, reply_paths) {
864916
Ok(_) => {},
865917
Err(_) => {
866918
log_warn!(
@@ -884,7 +936,7 @@ where
884936
}
885937

886938
fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> {
887-
core::mem::take(&mut self.commons.get_pending_offers_messages())
939+
core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
888940
}
889941
}
890942

@@ -1216,7 +1268,7 @@ where
12161268
.create_blinded_paths(context)
12171269
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
12181270

1219-
let mut pending_offers_messages = self.commons.get_pending_offers_messages();
1271+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
12201272
if refund.paths().is_empty() {
12211273
for reply_path in reply_paths {
12221274
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {

0 commit comments

Comments
 (0)