New: Add quickRewind feature to make it easy to rescan 2wks of blocks.

This helps address https://github.com/nighthawk-apps/nighthawk-wallet-android/issues/38
This commit is contained in:
Kevin Gorham 2021-05-03 22:53:23 -04:00
parent ddf785e7d8
commit 0acb439793
No known key found for this signature in database
GPG Key ID: CCA55602DF49FC38
3 changed files with 20 additions and 3 deletions

View File

@ -310,6 +310,10 @@ class SdkSynchronizer internal constructor(
processor.rewindToNearestHeight(height, alsoClearBlockCache)
}
override suspend fun quickRewind() {
processor.quickRewind()
}
//
// Storage APIs
//

View File

@ -291,6 +291,8 @@ interface Synchronizer {
*/
suspend fun rewindToNearestHeight(height: Int, alsoClearBlockCache: Boolean = false)
suspend fun quickRewind()
//
// Error Handling
//

View File

@ -604,6 +604,16 @@ class CompactBlockProcessor(
}
}
/**
* Rewind back at least two weeks worth of blocks.
*/
suspend fun quickRewind() = withContext(IO) {
val height = max(currentInfo.lastScannedHeight, repository.lastScannedHeight())
val blocksPerDay = 60 * 60 * 24 * 1000 / ZcashSdk.BLOCK_INTERVAL_MILLIS.toInt()
val twoWeeksBack = (height - blocksPerDay * 14).coerceAtLeast(lowerBoundHeight)
rewindToNearestHeight(twoWeeksBack, false)
}
/**
* @param alsoClearBlockCache when true, also clear the block cache which forces a redownload of
* blocks. Otherwise, the cached blocks will be used in the rescan, which in most cases, is fine.
@ -611,12 +621,13 @@ class CompactBlockProcessor(
suspend fun rewindToNearestHeight(height: Int, alsoClearBlockCache: Boolean = false) = withContext(IO) {
processingMutex.withLockLogged("rewindToHeight") {
val lastScannedHeight = currentInfo.lastScannedHeight
val lastLocalBlock = repository.lastScannedHeight()
val targetHeight = getNearestRewindHeight(height)
twig("Rewinding from $lastScannedHeight to requested height: $height using target height: $targetHeight")
if (targetHeight < lastScannedHeight) {
twig("Rewinding from $lastScannedHeight to requested height: $height using target height: $targetHeight with last local block: $lastLocalBlock")
if (targetHeight < lastScannedHeight || (lastScannedHeight == -1 && (targetHeight < lastLocalBlock))) {
rustBackend.rewindToHeight(targetHeight)
} else {
twig("not rewinding dataDb because the last scanned height is $lastScannedHeight which is less than the target height of $targetHeight")
twig("not rewinding dataDb because the last scanned height is $lastScannedHeight and the last local block is $lastLocalBlock both of which are less than the target height of $targetHeight")
}
if (alsoClearBlockCache) {