Skip to content

Commit 314c513

Browse files
committed
Refactor initiate_async_payment to improve separation of concerns
1. Previously, `initiate_async_payment` handled both the outbound payment state update (marking a static invoice as received) and the message queuing for async payments. This tightly coupled two distinct responsibilities within `ChannelManager`. 2. This refactor **separates concerns** by moving message queuing into a separate function, allowing `initiate_async_payment` to focus solely on updating `pending_outbound_payments`. This paves the way for moving message queuing to `flow.rs` in upcoming commits, ensuring `MessageRouter` logic remains independent of `ChannelManager`. 3. **This introduces a behavior change:** Since `pending_outbound_payments` is now handled separately, we drop PersistenceNotifierGuard before queuing messages. As a result, `NotifyOption` is no longer called for the message queuing process. 4. **Why is this safe?** Because `pending_async_payments_messages` is **not under `total_consistency_lock`**, meaning it does not require persistence notifications. This aligns with the lock order tree and maintains correct locking discipline. By making this change, we improve modularity, enforce a cleaner separation between `ChannelManager` and `MessageRouter`, and remove unnecessary persistence dependencies.
1 parent b1b28ca commit 314c513

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4755,7 +4755,7 @@ where
47554755
}
47564756

47574757
#[cfg(async_payments)]
4758-
fn initiate_async_payment(
4758+
fn handle_static_invoice_received(
47594759
&self, invoice: &StaticInvoice, payment_id: PaymentId
47604760
) -> Result<(), Bolt12PaymentError> {
47614761
let mut res = Ok(());
@@ -4777,43 +4777,51 @@ where
47774777
return NotifyOption::DoPersist
47784778
}
47794779
};
4780-
4781-
let nonce = Nonce::from_entropy_source(&*self.entropy_source);
4782-
let hmac = payment_id.hmac_for_async_payment(nonce, &self.inbound_payment_key);
4783-
let reply_paths = match self.create_blinded_paths(
4784-
MessageContext::AsyncPayments(
4785-
AsyncPaymentsContext::OutboundPayment { payment_id, nonce, hmac }
4786-
)
4787-
) {
4788-
Ok(paths) => paths,
4789-
Err(()) => {
4790-
self.abandon_payment_with_reason(payment_id, PaymentFailureReason::BlindedPathCreationFailed);
4791-
res = Err(Bolt12PaymentError::BlindedPathCreationFailed);
4792-
return NotifyOption::DoPersist
4793-
}
4794-
};
4795-
4796-
let mut pending_async_payments_messages = self.pending_async_payments_messages.lock().unwrap();
4797-
const HTLC_AVAILABLE_LIMIT: usize = 10;
4798-
reply_paths
4799-
.iter()
4800-
.flat_map(|reply_path| invoice.message_paths().iter().map(move |invoice_path| (invoice_path, reply_path)))
4801-
.take(HTLC_AVAILABLE_LIMIT)
4802-
.for_each(|(invoice_path, reply_path)| {
4803-
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
4804-
destination: Destination::BlindedPath(invoice_path.clone()),
4805-
reply_path: reply_path.clone(),
4806-
};
4807-
let message = AsyncPaymentsMessage::HeldHtlcAvailable(HeldHtlcAvailable {});
4808-
pending_async_payments_messages.push((message, instructions));
4809-
});
4810-
48114780
NotifyOption::DoPersist
48124781
});
48134782

48144783
res
48154784
}
48164785

4786+
#[cfg(async_payments)]
4787+
fn initiate_async_payment(
4788+
&self, invoice: &StaticInvoice, payment_id: PaymentId
4789+
) -> Result<(), Bolt12PaymentError> {
4790+
4791+
self.handle_static_invoice_received(invoice, payment_id)?;
4792+
4793+
let nonce = Nonce::from_entropy_source(&*self.entropy_source);
4794+
let hmac = payment_id.hmac_for_async_payment(nonce, &self.inbound_payment_key);
4795+
let reply_paths = match self.create_blinded_paths(
4796+
MessageContext::AsyncPayments(
4797+
AsyncPaymentsContext::OutboundPayment { payment_id, nonce, hmac }
4798+
)
4799+
) {
4800+
Ok(paths) => paths,
4801+
Err(()) => {
4802+
self.abandon_payment_with_reason(payment_id, PaymentFailureReason::BlindedPathCreationFailed);
4803+
return Err(Bolt12PaymentError::BlindedPathCreationFailed);
4804+
}
4805+
};
4806+
4807+
let mut pending_async_payments_messages = self.pending_async_payments_messages.lock().unwrap();
4808+
const HTLC_AVAILABLE_LIMIT: usize = 10;
4809+
reply_paths
4810+
.iter()
4811+
.flat_map(|reply_path| invoice.message_paths().iter().map(move |invoice_path| (invoice_path, reply_path)))
4812+
.take(HTLC_AVAILABLE_LIMIT)
4813+
.for_each(|(invoice_path, reply_path)| {
4814+
let instructions = MessageSendInstructions::WithSpecifiedReplyPath {
4815+
destination: Destination::BlindedPath(invoice_path.clone()),
4816+
reply_path: reply_path.clone(),
4817+
};
4818+
let message = AsyncPaymentsMessage::HeldHtlcAvailable(HeldHtlcAvailable {});
4819+
pending_async_payments_messages.push((message, instructions));
4820+
});
4821+
4822+
Ok(())
4823+
}
4824+
48174825
#[cfg(async_payments)]
48184826
fn send_payment_for_static_invoice(
48194827
&self, payment_id: PaymentId

0 commit comments

Comments
 (0)