Skip to content

Commit 6627c47

Browse files
committed
Move pending_offers_message to flows.rs
1 parent 959e748 commit 6627c47

File tree

3 files changed

+66
-84
lines changed

3 files changed

+66
-84
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,12 @@ use crate::ln::outbound_payment;
6565
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
6666
use crate::offers::invoice::Bolt12Invoice;
6767
use crate::offers::invoice::UnsignedBolt12Invoice;
68-
use crate::offers::invoice_request::InvoiceRequest;
6968
use crate::offers::nonce::Nonce;
70-
use crate::offers::parse::Bolt12SemanticError;
7169
use crate::offers::signer;
7270
#[cfg(async_payments)]
7371
use crate::offers::static_invoice::StaticInvoice;
7472
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
75-
use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
76-
use crate::onion_message::offers::OffersMessage;
73+
use crate::onion_message::messenger::{DefaultMessageRouter, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
7774
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
7875
use crate::sign::ecdsa::EcdsaChannelSigner;
7976
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
@@ -2114,8 +2111,6 @@ where
21142111
//
21152112
// Lock order tree:
21162113
//
2117-
// `pending_offers_messages`
2118-
//
21192114
// `pending_async_payments_messages`
21202115
//
21212116
// `total_consistency_lock`
@@ -2364,10 +2359,6 @@ where
23642359
event_persist_notifier: Notifier,
23652360
needs_persist_flag: AtomicBool,
23662361

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

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

3233-
pending_offers_messages: Mutex::new(Vec::new()),
32343224
pending_async_payments_messages: Mutex::new(Vec::new()),
32353225
pending_broadcast_messages: Mutex::new(Vec::new()),
32363226

@@ -9494,10 +9484,6 @@ where
94949484
MR::Target: MessageRouter,
94959485
L::Target: Logger,
94969486
{
9497-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>> {
9498-
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
9499-
}
9500-
95019487
#[cfg(feature = "dnssec")]
95029488
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
95039489
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
@@ -9616,42 +9602,6 @@ where
96169602
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
96179603
}
96189604

9619-
fn enqueue_invoice_request(
9620-
&self,
9621-
invoice_request: InvoiceRequest,
9622-
reply_paths: Vec<BlindedMessagePath>,
9623-
) -> Result<(), Bolt12SemanticError> {
9624-
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
9625-
if !invoice_request.paths().is_empty() {
9626-
reply_paths
9627-
.iter()
9628-
.flat_map(|reply_path| invoice_request.paths().iter().map(move |path| (path, reply_path)))
9629-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
9630-
.for_each(|(path, reply_path)| {
9631-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9632-
destination: Destination::BlindedPath(path.clone()),
9633-
reply_path: reply_path.clone(),
9634-
};
9635-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9636-
pending_offers_messages.push((message, instructions));
9637-
});
9638-
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
9639-
for reply_path in reply_paths {
9640-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9641-
destination: Destination::Node(node_id),
9642-
reply_path,
9643-
};
9644-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9645-
pending_offers_messages.push((message, instructions));
9646-
}
9647-
} else {
9648-
debug_assert!(false);
9649-
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
9650-
}
9651-
9652-
Ok(())
9653-
}
9654-
96559605
fn get_current_blocktime(&self) -> Duration {
96569606
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
96579607
}
@@ -9687,13 +9637,6 @@ where
96879637
}
96889638
}
96899639

9690-
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
9691-
/// along different paths.
9692-
/// Sending multiple requests increases the chances of successful delivery in case some
9693-
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
9694-
/// even if multiple invoices are received.
9695-
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
9696-
96979640
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>
96989641
where
96999642
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -13029,7 +12972,6 @@ where
1302912972

1303012973
funding_batch_states: Mutex::new(BTreeMap::new()),
1303112974

13032-
pending_offers_messages: Mutex::new(Vec::new()),
1303312975
pending_async_payments_messages: Mutex::new(Vec::new()),
1303412976

