Skip to content

Commit ee171fc

Browse files
ChannelManager: DRY queueing onion messages
The new utility will be used in upcoming commits as part of supporting static invoice server.
1 parent 3d37394 commit ee171fc

File tree

1 file changed

+34
-37
lines changed

1 file changed

+34
-37
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ use crate::onion_message::async_payments::{AsyncPaymentsMessage, HeldHtlcAvailab
7878
use crate::onion_message::dns_resolution::HumanReadableName;
7979
use crate::onion_message::messenger::{Destination, MessageRouter, Responder, ResponseInstruction, MessageSendInstructions};
8080
use crate::onion_message::offers::{OffersMessage, OffersMessageHandler};
81+
use crate::onion_message::packet::OnionMessageContents;
8182
use crate::sign::{EntropySource, NodeSigner, Recipient, SignerProvider};
8283
use crate::sign::ecdsa::EcdsaChannelSigner;
8384
use crate::util::config::{ChannelConfig, ChannelConfigUpdate, ChannelConfigOverrides, UserConfig};
@@ -4867,19 +4868,10 @@ where
48674868
};
48684869

48694870
let mut pending_async_payments_messages = self.pending_async_payments_messages.lock().unwrap();
4870-
const HTLC_AVAILABLE_LIMIT: usize = 10;
4871-
reply_paths
4872-
.iter()
4873-
.flat_map(|reply_path| invoice.message_paths().iter().map(move |invoice_path| (invoice_path, reply_path)))
4874-
.take(HTLC_AVAILABLE_LIMIT)
4875-
.for_each(|(invoice_path, reply_path)| {
4876-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
4877-
destination: Destination::BlindedPath(invoice_path.clone()),
4878-
reply_path: reply_path.clone(),
4879-
};
4880-
let message = AsyncPaymentsMessage::HeldHtlcAvailable(HeldHtlcAvailable {});
4881-
pending_async_payments_messages.push((message, instructions));
4882-
});
4871+
let message = AsyncPaymentsMessage::HeldHtlcAvailable(HeldHtlcAvailable {});
4872+
queue_onion_message_with_reply_paths(
4873+
message, invoice.message_paths(), reply_paths, &mut pending_async_payments_messages
4874+
);
48834875

48844876
NotifyOption::DoPersist
48854877
});
@@ -10434,18 +10426,10 @@ where
1043410426
) -> Result<(), Bolt12SemanticError> {
1043510427
let mut pending_offers_messages = self.pending_offers_messages.lock().unwrap();
1043610428
if !invoice_request.paths().is_empty() {
10437-
reply_paths
10438-
.iter()
10439-
.flat_map(|reply_path| invoice_request.paths().iter().map(move |path| (path, reply_path)))
10440-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
10441-
.for_each(|(path, reply_path)| {
10442-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
10443-
destination: Destination::BlindedPath(path.clone()),
10444-
reply_path: reply_path.clone(),
10445-
};
10446-
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
10447-
pending_offers_messages.push((message, instructions));
10448-
});
10429+
let message = OffersMessage::InvoiceRequest(invoice_request.clone());
10430+
queue_onion_message_with_reply_paths(
10431+
message, invoice_request.paths(), reply_paths, &mut pending_offers_messages
10432+
);
1044910433
} else if let Some(node_id) = invoice_request.issuer_signing_pubkey() {
1045010434
for reply_path in reply_paths {
1045110435
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
@@ -10543,18 +10527,10 @@ where
1054310527
pending_offers_messages.push((message, instructions));
1054410528
}
1054510529
} else {
10546-
reply_paths
10547-
.iter()
10548-
.flat_map(|reply_path| refund.paths().iter().map(move |path| (path, reply_path)))
10549-
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
10550-
.for_each(|(path, reply_path)| {
10551-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
10552-
destination: Destination::BlindedPath(path.clone()),
10553-
reply_path: reply_path.clone(),
10554-
};
10555-
let message = OffersMessage::Invoice(invoice.clone());
10556-
pending_offers_messages.push((message, instructions));
10557-
});
10530+
let message = OffersMessage::Invoice(invoice.clone());
10531+
queue_onion_message_with_reply_paths(
10532+
message, refund.paths(), reply_paths, &mut pending_offers_messages
10533+
);
1055810534
}
1055910535

1056010536
Ok(invoice)
@@ -12689,6 +12665,27 @@ where
1268912665
}
1269012666
}
1269112667

12668+
fn queue_onion_message_with_reply_paths<T: OnionMessageContents + Clone>(
12669+
message: T, message_paths: &[BlindedMessagePath], reply_paths: Vec<BlindedMessagePath>,
12670+
queue: &mut Vec<(T, MessageSendInstructions)>
12671+
) {
12672+
reply_paths
12673+
.iter()
12674+
.flat_map(|reply_path|
12675+
message_paths
12676+
.iter()
12677+
.map(move |path| (path.clone(), reply_path))
12678+
)
12679+
.take(OFFERS_MESSAGE_REQUEST_LIMIT)
12680+
.for_each(|(path, reply_path)| {
12681+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
12682+
destination: Destination::BlindedPath(path.clone()),
12683+
reply_path: reply_path.clone(),
12684+
};
12685+
queue.push((message.clone(), instructions));
12686+
});
12687+
}
12688+
1269212689
/// Fetches the set of [`NodeFeatures`] flags that are provided by or required by
1269312690
/// [`ChannelManager`].
1269412691
pub(crate) fn provided_node_features(config: &UserConfig) -> NodeFeatures {

0 commit comments

Comments
 (0)