diff --git a/app/src/main/java/to/bitkit/repositories/SparkExitRepo.kt b/app/src/main/java/to/bitkit/repositories/SparkExitRepo.kt index 9f0ed4c8b2..5cd6869cfe 100644 --- a/app/src/main/java/to/bitkit/repositories/SparkExitRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/SparkExitRepo.kt @@ -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 import breez_sdk_spark.PrepareRecoverFundsRequest import breez_sdk_spark.RecoveryTxKind import breez_sdk_spark.singleKeyCpfpSigner @@ -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) } } diff --git a/app/src/test/java/to/bitkit/repositories/SparkExitRepoTest.kt b/app/src/test/java/to/bitkit/repositories/SparkExitRepoTest.kt index 3307dd0d23..a56f59cbda 100644 --- a/app/src/test/java/to/bitkit/repositories/SparkExitRepoTest.kt +++ b/app/src/test/java/to/bitkit/repositories/SparkExitRepoTest.kt @@ -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() + 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)