Skip to content

Commit a5dfd09

Browse files
committed
Move AsyncPaymentsMessageHandler to flow.rs
1 parent 389bda3 commit a5dfd09

File tree

6 files changed

+156
-115
lines changed

6 files changed

+156
-115
lines changed

lightning-background-processor/src/lib.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,7 @@ mod tests {
10831083
IgnoringMessageHandler, MessageHandler, PeerManager, SocketDescriptor,
10841084
};
10851085
use lightning::ln::types::ChannelId;
1086+
use lightning::offers::flow;
10861087
use lightning::onion_message::messenger::{DefaultMessageRouter, OnionMessenger};
10871088
use lightning::routing::gossip::{NetworkGraph, P2PGossipSync};
10881089
use lightning::routing::router::{CandidateRouteHop, DefaultRouter, Path, RouteHop};
@@ -1178,6 +1179,19 @@ mod tests {
11781179
>,
11791180
>;
11801181

1182+
type OffersMessageFlow = flow::OffersMessageFlow<
1183+
Arc<KeysManager>,
1184+
Arc<ChannelManager>,
1185+
Arc<
1186+
DefaultMessageRouter<
1187+
Arc<NetworkGraph<Arc<test_utils::TestLogger>>>,
1188+
Arc<test_utils::TestLogger>,
1189+
Arc<KeysManager>,
1190+
>,
1191+
>,
1192+
Arc<test_utils::TestLogger>,
1193+
>;
1194+
11811195
type OM = OnionMessenger<
11821196
Arc<KeysManager>,
11831197
Arc<KeysManager>,
@@ -1191,7 +1205,7 @@ mod tests {
11911205
>,
11921206
>,
11931207
IgnoringMessageHandler,
1194-
Arc<ChannelManager>,
1208+
Arc<OffersMessageFlow>,
11951209
IgnoringMessageHandler,
11961210
IgnoringMessageHandler,
11971211
>;
@@ -1587,14 +1601,23 @@ mod tests {
15871601
params,
15881602
genesis_block.header.time,
15891603
));
1604+
let offers_message_flow = Arc::new(OffersMessageFlow::new(
1605+
manager.inbound_payment_key,
1606+
manager.get_our_node_id(),
1607+
keys_manager.clone(),
1608+
manager.clone(),
1609+
msg_router.clone(),
1610+
logger.clone(),
1611+
));
1612+
15901613
let messenger = Arc::new(OnionMessenger::new(
15911614
keys_manager.clone(),
15921615
keys_manager.clone(),
15931616
logger.clone(),
15941617
manager.clone(),
15951618
msg_router.clone(),
15961619
IgnoringMessageHandler {},
1597-
manager.clone(),
1620+
offers_message_flow,
15981621
IgnoringMessageHandler {},
15991622
IgnoringMessageHandler {},
16001623
));

lightning/src/ln/async_payments_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn ignore_unexpected_static_invoice() {
265265
nodes[0]
266266
.onion_messenger
267267
.handle_onion_message(nodes[1].node.get_our_node_id(), &unexpected_static_invoice_om);
268-
let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
268+
let async_pmts_msgs = nodes[0].offers_handler.release_pending_messages();
269269
assert!(async_pmts_msgs.is_empty());
270270
assert!(nodes[0].node.get_and_clear_pending_events().is_empty());
271271

@@ -289,7 +289,7 @@ fn ignore_unexpected_static_invoice() {
289289
nodes[0]
290290
.onion_messenger
291291
.handle_onion_message(nodes[1].node.get_our_node_id(), &static_invoice_om);
292-
let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
292+
let async_pmts_msgs = nodes[0].offers_handler.release_pending_messages();
293293
assert!(!async_pmts_msgs.is_empty());
294294
assert!(async_pmts_msgs
295295
.into_iter()
@@ -314,7 +314,7 @@ fn ignore_unexpected_static_invoice() {
314314
nodes[0]
315315
.onion_messenger
316316
.handle_onion_message(nodes[1].node.get_our_node_id(), &dup_static_invoice_om);
317-
let async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
317+
let async_pmts_msgs = nodes[0].offers_handler.release_pending_messages();
318318
assert!(async_pmts_msgs.is_empty());
319319
}
320320

@@ -369,7 +369,7 @@ fn pays_static_invoice() {
369369
nodes[0]
370370
.onion_messenger
371371
.handle_onion_message(nodes[1].node.get_our_node_id(), &static_invoice_om);
372-
let mut async_pmts_msgs = AsyncPaymentsMessageHandler::release_pending_messages(nodes[0].node);
372+
let mut async_pmts_msgs = nodes[0].offers_handler.release_pending_messages();
373373
assert!(!async_pmts_msgs.is_empty());
374374
assert!(async_pmts_msgs
375375
.iter()

lightning/src/ln/channelmanager.rs

Lines changed: 19 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use bitcoin::secp256k1::Secp256k1;
3333
use bitcoin::{secp256k1, Sequence, Weight};
3434

3535
use crate::events::FundingInfo;
36-
use crate::blinded_path::message::{AsyncPaymentsContext, MessageContext, OffersContext};
36+
use crate::blinded_path::message::{MessageContext, OffersContext};
3737
use crate::blinded_path::NodeIdLookUp;
3838
use crate::blinded_path::message::{BlindedMessagePath, MessageForwardNode};
3939
use crate::blinded_path::payment::{BlindedPaymentPath, Bolt12RefundContext, PaymentConstraints, PaymentContext, UnauthenticatedReceiveTlvs};
@@ -72,9 +72,8 @@ use crate::offers::offer::{Offer, OfferBuilder};
7272
use crate::offers::parse::Bolt12SemanticError;
7373
use crate::offers::refund::{Refund, RefundBuilder};
7474
use crate::offers::signer;
75-
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
7675
use crate::onion_message::dns_resolution::HumanReadableName;
77-
use crate::onion_message::messenger::{Destination, MessageRouter, Responder, ResponseInstruction, MessageSendInstructions};
76+
use crate::onion_message::messenger::{Destination, MessageRouter, MessageSendInstructions};
7877
use crate::onion_message::offers::OffersMessage;
7978
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8079
use crate::sign::ecdsa::EcdsaChannelSigner;
@@ -87,6 +86,7 @@ use crate::util::ser::TransactionU16LenLimited;
8786
use crate::util::logger::{Level, Logger, WithContext};
8887
use crate::util::errors::APIError;
8988
#[cfg(async_payments)] use {
89+
crate::blinded_path::message::AsyncPaymentsContext,
9090
crate::blinded_path::payment::AsyncBolt12OfferContext,
9191
crate::offers::offer::Amount,
9292
crate::offers::static_invoice::{DEFAULT_RELATIVE_EXPIRY as STATIC_INVOICE_DEFAULT_RELATIVE_EXPIRY, StaticInvoice, StaticInvoiceBuilder},
@@ -2529,7 +2529,10 @@ where
25292529

25302530
our_network_pubkey: PublicKey,
25312531

2532-
pub(super) inbound_payment_key: inbound_payment::ExpandedKey,
2532+
/// The [`ExpandedKey`] for use in encrypting and decrypting inbound payment data.
2533+
///
2534+
/// [`ExpandedKey`]: crate::ln::inbound_payment::ExpandedKey
2535+
pub inbound_payment_key: inbound_payment::ExpandedKey,
25332536

25342537
/// LDK puts the [fake scids] that it generates into namespaces, to identify the type of an
25352538
/// incoming payment. To make it harder for a third-party to identify the type of a payment,
@@ -2622,7 +2625,6 @@ where
26222625
pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
26232626
#[cfg(any(test, feature = "_test_utils"))]
26242627
pub(crate) pending_offers_messages: Mutex<Vec<(OffersMessage, MessageSendInstructions)>>,
2625-
pending_async_payments_messages: Mutex<Vec<(AsyncPaymentsMessage, MessageSendInstructions)>>,
26262628

26272629
/// Tracks the message events that are to be broadcasted when we are connected to some peer.
26282630
pending_broadcast_messages: Mutex<Vec<MessageSendEvent>>,
@@ -3555,7 +3557,6 @@ where
35553557
funding_batch_states: Mutex::new(BTreeMap::new()),
35563558

35573559
pending_offers_messages: Mutex::new(Vec::new()),
3558-
pending_async_payments_messages: Mutex::new(Vec::new()),
35593560
pending_broadcast_messages: Mutex::new(Vec::new()),
35603561

35613562
last_days_feerates: Mutex::new(VecDeque::new()),
@@ -4724,45 +4725,6 @@ where
47244725
res
47254726
}
47264727

4727-
#[cfg(async_payments)]
4728-
fn initiate_async_payment(
4729-
&self, invoice: &StaticInvoice, payment_id: PaymentId
4730-
) -> Result<(), Bolt12PaymentError> {
4731-
4732-
self.handle_static_invoice_received(invoice, payment_id)?;
4733-
4734-
let nonce = Nonce::from_entropy_source(&*self.entropy_source);
4735-
let hmac = payment_id.hmac_for_async_payment(nonce, &self.inbound_payment_key);
4736-
let reply_paths = match self.create_blinded_paths(
4737-
MessageContext::AsyncPayments(
4738-
AsyncPaymentsContext::OutboundPayment { payment_id, nonce, hmac }
4739-
)
4740-
) {
4741-
Ok(paths) => paths,
4742-
Err(()) => {
4743-
self.abandon_payment_with_reason(payment_id, PaymentFailureReason::BlindedPathCreationFailed);
4744-
return Err(Bolt12PaymentError::BlindedPathCreationFailed);
4745-
}
4746-
};
4747-
4748-
let mut pending_async_payments_messages = self.pending_async_payments_messages.lock().unwrap();
4749-
const HTLC_AVAILABLE_LIMIT: usize = 10;
4750-
reply_paths
4751-
.iter()
4752-
.flat_map(|reply_path| invoice.message_paths().iter().map(move |invoice_path| (invoice_path, reply_path)))
4753-
.take(HTLC_AVAILABLE_LIMIT)
4754-
.for_each(|(invoice_path, reply_path)| {
4755-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
4756-
destination: Destination::BlindedPath(invoice_path.clone()),
4757-
reply_path: reply_path.clone(),
4758-
};
4759-
let message = AsyncPaymentsMessage::HeldHtlcAvailable(HeldHtlcAvailable {});
4760-
pending_async_payments_messages.push((message, instructions));
4761-
});
4762-
4763-
Ok(())
4764-
}
4765-
47664728
#[cfg(async_payments)]
47674729
fn send_payment_for_static_invoice(
47684730
&self, payment_id: PaymentId
@@ -9900,6 +9862,18 @@ where
99009862
self.pending_outbound_payments.received_offer(payment_id, retryable_invoice_request)
99019863
}
99029864

9865+
#[cfg(async_payments)]
9866+
fn handle_static_invoice_received(
9867+
&self, invoice: &StaticInvoice, payment_id: PaymentId
9868+
) -> Result<(), Bolt12PaymentError> {
9869+
self.handle_static_invoice_received(invoice, payment_id)
9870+
}
9871+
9872+
#[cfg(async_payments)]
9873+
fn send_payment_for_static_invoice(&self, payment_id: PaymentId) -> Result<(), Bolt12PaymentError> {
9874+
self.send_payment_for_static_invoice(payment_id)
9875+
}
9876+
99039877
// ----Temporary Functions----
99049878
// Set of functions temporarily moved to OffersMessageCommons for easier
99059879
// transition of code from ChannelManager to OffersMessageFlow
@@ -9919,13 +9893,6 @@ where
99199893
) -> Result<(), Bolt12SemanticError> {
99209894
self.pay_for_offer_intern(offer, quantity, amount_msats, payer_note, payment_id, human_readable_name, create_pending_payment)
99219895
}
9922-
9923-
#[cfg(async_payments)]
9924-
fn initiate_async_payment(
9925-
&self, invoice: &StaticInvoice, payment_id: PaymentId
9926-
) -> Result<(), Bolt12PaymentError> {
9927-
self.initiate_async_payment(invoice, payment_id)
9928-
}
99299896
}
99309897

99319898
/// Defines the maximum number of [`OffersMessage`] including different reply paths to be sent
@@ -11904,48 +11871,6 @@ where
1190411871
}
1190511872
}
1190611873

