Skip to content

Commit af0c7b4

Browse files
committed
Move pending_offers_message to flows.rs
1 parent 5a85596 commit af0c7b4

File tree

3 files changed

+73
-86
lines changed

3 files changed

+73
-86
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,10 @@ use crate::ln::outbound_payment;
6767
use crate::ln::outbound_payment::{OutboundPayments, PendingOutboundPayment, RetryableInvoiceRequest, SendAlongPathArgs, StaleExpiration};
6868
use crate::offers::invoice::Bolt12Invoice;
6969
use crate::offers::invoice::UnsignedBolt12Invoice;
70-
use crate::offers::invoice_request::InvoiceRequest;
7170
use crate::offers::nonce::Nonce;
72-
use crate::offers::parse::Bolt12SemanticError;
7371
use crate::offers::signer;
74-
#[cfg(async_payments)]
75-
use crate::offers::static_invoice::StaticInvoice;
7672
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
77-
use crate::onion_message::messenger::{DefaultMessageRouter, Destination, MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
78-
use crate::onion_message::offers::OffersMessage;
73+
use crate::onion_message::messenger::{MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
7974
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8075
use crate::sign::ecdsa::EcdsaChannelSigner;
8176
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
@@ -90,8 +85,15 @@ use crate::util::errors::APIError;
9085
#[cfg(feature = "dnssec")]
9186
use crate::onion_message::dns_resolution::{DNSResolverMessage, OMNameResolver};
9287

88+
#[cfg(async_payments)]
89+
use {
90+
crate::offers::static_invoice::StaticInvoice,
91+
crate::onion_message::messenger::Destination,
92+
};
93+
9394
#[cfg(not(c_bindings))]
9495
use {
96+
crate::onion_message::messenger::DefaultMessageRouter,
9597
crate::routing::router::DefaultRouter,
9698
crate::routing::gossip::NetworkGraph,
9799
crate::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters},
@@ -2126,8 +2128,6 @@ where
21262128
//
21272129
// Lock order tree:
21282130
//
2129-
// `pending_offers_messages`
2130-
//
21312131
// `pending_async_payments_messages`
21322132
//
21332133
// `total_consistency_lock`
@@ -2378,10 +2378,6 @@ where
23782378
event_persist_notifier: Notifier,
23792379
needs_persist_flag: AtomicBool,
23802380

2381-
#[cfg(not(any(test, feature = "_test_utils")))]
2382-
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
2383-
#[cfg(any(test, feature = "_test_utils"))]
2384-
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
23852381
pending_async_payments_messages: Mutex<Vec<(AsyncPaymentsMessage, MessageSendInstructions)>>,
23862382

23872383
/// Tracks the message events that are to be broadcasted when we are connected to some peer.
@@ -3302,7 +3298,6 @@ where
33023298
needs_persist_flag: AtomicBool::new(false),
33033299
funding_batch_states: Mutex::new(BTreeMap::new()),
33043300

3305-
pending_offers_messages: Mutex::new(Vec::new()),
33063301
pending_async_payments_messages: Mutex::new(Vec::new()),
33073302
pending_broadcast_messages: Mutex::new(Vec::new()),
33083303

@@ -9502,10 +9497,6 @@ where
95029497
MR::Target: MessageRouter,
95039498
L::Target: Logger,
95049499
{
9505-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>> {
9506-
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
9507-
}
9508-
95099500
#[cfg(feature = "dnssec")]
95109501
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
95119502
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
@@ -9607,42 +9598,6 @@ where
96079598
self.pending_outbound_payments.release_invoice_requests_awaiting_invoice()
96089599
}
96099600

9610-
fn enqueue_invoice_request(
9611-
&self,
9612-
invoice_request: InvoiceRequest,
9613-
reply_paths: Vec<BlindedMessagePath>,
9614-
) -> Result<(), Bolt12SemanticError> {
9615-
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
9616-
if !invoice_request.paths().is_empty() {
9617-
reply_paths
9618-
.iter()
9619-
.flat_map(|reply_path| invoice_request.paths().iter().map(move |path| (path, reply_path)))
9620-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
9621-
.for_each(|(path, reply_path)| {
9622-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9623-
destination: Destination::BlindedPath(path.clone()),
9624-
reply_path: reply_path.clone(),
9625-
};
9626-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9627-
pending_offers_messages.push((message, instructions));
9628-
});
9629-
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
9630-
for reply_path in reply_paths {
9631-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9632-
destination: Destination::Node(node_id),
9633-
reply_path,
9634-
};
9635-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9636-
pending_offers_messages.push((message, instructions));
9637-
}
9638-
} else {
9639-
debug_assert!(false);
9640-
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
9641-
}
9642-
9643-
Ok(())
9644-
}
9645-
96469601
fn get_current_blocktime(&self) -> Duration {
96479602
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
96489603
}
@@ -9743,13 +9698,6 @@ where
97439698
}
97449699
}
97459700

9746-
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
9747-
/// along different paths.
9748-
/// Sending multiple requests increases the chances of successful delivery in case some
9749-
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
9750-
/// even if multiple invoices are received.
9751-
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
9752-
97539701
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>
97549702
where
97559703
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -13140,7 +13088,6 @@ where
1314013088

1314113089
funding_batch_states: Mutex::new(BTreeMap::new()),
1314213090

13143-
pending_offers_messages: Mutex::new(Vec::new()),
1314413091
pending_async_payments_messages: Mutex::new(Vec::new()),
1314513092

