Skip to content

Commit 36c4dc9

Browse files
committed
Fix restore LN channel
1 parent 74d7e4b commit 36c4dc9

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

app/src/main/java/to/bitkit/ui/MainActivity.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,17 @@ class MainActivity : FragmentActivity() {
100100
.map { it.walletExists }
101101
.collectAsStateWithLifecycle(initialValue = walletViewModel.walletExists)
102102
val isShowingMigrationLoading by walletViewModel.isShowingMigrationLoading.collectAsStateWithLifecycle()
103+
val restoreState by walletViewModel.restoreState.collectAsStateWithLifecycle()
103104
val hazeState = rememberHazeState(blurEnabled = true)
104105

105106
LaunchedEffect(
106107
walletExists,
107108
isRecoveryMode,
108-
notificationsGranted
109+
notificationsGranted,
110+
restoreState,
109111
) {
110-
if (walletExists && !isRecoveryMode && notificationsGranted) {
112+
val canStartService = walletExists && notificationsGranted && restoreState.isIdle()
113+
if (canStartService && !isRecoveryMode) {
111114
tryStartForegroundService()
112115
}
113116
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import kotlinx.coroutines.flow.update
4646
import kotlinx.coroutines.launch
4747
import kotlinx.coroutines.withContext
4848
import kotlinx.coroutines.withTimeout
49+
import org.lightningdevkit.ldknode.ChannelDataMigration
4950
import org.lightningdevkit.ldknode.Event
5051
import org.lightningdevkit.ldknode.PaymentId
5152
import org.lightningdevkit.ldknode.SpendableUtxo
@@ -351,13 +352,40 @@ class AppViewModel @Inject constructor(
351352
}
352353

353354
private suspend fun completeRNRemoteBackupRestore() {
355+
val channelMigration = buildChannelMigrationIfAvailable()
356+
357+
if (channelMigration != null) {
358+
lightningRepo.stop().onFailure {
359+
Logger.error("Failed to stop node during remote restore restart", it, context = TAG)
360+
}
361+
delay(REMOTE_RESTORE_NODE_RESTART_DELAY_MS)
362+
lightningRepo.start(channelMigration = channelMigration, shouldRetry = false)
363+
.onSuccess {
364+
migrationService.consumePendingChannelMigration()
365+
walletRepo.syncNodeAndWallet()
366+
walletRepo.syncBalances()
367+
}
368+
.onFailure { e ->
369+
Logger.error("Failed to restart node after remote restore: $e", e, context = TAG)
370+
}
371+
}
372+
354373
lightningRepo.getPayments().onSuccess { activityRepo.syncLdkNodePayments(it) }
355374
migrationService.reapplyMetadataAfterSync()
356375
activityRepo.syncActivities()
376+
walletRepo.syncBalances()
357377
migrationService.setRestoringFromRNRemoteBackup(false)
358378
migrationService.setShowingMigrationLoading(false)
359379
}
360380

381+
private fun buildChannelMigrationIfAvailable(): ChannelDataMigration? {
382+
val migration = migrationService.peekPendingChannelMigration() ?: return null
383+
return ChannelDataMigration(
384+
channelManager = migration.channelManager.map { it.toUByte() },
385+
channelMonitors = migration.channelMonitors.map { monitor -> monitor.map { it.toUByte() } },
386+
)
387+
}
388+
361389
private suspend fun completeMigration() {
362390
if (isCompletingMigration) return
363391
isCompletingMigration = true
@@ -1986,6 +2014,7 @@ class AppViewModel @Inject constructor(
19862014
private const val SCREEN_TRANSITION_DELAY_MS = 300L
19872015
private const val MIGRATION_LOADING_TIMEOUT_MS = 300_000L
19882016
private const val MIGRATION_AUTH_RESET_DELAY_MS = 500L
2017+
private const val REMOTE_RESTORE_NODE_RESTART_DELAY_MS = 500L
19892018
private const val AUTH_CHECK_INITIAL_DELAY_MS = 1000L
19902019
private const val AUTH_CHECK_SPLASH_DELAY_MS = 500L
19912020
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ class WalletViewModel @Inject constructor(
275275
) {
276276
lightningRepo.start(walletIndex, channelMigration = channelMigration)
277277
.onSuccess {
278+
if (channelMigration != null) {
279+
migrationService.consumePendingChannelMigration()
280+
}
278281
walletRepo.setWalletExistsState()
279282
walletRepo.syncBalances()
280283
if (_restoreState.value.isIdle()) {

0 commit comments

Comments
 (0)