New: Improve behavior and messaging when an account is missing.

Scanning without an account setup is a programming error and prior to this change it wasted a lot of resources and would always crash eventually. Now, this error is caught sooner and surfaced with a clear message.
This commit is contained in:
Kevin Gorham 2021-05-05 14:26:13 -04:00
parent b82a296278
commit 670ec68cd5
No known key found for this signature in database
GPG Key ID: CCA55602DF49FC38
6 changed files with 27 additions and 13 deletions

View File

@ -375,22 +375,25 @@ class CompactBlockProcessor(
*/
private suspend fun verifySetup() {
// verify that the data is initialized
var error = if (!repository.isInitialized()) {
CompactBlockProcessorException.Uninitialized
} else {
// verify that the server is correct
downloader.getServerInfo().let { info ->
val clientBranch = "%x".format(rustBackend.getBranchIdForHeight(info.blockHeight.toInt()))
val network = rustBackend.network.networkName
when {
!info.matchingNetwork(network) -> MismatchedNetwork(clientNetwork = network, serverNetwork = info.chainName)
!info.matchingConsensusBranchId(clientBranch) -> MismatchedBranch(clientBranch = clientBranch, serverBranch = info.consensusBranchId, networkName = network)
else -> null
var error = when {
!repository.isInitialized() -> CompactBlockProcessorException.Uninitialized
repository.getAccountCount() == 0 -> CompactBlockProcessorException.NoAccount
else -> {
// verify that the server is correct
downloader.getServerInfo().let { info ->
val clientBranch = "%x".format(rustBackend.getBranchIdForHeight(info.blockHeight.toInt()))
val network = rustBackend.network.networkName
when {
!info.matchingNetwork(network) -> MismatchedNetwork(clientNetwork = network, serverNetwork = info.chainName)
!info.matchingConsensusBranchId(clientBranch) -> MismatchedBranch(clientBranch = clientBranch, serverBranch = info.consensusBranchId, networkName = network)
else -> null
}
}
}
}
if (error != null) {
twig("Validating setup prior to scanning . . . ISSUE FOUND! - ${error.javaClass.simpleName}")
// give listener a chance to override
if (onSetupErrorListener?.invoke(error) != true) {
throw error

View File

@ -227,6 +227,9 @@ interface SentDao {
@Dao
interface AccountDao {
@Query("SELECT COUNT(account) FROM accounts")
fun count(): Int
@Query(
"""
SELECT account AS accountId,

View File

@ -83,6 +83,10 @@ sealed class CompactBlockProcessorException(message: String, cause: Throwable? =
" initialized. Verify that the seed phrase was properly created or imported. If so, then this problem" +
" can be fixed by re-importing the wallet."
)
object NoAccount : CompactBlockProcessorException(
"Attempting to scan without an account. This is probably a setup error or a race condition."
)
open class EnhanceTransactionError(message: String, val height: Int, cause: Throwable) : CompactBlockProcessorException(message, cause) {
class EnhanceTxDownloadError(height: Int, cause: Throwable) : EnhanceTransactionError("Error while attempting to download a transaction to enhance", height, cause)
class EnhanceTxDecryptError(height: Int, cause: Throwable) : EnhanceTransactionError("Error while attempting to decrypt and store a transaction to enhance", height, cause)

View File

@ -105,6 +105,8 @@ open class PagedTransactionRepository(
override suspend fun getAccount(accountId: Int): UnifiedAddressAccount? = accounts.findAccountById(accountId)
override suspend fun getAccountCount(): Int = accounts.count()
/**
* Close the underlying database.
*/

View File

@ -86,6 +86,8 @@ interface TransactionRepository {
suspend fun getAccount(accountId: Int): UnifiedAddressAccount?
suspend fun getAccountCount(): Int
//
// Transactions
//

View File

@ -789,7 +789,7 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_scanBlocks(
match scan_cached_blocks(&network, &db_cache, &mut db_data, None) {
Ok(()) => Ok(JNI_TRUE),
Err(e) => Err(format_err!("Error while scanning blocks: {}", e)),
Err(e) => Err(format_err!("Rust error while scanning blocks: {}", e)),
}
});
unwrap_exc_or(&env, res, JNI_FALSE)
@ -884,7 +884,7 @@ pub unsafe extern "C" fn Java_cash_z_ecc_android_sdk_jni_RustBackend_scanBlockBa
match scan_cached_blocks(&network, &db_cache, &mut db_data, Some(limit as u32)) {
Ok(()) => Ok(JNI_TRUE),
Err(e) => Err(format_err!("Error while scanning blocks: {}", e)),
Err(e) => Err(format_err!("Rust error while scanning block batch: {}", e)),
}
});
unwrap_exc_or(&env, res, JNI_FALSE)