Skip to content

Commit e9c2b3f

Browse files
committed
Merge branch 'fix/get-detail-by-tx-id' into fix/tag-syncing
2 parents 2c1f6af + aac5ba4 commit e9c2b3f

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

app/src/main/java/to/bitkit/repositories/ActivityRepo.kt

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import com.synonym.bitkitcore.ActivityFilter
66
import com.synonym.bitkitcore.PaymentType
77
import com.synonym.bitkitcore.SortDirection
88
import kotlinx.coroutines.CoroutineDispatcher
9-
import kotlinx.coroutines.delay
9+
import kotlinx.coroutines.TimeoutCancellationException
10+
import kotlinx.coroutines.flow.MutableStateFlow
1011
import kotlinx.coroutines.flow.first
1112
import kotlinx.coroutines.flow.map
1213
import kotlinx.coroutines.withContext
14+
import kotlinx.coroutines.withTimeout
1315
import org.lightningdevkit.ldknode.PaymentDetails
1416
import to.bitkit.data.CacheStore
1517
import to.bitkit.data.dto.InProgressTransfer
@@ -22,7 +24,8 @@ import to.bitkit.services.CoreService
2224
import to.bitkit.utils.Logger
2325
import javax.inject.Inject
2426
import javax.inject.Singleton
25-
import kotlin.time.Duration.Companion.seconds
27+
28+
private const val SYNC_TIMEOUT_MS = 40_000L
2629

