Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
18ff6d0
fix: reuse a spark send's key across retries
ovitrif Nov 24, 2026
66fc8b3
fix: retry a spark send spark can't select leaves for
ovitrif Nov 24, 2026
5d47da0
fix: retry a refused spark send under a new key
ovitrif Nov 25, 2026
15abcb7
fix: report a spark send failure a retry gets back
ovitrif Nov 25, 2026
363241b
fix: release a failed spark send's invoice
ovitrif Nov 25, 2026
188bf74
test: cover spark send attempt pruning and restore
ovitrif Nov 25, 2026
2095114
fix: keep the node off a refused spark send until spark syncs
ovitrif Nov 25, 2026
a3b2d60
fix: leave payment details out of the restore log
ovitrif Nov 25, 2026
2e951c3
fix: keep a paid spark send paid after a storage error
ovitrif Nov 25, 2026
9647c19
test: cover the refusal of a spark send that may have reached spark
ovitrif Nov 25, 2026
7c7023c
fix: keep spark settlement writes best-effort and the usdt address ou…
ovitrif Nov 25, 2026
e888aa6
fix: keep the node off a refused spark send until its invoice expires
ovitrif Nov 25, 2026
04194a0
fix: treat a spark send refused after a leaf failure as uncertain
ovitrif Nov 25, 2026
4038326
fix: keep a spark send refused after a leaf failure open where it may…
ovitrif Nov 25, 2026
e34b5e6
fix: keep a spark send held when spark can't select its leaves
ovitrif Nov 25, 2026
67e123a
fix: let a spark retry of a held send go out under its key
ovitrif Nov 25, 2026
a14a004
fix: keep a spark send held when a leaf retry reads as unsent
ovitrif Nov 25, 2026
e22f2e7
fix: keep a held spark send's hold when its retry never goes out
ovitrif Nov 25, 2026
b09e44b
fix: keep a saved spark hold through a retry that never goes out
ovitrif Nov 25, 2026
22a8c49
fix: settle a held spark send before a new key's retry
ovitrif Nov 25, 2026
d42fac2
fix: report a held spark send's result to its waiters
ovitrif Nov 25, 2026
9468eda
fix: keep polling a spark hold a retry left in place
ovitrif Nov 25, 2026
868ab19
fix: poll a spark hold once when a retry holds it again
ovitrif Nov 25, 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
27 changes: 27 additions & 0 deletions app/src/main/java/to/bitkit/data/CacheStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,27 @@ class CacheStore internal constructor(
}
}

/**
* Keeps [attempt] as the latest attempt at paying the invoice with [paymentHash], which expires at [expiresAt],
* and drops the attempts of invoices expired before [now], both in epoch seconds.
*/
suspend fun setSparkSendAttempt(paymentHash: String, attempt: Int, expiresAt: Long, now: Long) {
val hash = paymentHash.lowercase()
store.updateData {
val live = it.sparkSendAttempts.filterValues { kept -> kept.expiresAt >= now }
it.copy(sparkSendAttempts = live + (hash to SparkSendAttempt(attempt, expiresAt)))
}
}

/** Puts back [previous], the attempt kept before a send whose key never reached Spark, or drops the invoice's. */
suspend fun restoreSparkSendAttempt(paymentHash: String, previous: SparkSendAttempt?) {
val hash = paymentHash.lowercase()
store.updateData {
val attempts = it.sparkSendAttempts - hash
it.copy(sparkSendAttempts = if (previous == null) attempts else attempts + (hash to previous))
}
}

