Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
a9bdaf2
fix: skip quickpay's node wait when spark covers the amount
jvsena42 Nov 25, 2026
e14b9d6
chore: rename changelog fragment
jvsena42 Nov 25, 2026
646a152
fix: retry a held spark send once the node starts
jvsena42 Nov 25, 2026
8f47458
fix: hold a spark send through a node stop, start failure and wipe
jvsena42 Nov 25, 2026
285f078
fix: keep a held spark send from throwing and from ending early
jvsena42 Nov 25, 2026
bb59960
chore: move a test comment above its annotation
jvsena42 Nov 25, 2026
229581d
fix: keep the spark hold through a node that stops again
jvsena42 Nov 25, 2026
6015762
fix: report quickpay already paid when the send skips dispatch
jvsena42 Nov 26, 2026
09f4a87
Merge branch 'master' into fix/quickpay-skip-node-wait-spark
jvsena42 Nov 27, 2026
a0045ff
fix: show quickpay pending for an earlier send still in flight
jvsena42 Nov 27, 2026
a7ed600
fix: settle a quickpay earlier send from its rows before pending
jvsena42 Nov 27, 2026
0ba8501
fix: keep a quickpay earlier send pending unless its rail settled it
jvsena42 Nov 27, 2026
3128ffa
fix: refund an undispatched quickpay reserve on an earlier success
jvsena42 Nov 27, 2026
1abe3bf
fix: keep a stale quickpay dispatch off a later pay of the invoice
jvsena42 Nov 27, 2026
668f8a9
fix: resolve an earlier failed node send with no live attempt
jvsena42 Nov 27, 2026
57bdfe0
docs: describe failed node sends in the quickpay settled lookup
jvsena42 Nov 27, 2026
d953045
Merge remote-tracking branch 'origin/master' into fix/quickpay-skip-n…
jvsena42 Nov 27, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
272 changes: 190 additions & 82 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Large diffs are not rendered by default.

120 changes: 92 additions & 28 deletions app/src/main/java/to/bitkit/repositories/QuickPayRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import kotlinx.coroutines.withTimeoutOrNull
import org.lightningdevkit.ldknode.Event
import org.lightningdevkit.ldknode.NodeException
import org.lightningdevkit.ldknode.PaymentDetails
import org.lightningdevkit.ldknode.PaymentDirection
Expand Down Expand Up @@ -283,8 +284,10 @@ class QuickPayRepo @Inject constructor(
PreparePayResult.REJECTED -> return
PreparePayResult.RECOVERED -> settleRecovered(invoiceHash)
PreparePayResult.FRESH -> {
dispatchBolt11(invoice, invoiceHash, canDispatch)
awaitCompletionOrPending(invoiceHash)
val job = currentCoroutineContext()[Job]
val op = mutex.withLock { opsByKey[invoiceHash]?.takeIf { it.job === job } } ?: return
dispatchBolt11(invoice, op, canDispatch)
awaitCompletionOrPending(op)
}
}
}
Expand Down Expand Up @@ -432,27 +435,29 @@ class QuickPayRepo @Inject constructor(
}
}

