Skip to content

Commit dd177d2

Browse files
committed
Move pending_offers_message to flows.rs
1 parent f12c835 commit dd177d2

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},
@@ -2140,8 +2142,6 @@ where
21402142
//
21412143
// Lock order tree:
21422144
//
2143-
// `pending_offers_messages`
2144-
//
21452145
// `pending_async_payments_messages`
21462146
//
21472147
// `total_consistency_lock`
@@ -2392,10 +2392,6 @@ where
23922392
event_persist_notifier: Notifier,
23932393
needs_persist_flag: AtomicBool,
23942394

2395-
#[cfg(not(any(test, feature = "_test_utils")))]
2396-
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
2397-
#[cfg(any(test, feature = "_test_utils"))]
2398-
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
23992395
pending_async_payments_messages: Mutex<Vec<(AsyncPaymentsMessage, MessageSendInstructions)>>,
24002396

24012397
/// Tracks the message events that are to be broadcasted when we are connected to some peer.
@@ -3315,7 +3311,6 @@ where
33153311
needs_persist_flag: AtomicBool::new(false),
33163312
funding_batch_states: Mutex::new(BTreeMap::new()),
33173313

3318-
pending_offers_messages: Mutex::new(Vec::new()),
33193314
pending_async_payments_messages: Mutex::new(Vec::new()),
33203315
pending_broadcast_messages: Mutex::new(Vec::new()),
33213316

@@ -9545,10 +9540,6 @@ where
95459540
MR::Target: MessageRouter,
95469541
L::Target: Logger,
95479542
{
9548-
fn get_pending_offers_messages(&self) -> MutexGuard<'_, Vec<(OffersMessage, MessageSendInstructions)>> {
9549-
self.pending_offers_messages.lock().expect("Mutex is locked by other thread.")
9550-
}
9551-
95529543
#[cfg(feature = "dnssec")]
95539544
fn get_pending_dns_onion_messages(&self) -> MutexGuard<'_, Vec<(DNSResolverMessage, MessageSendInstructions)>> {
95549545
self.pending_dns_onion_messages.lock().expect("Mutex is locked by other thread.")
@@ -9658,42 +9649,6 @@ where
96589649
self.pending_outbound_payments.release_invoice_requests_awaiting_invoice()
96599650
}
96609651

9661-
fn enqueue_invoice_request(
9662-
&self,
9663-
invoice_request: InvoiceRequest,
9664-
reply_paths: Vec<BlindedMessagePath>,
9665-
) -> Result<(), Bolt12SemanticError> {
9666-
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
9667-
if !invoice_request.paths().is_empty() {
9668-
reply_paths
9669-
.iter()
9670-
.flat_map(|reply_path| invoice_request.paths().iter().map(move |path| (path, reply_path)))
9671-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
9672-
.for_each(|(path, reply_path)| {
9673-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9674-
destination: Destination::BlindedPath(path.clone()),
9675-
reply_path: reply_path.clone(),
9676-
};
9677-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9678-
pending_offers_messages.push((message, instructions));
9679-
});
9680-
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
9681-
for reply_path in reply_paths {
9682-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
9683-
destination: Destination::Node(node_id),
9684-
reply_path,
9685-
};
9686-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
9687-
pending_offers_messages.push((message, instructions));
9688-
}
9689-
} else {
9690-
debug_assert!(false);
9691-
return Err(Bolt12SemanticError::MissingIssuerSigningPubkey);
9692-
}
9693-
9694-
Ok(())
9695-
}
9696-
96979652
fn get_current_blocktime(&self) -> Duration {
96989653
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
96999654
}
@@ -9794,13 +9749,6 @@ where
97949749
}
97959750
}
97969751

9797-
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
9798-
/// along different paths.
9799-
/// Sending multiple requests increases the chances of successful delivery in case some
9800-
/// paths are unavailable. However, only one invoice for a given [`PaymentId`] will be paid,
9801-
/// even if multiple invoices are received.
9802-
pub const OFFERS_MESSAGE_REQUEST_LIMIT: usize = 10;
9803-
98049752
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>
98059753
where
98069754
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
@@ -13194,7 +13142,6 @@ where
1319413142

1319513143
funding_batch_states: Mutex::new(BTreeMap::new()),
1319613144

13197-
pending_offers_messages: Mutex::new(Vec::new()),
1319813145
pending_async_payments_messages: Mutex::new(Vec::new()),
1319913146

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

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

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

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

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

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

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

21612161
let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
21622162
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)