suspend fun addSparkUnsavedOwnFailure(paymentId: String) {
if (paymentId in store.data.first().sparkUnsavedOwnFailures) return
store.updateData { it.copy(sparkUnsavedOwnFailures = it.sparkUnsavedOwnFailures + paymentId) }
Expand Down Expand Up @@ -413,6 +434,8 @@ data class AppCacheData(
* only its own outcome settles it. A hold without one waits for a paid attempt or its invoice's expiry.
*/
val sparkSendAttemptIds: Map<String, String> = mapOf(),
/** The latest attempt at each invoice Spark sent, by hash, whose number picks the attempt's idempotency key. */
val sparkSendAttempts: Map<String, SparkSendAttempt> = mapOf(),
/**
* Payment ids of Spark send attempts whose own failure a failed Activity write still has to save over an earlier
* attempt's row, kept across a restart until it saves.
Expand Down Expand Up @@ -463,6 +486,10 @@ data class AppCacheData(
fun invalidateReceiveOnchainAddress() = copy(bip21 = "", onchainAddress = "")
}

/** An attempt at paying an invoice through Spark, the [attempt]th, of an invoice that expires at [expiresAt]. */
@Serializable
data class SparkSendAttempt(val attempt: Int, val expiresAt: Long)

@Serializable
data class CachedUsdtBalance(
val address: String,
Expand Down
42 changes: 34 additions & 8 deletions app/src/main/java/to/bitkit/repositories/BackupRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import to.bitkit.R
import to.bitkit.async.appScope
import to.bitkit.data.AppCacheData
import to.bitkit.data.AppDb
import to.bitkit.data.CacheStore
import to.bitkit.data.HwWalletStore
Expand Down Expand Up @@ -1336,14 +1337,7 @@ class BackupRepo @Inject constructor(
}
runCatching { restoreSparkExitEarlierAddresses(parsed.sparkExitEarlierAddresses) }
.onFailure { Logger.warn("Failed to restore the earlier Spark exit addresses", it, context = TAG) }
// Payment history and Spark attempt ids stay out of the log, as support requests attach it.
val logged = parsed.cache.copy(
cachedRates = emptyList(),
moveToSparkHashes = emptyList(),
sparkSendAttemptIds = emptyMap(),
sparkUnsavedOwnFailures = emptySet(),
)
Logger.debug("Restored caches: ${jsonLogOf(logged)}", TAG)
Logger.debug("Restored caches: ${jsonLogOf(parsed.cache.withoutPaymentDetails())}", TAG)
onCacheRestored()
val persisted = preActivityMetadataRepo.upsertPreActivityMetadata(parsed.tagMetadata)
.onFailure { Logger.warn("Failed to restore pre-activity metadata", it, context = TAG) }
Expand Down Expand Up @@ -1720,3 +1714,35 @@ internal fun Throwable.isTemporaryBackupFailure(): Boolean = isPaykitTemporarily
-> true
else -> false
}

/**
* The cache as the restore log shows it: without the addresses, invoices, payment hashes, payment and activity ids,
* transaction ids and comments that identify a payment, as support requests attach the log.
*/
internal fun AppCacheData.withoutPaymentDetails() = copy(
cachedRates = emptyList(),
onchainAddress = "",
bolt11 = "",
bolt11PaymentHash = "",
bip21 = "",
usdtDisplayBalance = null,
deletedActivities = emptyList(),
detachedActivityContacts = emptySet(),
pendingBoostActivities = emptyList(),
backgroundReceive = null,
quickPayLedger = quickPayLedger?.copy(records = emptyList()),
pendingLightningMessages = emptyMap(),
pendingSparkWithdrawal = null,
sentSparkWithdrawals = emptyList(),
unfinishedPubkySwaps = emptyList(),
pubkySwapHoldHashes = emptyMap(),
sparkSendHashes = emptyList(),
sparkSendExpiries = emptyMap(),
sparkUncertainSends = emptyMap(),
sparkSendAttemptIds = emptyMap(),
sparkSendAttempts = emptyMap(),
sparkUnsavedOwnFailures = emptySet(),
moveToSparkHashes = emptyList(),
ldkSendHashes = emptyList(),
ldkSendExpiries = emptyMap(),
)
7 changes: 6 additions & 1 deletion app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,12 @@ class LightningRepo @Inject constructor(
var isAdmitted = false
var isRefused = false
var railBefore: LightningRail? = null
val authorizeSend: suspend () -> Boolean = {
var isChecked = false
val authorizeSend: suspend () -> Boolean = authorize@{
// Spark's retry of a send whose leaves it couldn't select keeps the send's admission and rechecks only
// the caller's check at submission, which has no side effects.
if (isChecked) return@authorize isAuthorizedAtSubmission?.invoke() ?: true
isChecked = true
val isAuthorized = onBeforeSend().also { if (it) requireWalletRestoreComplete() }
if (isAuthorized) {
isAdmitted = admitSend(key, LightningRail.Spark) {
Expand Down
Loading
Loading