From eddb097b1e0a91848d92861d2be3e6b57034be55 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Sat, 14 Nov 2026 02:33:06 +0100 Subject: [PATCH] fix: spark send mark read before liveness A refused Spark send reads the send record mark before liveness, so a competing send that finishes during the row read keeps it pending instead of refused. --- .../to/bitkit/repositories/LightningRepo.kt | 5 ++-- .../bitkit/repositories/LightningRepoTest.kt | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt index f15d8c59da..03e47b4042 100644 --- a/app/src/main/java/to/bitkit/repositories/LightningRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/LightningRepo.kt @@ -1640,9 +1640,10 @@ class LightningRepo @Inject constructor( key: String, ldkRecordsBefore: Long, ): Result? { - // Read before the row too, so a send that ends while it is read still counts as live. - val wasLive = isSendLive(paymentHash, key, ldkRecordsBefore) + // Read before the row too, so a send that ends while it is read still counts as live. The mark comes first, + // so a send it counts as recorded is already admitted or in flight when liveness is read. val sparkRecordsBefore = sparkRepo.sendRecordMark() + val wasLive = isSendLive(paymentHash, key, ldkRecordsBefore) val send = sparkRepo.lightningSend(paymentHash) as? SparkLightningSend.Found ?: return null val isLive = wasLive || isSendLive(paymentHash, key, ldkRecordsBefore) || sparkRepo.hasRecordedSendSince(sparkRecordsBefore, paymentHash) diff --git a/app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt b/app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt index 4cb87ee779..69ea55609e 100644 --- a/app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt +++ b/app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt @@ -1759,6 +1759,33 @@ class LightningRepoTest : BaseUnitTest() { assertIs(sut.payInvoice("h", 1_000uL).exceptionOrNull(), "a send that started") } + @Test + fun `a spark send refused over a failed spark row stays pending when a send records as its mark is read`() = + test { + startNodeForTesting() + whenever(lightningService.listPayments()).thenReturn(emptyList()) + whenever(sparkRepo.canSend(1_000uL)).thenReturn(true) + stubSparkSendRefusedForARecordedOne() + var isInFlight = false + whenever(sparkRepo.isSendInFlight("h")).thenAnswer { isInFlight } + // The refused send's settling mark read is its second; a competing retry is admitted and records just then. + var marks = 0 + whenever(sparkRepo.sendRecordMark()).thenAnswer { + if (++marks == 2) { + isInFlight = true + recordSparkSend() + } + sparkRecords + } + // The competing retry completes and is released while the older attempt's failed row is read. + whenever(sparkRepo.lightningSend("h")).doSuspendableAnswer { + isInFlight = false + SparkLightningSend.Found(SparkPaymentStatus.FAILED, null) + } + + assertIs(sut.payInvoice("h", 1_000uL).exceptionOrNull()) + } + @Test fun `a spark send refused over a failed spark row stays pending once the node sent the invoice`() = test { startNodeForTesting()