From 4d655b0f040a50e844ab99ef242f69aaf8f8db3a Mon Sep 17 00:00:00 2001 From: Kevin Gorham Date: Tue, 11 Feb 2020 20:00:35 -0500 Subject: [PATCH] Add logic to validate processor setup. --- .../java/cash/z/wallet/sdk/block/CompactBlockProcessor.kt | 6 +++++- src/main/java/cash/z/wallet/sdk/exception/Exceptions.kt | 7 +++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/main/java/cash/z/wallet/sdk/block/CompactBlockProcessor.kt b/src/main/java/cash/z/wallet/sdk/block/CompactBlockProcessor.kt index bd2e825f..a37b14b1 100644 --- a/src/main/java/cash/z/wallet/sdk/block/CompactBlockProcessor.kt +++ b/src/main/java/cash/z/wallet/sdk/block/CompactBlockProcessor.kt @@ -113,8 +113,8 @@ class CompactBlockProcessor( * return the block height where an error was found. */ private suspend fun processNewBlocks(): Int = withContext(IO) { + verifySetup() twig("beginning to process new blocks (with lower bound: $lowerBoundHeight)...") - // Get the latest info (but don't transmit it on the channel) and then use that to update the scan/download ranges ProcessorInfo( networkBlockHeight = downloader.getLatestBlockHeight(), @@ -158,6 +158,10 @@ class CompactBlockProcessor( } + private fun verifySetup() { + if (!repository.isInitialized()) throw CompactBlockProcessorException.Uninitialized + } + @VisibleForTesting //allow mocks to verify how this is called, rather than the downloader, which is more complex internal suspend fun downloadNewBlocks(range: IntRange) = withContext(IO) { if (range.isEmpty()) { diff --git a/src/main/java/cash/z/wallet/sdk/exception/Exceptions.kt b/src/main/java/cash/z/wallet/sdk/exception/Exceptions.kt index 22b4122a..222b4421 100644 --- a/src/main/java/cash/z/wallet/sdk/exception/Exceptions.kt +++ b/src/main/java/cash/z/wallet/sdk/exception/Exceptions.kt @@ -41,6 +41,9 @@ sealed class CompactBlockProcessorException(message: String, cause: Throwable? = class FailedReorgRepair(message: String) : CompactBlockProcessorException(message) object FailedScan : CompactBlockProcessorException("Error while scanning blocks. This most " + "likely means a block is missing or a reorg was mishandled. See Rust logs for details.") + object Uninitialized : CompactBlockProcessorException("Cannot process blocks because the wallet has not been" + + " initialized. Verify that the seed phrase was properly created or imported. If so, then this problem" + + " can be fixed by re-importing the wallet.") } sealed class CompactBlockStreamException(message: String, cause: Throwable? = null) : SdkException(message, cause) { @@ -77,8 +80,8 @@ sealed class InitializerException(message: String, cause: Throwable? = null) : " because it already exists in $dbPath", cause) object DatabasePathException : InitializerException("Critical failure to locate path for storing databases. Perhaps this" + - " device prevents apps from storing data? We cannot manage initialize the wallet" + - " unless we can store data.") + " device prevents apps from storing data? We cannot initialize the wallet unless" + + " we can store data.") } sealed class LightwalletException(message: String, cause: Throwable? = null) : SdkException(message, cause) {