Skip to content

Commit d055fda

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

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
@@ -4280,6 +4280,21 @@ where
42804280
Ok(())
42814281
}
42824282

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

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

1095510980
fn release_pending_messages(&self) -> Vec<PendingOnionMessage<AsyncPaymentsMessage>> {
1095610981
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
@@ -916,6 +916,47 @@ impl OutboundPayments {
916916
};
917917
}
918918

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

0 commit comments

Comments
 (0)