1303512977
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
@@ -1326,7 +1326,7 @@ fn fails_authentication_when_handling_invoice_request() {
13261326
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
13271327

13281328
connect_peers(david, alice);
1329-
match &mut david.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1329+
match &mut david.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
13301330
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
13311331
*destination = Destination::Node(alice_id),
13321332
_ => panic!(),
@@ -1351,7 +1351,7 @@ fn fails_authentication_when_handling_invoice_request() {
13511351
.unwrap();
13521352
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
13531353

1354-
match &mut david.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1354+
match &mut david.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
13551355
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
13561356
*destination = Destination::BlindedPath(invalid_path),
13571357
_ => panic!(),
@@ -1431,7 +1431,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
14311431

14321432
// Don't send the invoice request, but grab its reply path to use with a different request.
14331433
let invalid_reply_path = {
1434-
let mut pending_offers_messages = david.node.pending_offers_messages.lock().unwrap();
1434+
let mut pending_offers_messages = david.offers_handler.pending_offers_messages.lock().unwrap();
14351435
let pending_invoice_request = pending_offers_messages.pop().unwrap();
14361436
pending_offers_messages.clear();
14371437
match pending_invoice_request.1 {
@@ -1448,7 +1448,7 @@ fn fails_authentication_when_handling_invoice_for_offer() {
14481448
// Swap out the reply path to force authentication to fail when handling the invoice since it
14491449
// will be sent over the wrong blinded path.
14501450
{
1451-
let mut pending_offers_messages = david.node.pending_offers_messages.lock().unwrap();
1451+
let mut pending_offers_messages = david.offers_handler.pending_offers_messages.lock().unwrap();
14521452
let mut pending_invoice_request = pending_offers_messages.first_mut().unwrap();
14531453
match &mut pending_invoice_request.1 {
14541454
MessageSendInstructions::WithSpecifiedReplyPath { reply_path, .. } =>
@@ -1535,7 +1535,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
15351535
let expected_invoice = alice.offers_handler.request_refund_payment(&refund).unwrap();
15361536

15371537
connect_peers(david, alice);
1538-
match &mut alice.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1538+
match &mut alice.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
15391539
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
15401540
*destination = Destination::Node(david_id),
15411541
_ => panic!(),
@@ -1566,7 +1566,7 @@ fn fails_authentication_when_handling_invoice_for_refund() {
15661566

15671567
let expected_invoice = alice.offers_handler.request_refund_payment(&refund).unwrap();
15681568

1569-
match &mut alice.node.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
1569+
match &mut alice.offers_handler.pending_offers_messages.lock().unwrap().first_mut().unwrap().1 {
15701570
MessageSendInstructions::WithSpecifiedReplyPath { destination, .. } =>
15711571
*destination = Destination::BlindedPath(invalid_path),
15721572
_ => panic!(),
@@ -2157,7 +2157,7 @@ fn fails_paying_invoice_with_unknown_required_features() {
21572157
destination: Destination::BlindedPath(reply_path),
21582158
};
21592159
let message = OffersMessage::Invoice(invoice);
2160-
alice.node.pending_offers_messages.lock().unwrap().push((message, instructions));
2160+
alice.offers_handler.pending_offers_messages.lock().unwrap().push((message, instructions));
21612161

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

lightning/src/offers/flow.rs

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ use crate::blinded_path::payment::{
2727
BlindedPaymentPath, Bolt12OfferContext, Bolt12RefundContext, PaymentContext,
2828
};
2929
use crate::events::PaymentFailureReason;
30-
use crate::ln::channelmanager::{
31-
Bolt12PaymentError, PaymentId, Verification, OFFERS_MESSAGE_REQUEST_LIMIT,
32-
};
30+
use crate::ln::channelmanager::{Bolt12PaymentError, PaymentId, Verification};
3331
use crate::ln::outbound_payment::{Retry, RetryableInvoiceRequest, StaleExpiration};
3432
use crate::offers::invoice::{
3533
Bolt12Invoice, DerivedSigningPubkey, ExplicitSigningPubkey, InvoiceBuilder,
@@ -71,11 +69,6 @@ use {
7169
///
7270
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
7371
pub trait OffersMessageCommons {
74-
/// Get pending offers messages
75-
fn get_pending_offers_messages(
76-
&self,
77-
) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>>;
78-
7972
#[cfg(feature = "dnssec")]
8073
/// Get pending DNS onion messages
8174
fn get_pending_dns_onion_messages(
@@ -174,11 +167,6 @@ pub trait OffersMessageCommons {
174167
/// [`MessageRouter::create_blinded_paths`]: crate::onion_message::messenger::MessageRouter::create_blinded_paths
175168
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()>;
176169

177-
/// Enqueue invoice request
178-
fn enqueue_invoice_request(
179-
&self, invoice_request: InvoiceRequest, reply_paths: Vec<BlindedMessagePath>,
180-
) -> Result<(), Bolt12SemanticError>;
181-
182170
/// Get the current time determined by highest seen timestamp
183171
fn get_current_blocktime(&self) -> Duration;
184172

@@ -569,6 +557,11 @@ where
569557

570558
message_router: MR,
571559

560+
#[cfg(not(any(test, feature = "_test_utils")))]
561+
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
562+
#[cfg(any(test, feature = "_test_utils"))]
563+
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
564+
572565
#[cfg(feature = "_test_utils")]
573566
/// In testing, it is useful be able to forge a name -> offer mapping so that we can pay an
574567
/// offer generated in the test.
@@ -601,9 +594,13 @@ where
601594
inbound_payment_key: expanded_inbound_key,
602595
our_network_pubkey,
603596
secp_ctx,
597+
entropy_source,
598+
604599
commons,
600+
605601
message_router,
606-
entropy_source,
602+
603+
pending_offers_messages: Mutex::new(Vec::new()),
607604
#[cfg(feature = "_test_utils")]
608605
testing_dnssec_proof_offer_resolution_override: Mutex::new(new_hash_map()),
609606
logger,
@@ -636,6 +633,13 @@ where
636633
/// [`Refund`]: crate::offers::refund
637634
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
638635

636+
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
637+
/// along different paths.
638+
/// Sending multiple requests increases the chances of successful delivery in case some
639+
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
640+
/// even if multiple invoices are received.
641+
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
642+
639643
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
640644
where
641645
ES::Target: EntropySource,
@@ -694,6 +698,42 @@ where
694698
)
695699
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
696700
}
701+
702+
fn enqueue_invoice_request(
703+
&self, invoice_request: InvoiceRequest, reply_paths: Vec<BlindedMessagePath>,
704+
) -> Result<(), Bolt12SemanticError> {
705+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
706+
if !invoice_request.paths().is_empty() {
707+
reply_paths
708+
.iter()
709+
.flat_map(|reply_path| {
710+
invoice_request.paths().iter().map(move |path| (path, reply_path))
711+
})
712+
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
713+
.for_each(|(path, reply_path)| {
714+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
715+
destination: Destination::BlindedPath(path.clone()),
716+
reply_path: reply_path.clone(),
717+
};
718+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
719+
pending_offers_messages.push((message, instructions));
720+
});
721+
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
722+
for reply_path in reply_paths {
723+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
724+
destination: Destination::Node(node_id),
725+
reply_path,
726+
};
727+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
728+
pending_offers_messages.push((message, instructions));
729+
}
730+
} else {
731+
debug_assert!(false);
732+
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
733+
}
734+
735+
Ok(())
736+
}
697737
}
698738

699739
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
@@ -750,7 +790,7 @@ where
750790

751791
create_pending_payment(&invoice_request, nonce)?;
752792

753-
self.commons.enqueue_invoice_request(invoice_request, reply_paths)
793+
self.enqueue_invoice_request(invoice_request, reply_paths)
754794
}
755795
}
756796

@@ -1018,7 +1058,7 @@ where
10181058
});
10191059
match self.commons.create_blinded_paths(context) {
10201060
Ok(reply_paths) => {
1021-
match self.commons.enqueue_invoice_request(invoice_request, reply_paths) {
1061+
match self.enqueue_invoice_request(invoice_request, reply_paths) {
10221062
Ok(_) => {},
10231063
Err(_) => {
10241064
log_warn!(
@@ -1042,7 +1082,7 @@ where
10421082
}
10431083

10441084
fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> {
1045-
core::mem::take(&mut self.commons.get_pending_offers_messages())
1085+
core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
10461086
}
10471087
}
10481088

@@ -1376,7 +1416,7 @@ where
13761416
.create_blinded_paths(context)
13771417
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
13781418

1379-
let mut pending_offers_messages = self.commons.get_pending_offers_messages();
1419+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
13801420
if refund.paths().is_empty() {
13811421
for reply_path in reply_paths {
13821422
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {

0 commit comments

Comments
 (0)