Skip to content

Commit 2a34be2

Browse files
Support sending async payments as an always-online sender.
Async receive is not yet supported.
1 parent b4be8dd commit 2a34be2

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

lightning/src/ln/channelmanager.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -4161,6 +4161,21 @@ where
41614161
Ok(())
41624162
}
41634163

4164+
#[cfg(async_payments)]
4165+
fn send_payment_for_static_invoice(
4166+
&self, payment_id: PaymentId, payment_release_secret: [u8; 32]
4167+
) -> Result<(), Bolt12PaymentError> {
4168+
let best_block_height = self.best_block.read().unwrap().height;
4169+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4170+
self.pending_outbound_payments
4171+
.send_payment_for_static_invoice(
4172+
payment_id, payment_release_secret, &self.router, self.list_usable_channels(),
4173+
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
4174+
best_block_height, &self.logger, &self.pending_events,
4175+
|args| self.send_payment_along_path(args)
4176+
)
4177+
}
4178+
41644179
/// Signals that no further attempts for the given payment should occur. Useful if you have a
41654180
/// pending outbound payment with retries remaining, but wish to stop retrying the payment before
41664181
/// retries are exhausted.
@@ -10732,7 +10747,17 @@ where
1073210747
ResponseInstruction::NoResponse
1073310748
}
1073410749

10735-
fn release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {}
10750+
fn release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
10751+
#[cfg(async_payments)] {
10752+
let AsyncPaymentsContext::OutboundPayment { payment_id } = _context;
10753+
if let Err(e) = self.send_payment_for_static_invoice(payment_id, _message.payment_release_secret) {
10754+
log_trace!(
10755+
self.logger, "Failed to release held HTLC with payment id {} and release secret {:02x?}: {:?}",
10756+
payment_id, _message.payment_release_secret, e
10757+
);
10758+
}
10759+
}
10760+
}
1073610761

1073710762
fn release_pending_messages(&self) -> Vec<PendingOnionMessage<AsyncPaymentsMessage>> {
1073810763
core::mem::take(&mut self.pending_async_payments_messages.lock().unwrap())

lightning/src/ln/outbound_payment.rs

+56-1
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,47 @@ impl OutboundPayments {
912912
};
913913
}
914914

915+
#[cfg(async_payments)]
916+
pub(super) fn send_payment_for_static_invoice<R: Deref, ES: Deref, NS: Deref, IH, SP, L: Deref>(
917+
&self, payment_id: PaymentId, payment_release_secret: [u8; 32], router: &R,
918+
first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
919+
best_block_height: u32, logger: &L,
920+
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
921+
send_payment_along_path: SP,
922+
) -> Result<(), Bolt12PaymentError>
923+
where
924+
R::Target: Router,
925+
ES::Target: EntropySource,
926+
NS::Target: NodeSigner,
927+
L::Target: Logger,
928+
IH: Fn() -> InFlightHtlcs,
929+
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
930+
{
931+
let (payment_hash, route_params) =
932+
match self.pending_outbound_payments.lock().unwrap().entry(payment_id) {
933+
hash_map::Entry::Occupied(entry) => match entry.get() {
934+
PendingOutboundPayment::StaticInvoiceReceived {
935+
payment_hash, payment_release_secret: release_secret, route_params, ..
936+
} => {
937+
if payment_release_secret != *release_secret {
938+
return Err(Bolt12PaymentError::UnexpectedInvoice)
939+
}
940+
(*payment_hash, route_params.clone())
941+
},
942+
_ => return Err(Bolt12PaymentError::DuplicateInvoice),
943+
},
944+
hash_map::Entry::Vacant(_) => return Err(Bolt12PaymentError::UnexpectedInvoice),
945+
};
946+
947+
self.find_route_and_send_payment(
948+
payment_hash, payment_id, route_params, router, first_hops, &inflight_htlcs,
949+
entropy_source, node_signer, best_block_height, logger, pending_events,
950+
&send_payment_along_path
951+
);
952+
953+
Ok(())
954+
}
955+
915956
pub(super) fn check_retry_payments<R: Deref, ES: Deref, NS: Deref, SP, IH, FH, L: Deref>(
916957
&self, router: &R, first_hops: FH, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
917958
best_block_height: u32,
@@ -1177,7 +1218,21 @@ impl OutboundPayments {
11771218
*payment.into_mut() = retryable_payment;
11781219
(total_amount, recipient_onion, None, onion_session_privs)
11791220
},
1180-
PendingOutboundPayment::StaticInvoiceReceived { .. } => todo!(),
1221+
PendingOutboundPayment::StaticInvoiceReceived {
1222+
payment_hash, keysend_preimage, retry_strategy, ..
1223+
} => {
1224+
let keysend_preimage = Some(*keysend_preimage);
1225+
let total_amount = route_params.final_value_msat;
1226+
let recipient_onion = RecipientOnionFields::spontaneous_empty();
1227+
let retry_strategy = Some(*retry_strategy);
1228+
let payment_params = Some(route_params.payment_params.clone());
1229+
let (retryable_payment, onion_session_privs) = self.create_pending_payment(
1230+
*payment_hash, recipient_onion.clone(), keysend_preimage, &route,
1231+
retry_strategy, payment_params, entropy_source, best_block_height
1232+
);
1233+
*payment.into_mut() = retryable_payment;
1234+
(total_amount, recipient_onion, keysend_preimage, onion_session_privs)
1235+
},
11811236
PendingOutboundPayment::Fulfilled { .. } => {
11821237
log_error!(logger, "Payment already completed");
11831238
return

0 commit comments

Comments
 (0)