private suspend fun dispatchBolt11(invoice: ResolvedInvoice, invoiceHash: String, canDispatch: () -> Boolean) {
private suspend fun dispatchBolt11(invoice: ResolvedInvoice, op: InFlightOp, canDispatch: () -> Boolean) {
var isAuthorizationLapsed = false
lightningRepo.payInvoiceAuthorizedAtSubmission(invoice.bolt11, sats = null, canDispatch) {
isAuthorizationLapsed = !canDispatch()
!isAuthorizationLapsed && tryMarkDispatched(invoiceHash)
!isAuthorizationLapsed && tryMarkDispatched(op)
}.fold(
onSuccess = { onInvoiceAccepted(invoiceHash, it) },
onSuccess = { onInvoiceAccepted(op, invoice.bolt11, it) },
onFailure = {
when {
isAuthorizationLapsed -> fallBackBeforeDispatch(invoiceHash)
isAuthorizationLapsed -> fallBackBeforeDispatch(op)
// Stopped on the node's queue, after it was marked dispatched but before anything was submitted.
it.isStoppedAtSubmission() -> fallBackBeforeDispatch(invoiceHash, isMarkedDispatched = true)
else -> onInvoiceRejected(invoiceHash, invoice.bolt11, it)
it.isStoppedAtSubmission() -> fallBackBeforeDispatch(op, isMarkedDispatched = true)
else -> onInvoiceRejected(op, invoice.bolt11, it)
}
},
)
}

/** Releases a payment that was never submitted and sends its session to the swipe review. */
private suspend fun fallBackBeforeDispatch(invoiceHash: String, isMarkedDispatched: Boolean = false) {
private suspend fun fallBackBeforeDispatch(op: InFlightOp, isMarkedDispatched: Boolean = false) {
val invoiceHash = op.invoiceHash
mutex.withLock {
if (isSupersededLocked(op)) return@withLock
val current = opsByKey[invoiceHash]
if (current == null || current.dispatched != isMarkedDispatched) return@withLock
spend.release(invoiceHash)
Expand All @@ -461,10 +466,10 @@ class QuickPayRepo @Inject constructor(
}
}

private suspend fun tryMarkDispatched(invoiceHash: String): Boolean {
private suspend fun tryMarkDispatched(op: InFlightOp): Boolean {
val settings = readSettings()
return mutex.withLock {
val current = opsByKey[invoiceHash] ?: return@withLock false
val current = opsByKey[op.invoiceHash]?.takeIf { it === op } ?: return@withLock false
if (current.rejection != null) return@withLock false
val rejection = sendRejectionLocked(current, settings)
if (rejection != null) {
Expand All @@ -487,8 +492,18 @@ class QuickPayRepo @Inject constructor(
return null
}

private suspend fun onInvoiceAccepted(invoiceHash: String, paymentId: String) {
private suspend fun onInvoiceAccepted(op: InFlightOp, paymentRequest: String, paymentId: String) {
val invoiceHash = op.invoiceHash
mutex.withLock {
if (isSupersededLocked(op)) return@withLock
val undispatched = opsByKey[invoiceHash]?.takeIf { !it.dispatched }
if (undispatched != null) {
// Accepted without a dispatch, so an earlier send of the invoice paid it and nothing new went out.
spend.release(invoiceHash)
emitErrorLocked(undispatched, QuickPayAlreadyPaidError(), paymentRequest)
removeOpLocked(undispatched)
return@withLock
}
spend.markSubmitted(invoiceHash, paymentId)
val current = opsByKey[invoiceHash] ?: return@withLock
current.paymentId = paymentId
Expand All @@ -499,30 +514,31 @@ class QuickPayRepo @Inject constructor(
}

private suspend fun onInvoiceRejected(
invoiceHash: String,
op: InFlightOp,
paymentRequest: String,
error: Throwable,
) {
if (error is PaymentAbortedBeforeSend) {
releaseIfNotDispatched(invoiceHash)
releaseIfNotDispatched(op)
return
}
handleDispatchError(invoiceHash, paymentRequest, error)
handleDispatchError(op, paymentRequest, error)
}

private suspend fun awaitCompletionOrPending(invoiceHash: String) {
val current = mutex.withLock { opsByKey[invoiceHash] } ?: return
private suspend fun awaitCompletionOrPending(op: InFlightOp) {
if (mutex.withLock { opsByKey[op.invoiceHash] !== op }) return
withTimeoutOrNull(LightningRepo.SEND_LN_TIMEOUT) {
current.settled.await()
op.settled.await()
}
mutex.withLock {
val live = opsByKey[invoiceHash] ?: return@withLock
emitPendingLocked(live)
if (opsByKey[op.invoiceHash] === op) emitPendingLocked(op)
}
}

private suspend fun releaseIfNotDispatched(invoiceHash: String) {
private suspend fun releaseIfNotDispatched(op: InFlightOp) {
val invoiceHash = op.invoiceHash
mutex.withLock {
if (isSupersededLocked(op)) return@withLock
val current = opsByKey[invoiceHash]
if (current == null || current.dispatched) return@withLock
spend.release(invoiceHash)
Expand Down Expand Up @@ -562,13 +578,15 @@ class QuickPayRepo @Inject constructor(
}

private suspend fun handleDispatchError(
invoiceHash: String,
op: InFlightOp,
paymentRequest: String,
error: Throwable,
) {
val invoiceHash = op.invoiceHash
when (val kind = classifyDispatchError(error)) {
QuickPayDispatchClass.PRE_DISPATCH_REJECTION -> {
mutex.withLock {
if (isSupersededLocked(op)) return@withLock
signalCompletionLocked(
paymentId = null,
paymentHash = invoiceHash,
Expand All @@ -581,7 +599,8 @@ class QuickPayRepo @Inject constructor(
QuickPayDispatchClass.AMBIGUOUS,
-> {
val rows = loadPaymentRows()
mutex.withLock {
val isEarlierSendTracked = mutex.withLock {
if (isSupersededLocked(op)) return@withLock false
settleAmbiguousLocked(
invoiceHash = invoiceHash,
paymentRequest = paymentRequest,
Expand All @@ -590,23 +609,35 @@ class QuickPayRepo @Inject constructor(
duplicate = kind == QuickPayDispatchClass.DUPLICATE_PAYMENT,
)
}
if (isEarlierSendTracked) resolveSettledEarlierSend(invoiceHash)
}
}
}

/** Settles a dispatch error, true when it tracked an earlier send of the invoice that QuickPay didn't make. */
private suspend fun settleAmbiguousLocked(
invoiceHash: String,
paymentRequest: String,
error: Throwable,
rows: List<QuickPayReconcileRow>?,
duplicate: Boolean,
) {
): Boolean {
val op = opsByKey[invoiceHash]
if (!duplicate && op != null && !op.dispatched) {
spend.release(invoiceHash)
emitErrorLocked(op, error, paymentRequest)
removeOpLocked(op)
Comment thread
jvsena42 marked this conversation as resolved.
Resolved
return
if (error !is PaymentPendingException) {
emitErrorLocked(op, error, paymentRequest)
return false
}
// Refused for an earlier send of the invoice, which may have been paid since. A failed row proves nothing,
// as the refusal can be for a retry of it still in flight, so anything else is tracked as pending.
if (rows != null && isEarlierSendPaid(invoiceHash, rows)) {
emitErrorLocked(op, QuickPayAlreadyPaidError(), paymentRequest)
return false
}
emitPendingLocked(op)
return true
}
val record = spend.matching(invoiceHash)
val applied = if (record != null && rows != null) {
Expand All @@ -618,9 +649,9 @@ class QuickPayRepo @Inject constructor(
if (remaining != null) {
op?.dispatched = true
op?.let { emitPendingLocked(it) }
return
return false
}
if (op == null) return
if (op == null) return false
when (applied) {
AmbiguousApply.SUCCEEDED -> emitSuccessLocked(op, feePaidMsat = null)
AmbiguousApply.ALREADY_PAID -> emitErrorLocked(op, QuickPayAlreadyPaidError(), paymentRequest)
Expand All @@ -629,6 +660,29 @@ class QuickPayRepo @Inject constructor(
-> emitErrorLocked(op, error, paymentRequest)
}
removeOpLocked(op)
return false
}

/**
* Resolves the tracked earlier send of [invoiceHash] when its rail shows it settled, as its event may have been
* handled before it was tracked, which no later event resolves.
*/
private suspend fun resolveSettledEarlierSend(invoiceHash: String) {
// Settled only when confirmed: once it succeeded, or once it failed with no attempt of it live on either rail.
val event = runSuspendCatching { lightningRepo.settledSparkSendEvent(invoiceHash) }.getOrNull()
val resolution = when (event) {
is Event.PaymentSuccessful if event.paymentHash == invoiceHash ->
PendingPaymentResolution.Success(invoiceHash)
is Event.PaymentFailed if event.paymentHash == invoiceHash ->
PendingPaymentResolution.Failure(invoiceHash, event.reason)
else -> return
}
if (pendingPaymentRepo.isPending(invoiceHash)) pendingPaymentRepo.resolve(resolution)
}

private fun isEarlierSendPaid(invoiceHash: String, rows: List<QuickPayReconcileRow>): Boolean = rows.any {
it.isOutboundBolt11 && it.status == QuickPayReconcileRow.Status.SUCCEEDED &&
it.invoicePaymentHash.equals(invoiceHash, ignoreCase = true)
}

private suspend fun detachSession(sessionId: String) {
Expand Down Expand Up @@ -668,6 +722,13 @@ class QuickPayRepo @Inject constructor(
if (!success && !isAttributedFailure(record, op, paymentId, paymentHash)) {
return QuickPayCompletionOutcome.None
}
if (success && op != null && !op.dispatched) {
// QuickPay sent nothing yet, so the success is an earlier send's: the invoice is paid, and not by QuickPay.
spend.release(record.invoicePaymentHash)
emitErrorLocked(op, QuickPayAlreadyPaidError(), op.paymentRequest)
removeOpLocked(op)
Comment thread
ovitrif marked this conversation as resolved.
Resolved
return QuickPayCompletionOutcome.None
}

spend.settle(keys, success)

Expand Down Expand Up @@ -806,6 +867,9 @@ class QuickPayRepo @Inject constructor(
op.paymentId?.takeIf { it.isNotBlank() && it != op.invoiceHash }?.let { opsByKey[it] = op }
}

/** True when a later pay registered its own op for this op's invoice, which this op's dispatch must not touch. */
private fun isSupersededLocked(op: InFlightOp): Boolean = opsByKey[op.invoiceHash].let { it != null && it !== op }

private fun removeOpLocked(op: InFlightOp) {
opsByKey.entries.removeAll { it.value === op }
}
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6648,7 +6648,7 @@ class AppViewModel @Inject constructor(
)
if (quickPayHandled) return

waitForUsableChannels(walletGeneration, amount)
waitForUsableChannels(walletGeneration, amount.coerceAtLeast(1u))
if (!canSendLightning(amount)) {
val maxSendLightning = walletRepo.balanceState.value.maxSendLightningSats
val shortfall = amount.safe() - maxSendLightning.safe()
Expand Down Expand Up @@ -6932,8 +6932,12 @@ class AppViewModel @Inject constructor(
val open = invoiceHash != null && quickPayRepo.hasOpen(invoiceHash)
// QuickPay has no confirm screen to show the update fee, so a payment that needs the update is reviewed. While
// the node starts or its channels reconnect their balance is unknown, so that is decided once they are usable.
// An amount Spark covers alone never needs the update, so it does not wait for the node. An amountless
// invoice has no amount to cover yet, so it keeps the wait.
val isQuickPayOn = settingsStore.data.first().isQuickPayEnabled
if (!open && isQuickPayOn && channelsAreSettling()) waitForUsableChannels(walletGeneration)
if (!open && isQuickPayOn && channelsAreSettling()) {
waitForUsableChannels(walletGeneration, amountSats.takeIf { it > 0uL })
}
if (!open && moveToSparkRepo.needsUpdateToSend(amountSats)) return false
val canApply = open || canApplyQuickPay(amountSats)
requireCurrentPaymentPreparation(walletGeneration)
Expand Down Expand Up @@ -8983,7 +8987,11 @@ class AppViewModel @Inject constructor(
outcome(it)?.let { result -> WatchResult.Complete(result) } ?: WatchResult.Continue()
}
}
lightningRepo.settledSparkSendEvent(hash)?.let(::outcome)?.also { watch.cancel() } ?: watch.await()
// A node failure comes with its reason in the watched event, which the payment list does not carry.
lightningRepo.settledSparkSendEvent(hash, includeNodeFailure = false)
?.let(::outcome)
?.also { watch.cancel() }
?: watch.await()
}
return result ?: Result.failure(PaymentPendingException(hash))
}
Expand Down
Loading
Loading