Skip to content

Commit 79cfd24

Browse files
committed
Move pending_offers_message to flows.rs
1 parent 7b3747c commit 79cfd24

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};
@@ -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

@@ -9489,10 +9479,6 @@ where
94899479
MR::Target: MessageRouter,
94909480
L::Target: Logger,
94919481
{
9492-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>> {
9493-
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
9494-
}
9495-
94969482
#[cfg(feature = "dnssec")]
94979483
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
94989484
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
@@ -9611,42 +9597,6 @@ where
96119597
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
96129598
}
96139599

9614-
fn enqueue_invoice_request(
9615-
&self,
9616-
invoice_request: InvoiceRequest,
9617-
reply_paths: Vec<BlindedMessagePath>,
9618-
) -> Result<(), Bolt12SemanticError> {
9619-
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
9620-
if !invoice_request.paths().is_empty() {
9621-
reply_paths
9622-
.iter()
9623-
.flat_map(|reply_path| invoice_request.paths().iter().map(move |path| (path, reply_path)))
9624-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
9625-
.for_each(|(path, reply_path)| {
9626-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9627-
destination: Destination::BlindedPath(path.clone()),
9628-
reply_path: reply_path.clone(),
9629-
};
9630-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9631-
pending_offers_messages.push((message, instructions));
9632-
});
9633-
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
9634-
for reply_path in reply_paths {
9635-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9636-
destination: Destination::Node(node_id),
9637-
reply_path,
9638-
};
9639-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9640-
pending_offers_messages.push((message, instructions));
9641-
}
9642-
} else {
9643-
debug_assert!(false);
9644-
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
9645-
}
9646-
9647-
Ok(())
9648-
}
9649-
96509600
fn get_current_blocktime(&self) -> Duration {
96519601
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
96529602
}
@@ -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
@@ -28,9 +28,7 @@ use crate::blinded_path::payment::{
2828
BlindedPaymentPath, Bolt12OfferContext, Bolt12RefundContext, PaymentContext,
2929
};
3030
use crate::events::PaymentFailureReason;
31-
use crate::ln::channelmanager::{
32-
Bolt12PaymentError, PaymentId, Verification, OFFERS_MESSAGE_REQUEST_LIMIT,
33-
};
31+
use crate::ln::channelmanager::{Bolt12PaymentError, PaymentId, Verification};
3432
use crate::ln::inbound_payment;
3533
use crate::ln::outbound_payment::{Retry, RetryableInvoiceRequest, StaleExpiration};
3634
use crate::offers::invoice::{
@@ -73,11 +71,6 @@ use {
7371
///
7472
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
7573
pub trait OffersMessageCommons {
76-
/// Get pending offers messages
77-
fn get_pending_offers_messages(
78-
&self,
79-
) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>>;
80-
8174
#[cfg(feature = "dnssec")]
8275
/// Get pending DNS onion messages
8376
fn get_pending_dns_onion_messages(
@@ -176,11 +169,6 @@ pub trait OffersMessageCommons {
176169
/// [`MessageRouter::create_blinded_paths`]: crate::onion_message::messenger::MessageRouter::create_blinded_paths
177170
fn create_blinded_paths(&self, context: MessageContext) -> Result<Vec<BlindedMessagePath>, ()>;
178171

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

@@ -575,6 +563,11 @@ where
575563

576564
message_router: MR,
577565

566+
#[cfg(not(any(test, feature = "_test_utils")))]
567+
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
568+
#[cfg(any(test, feature = "_test_utils"))]
569+
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
570+
578571
#[cfg(feature = "_test_utils")]
579572
/// In testing, it is useful be able to forge a name -> offer mapping so that we can pay an
580573
/// offer generated in the test.
@@ -607,9 +600,13 @@ where
607600
inbound_payment_key: expanded_inbound_key,
608601
our_network_pubkey,
609602
secp_ctx,
603+
entropy_source,
604+
610605
commons,
606+
611607
message_router,
612-
entropy_source,
608+
609+
pending_offers_messages: Mutex::new(Vec::new()),
613610
#[cfg(feature = "_test_utils")]
614611
testing_dnssec_proof_offer_resolution_override: Mutex::new(new_hash_map()),
615612
logger,
@@ -642,6 +639,13 @@ where
642639
/// [`Refund`]: crate::offers::refund
643640
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
644641

642+
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
643+
/// along different paths.
644+
/// Sending multiple requests increases the chances of successful delivery in case some
645+
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
646+
/// even if multiple invoices are received.
647+
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
648+
645649
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
646650
where
647651
ES::Target: EntropySource,
@@ -700,6 +704,42 @@ where
700704
)
701705
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
702706
}
707+
708+
fn enqueue_invoice_request(
709+
&self, invoice_request: InvoiceRequest, reply_paths: Vec<BlindedMessagePath>,
710+
) -> Result<(), Bolt12SemanticError> {
711+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
712+
if !invoice_request.paths().is_empty() {
713+
reply_paths
714+
.iter()
715+
.flat_map(|reply_path| {
716+
invoice_request.paths().iter().map(move |path| (path, reply_path))
717+
})
718+
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
719+
.for_each(|(path, reply_path)| {
720+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
721+
destination: Destination::BlindedPath(path.clone()),
722+
reply_path: reply_path.clone(),
723+
};
724+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
725+
pending_offers_messages.push((message, instructions));
726+
});
727+
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
728+
for reply_path in reply_paths {
729+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
730+
destination: Destination::Node(node_id),
731+
reply_path,
732+
};
733+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
734+
pending_offers_messages.push((message, instructions));
735+
}
736+
} else {
737+
debug_assert!(false);
738+
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
739+
}
740+
741+
Ok(())
742+
}
703743
}
704744

705745
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
@@ -756,7 +796,7 @@ where
756796

757797
create_pending_payment(&invoice_request, nonce)?;
758798

759-
self.commons.enqueue_invoice_request(invoice_request, reply_paths)
799+
self.enqueue_invoice_request(invoice_request, reply_paths)
760800
}
761801
}
762802

@@ -1024,7 +1064,7 @@ where
10241064
});
10251065
match self.commons.create_blinded_paths(context) {
10261066
Ok(reply_paths) => {
1027-
match self.commons.enqueue_invoice_request(invoice_request, reply_paths) {
1067+
match self.enqueue_invoice_request(invoice_request, reply_paths) {
10281068
Ok(_) => {},
10291069
Err(_) => {
10301070
log_warn!(
@@ -1048,7 +1088,7 @@ where
10481088
}
10491089

10501090
fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> {
1051-
core::mem::take(&mut self.commons.get_pending_offers_messages())
1091+
core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
10521092
}
10531093
}
10541094

@@ -1382,7 +1422,7 @@ where
13821422
.create_blinded_paths(context)
13831423
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
13841424

1385-
let mut pending_offers_messages = self.commons.get_pending_offers_messages();
1425+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
13861426
if refund.paths().is_empty() {
13871427
for reply_path in reply_paths {
13881428
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {

0 commit comments

Comments
 (0)