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
10 changes: 7 additions & 3 deletions app/src/main/java/to/bitkit/repositories/SparkExitRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import breez_sdk_spark.CpfpFundingKind
import breez_sdk_spark.CpfpFundingStrategy
import breez_sdk_spark.CpfpSigner
import breez_sdk_spark.ExitLeafSelection
import breez_sdk_spark.ExitTransactionStatus
Comment thread
ovitrif marked this conversation as resolved.
Resolved
import breez_sdk_spark.PrepareRecoverFundsRequest
import breez_sdk_spark.RecoveryTxKind
import breez_sdk_spark.singleKeyCpfpSigner
Expand Down Expand Up @@ -384,9 +385,12 @@ class SparkExitRepo @Inject constructor(
}
val transferId = record.transferId ?: return
if (transferRepo.isSettled(transferId).getOrDefault(true)) return
// Leaves the operators could not co-sign are still on their way to savings, before and while they are retried:
// the transfer keeps counting them, less what already confirmed.
if (!record.isActive || (sweepActivity != null && !record.hasLeavesOutstanding)) {
// Leaves the operators could not co-sign are still on their way to savings, before and while they are retried,
// as are cooperative steps not yet in a block: the transfer keeps counting them, less what already confirmed.
val isCooperativePending = record.recovery?.transactions.orEmpty().any {
it.kind == RecoveryTxKind.COOPERATIVE && it.status !is ExitTransactionStatus.Confirmed
}
if (!record.isActive || (sweepActivity != null && !record.hasLeavesOutstanding && !isCooperativePending)) {
transferRepo.markSettled(transferId)
}
}
Expand Down
39 changes: 39 additions & 0 deletions app/src/test/java/to/bitkit/repositories/SparkExitRepoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,45 @@ class SparkExitRepoTest : BaseUnitTest() {
verify(transferRepo).markSettled("transfer-1")
}

@Test
fun `a landed sweep keeps the transfer while a cooperative step is still on its way`() = test {
val sweep = SparkExitFixtures.tx(
"s",
kind = RecoveryTxKind.SWEEP,
status = ExitTransactionStatus.Confirmed(50u),
)
fun cooperative(status: ExitTransactionStatus) =
SparkExitFixtures.tx("c", kind = RecoveryTxKind.COOPERATIVE, status = status)
val running = SparkExitFixtures.record(Phase.Running)
val activity = OnchainActivity.create(
walletId = "wallet0",
id = "sweep",
txType = PaymentType.RECEIVED,
txId = "s",
value = 90_000uL,
fee = 0uL,
address = "bcrt1qdestination",
timestamp = 0uL,
feeRate = 1uL,
).copy(isTransfer = true)
val activityRepo = mock<ActivityRepo>()
whenever(activityRepo.getOnchainActivityByTxId(any(), any())).thenReturn(activity)
whenever(transferRepo.activeTransfers).thenReturn(flowOf(listOf(transfer("transfer-1"))))
whenever(transferRepo.isSettled(any())).thenReturn(Result.success(false))
whenever(transferRepo.markSettled(any())).thenReturn(Result.success(Unit))
val store = SparkExitStore(File(Env.sparkExitRecordPath(0)))

val pending = SparkExitFixtures.recovery(listOf(sweep, cooperative(ExitTransactionStatus.Ready)))
store.save(running.adopt(pending, 0L))
createSut(activityRepo = activityRepo).load().getOrThrow()
verify(transferRepo, never()).markSettled(any())

val landed = SparkExitFixtures.recovery(listOf(sweep, cooperative(ExitTransactionStatus.Confirmed(51u))))
store.save(running.adopt(landed, 0L))
createSut(activityRepo = activityRepo).load().getOrThrow()
verify(transferRepo).markSettled("transfer-1")
}

@Test
fun `a sweep seen before failed leaves are retried keeps the transfer`() = test {
val sweep = SparkExitFixtures.tx("s", kind = RecoveryTxKind.SWEEP)
Expand Down
Loading