11907-
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
11908-
AsyncPaymentsMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
11909-
where
11910-
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
11911-
T::Target: BroadcasterInterface,
11912-
ES::Target: EntropySource,
11913-
NS::Target: NodeSigner,
11914-
SP::Target: SignerProvider,
11915-
F::Target: FeeEstimator,
11916-
R::Target: Router,
11917-
MR::Target: MessageRouter,
11918-
L::Target: Logger,
11919-
{
11920-
fn handle_held_htlc_available(
11921-
&self, _message: HeldHtlcAvailable, _context: AsyncPaymentsContext,
11922-
_responder: Option<Responder>
11923-
) -> Option<(ReleaseHeldHtlc, ResponseInstruction)> {
11924-
None
11925-
}
11926-
11927-
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
11928-
#[cfg(async_payments)] {
11929-
let (payment_id, nonce, hmac) = match _context {
11930-
AsyncPaymentsContext::OutboundPayment { payment_id, hmac, nonce } => {
11931-
(payment_id, nonce, hmac)
11932-
},
11933-
_ => return
11934-
};
11935-
if payment_id.verify_for_async_payment(hmac, nonce, &self.inbound_payment_key).is_err() { return }
11936-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
11937-
log_trace!(
11938-
self.logger, "Failed to release held HTLC with payment id {}: {:?}", payment_id, e
11939-
);
11940-
}
11941-
}
11942-
}
11943-
11944-
fn release_pending_messages(&self) -> Vec<(AsyncPaymentsMessage, MessageSendInstructions)> {
11945-
core::mem::take(&mut self.pending_async_payments_messages.lock().unwrap())
11946-
}
11947-
}
11948-
1194911874
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
1195011875
NodeIdLookUp for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
1195111876
where
@@ -13862,7 +13787,6 @@ where
1386213787
funding_batch_states: Mutex::new(BTreeMap::new()),
1386313788

