Skip to content

Commit b179026

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

File tree

2 files changed

+86
-8
lines changed

2 files changed

+86
-8
lines changed

lightning/src/ln/channelmanager.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -4075,6 +4075,21 @@ where
40754075
Ok(())
40764076
}
40774077

4078+
#[cfg(async_payments)]
4079+
fn send_payment_for_static_invoice(
4080+
&self, payment_id: PaymentId, payment_release_secret: [u8; 32]
4081+
) -> Result<(), Bolt12PaymentError> {
4082+
let best_block_height = self.best_block.read().unwrap().height;
4083+
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
4084+
self.pending_outbound_payments
4085+
.send_payment_for_static_invoice(
4086+
payment_id, payment_release_secret, &self.router, self.list_usable_channels(),
4087+
|| self.compute_inflight_htlcs(), &self.entropy_source, &self.node_signer,
4088+
best_block_height, &self.logger, &self.pending_events,
4089+
|args| self.send_payment_along_path(args)
4090+
)
4091+
}
4092+
40784093
/// Signals that no further attempts for the given payment should occur. Useful if you have a
40794094
/// pending outbound payment with retries remaining, but wish to stop retrying the payment before
40804095
/// retries are exhausted.
@@ -10466,7 +10481,17 @@ where
1046610481
ResponseInstruction::NoResponse
1046710482
}
1046810483

10469-
fn release_held_htlc(&self, _message: ReleaseHeldHtlc, _payment_id: Option<PaymentId>) {}
10484+
fn release_held_htlc(&self, _message: ReleaseHeldHtlc, _payment_id: Option<PaymentId>) {
10485+
#[cfg(async_payments)] {
10486+
let payment_id = if let Some(id) = _payment_id { id } else { return };
10487+
if let Err(e) = self.send_payment_for_static_invoice(payment_id, _message.payment_release_secret) {
10488+
log_trace!(
10489+
self.logger, "Failed to release held HTLC with payment id {} and release secret {:02x?}: {:?}",
10490+
payment_id, _message.payment_release_secret, e
10491+
);
10492+
}
10493+
}
10494+
}
1047010495

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

lightning/src/ln/outbound_payment.rs

+60-7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ impl PendingOutboundPayment {
186186
}
187187
}
188188

