Skip to content

Commit 5b5f5fa

Browse files
committed
Move AsyncPaymentsMessageHandler to flow.rs
1 parent 7c02ca2 commit 5b5f5fa

File tree

6 files changed

+139
-116
lines changed

6 files changed

+139
-116
lines changed

lightning-background-processor/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1192,7 +1192,7 @@ mod tests {
11921192
>,
11931193
>,
11941194
IgnoringMessageHandler,
1195-
Arc<ChannelManager>,
1195+
IgnoringMessageHandler,
11961196
IgnoringMessageHandler,
11971197
IgnoringMessageHandler,
11981198
>;
@@ -1595,7 +1595,7 @@ mod tests {
15951595
manager.clone(),
15961596
msg_router.clone(),
15971597
IgnoringMessageHandler {},
1598-
manager.clone(),
1598+
IgnoringMessageHandler {},
15991599
IgnoringMessageHandler {},
16001600
IgnoringMessageHandler {},
16011601
));

lightning/src/ln/async_payments_tests.rs

+4-4
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

+16-97
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, MessageForwardNode};
36+
use crate::blinded_path::message::MessageForwardNode;
3737
use crate::blinded_path::NodeIdLookUp;
3838
use crate::blinded_path::payment::{BlindedPaymentPath, PaymentConstraints, PaymentContext, UnauthenticatedReceiveTlvs};
3939
use crate::chain;
@@ -68,8 +68,7 @@ use crate::offers::invoice::Bolt12Invoice;
6868
use crate::offers::invoice::UnsignedBolt12Invoice;
6969
use crate::offers::nonce::Nonce;
7070
use crate::offers::signer;
71-
use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailable, ReleaseHeldHtlc, AsyncPaymentsMessageHandler};
72-
use crate::onion_message::messenger::{MessageRouter, MessageSendInstructions, Responder, ResponseInstruction};
71+
use crate::onion_message::messenger::MessageRouter;
7372
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
7473
use crate::sign::ecdsa::EcdsaChannelSigner;
7574
use crate::util::config::{UserConfig, ChannelConfig, ChannelConfigUpdate};
@@ -88,7 +87,6 @@ use crate::onion_message::dns_resolution::OMNameResolver;
8887
use {
8988
crate::blinded_path::message::{BlindedMessagePath, MessageContext},
9089
crate::offers::static_invoice::StaticInvoice,
91-
crate::onion_message::messenger::Destination,
9290
};
9391

9492
#[cfg(not(c_bindings))]
@@ -2418,8 +2416,6 @@ where
24182416
event_persist_notifier: Notifier,
24192417
needs_persist_flag: AtomicBool,
24202418

2421-
pending_async_payments_messages: Mutex<Vec<(AsyncPaymentsMessage, MessageSendInstructions)>>,
2422-
24232419
/// Tracks the message events that are to be broadcasted when we are connected to some peer.
24242420
pending_broadcast_messages: Mutex<Vec<MessageSendEvent>>,
24252421

@@ -3339,7 +3335,6 @@ where
33393335
needs_persist_flag: AtomicBool::new(false),
33403336
funding_batch_states: Mutex::new(BTreeMap::new()),
33413337

3342-
pending_async_payments_messages: Mutex::new(Vec::new()),
33433338
pending_broadcast_messages: Mutex::new(Vec::new()),
33443339

33453340
last_days_feerates: Mutex::new(VecDeque::new()),
@@ -4508,45 +4503,6 @@ where
45084503
res
45094504
}
45104505

4511-
#[cfg(async_payments)]
4512-
fn initiate_async_payment(
4513-
&self, invoice: &StaticInvoice, payment_id: PaymentId
4514-
) -> Result<(), Bolt12PaymentError> {
4515-
4516-
self.handle_static_invoice_received(invoice, payment_id)?;
4517-
4518-
let nonce = Nonce::from_entropy_source(&*self.entropy_source);
4519-
let hmac = payment_id.hmac_for_async_payment(nonce, &self.inbound_payment_key);
4520-
let reply_paths = match self.create_blinded_paths(
4521-
MessageContext::AsyncPayments(
4522-
AsyncPaymentsContext::OutboundPayment { payment_id, nonce, hmac }
4523-
)
4524-
) {
4525-
Ok(paths) => paths,
4526-
Err(()) => {
4527-
self.abandon_payment_with_reason(payment_id, PaymentFailureReason::BlindedPathCreationFailed);
4528-
return Err(Bolt12PaymentError::BlindedPathCreationFailed);
4529-
}
4530-
};
4531-
4532-
let mut pending_async_payments_messages = self.pending_async_payments_messages.lock().unwrap();
4533-
const HTLC_AVAILABLE_LIMIT: usize = 10;
4534-
reply_paths
4535-
.iter()
4536-
.flat_map(|reply_path| invoice.message_paths().iter().map(move |invoice_path| (invoice_path, reply_path)))
4537-
.take(HTLC_AVAILABLE_LIMIT)
4538-
.for_each(|(invoice_path, reply_path)| {
4539-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
4540-
destination: Destination::BlindedPath(invoice_path.clone()),
4541-
reply_path: reply_path.clone(),
4542-
};
4543-
let message = AsyncPaymentsMessage::HeldHtlcAvailable(HeldHtlcAvailable {});
4544-
pending_async_payments_messages.push((message, instructions));
4545-
});
4546-
4547-
Ok(())
4548-
}
4549-
45504506
#[cfg(async_payments)]
45514507
fn send_payment_for_static_invoice(
45524508
&self, payment_id: PaymentId
@@ -9528,13 +9484,6 @@ where
95289484
Duration::from_secs(self.highest_seen_timestamp.load(Ordering::Acquire) as u64)
95299485
}
95309486

