Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 3 additions & 2 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1640,9 +1640,10 @@ class LightningRepo @Inject constructor(
key: String,
ldkRecordsBefore: Long,
): Result<PaymentId>? {
// 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.
Comment thread
ovitrif marked this conversation as resolved.
Resolved
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)
Expand Down
27 changes: 27 additions & 0 deletions app/src/test/java/to/bitkit/repositories/LightningRepoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,33 @@ class LightningRepoTest : BaseUnitTest() {
assertIs<PaymentPendingException>(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<PaymentPendingException>(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()
Expand Down
Loading