1314613093
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
@@ -26,9 +26,7 @@ use crate::blinded_path::payment::{
2626
BlindedPaymentPath, Bolt12OfferContext, Bolt12RefundContext, PaymentContext,
2727
};
2828
use crate::events::PaymentFailureReason;
29-
use crate::ln::channelmanager::{
30-
Bolt12PaymentError, PaymentId, Verification, OFFERS_MESSAGE_REQUEST_LIMIT,
31-
};
29+
use crate::ln::channelmanager::{Bolt12PaymentError, PaymentId, Verification};
3230
use crate::ln::inbound_payment;
3331
use crate::ln::outbound_payment::{Retry, RetryableInvoiceRequest, StaleExpiration};
3432
use crate::offers::invoice::{
@@ -77,11 +75,6 @@ use {
7775
///
7876
/// [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
7977
pub trait OffersMessageCommons {
80-
/// Get pending offers messages
81-
fn get_pending_offers_messages(
82-
&self,
83-
) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>>;
84-
8578
#[cfg(feature = "dnssec")]
8679
/// Get pending DNS onion messages
8780
fn get_pending_dns_onion_messages(
@@ -172,11 +165,6 @@ pub trait OffersMessageCommons {
172165
&self,
173166
) -> Vec<(PaymentId, RetryableInvoiceRequest)>;
174167

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

@@ -577,6 +565,11 @@ where
577565

578566
message_router: MR,
579567

568+
#[cfg(not(any(test, feature = "_test_utils")))]
569+
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
570+
#[cfg(any(test, feature = "_test_utils"))]
571+
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
572+
580573
#[cfg(feature = "_test_utils")]
581574
/// In testing, it is useful be able to forge a name -> offer mapping so that we can pay an
582575
/// offer generated in the test.
@@ -609,9 +602,13 @@ where
609602
inbound_payment_key: expanded_inbound_key,
610603
our_network_pubkey,
611604
secp_ctx,
605+
entropy_source,
606+
612607
commons,
608+
613609
message_router,
614-
entropy_source,
610+
611+
pending_offers_messages: Mutex::new(Vec::new()),
615612
#[cfg(feature = "_test_utils")]
616613
testing_dnssec_proof_offer_resolution_override: Mutex::new(new_hash_map()),
617614
logger,
@@ -644,6 +641,13 @@ where
644641
/// [`Refund`]: crate::offers::refund
645642
pub const MAX_SHORT_LIVED_RELATIVE_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
646643

644+
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
645+
/// along different paths.
646+
/// Sending multiple requests increases the chances of successful delivery in case some
647+
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
648+
/// even if multiple invoices are received.
649+
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
650+
647651
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
648652
where
649653
ES::Target: EntropySource,
@@ -722,6 +726,42 @@ where
722726
)
723727
.and_then(|paths| (!paths.is_empty()).then(|| paths).ok_or(()))
724728
}
729+
730+
fn enqueue_invoice_request(
731+
&self, invoice_request: InvoiceRequest, reply_paths: Vec<BlindedMessagePath>,
732+
) -> Result<(), Bolt12SemanticError> {
733+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
734+
if !invoice_request.paths().is_empty() {
735+
reply_paths
736+
.iter()
737+
.flat_map(|reply_path| {
738+
invoice_request.paths().iter().map(move |path| (path, reply_path))
739+
})
740+
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
741+
.for_each(|(path, reply_path)| {
742+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
743+
destination: Destination::BlindedPath(path.clone()),
744+
reply_path: reply_path.clone(),
745+
};
746+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
747+
pending_offers_messages.push((message, instructions));
748+
});
749+
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
750+
for reply_path in reply_paths {
751+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
752+
destination: Destination::Node(node_id),
753+
reply_path,
754+
};
755+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
756+
pending_offers_messages.push((message, instructions));
757+
}
758+
} else {
759+
debug_assert!(false);
760+
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
761+
}
762+
763+
Ok(())
764+
}
725765
}
726766

727767
impl<ES: Deref, OMC: Deref, MR: Deref, L: Deref> OffersMessageFlow<ES, OMC, MR, L>
@@ -776,7 +816,7 @@ where
776816

777817
create_pending_payment(&invoice_request, nonce)?;
778818

779-
self.commons.enqueue_invoice_request(invoice_request, reply_paths)
819+
self.enqueue_invoice_request(invoice_request, reply_paths)
780820
}
781821
}
782822

@@ -1044,7 +1084,7 @@ where
10441084
});
10451085
match self.create_blinded_paths(context) {
10461086
Ok(reply_paths) => {
1047-
match self.commons.enqueue_invoice_request(invoice_request, reply_paths) {
1087+
match self.enqueue_invoice_request(invoice_request, reply_paths) {
10481088
Ok(_) => {},
10491089
Err(_) => {
10501090
log_warn!(
@@ -1068,7 +1108,7 @@ where
10681108
}
10691109

10701110
fn release_pending_messages(&self) -> Vec<(OffersMessage, MessageSendInstructions)> {
1071-
core::mem::take(&mut self.commons.get_pending_offers_messages())
1111+
core::mem::take(&mut self.pending_offers_messages.lock().unwrap())
10721112
}
10731113
}
10741114

@@ -1398,7 +1438,7 @@ where
13981438
.create_blinded_paths(context)
13991439
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
14001440

1401-
let mut pending_offers_messages = self.commons.get_pending_offers_messages();
1441+
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
14021442
if refund.paths().is_empty() {
14031443
for reply_path in reply_paths {
14041444
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {

0 commit comments

Comments
 (0)