2730
@Singleton
2831
class ActivityRepo @Inject constructor(
@@ -31,39 +34,49 @@ class ActivityRepo @Inject constructor(
3134
private val lightningRepo: LightningRepo,
3235
private val cacheStore: CacheStore,
3336
) {
34-
35-
var isSyncingLdkNodePayments = false
37+
var isSyncingLdkNodePayments = MutableStateFlow(false)
38+
private set
3639

3740
val inProgressTransfers = cacheStore.data.map { it.inProgressTransfers }
3841

3942
suspend fun syncActivities(): Result<Unit> = withContext(bgDispatcher) {
4043
Logger.debug("syncActivities called", context = TAG)
4144

4245
return@withContext runCatching {
43-
if (isSyncingLdkNodePayments) {
44-
Logger.warn("LDK-node payments are already being synced, skipping", context = TAG)
45-
return@withContext Result.failure(Exception())
46+
withTimeout(SYNC_TIMEOUT_MS) {
47+
Logger.debug("isSyncingLdkNodePayments = ${isSyncingLdkNodePayments.value}", context = TAG)
48+
isSyncingLdkNodePayments.first { !it }
4649
}
4750

48-
deletePendingActivities()
51+
isSyncingLdkNodePayments = MutableStateFlow(true)
4952

50-
isSyncingLdkNodePayments = true
53+
deletePendingActivities()
5154
return@withContext lightningRepo.getPayments()
5255
.onSuccess { payments ->
5356
Logger.debug("Got payments with success, syncing activities", context = TAG)
5457
syncLdkNodePayments(payments = payments)
5558
updateActivitiesMetadata()
5659
boostPendingActivities()
5760
updateInProgressTransfers()
58-
isSyncingLdkNodePayments = false
61+
isSyncingLdkNodePayments = MutableStateFlow(false)
5962
return@withContext Result.success(Unit)
6063
}.onFailure { e ->
6164
Logger.error("Failed to sync ldk-node payments", e, context = TAG)
62-
isSyncingLdkNodePayments = false
65+
isSyncingLdkNodePayments = MutableStateFlow(false)
6366
return@withContext Result.failure(e)
6467
}.map { Unit }
6568
}.onFailure { e ->
66-
Logger.error("syncLdkNodePayments error", e, context = TAG)
69+
when (e) {
70+
is TimeoutCancellationException -> {
71+
isSyncingLdkNodePayments = MutableStateFlow(false)
72+
Logger.error("Timeout waiting for sync to complete, forcing reset", e, context = TAG)
73+
}
74+
75+
else -> {
76+
isSyncingLdkNodePayments = MutableStateFlow(false)
77+
Logger.error("syncActivities error", e, context = TAG)
78+
}
79+
}
6780
}
6881
}
6982

@@ -127,9 +140,6 @@ class ActivityRepo @Inject constructor(
127140
"activity with paymentHashOrTxId:$paymentHashOrTxId not found, trying again after sync",
128141
context = TAG
129142
)
130-
Logger.debug("5 seconds delay", context = TAG)
131-
delay(5.seconds)
132-
Logger.debug("Syncing LN node called", context = TAG)
133143

134144
lightningRepo.sync().onSuccess {
135145
Logger.debug("Syncing LN node SUCCESS", context = TAG)

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,9 @@ class LightningRepo @Inject constructor(
258258
suspend fun sync(): Result<Unit> = executeWhenNodeRunning("Sync") {
259259
syncState()
260260
if (_lightningState.value.isSyncingWallet) {
261-
Logger.warn("Sync already in progress, waiting for existing sync.")
262-
return@executeWhenNodeRunning Result.success(Unit)
261+
Logger.warn("Sync already in progress, waiting for existing sync.", context = TAG)
263262
}
263+
_lightningState.first { !it.isSyncingWallet }
264264

265265
_lightningState.update { it.copy(isSyncingWallet = true) }
266266
lightningService.sync()

app/src/main/java/to/bitkit/ui/screens/wallets/activity/ActivityDetailScreen.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import to.bitkit.ui.theme.AppThemeSurface
7171
import to.bitkit.ui.theme.Colors
7272
import to.bitkit.ui.utils.copyToClipboard
7373
import to.bitkit.ui.utils.getScreenTitleRes
74+
import to.bitkit.utils.Logger
7475
import to.bitkit.viewmodels.ActivityDetailViewModel
7576
import to.bitkit.viewmodels.ActivityListViewModel
7677

@@ -85,7 +86,11 @@ fun ActivityDetailScreen(
8586
) {
8687
val activities by listViewModel.filteredActivities.collectAsStateWithLifecycle()
8788
val item = activities?.find { it.rawId() == route.id }
88-
?: return
89+
if (item == null) {
90+
Logger.error("Activity not found")
91+
return
92+
}
93+
8994

9095
val app = appViewModel ?: return
9196
val copyToastTitle = stringResource(R.string.common__copied)

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,7 @@ class AppViewModel @Inject constructor(
944944
)
945945
)
946946
)
947+
lightningRepo.sync()
947948
}.onFailure { e ->
948949
Logger.error(msg = "Error sending onchain payment", e = e, context = TAG)
949950
toast(

app/src/test/java/to/bitkit/repositories/ActivityRepoTest.kt

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,7 @@ class ActivityRepoTest : BaseUnitTest() {
8686
assertTrue(result.isSuccess)
8787
verify(lightningRepo).getPayments()
8888
verify(coreService.activity).syncLdkNodePayments(payments, forceUpdate = false)
89-
assertFalse(sut.isSyncingLdkNodePayments)
90-
}
91-
92-
@Test
93-
fun `syncActivities skips when already syncing`() = test {
94-
sut.isSyncingLdkNodePayments = true
95-
96-
val result = sut.syncActivities()
97-
98-
assertTrue(result.isFailure)
99-
verify(lightningRepo, never()).getPayments()
89+
assertFalse(sut.isSyncingLdkNodePayments.value)
10090
}
10191

10292
@Test
@@ -108,7 +98,7 @@ class ActivityRepoTest : BaseUnitTest() {
10898

10999
assertTrue(result.isFailure)
110100
assertEquals(exception, result.exceptionOrNull())
111-
assertFalse(sut.isSyncingLdkNodePayments)
101+
assertFalse(sut.isSyncingLdkNodePayments.value)
112102
}
113103

114104
@Test

0 commit comments

Comments
 (0)