9531-
#[cfg(async_payments)]
9532-
fn initiate_async_payment(
9533-
&self, invoice: &StaticInvoice, payment_id: PaymentId
9534-
) -> Result<(), Bolt12PaymentError> {
9535-
self.initiate_async_payment(invoice, payment_id)
9536-
}
9537-
95389487
fn get_chain_hash(&self) -> ChainHash {
95399488
self.chain_hash
95409489
}
@@ -9568,6 +9517,20 @@ where
95689517
) -> Result<(), ()> {
95699518
self.pending_outbound_payments.received_offer(payment_id, retryable_invoice_request)
95709519
}
9520+
9521+
#[cfg(async_payments)]
9522+
fn handle_static_invoice_received(
9523+
&self, invoice: &StaticInvoice, payment_id: PaymentId
9524+
) -> Result<(), Bolt12PaymentError> {
9525+
self.handle_static_invoice_received(invoice, payment_id)
9526+
}
9527+
9528+
#[cfg(async_payments)]
9529+
fn send_payment_for_static_invoice(
9530+
&self, payment_id: PaymentId
9531+
) -> Result<(), Bolt12PaymentError> {
9532+
self.send_payment_for_static_invoice(payment_id)
9533+
}
95719534
}
95729535

95739536
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>
@@ -11141,48 +11104,6 @@ where
1114111104
}
1114211105
}
1114311106

11144-
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
11145-
AsyncPaymentsMessageHandler for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
11146-
where
11147-
M::Target: chain::Watch<<SP::Target as SignerProvider>::EcdsaSigner>,
11148-
T::Target: BroadcasterInterface,
11149-
ES::Target: EntropySource,
11150-
NS::Target: NodeSigner,
11151-
SP::Target: SignerProvider,
11152-
F::Target: FeeEstimator,
11153-
R::Target: Router,
11154-
MR::Target: MessageRouter,
11155-
L::Target: Logger,
11156-
{
11157-
fn handle_held_htlc_available(
11158-
&self, _message: HeldHtlcAvailable, _context: AsyncPaymentsContext,
11159-
_responder: Option<Responder>
11160-
) -> Option<(ReleaseHeldHtlc, ResponseInstruction)> {
11161-
None
11162-
}
11163-
11164-
fn handle_release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
11165-
#[cfg(async_payments)] {
11166-
let (payment_id, nonce, hmac) = match _context {
11167-
AsyncPaymentsContext::OutboundPayment { payment_id, hmac, nonce } => {
11168-
(payment_id, nonce, hmac)
11169-
},
11170-
_ => return
11171-
};
11172-
if payment_id.verify_for_async_payment(hmac, nonce, &self.inbound_payment_key).is_err() { return }
11173-
if let Err(e) = self.send_payment_for_static_invoice(payment_id) {
11174-
log_trace!(
11175-
self.logger, "Failed to release held HTLC with payment id {}: {:?}", payment_id, e
11176-
);
11177-
}
11178-
}
11179-
}
11180-
11181-
fn release_pending_messages(&self) -> Vec<(AsyncPaymentsMessage, MessageSendInstructions)> {
11182-
core::mem::take(&mut self.pending_async_payments_messages.lock().unwrap())
11183-
}
11184-
}
11185-
1118611107
impl<M: Deref, T: Deref, ES: Deref, NS: Deref, SP: Deref, F: Deref, R: Deref, MR: Deref, L: Deref>
1118711108
NodeIdLookUp for ChannelManager<M, T, ES, NS, SP, F, R, MR, L>
1118811109
where
@@ -13102,8 +13023,6 @@ where
1310213023

1310313024
funding_batch_states: Mutex::new(BTreeMap::new()),
1310413025

13105-
pending_async_payments_messages: Mutex::new(Vec::new()),
13106-
1310713026
pending_broadcast_messages: Mutex::new(Vec::new()),
1310813027

1310913028
entropy_source: args.entropy_source,

lightning/src/ln/functional_test_utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -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)