Skip to content

Commit

Permalink
fix: add delay to full scan
Browse files Browse the repository at this point in the history
  • Loading branch information
jvsena42 committed Oct 28, 2024
1 parent cabd7a5 commit 9607995
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class HomeViewModel(
delay(5.seconds)
updateUI()
walletManager.sync()
delay(5.seconds)
syncInLoop()
}
}
Expand Down

0 comments on commit 9607995

Please sign in to comment.