diff --git a/app/src/main/java/com/github/jvsena42/floresta/domain/bitcoin/WalletManager.kt b/app/src/main/java/com/github/jvsena42/floresta/domain/bitcoin/WalletManager.kt index 9929818..b64dc87 100644 --- a/app/src/main/java/com/github/jvsena42/floresta/domain/bitcoin/WalletManager.kt +++ b/app/src/main/java/com/github/jvsena42/floresta/domain/bitcoin/WalletManager.kt @@ -9,6 +9,7 @@ import com.github.jvsena42.floresta.domain.model.TxType import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import org.bitcoindevkit.Address import org.bitcoindevkit.AddressInfo @@ -27,6 +28,7 @@ import org.bitcoindevkit.WordCount import org.rustbitcoin.bitcoin.Amount import org.rustbitcoin.bitcoin.FeeRate import org.rustbitcoin.bitcoin.Network +import kotlin.time.Duration.Companion.seconds import org.bitcoindevkit.ChainPosition as BdkChainPosition class WalletManager( @@ -158,23 +160,31 @@ class WalletManager( florestaRpc.loadDescriptor(descriptor.toString()) } - private fun fullScan() { - val fullScanRequest = wallet.startFullScan().build() - val update: Update = blockchainClient.fullScan( - fullScanRequest = fullScanRequest, - stopGap = 100u, - batchSize = 10u, - fetchPrevTxouts = true - ) - wallet.applyUpdate(update) - wallet.persist(dbConnection) + private suspend fun fullScan() { + Log.d(TAG, "fullScan: ") + try { + val fullScanRequest = wallet.startFullScan().build() + val update: Update = blockchainClient.fullScan( + fullScanRequest = fullScanRequest, + stopGap = 20u, + batchSize = 10u, + fetchPrevTxouts = true + ) + wallet.applyUpdate(update) + wallet.persist(dbConnection) + } catch (e: Exception) { + Log.e(TAG, "fullScan error:", e) + } } - fun sync() { + suspend fun sync() { + if (!walletRepository.doesWalletExist()) return + if (fullScanRequired) { Log.d(TAG, "sync: fullScanRequired") - fullScan() fullScanRequired = false + delay(15.seconds) + fullScan() } else { Log.d(TAG, "sync: normal sync") val syncRequest = wallet.startSyncWithRevealedSpks().build() diff --git a/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt b/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt index b70d157..e16e678 100644 --- a/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt +++ b/app/src/main/java/com/github/jvsena42/floresta/presentation/ui/screens/home/HomeViewModel.kt @@ -72,7 +72,6 @@ class HomeViewModel( delay(5.seconds) updateUI() walletManager.sync() - delay(5.seconds) syncInLoop() } }