Deduplicate balance refreshing APIs

This commit is contained in:
Jack Grigg 2024-01-26 21:20:57 +00:00 committed by str4d
parent 1f19360ca3
commit 55d0dd47d8
3 changed files with 19 additions and 102 deletions

View File

@ -3,7 +3,6 @@ package cash.z.ecc.android.sdk.darkside.test
import androidx.test.core.app.ApplicationProvider
import androidx.test.platform.app.InstrumentationRegistry
import cash.z.ecc.android.sdk.ext.Darkside
import cash.z.ecc.android.sdk.model.Account
import cash.z.ecc.android.sdk.model.BlockHeight
import cash.z.ecc.android.sdk.model.ZcashNetwork
import co.electriccoin.lightwallet.client.internal.DarksideApi
@ -204,20 +203,6 @@ class DarksideTestCoordinator(val wallet: TestWallet) {
)
}
}
suspend fun validateBalance(
available: Long = -1,
total: Long = -1,
account: Account
) {
val balance = synchronizer.processor.getBalanceInfo(account)
if (available > 0) {
assertEquals("invalid available balance", available, balance.available)
}
if (total > 0) {
assertEquals("invalid total balance", total, balance.total)
}
}
}
//

View File

@ -363,28 +363,12 @@ class SdkSynchronizer private constructor(
* because of the current limited Orchard support.
*/
suspend fun refreshAllBalances() {
processor.checkAllBalances()
processor.refreshWalletSummary()
// TODO [#682]: refresh orchard balance
// TODO [#682]: https://github.com/zcash/zcash-android-wallet-sdk/issues/682
Twig.warn { "Warning: Orchard balance does not yet refresh. Only some of the plumbing is in place." }
}
/**
* Calculate the latest Sapling balance based on the blocks that have been scanned and transmit this information
* into the [saplingBalances] flow.
*/
suspend fun refreshSaplingBalance() {
processor.checkSaplingBalance()
}
/**
* Calculate the latest Transparent balance based on the blocks that have been scanned and transmit this information
* into the [saplingBalances] flow.
*/
suspend fun refreshTransparentBalance() {
processor.checkTransparentBalance()
}
suspend fun isValidAddress(address: String): Boolean {
return !validateAddress(address).isNotValid
}

View File

@ -20,7 +20,6 @@ import cash.z.ecc.android.sdk.exception.CompactBlockProcessorException.EnhanceTr
import cash.z.ecc.android.sdk.exception.CompactBlockProcessorException.MismatchedNetwork
import cash.z.ecc.android.sdk.exception.InitializeException
import cash.z.ecc.android.sdk.exception.LightWalletException
import cash.z.ecc.android.sdk.exception.RustLayerException
import cash.z.ecc.android.sdk.ext.ZcashSdk
import cash.z.ecc.android.sdk.ext.ZcashSdk.MAX_BACKOFF_INTERVAL
import cash.z.ecc.android.sdk.ext.ZcashSdk.POLL_INTERVAL
@ -49,7 +48,6 @@ import cash.z.ecc.android.sdk.model.Account
import cash.z.ecc.android.sdk.model.BlockHeight
import cash.z.ecc.android.sdk.model.PercentDecimal
import cash.z.ecc.android.sdk.model.WalletBalance
import cash.z.ecc.android.sdk.model.Zatoshi
import cash.z.ecc.android.sdk.model.ZcashNetwork
import co.electriccoin.lightwallet.client.model.BlockHeightUnsafe
import co.electriccoin.lightwallet.client.model.GetAddressUtxosReplyUnsafe
@ -460,15 +458,7 @@ class CompactBlockProcessor internal constructor(
enhanceStartHeight = firstUnenhancedHeight
).collect { batchSyncProgress ->
// Update sync progress and wallet balance
when (val result = getWalletSummary(backend)) {
is GetWalletSummaryResult.Success -> {
val resultProgress = result.scanProgressPercentDecimal()
Twig.info { "Progress from rust: ${resultProgress.decimal}" }
setProgress(resultProgress)
updateAllBalances(result.walletSummary)
}
else -> { /* Do not report the progress and balances in case of any error */ }
}
refreshWalletSummary()
when (batchSyncProgress.resultState) {
SyncingResult.UpdateBirthday -> {
@ -565,15 +555,7 @@ class CompactBlockProcessor internal constructor(
enhanceStartHeight = firstUnenhancedHeight
).map { batchSyncProgress ->
// Update sync progress and wallet balance
when (val result = getWalletSummary(backend)) {
is GetWalletSummaryResult.Success -> {
val resultProgress = result.scanProgressPercentDecimal()
Twig.info { "Progress from rust: ${resultProgress.decimal}" }
setProgress(resultProgress)
updateAllBalances(result.walletSummary)
}
else -> { /* Do not report the progress and balances in case of any error */ }
}
refreshWalletSummary()
when (batchSyncProgress.resultState) {
SyncingResult.UpdateBirthday -> {
@ -733,26 +715,6 @@ class CompactBlockProcessor internal constructor(
transactionStorage.invalidate()
}
/**
* Calculate the latest balances, based on the blocks that have been scanned and transmit this
* information into the related internal flows. Note that the Orchard balance is not supported.
*/
internal suspend fun checkAllBalances() {
checkSaplingBalance()
checkTransparentBalance()
// TODO [#682]: refresh orchard balance
// TODO [#682]: https://github.com/zcash/zcash-android-wallet-sdk/issues/682
}
/**
* Calculate the latest Sapling balance, based on the blocks that have been scanned and transmit this
* information into the internal [saplingBalances] flow.
*/
internal suspend fun checkSaplingBalance() {
Twig.debug { "Checking Sapling balance" }
saplingBalances.value = getBalanceInfo(Account.DEFAULT)
}
/**
* Calculate the latest Transparent balance, based on the blocks that have been scanned and transmit this
* information into the internal [transparentBalances] flow.
@ -776,6 +738,22 @@ class CompactBlockProcessor internal constructor(
checkTransparentBalance()
}
/**
* Refreshes the SDK's wallet summary from the Rust backend, and transmits this information
* into the related internal flows. Note that the Orchard balance is not yet supported.
*/
internal suspend fun refreshWalletSummary() {
when (val result = getWalletSummary(backend)) {
is GetWalletSummaryResult.Success -> {
val resultProgress = result.scanProgressPercentDecimal()
Twig.info { "Progress from rust: ${resultProgress.decimal}" }
setProgress(resultProgress)
updateAllBalances(result.walletSummary)
}
else -> { /* Do not report the progress and balances in case of any error */ }
}
}
sealed class BlockProcessingResult {
object NoBlocksToProcess : BlockProcessingResult()
@ -2118,36 +2096,6 @@ class CompactBlockProcessor internal constructor(
} ?: lowerBoundHeight
}
/**
* Calculates the latest balance info.
*
* @param account the account to check for balance info.
*
* @return an instance of WalletBalance containing information about available and total funds.
*
* @throws RustLayerException.BalanceException if any error occurs while getting the balances via the Rust layer
*/
suspend fun getBalanceInfo(account: Account): WalletBalance {
return runCatching {
val walletSummary = backend.getWalletSummary()
val accountBalance = walletSummary?.accountBalances?.get(account)
// `None` means that the caller has not yet called `updateChainTip` on a
// brand-new wallet, so we can assume the balance is zero.
val saplingBalance =
accountBalance?.sapling ?: WalletBalance(
total = Zatoshi(0L),
available = Zatoshi(0L)
)
Twig.info { "Found total balance: ${saplingBalance.total}" }
Twig.info { "Found available balance: ${saplingBalance.available}" }
saplingBalance
}.onFailure {
Twig.error(it) { "Failed to get balance due to ${it.localizedMessage}" }
}.getOrElse {
throw RustLayerException.BalanceException(it)
}
}
suspend fun getUtxoCacheBalance(address: String): WalletBalance = backend.getDownloadedUtxoBalance(address)
/**