189+
fn keysend_preimage(&self) -> Option<PaymentPreimage> {
190+
match self {
191+
PendingOutboundPayment::StaticInvoiceReceived { keysend_preimage, .. } => Some(*keysend_preimage),
192+
PendingOutboundPayment::Retryable { keysend_preimage, .. } => *keysend_preimage,
193+
PendingOutboundPayment::Legacy { .. } => None,
194+
PendingOutboundPayment::AwaitingInvoice { .. } => None,
195+
PendingOutboundPayment::InvoiceReceived { .. } => None,
196+
PendingOutboundPayment::Fulfilled { .. } => None,
197+
PendingOutboundPayment::Abandoned { .. } => None,
198+
}
199+
}
200+
189201
fn mark_fulfilled(&mut self) {
190202
let mut session_privs = new_hash_set();
191203
core::mem::swap(&mut session_privs, match self {
@@ -898,6 +910,47 @@ impl OutboundPayments {
898910
};
899911
}
900912

913+
#[cfg(async_payments)]
914+
pub(super) fn send_payment_for_static_invoice<R: Deref, ES: Deref, NS: Deref, IH, SP, L: Deref>(
915+
&self, payment_id: PaymentId, payment_release_secret: [u8; 32], router: &R,
916+
first_hops: Vec<ChannelDetails>, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
917+
best_block_height: u32, logger: &L,
918+
pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>,
919+
send_payment_along_path: SP,
920+
) -> Result<(), Bolt12PaymentError>
921+
where
922+
R::Target: Router,
923+
ES::Target: EntropySource,
924+
NS::Target: NodeSigner,
925+
L::Target: Logger,
926+
IH: Fn() -> InFlightHtlcs,
927+
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
928+
{
929+
let (payment_hash, route_params) =
930+
match self.pending_outbound_payments.lock().unwrap().entry(payment_id) {
931+
hash_map::Entry::Occupied(entry) => match entry.get() {
932+
PendingOutboundPayment::StaticInvoiceReceived {
933+
payment_hash, payment_release_secret: release_secret, route_params, ..
934+
} => {
935+
if payment_release_secret != *release_secret {
936+
return Err(Bolt12PaymentError::UnexpectedInvoice)
937+
}
938+
(*payment_hash, route_params.clone())
939+
},
940+
_ => return Err(Bolt12PaymentError::DuplicateInvoice),
941+
},
942+
hash_map::Entry::Vacant(_) => return Err(Bolt12PaymentError::UnexpectedInvoice),
943+
};
944+
945+
self.find_route_and_send_payment(
946+
payment_hash, payment_id, route_params, router, first_hops, &inflight_htlcs,
947+
entropy_source, node_signer, best_block_height, logger, pending_events,
948+
&send_payment_along_path
949+
);
950+
951+
Ok(())
952+
}
953+
901954
pub(super) fn check_retry_payments<R: Deref, ES: Deref, NS: Deref, SP, IH, FH, L: Deref>(
902955
&self, router: &R, first_hops: FH, inflight_htlcs: IH, entropy_source: &ES, node_signer: &NS,
903956
best_block_height: u32,
@@ -1103,10 +1156,10 @@ impl OutboundPayments {
11031156
let mut outbounds = self.pending_outbound_payments.lock().unwrap();
11041157
match outbounds.entry(payment_id) {
11051158
hash_map::Entry::Occupied(mut payment) => {
1159+
let keysend_preimage = payment.get().keysend_preimage();
11061160
match payment.get() {
11071161
PendingOutboundPayment::Retryable {
1108-
total_msat, keysend_preimage, payment_secret, payment_metadata,
1109-
custom_tlvs, pending_amt_msat, ..
1162+
total_msat, payment_secret, payment_metadata, custom_tlvs, pending_amt_msat, ..
11101163
} => {
11111164
const RETRY_OVERFLOW_PERCENTAGE: u64 = 10;
11121165
let retry_amt_msat = route.get_total_amount();
@@ -1128,7 +1181,6 @@ impl OutboundPayments {
11281181
payment_metadata: payment_metadata.clone(),
11291182
custom_tlvs: custom_tlvs.clone(),
11301183
};
1131-
let keysend_preimage = *keysend_preimage;
11321184

11331185
let mut onion_session_privs = Vec::with_capacity(route.paths.len());
11341186
for _ in 0..route.paths.len() {
@@ -1151,7 +1203,9 @@ impl OutboundPayments {
11511203
log_error!(logger, "Payment not yet sent");
11521204
return
11531205
},
1154-
PendingOutboundPayment::InvoiceReceived { payment_hash, retry_strategy, .. } => {
1206+
PendingOutboundPayment::InvoiceReceived { payment_hash, retry_strategy, .. } |
1207+
PendingOutboundPayment::StaticInvoiceReceived { payment_hash, retry_strategy, .. } =>
1208+
{
11551209
let total_amount = route_params.final_value_msat;
11561210
let recipient_onion = RecipientOnionFields {
11571211
payment_secret: None,
@@ -1161,13 +1215,12 @@ impl OutboundPayments {
11611215
let retry_strategy = Some(*retry_strategy);
11621216
let payment_params = Some(route_params.payment_params.clone());
11631217
let (retryable_payment, onion_session_privs) = self.create_pending_payment(
1164-
*payment_hash, recipient_onion.clone(), None, &route,
1218+
*payment_hash, recipient_onion.clone(), keysend_preimage, &route,
11651219
retry_strategy, payment_params, entropy_source, best_block_height
11661220
);
11671221
*payment.into_mut() = retryable_payment;
1168-
(total_amount, recipient_onion, None, onion_session_privs)
1222+
(total_amount, recipient_onion, keysend_preimage, onion_session_privs)
11691223
},
1170-
PendingOutboundPayment::StaticInvoiceReceived { .. } => todo!(),
11711224
PendingOutboundPayment::Fulfilled { .. } => {
11721225
log_error!(logger, "Payment already completed");
11731226
return

0 commit comments

Comments
 (0)