2019-06-14 16:24:52 -07:00
|
|
|
package cash.z.wallet.sdk.block
|
|
|
|
|
|
|
|
import androidx.annotation.VisibleForTesting
|
|
|
|
import cash.z.wallet.sdk.annotation.OpenForTesting
|
|
|
|
import cash.z.wallet.sdk.data.TransactionRepository
|
|
|
|
import cash.z.wallet.sdk.data.Twig
|
|
|
|
import cash.z.wallet.sdk.data.twig
|
|
|
|
import cash.z.wallet.sdk.exception.CompactBlockProcessorException
|
2019-09-26 09:58:37 -07:00
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.DOWNLOAD_BATCH_SIZE
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.MAX_BACKOFF_INTERVAL
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.MAX_REORG_SIZE
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.POLL_INTERVAL
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.RETRIES
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.REWIND_DISTANCE
|
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk.SAPLING_ACTIVATION_HEIGHT
|
|
|
|
import cash.z.wallet.sdk.ext.retryUpTo
|
|
|
|
import cash.z.wallet.sdk.ext.retryWithBackoff
|
2019-06-14 16:24:52 -07:00
|
|
|
import cash.z.wallet.sdk.jni.RustBackend
|
|
|
|
import cash.z.wallet.sdk.jni.RustBackendWelding
|
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
|
|
|
import kotlinx.coroutines.channels.ConflatedBroadcastChannel
|
|
|
|
import kotlinx.coroutines.channels.ReceiveChannel
|
|
|
|
import kotlinx.coroutines.delay
|
|
|
|
import kotlinx.coroutines.isActive
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger
|
2019-09-26 09:58:37 -07:00
|
|
|
import kotlin.math.max
|
|
|
|
import kotlin.math.min
|
|
|
|
import kotlin.math.roundToInt
|
2019-06-14 16:24:52 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Responsible for processing the compact blocks that are received from the lightwallet server. This class encapsulates
|
|
|
|
* all the business logic required to validate and scan the blockchain and is therefore tightly coupled with
|
|
|
|
* librustzcash.
|
|
|
|
*/
|
|
|
|
@OpenForTesting
|
|
|
|
class CompactBlockProcessor(
|
|
|
|
internal val downloader: CompactBlockDownloader,
|
|
|
|
private val repository: TransactionRepository,
|
2019-09-26 09:58:37 -07:00
|
|
|
private val rustBackend: RustBackendWelding
|
2019-06-14 16:24:52 -07:00
|
|
|
) {
|
2019-06-17 02:01:29 -07:00
|
|
|
var onErrorListener: ((Throwable) -> Boolean)? = null
|
|
|
|
var isConnected: Boolean = false
|
|
|
|
var isSyncing: Boolean = false
|
|
|
|
var isScanning: Boolean = false
|
2019-06-19 14:52:15 -07:00
|
|
|
private val progressChannel = ConflatedBroadcastChannel(0)
|
2019-06-14 16:24:52 -07:00
|
|
|
private var isStopped = false
|
2019-06-17 02:01:29 -07:00
|
|
|
private val consecutiveChainErrors = AtomicInteger(0)
|
2019-06-14 16:24:52 -07:00
|
|
|
|
|
|
|
fun progress(): ReceiveChannel<Int> = progressChannel.openSubscription()
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download compact blocks, verify and scan them.
|
|
|
|
*/
|
|
|
|
suspend fun start() = withContext(IO) {
|
|
|
|
twig("processor starting")
|
|
|
|
|
|
|
|
// using do/while makes it easier to execute exactly one loop which helps with testing this processor quickly
|
2019-06-17 02:01:29 -07:00
|
|
|
// (because you can start and then immediately set isStopped=true to always get precisely one loop)
|
2019-06-14 16:24:52 -07:00
|
|
|
do {
|
2019-09-26 09:58:37 -07:00
|
|
|
retryWithBackoff(::onConnectionError, maxDelayMillis = MAX_BACKOFF_INTERVAL) {
|
2019-06-14 16:24:52 -07:00
|
|
|
val result = processNewBlocks()
|
|
|
|
// immediately process again after failures in order to download new blocks right away
|
|
|
|
if (result < 0) {
|
2019-06-17 02:01:29 -07:00
|
|
|
isSyncing = false
|
|
|
|
isScanning = false
|
|
|
|
consecutiveChainErrors.set(0)
|
2019-09-26 09:58:37 -07:00
|
|
|
twig("Successfully processed new blocks. Sleeping for ${POLL_INTERVAL}ms")
|
|
|
|
delay(POLL_INTERVAL)
|
2019-06-14 16:24:52 -07:00
|
|
|
} else {
|
2019-09-26 09:58:37 -07:00
|
|
|
if(consecutiveChainErrors.get() >= RETRIES) {
|
2019-06-17 02:01:29 -07:00
|
|
|
val errorMessage = "ERROR: unable to resolve reorg at height $result after ${consecutiveChainErrors.get()} correction attempts!"
|
2019-06-14 16:24:52 -07:00
|
|
|
fail(CompactBlockProcessorException.FailedReorgRepair(errorMessage))
|
|
|
|
} else {
|
|
|
|
handleChainError(result)
|
|
|
|
}
|
2019-06-17 02:01:29 -07:00
|
|
|
consecutiveChainErrors.getAndIncrement()
|
2019-06-14 16:24:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (isActive && !isStopped)
|
|
|
|
twig("processor complete")
|
|
|
|
stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun stop() {
|
|
|
|
isStopped = true
|
|
|
|
}
|
|
|
|
|
|
|
|
fun fail(error: Throwable) {
|
|
|
|
stop()
|
|
|
|
twig("${error.message}")
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process new blocks returning false whenever an error was found.
|
|
|
|
*
|
|
|
|
* @return -1 when processing was successful and did not encounter errors during validation or scanning. Otherwise
|
|
|
|
* return the block height where an error was found.
|
|
|
|
*/
|
2019-06-19 14:52:15 -07:00
|
|
|
private suspend fun processNewBlocks(): Int = withContext(IO) {
|
2019-06-14 16:24:52 -07:00
|
|
|
twig("beginning to process new blocks...")
|
|
|
|
|
|
|
|
// define ranges
|
|
|
|
val latestBlockHeight = downloader.getLatestBlockHeight()
|
2019-06-17 02:01:29 -07:00
|
|
|
isConnected = true // no exception on downloader call
|
|
|
|
isSyncing = true
|
2019-09-26 09:58:37 -07:00
|
|
|
val lastDownloadedHeight = max(getLastDownloadedHeight(), SAPLING_ACTIVATION_HEIGHT - 1)
|
2019-06-14 16:24:52 -07:00
|
|
|
val lastScannedHeight = getLastScannedHeight()
|
|
|
|
|
|
|
|
twig("latestBlockHeight: $latestBlockHeight\tlastDownloadedHeight: $lastDownloadedHeight" +
|
|
|
|
"\tlastScannedHeight: $lastScannedHeight")
|
|
|
|
|
|
|
|
// as long as the database has the sapling tree (like when it's initialized from a checkpoint) we can avoid
|
|
|
|
// downloading earlier blocks so take the larger of these two numbers
|
2019-09-26 09:58:37 -07:00
|
|
|
val rangeToDownload = (max(lastDownloadedHeight, lastScannedHeight) + 1)..latestBlockHeight
|
2019-06-14 16:24:52 -07:00
|
|
|
val rangeToScan = (lastScannedHeight + 1)..latestBlockHeight
|
|
|
|
|
|
|
|
downloadNewBlocks(rangeToDownload)
|
|
|
|
val error = validateNewBlocks(rangeToScan)
|
2019-06-19 14:52:15 -07:00
|
|
|
if (error < 0) {
|
2019-09-26 09:58:37 -07:00
|
|
|
// in theory, a scan should not fail after validation succeeds but maybe consider
|
|
|
|
// changing the rust layer to return the failed block height whenever scan does fail
|
|
|
|
// rather than a boolean
|
|
|
|
val success = scanNewBlocks(rangeToScan)
|
|
|
|
if (!success) throw CompactBlockProcessorException.FailedScan
|
|
|
|
-1
|
2019-06-14 16:24:52 -07:00
|
|
|
} else {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@VisibleForTesting //allow mocks to verify how this is called, rather than the downloader, which is more complex
|
2019-06-19 14:52:15 -07:00
|
|
|
internal suspend fun downloadNewBlocks(range: IntRange) = withContext<Unit>(IO) {
|
2019-06-14 16:24:52 -07:00
|
|
|
if (range.isEmpty()) {
|
|
|
|
twig("no blocks to download")
|
2019-06-19 14:52:15 -07:00
|
|
|
} else {
|
|
|
|
Twig.sprout("downloading")
|
|
|
|
twig("downloading blocks in range $range")
|
|
|
|
|
2019-09-26 09:58:37 -07:00
|
|
|
var downloadedBlockHeight = range.first
|
2019-06-19 14:52:15 -07:00
|
|
|
val missingBlockCount = range.last - range.first + 1
|
2019-09-26 09:58:37 -07:00
|
|
|
val batches = (missingBlockCount / DOWNLOAD_BATCH_SIZE
|
|
|
|
+ (if (missingBlockCount.rem(DOWNLOAD_BATCH_SIZE) == 0) 0 else 1))
|
2019-06-19 14:52:15 -07:00
|
|
|
var progress: Int
|
2019-09-26 09:58:37 -07:00
|
|
|
twig("found $missingBlockCount missing blocks, downloading in $batches batches of ${DOWNLOAD_BATCH_SIZE}...")
|
2019-06-19 14:52:15 -07:00
|
|
|
for (i in 1..batches) {
|
2019-09-26 09:58:37 -07:00
|
|
|
retryUpTo(RETRIES) {
|
|
|
|
val end = min(range.first + (i * DOWNLOAD_BATCH_SIZE), range.last + 1)
|
|
|
|
twig("downloaded $downloadedBlockHeight..${(end - 1)} (batch $i of $batches)") {
|
|
|
|
downloader.downloadBlockRange(downloadedBlockHeight until end)
|
2019-06-19 14:52:15 -07:00
|
|
|
}
|
2019-09-26 09:58:37 -07:00
|
|
|
progress = (i / batches.toFloat() * 100).roundToInt()
|
2019-06-19 14:52:15 -07:00
|
|
|
// only report during large downloads. TODO: allow for configuration of "large"
|
2019-06-20 01:37:48 -07:00
|
|
|
progressChannel.send(progress)
|
2019-06-19 14:52:15 -07:00
|
|
|
downloadedBlockHeight = end
|
2019-06-14 16:24:52 -07:00
|
|
|
}
|
|
|
|
}
|
2019-06-19 14:52:15 -07:00
|
|
|
Twig.clip("downloading")
|
2019-06-14 16:24:52 -07:00
|
|
|
}
|
2019-06-20 01:37:48 -07:00
|
|
|
progressChannel.send(100)
|
2019-06-14 16:24:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun validateNewBlocks(range: IntRange?): Int {
|
|
|
|
if (range?.isEmpty() != false) {
|
|
|
|
twig("no blocks to validate: $range")
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
Twig.sprout("validating")
|
|
|
|
twig("validating blocks in range $range")
|
2019-09-26 09:58:37 -07:00
|
|
|
val result = rustBackend.validateCombinedChain()
|
2019-06-14 16:24:52 -07:00
|
|
|
Twig.clip("validating")
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun scanNewBlocks(range: IntRange?): Boolean {
|
|
|
|
if (range?.isEmpty() != false) {
|
|
|
|
twig("no blocks to scan")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
Twig.sprout("scanning")
|
|
|
|
twig("scanning blocks in range $range")
|
2019-06-17 02:01:29 -07:00
|
|
|
isScanning = true
|
2019-09-26 09:58:37 -07:00
|
|
|
val result = rustBackend.scanBlocks()
|
2019-06-17 02:01:29 -07:00
|
|
|
isScanning = false
|
2019-06-14 16:24:52 -07:00
|
|
|
Twig.clip("scanning")
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
private suspend fun handleChainError(errorHeight: Int) = withContext(IO) {
|
|
|
|
val lowerBound = determineLowerBound(errorHeight)
|
|
|
|
twig("handling chain error at $errorHeight by rewinding to block $lowerBound")
|
2019-09-26 09:58:37 -07:00
|
|
|
rustBackend.rewindToHeight(lowerBound)
|
2019-06-14 16:24:52 -07:00
|
|
|
downloader.rewindTo(lowerBound)
|
|
|
|
}
|
|
|
|
|
2019-06-17 02:01:29 -07:00
|
|
|
private fun onConnectionError(throwable: Throwable): Boolean {
|
|
|
|
isConnected = false
|
|
|
|
isSyncing = false
|
|
|
|
isScanning = false
|
|
|
|
return onErrorListener?.invoke(throwable) ?: true
|
|
|
|
}
|
|
|
|
|
2019-06-14 16:24:52 -07:00
|
|
|
private fun determineLowerBound(errorHeight: Int): Int {
|
2019-09-26 09:58:37 -07:00
|
|
|
val offset = Math.min(MAX_REORG_SIZE, REWIND_DISTANCE * (consecutiveChainErrors.get() + 1))
|
2019-06-14 16:24:52 -07:00
|
|
|
return Math.max(errorHeight - offset, SAPLING_ACTIVATION_HEIGHT)
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun getLastDownloadedHeight() = withContext(IO) {
|
|
|
|
downloader.getLastDownloadedHeight()
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun getLastScannedHeight() = withContext(IO) {
|
|
|
|
repository.lastScannedHeight()
|
|
|
|
}
|
2019-09-26 09:58:37 -07:00
|
|
|
}
|