Skip to content

Commit 5abdf23

Browse files
Support sending async payments as an always-online sender.
Async receive is not yet supported.
1 parent 89217cb commit 5abdf23

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4284,6 +4284,21 @@ where
42844284
Ok(())
42854285
}
42864286

4287+
#[cfg(async_payments)]
4288+
fn send_payment_for_static_invoice(
4289+
&self, payment_id: PaymentId, payment_release_secret: [u8; 32]
4290+
) -> Result<(), Bolt12PaymentError> {
4291+
let best_block_height = self.best_block.read().unwrap().height;
4292+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4293+
self.pending_outbound_payments
4294+
.send_payment_for_static_invoice(
4295+
payment_id, payment_release_secret, &self.router, self.list_usable_channels(),
4296+
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
4297+
best_block_height, &self.logger, &self.pending_events,
4298+
|args| self.send_payment_along_path(args)
4299+
)
4300+
}
4301+
42874302
/// Signals that no further attempts for the given payment should occur. Useful if you have a
42884303
/// pending outbound payment with retries remaining, but wish to stop retrying the payment before
42894304
/// retries are exhausted.
@@ -10952,7 +10967,17 @@ where
1095210967
ResponseInstruction::NoResponse
1095310968
}
1095410969

10955-
fn release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {}
10970+
fn release_held_htlc(&self, _message: ReleaseHeldHtlc, _context: AsyncPaymentsContext) {
10971+
#[cfg(async_payments)] {
10972+
let AsyncPaymentsContext::OutboundPayment { payment_id } = _context;
10973+
if let Err(e) = self.send_payment_for_static_invoice(payment_id, _message.payment_release_secret) {
10974+
log_trace!(
10975+
self.logger, "Failed to release held HTLC with payment id {} and release secret {:02x?}: {:?}",
10976+
payment_id, _message.payment_release_secret, e
10977+
);
10978+
}
10979+
}
10980+
}
1095610981

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

lightning/src/ln/outbound_payment.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,47 @@ impl OutboundPayments {
956956
};
957957
}
958958

959+
#[cfg(async_payments)]
960+
pub(super) fn send_payment_for_static_invoice<R: Deref, ES: Deref, NS: Deref, IH, SP, L: Deref>(
961+
&self, payment_id: PaymentId, payment_release_secret: [u8; 32], router: &R,
962+
first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
963+
best_block_height: u32, logger: &L,
964+
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
965+
send_payment_along_path: SP,
966+
) -> Result<(), Bolt12PaymentError>
967+
where
968+
R::Target: Router,
969+
ES::Target: EntropySource,
970+
NS::Target: NodeSigner,
971+
L::Target: Logger,
972+
IH: Fn() -> InFlightHtlcs,
973+
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
974+
{
975+
let (payment_hash, route_params) =
976+
match self.pending_outbound_payments.lock().unwrap().entry(payment_id) {
977+
hash_map::Entry::Occupied(entry) => match entry.get() {
978+
PendingOutboundPayment::StaticInvoiceReceived {
979+
payment_hash, payment_release_secret: release_secret, route_params, ..
980+
} => {
981+
if payment_release_secret != *release_secret {
982+
return Err(Bolt12PaymentError::UnexpectedInvoice)
983+
}
984+
(*payment_hash, route_params.clone())
985+
},
986+
_ => return Err(Bolt12PaymentError::DuplicateInvoice),
987+
},
988+
hash_map::Entry::Vacant(_) => return Err(Bolt12PaymentError::UnexpectedInvoice),
989+
};
990+
991+
self.find_route_and_send_payment(
992+
payment_hash, payment_id, route_params, router, first_hops, &inflight_htlcs,
993+
entropy_source, node_signer, best_block_height, logger, pending_events,
994+
&send_payment_along_path
995+
);
996+
997+
Ok(())
998+
}
999+
9591000
pub(super) fn check_retry_payments<R: Deref, ES: Deref, NS: Deref, SP, IH, FH, L: Deref>(
9601001
&self, router: &R, first_hops: FH, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
9611002
best_block_height: u32,
@@ -1235,7 +1276,21 @@ impl OutboundPayments {
12351276
debug_assert!(false);
12361277
return
12371278
},
1238-
PendingOutboundPayment::StaticInvoiceReceived { .. } => todo!(),
1279+
PendingOutboundPayment::StaticInvoiceReceived {
1280+
payment_hash, keysend_preimage, retry_strategy, ..
1281+
} => {
1282+
let keysend_preimage = Some(*keysend_preimage);
1283+
let total_amount = route_params.final_value_msat;
1284+
let recipient_onion = RecipientOnionFields::spontaneous_empty();
1285+
let retry_strategy = Some(*retry_strategy);
1286+
let payment_params = Some(route_params.payment_params.clone());
1287+
let (retryable_payment, onion_session_privs) = self.create_pending_payment(
1288+
*payment_hash, recipient_onion.clone(), keysend_preimage, &route,
1289+
retry_strategy, payment_params, entropy_source, best_block_height
1290+
);
1291+
*payment.into_mut() = retryable_payment;
1292+
(total_amount, recipient_onion, keysend_preimage, onion_session_privs)
1293+
},
12391294
PendingOutboundPayment::Fulfilled { .. } => {
12401295
log_error!(logger, "Payment already completed");
12411296
return

0 commit comments

Comments
 (0)