1386413789
pending_offers_messages: Mutex::new(Vec::new()),
13865-
pending_async_payments_messages: Mutex::new(Vec::new()),
1386613790

1386713791
pending_broadcast_messages: Mutex::new(Vec::new()),
1386813792

lightning/src/ln/functional_test_utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ type TestOnionMessenger<'chan_man, 'node_cfg, 'chan_mon_cfg> = OnionMessenger<
427427
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
428428
&'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>,
429429
Arc<TestOffersMessageFlow<'chan_man, 'node_cfg, 'chan_mon_cfg>>,
430-
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
430+
Arc<TestOffersMessageFlow<'chan_man, 'node_cfg, 'chan_mon_cfg>>,
431431
IgnoringMessageHandler,
432432
IgnoringMessageHandler,
433433
>;
@@ -440,7 +440,7 @@ type TestOnionMessenger<'chan_man, 'node_cfg, 'chan_mon_cfg> = OnionMessenger<
440440
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
441441
&'node_cfg test_utils::TestMessageRouter<'chan_mon_cfg>,
442442
Arc<TestOffersMessageFlow<'chan_man, 'node_cfg, 'chan_mon_cfg>>,
443-
&'chan_man TestChannelManager<'node_cfg, 'chan_mon_cfg>,
443+
Arc<TestOffersMessageFlow<'chan_man, 'node_cfg, 'chan_mon_cfg>>,
444444
Arc<TestOffersMessageFlow<'chan_man, 'node_cfg, 'chan_mon_cfg>>,
445445
IgnoringMessageHandler,
446446
>;
@@ -3352,13 +3352,13 @@ pub fn create_network<'a, 'b: 'a, 'c: 'b>(node_count: usize, cfgs: &'b Vec<NodeC
33523352
#[cfg(feature = "dnssec")]
33533353
let onion_messenger = OnionMessenger::new(
33543354
dedicated_entropy, cfgs[i].keys_manager, cfgs[i].logger, &chan_mgrs[i],
3355-
&cfgs[i].message_router, offers_handler.clone(), &chan_mgrs[i], offers_handler.clone(),
3355+
&cfgs[i].message_router, offers_handler.clone(), offers_handler.clone(), offers_handler.clone(),
33563356
IgnoringMessageHandler {},
33573357
);
33583358
#[cfg(not(feature = "dnssec"))]
33593359
let onion_messenger = OnionMessenger::new(
33603360
dedicated_entropy, cfgs[i].keys_manager, cfgs[i].logger, &chan_mgrs[i],
3361-
&cfgs[i].message_router, offers_handler.clone(), &chan_mgrs[i], IgnoringMessageHandler {},
3361+
&cfgs[i].message_router, offers_handler.clone(), offers_handler.clone(), IgnoringMessageHandler {},
33623362
IgnoringMessageHandler {},
33633363
);
33643364
let gossip_sync = P2PGossipSync::new(cfgs[i].network_graph.as_ref(), None, cfgs[i].logger);

0 commit comments

Comments
 (0)