Skip to content

Commit

Permalink
Merge pull request #1356 from Electric-Coin-Company/perf-and-summary-…
Browse files Browse the repository at this point in the history
…updates

Migrate to in-progress version of FFI backend 0.5.0
  • Loading branch information
str4d authored Jan 29, 2024
2 parents 06d2b69 + ded20fe commit 2a8f6c2
Show file tree
Hide file tree
Showing 21 changed files with 1,043 additions and 359 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/zcash-hackworks/zcash-light-client-ffi",
"state" : {
"revision" : "514dcd7e6fbfa252bf36d9f00d6b98f465a70704",
"version" : "0.4.1"
"revision" : "5f58e32ffae1b285cae1c01bbf350eaae31ffeb5"
}
}
],
Expand Down
3 changes: 1 addition & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/zcash-hackworks/zcash-light-client-ffi",
"state" : {
"revision" : "9bc5877ef6302e877922f79ebead52e50bce94fd",
"version" : "0.4.0"
"revision" : "5f58e32ffae1b285cae1c01bbf350eaae31ffeb5"
}
}
],
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/grpc/grpc-swift.git", from: "1.19.1"),
.package(url: "https://github.com/stephencelis/SQLite.swift.git", from: "0.14.1"),
.package(url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", from: "0.4.1")
// Compiled from revision `d5bd88138610b15f9585fcba696bd1023e716fd8`.
.package(url: "https://github.com/zcash-hackworks/zcash-light-client-ffi", revision: "5f58e32ffae1b285cae1c01bbf350eaae31ffeb5")
],
targets: [
.target(
Expand All @@ -28,6 +29,7 @@ let package = Package(
],
exclude: [
"Modules/Service/GRPC/ProtoBuf/proto/compact_formats.proto",
"Modules/Service/GRPC/ProtoBuf/proto/proposal.proto",
"Modules/Service/GRPC/ProtoBuf/proto/service.proto",
"Error/Sourcery/"
],
Expand Down
4 changes: 2 additions & 2 deletions Sources/ZcashLightClientKit/Block/Actions/ScanAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension ScanAction: Action {
let incrementedProcessedHeight = processedHeight + BlockHeight(increment)
await context.update(processedHeight: incrementedProcessedHeight)
await self?.latestBlocksDataProvider.updateScannedData()

// ScanAction is controlled locally so it must report back the updated scanned height
await context.update(lastScannedHeight: lastScannedHeight)
}
Expand All @@ -74,7 +74,7 @@ extension ScanAction: Action {
// TODO: [#1353] Advanced progress reporting, https://github.com/Electric-Coin-Company/zcash-swift-wallet-sdk/issues/1353
if progressReportReducer == 0 {
// report scan progress only if it's available
if let scanProgress = try? await rustBackend.getScanProgress() {
if let scanProgress = try? await rustBackend.getWalletSummary()?.scanProgress {
let progress = try scanProgress.progress()
logger.debug("progress: \(progress)")
await didUpdate(.syncProgress(progress))
Expand Down
9 changes: 2 additions & 7 deletions Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -658,13 +658,8 @@ extension CompactBlockProcessor {
await send(event: .finished(lastScannedHeight))
await context.update(state: .finished)

let verifiedBalance = Zatoshi((try? await rustBackend.getVerifiedBalance(account: 0)) ?? 0)
let totalBalance = Zatoshi((try? await rustBackend.getBalance(account: 0)) ?? 0)
let shieldedBalance = WalletBalance(
verified: verifiedBalance,
total: totalBalance
)
await metrics.logCBPOverviewReport(logger, shieldedBalance: shieldedBalance)
let walletSummary = try? await rustBackend.getWalletSummary()
await metrics.logCBPOverviewReport(logger, walletSummary: walletSummary)

// If new blocks were mined during previous sync run the sync process again
if newerBlocksWereMinedDuringSync {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// HandleSaplingParametersIfNeeded.swift
//
// SaplingParametersHandler.swift
//
//
// Created by Lukáš Korba on 23.11.2022.
//
Expand Down Expand Up @@ -28,11 +28,13 @@ extension SaplingParametersHandlerImpl: SaplingParametersHandler {
try Task.checkCancellation()

do {
let totalShieldedBalance = try await rustBackend.getBalance(account: Int32(0))
let totalSaplingBalance =
try await rustBackend.getWalletSummary()?.accountBalances[0]?.saplingBalance.total().amount
?? 0
let totalTransparentBalance = try await rustBackend.getTransparentBalance(account: Int32(0))

// Download Sapling parameters only if sapling funds are detected.
guard totalShieldedBalance > 0 || totalTransparentBalance > 0 else { return }
guard totalSaplingBalance > 0 || totalTransparentBalance > 0 else { return }
} catch {
// if sapling balance can't be detected of we fail to obtain the balance
// for some reason we shall not proceed to download the parameters and
Expand Down
11 changes: 6 additions & 5 deletions Sources/ZcashLightClientKit/Block/Scan/BlockScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,21 @@ extension BlockScannerImpl: BlockScanner {

let batchSize = UInt32(config.scanningBatchSize)

// TODO: [#1355] Do more with ScanSummary
// https://github.com/Electric-Coin-Company/zcash-swift-wallet-sdk/issues/1355
let scanSummary: ScanSummary
let scanStartTime = Date()
do {
try await self.rustBackend.scanBlocks(fromHeight: Int32(startHeight), limit: batchSize)
scanSummary = try await self.rustBackend.scanBlocks(fromHeight: Int32(startHeight), limit: batchSize)
} catch {
logger.debug("block scanning failed with error: \(String(describing: error))")
throw error
}

let scanFinishTime = Date()

// TODO: [#1259] potential bug when rustBackend.scanBlocks scan less blocks than batchSize,
// https://github.com/zcash/ZcashLightClientKit/issues/1259
lastScannedHeight = startHeight + Int(batchSize) - 1

lastScannedHeight = scanSummary.scannedRange.upperBound - 1

scannedNewBlocks = previousScannedHeight != lastScannedHeight
if scannedNewBlocks {
try await didScan(lastScannedHeight, batchSize)
Expand Down
26 changes: 6 additions & 20 deletions Sources/ZcashLightClientKit/Error/ZcashError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ public enum ZcashError: Equatable, Error {
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0003
case rustDecryptAndStoreTransaction(_ rustError: String)
/// Error from rust layer when calling ZcashRustBackend.getBalance
/// - `account` is account passed to ZcashRustBackend.getBalance.
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0004
case rustGetBalance(_ account: Int, _ rustError: String)
/// Error from rust layer when calling ZcashRustBackend.getCurrentAddress
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0005
Expand Down Expand Up @@ -153,11 +148,6 @@ public enum ZcashError: Equatable, Error {
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0011
case rustGetTransparentBalance(_ account: Int, _ rustError: String)
/// Error from rust layer when calling ZcashRustBackend.getVerifiedBalance
/// - `account` is account passed to ZcashRustBackend.getVerifiedBalance.
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0012
case rustGetVerifiedBalance(_ account: Int, _ rustError: String)
/// account parameter is lower than 0 when calling ZcashRustBackend.getVerifiedTransparentBalance
/// - `account` is account passed to ZcashRustBackend.getVerifiedTransparentBalance.
/// ZRUST0013
Expand Down Expand Up @@ -297,10 +287,6 @@ public enum ZcashError: Equatable, Error {
/// Invalid transaction ID length when calling ZcashRustBackend.getMemo. txId must be 32 bytes.
/// ZRUST0050
case rustGetMemoInvalidTxIdLength
/// Error from rust layer when calling ZcashRustBackend.getScanProgress
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0051
case rustGetScanProgress(_ rustError: String)
/// Error from rust layer when calling ZcashRustBackend.fullyScannedHeight
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0052
Expand All @@ -317,6 +303,10 @@ public enum ZcashError: Equatable, Error {
/// - `progress` value reported
/// ZRUST0055
case rustScanProgressOutOfRange(_ progress: String)
/// Error from rust layer when calling ZcashRustBackend.getWalletSummary
/// - `rustError` contains error generated by the rust layer.
/// ZRUST0056
case rustGetWalletSummary(_ rustError: String)
/// SQLite query failed when fetching all accounts from the database.
/// - `sqliteError` is error produced by SQLite library.
/// ZADAO0001
Expand Down Expand Up @@ -614,15 +604,13 @@ public enum ZcashError: Equatable, Error {
case .rustCreateAccount: return "Error from rust layer when calling ZcashRustBackend.createAccount"
case .rustCreateToAddress: return "Error from rust layer when calling ZcashRustBackend.createToAddress"
case .rustDecryptAndStoreTransaction: return "Error from rust layer when calling ZcashRustBackend.decryptAndStoreTransaction"
case .rustGetBalance: return "Error from rust layer when calling ZcashRustBackend.getBalance"
case .rustGetCurrentAddress: return "Error from rust layer when calling ZcashRustBackend.getCurrentAddress"
case .rustGetCurrentAddressInvalidAddress: return "Unified address generated by rust layer is invalid when calling ZcashRustBackend.getCurrentAddress"
case .rustGetNearestRewindHeight: return "Error from rust layer when calling ZcashRustBackend.getNearestRewindHeight"
case .rustGetNextAvailableAddress: return "Error from rust layer when calling ZcashRustBackend.getNextAvailableAddress"
case .rustGetNextAvailableAddressInvalidAddress: return "Unified address generated by rust layer is invalid when calling ZcashRustBackend.getNextAvailableAddress"
case .rustGetTransparentBalanceNegativeAccount: return "account parameter is lower than 0 when calling ZcashRustBackend.getTransparentBalance"
case .rustGetTransparentBalance: return "Error from rust layer when calling ZcashRustBackend.getTransparentBalance"
case .rustGetVerifiedBalance: return "Error from rust layer when calling ZcashRustBackend.getVerifiedBalance"
case .rustGetVerifiedTransparentBalanceNegativeAccount: return "account parameter is lower than 0 when calling ZcashRustBackend.getVerifiedTransparentBalance"
case .rustGetVerifiedTransparentBalance: return "Error from rust layer when calling ZcashRustBackend.getVerifiedTransparentBalance"
case .rustInitDataDb: return "Error from rust layer when calling ZcashRustBackend.initDataDb"
Expand Down Expand Up @@ -660,11 +648,11 @@ public enum ZcashError: Equatable, Error {
case .rustUpdateChainTip: return "Error from rust layer when calling ZcashRustBackend.updateChainTip"
case .rustSuggestScanRanges: return "Error from rust layer when calling ZcashRustBackend.suggestScanRanges"
case .rustGetMemoInvalidTxIdLength: return "Invalid transaction ID length when calling ZcashRustBackend.getMemo. txId must be 32 bytes."
case .rustGetScanProgress: return "Error from rust layer when calling ZcashRustBackend.getScanProgress"
case .rustFullyScannedHeight: return "Error from rust layer when calling ZcashRustBackend.fullyScannedHeight"
case .rustMaxScannedHeight: return "Error from rust layer when calling ZcashRustBackend.maxScannedHeight"
case .rustLatestCachedBlockHeight: return "Error from rust layer when calling ZcashRustBackend.latestCachedBlockHeight"
case .rustScanProgressOutOfRange: return "Rust layer's call ZcashRustBackend.getScanProgress returned values that after computation are outside of allowed range 0-100%."
case .rustGetWalletSummary: return "Error from rust layer when calling ZcashRustBackend.getWalletSummary"
case .accountDAOGetAll: return "SQLite query failed when fetching all accounts from the database."
case .accountDAOGetAllCantDecode: return "Fetched accounts from SQLite but can't decode them."
case .accountDAOFindBy: return "SQLite query failed when seaching for accounts in the database."
Expand Down Expand Up @@ -785,15 +773,13 @@ public enum ZcashError: Equatable, Error {
case .rustCreateAccount: return .rustCreateAccount
case .rustCreateToAddress: return .rustCreateToAddress
case .rustDecryptAndStoreTransaction: return .rustDecryptAndStoreTransaction
case .rustGetBalance: return .rustGetBalance
case .rustGetCurrentAddress: return .rustGetCurrentAddress
case .rustGetCurrentAddressInvalidAddress: return .rustGetCurrentAddressInvalidAddress
case .rustGetNearestRewindHeight: return .rustGetNearestRewindHeight
case .rustGetNextAvailableAddress: return .rustGetNextAvailableAddress
case .rustGetNextAvailableAddressInvalidAddress: return .rustGetNextAvailableAddressInvalidAddress
case .rustGetTransparentBalanceNegativeAccount: return .rustGetTransparentBalanceNegativeAccount
case .rustGetTransparentBalance: return .rustGetTransparentBalance
case .rustGetVerifiedBalance: return .rustGetVerifiedBalance
case .rustGetVerifiedTransparentBalanceNegativeAccount: return .rustGetVerifiedTransparentBalanceNegativeAccount
case .rustGetVerifiedTransparentBalance: return .rustGetVerifiedTransparentBalance
case .rustInitDataDb: return .rustInitDataDb
Expand Down Expand Up @@ -831,11 +817,11 @@ public enum ZcashError: Equatable, Error {
case .rustUpdateChainTip: return .rustUpdateChainTip
case .rustSuggestScanRanges: return .rustSuggestScanRanges
case .rustGetMemoInvalidTxIdLength: return .rustGetMemoInvalidTxIdLength
case .rustGetScanProgress: return .rustGetScanProgress
case .rustFullyScannedHeight: return .rustFullyScannedHeight
case .rustMaxScannedHeight: return .rustMaxScannedHeight
case .rustLatestCachedBlockHeight: return .rustLatestCachedBlockHeight
case .rustScanProgressOutOfRange: return .rustScanProgressOutOfRange
case .rustGetWalletSummary: return .rustGetWalletSummary
case .accountDAOGetAll: return .accountDAOGetAll
case .accountDAOGetAllCantDecode: return .accountDAOGetAllCantDecode
case .accountDAOFindBy: return .accountDAOFindBy
Expand Down
8 changes: 2 additions & 6 deletions Sources/ZcashLightClientKit/Error/ZcashErrorCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public enum ZcashErrorCode: String {
case rustCreateToAddress = "ZRUST0002"
/// Error from rust layer when calling ZcashRustBackend.decryptAndStoreTransaction
case rustDecryptAndStoreTransaction = "ZRUST0003"
/// Error from rust layer when calling ZcashRustBackend.getBalance
case rustGetBalance = "ZRUST0004"
/// Error from rust layer when calling ZcashRustBackend.getCurrentAddress
case rustGetCurrentAddress = "ZRUST0005"
/// Unified address generated by rust layer is invalid when calling ZcashRustBackend.getCurrentAddress
Expand All @@ -87,8 +85,6 @@ public enum ZcashErrorCode: String {
case rustGetTransparentBalanceNegativeAccount = "ZRUST0010"
/// Error from rust layer when calling ZcashRustBackend.getTransparentBalance
case rustGetTransparentBalance = "ZRUST0011"
/// Error from rust layer when calling ZcashRustBackend.getVerifiedBalance
case rustGetVerifiedBalance = "ZRUST0012"
/// account parameter is lower than 0 when calling ZcashRustBackend.getVerifiedTransparentBalance
case rustGetVerifiedTransparentBalanceNegativeAccount = "ZRUST0013"
/// Error from rust layer when calling ZcashRustBackend.getVerifiedTransparentBalance
Expand Down Expand Up @@ -163,8 +159,6 @@ public enum ZcashErrorCode: String {
case rustSuggestScanRanges = "ZRUST0049"
/// Invalid transaction ID length when calling ZcashRustBackend.getMemo. txId must be 32 bytes.
case rustGetMemoInvalidTxIdLength = "ZRUST0050"
/// Error from rust layer when calling ZcashRustBackend.getScanProgress
case rustGetScanProgress = "ZRUST0051"
/// Error from rust layer when calling ZcashRustBackend.fullyScannedHeight
case rustFullyScannedHeight = "ZRUST0052"
/// Error from rust layer when calling ZcashRustBackend.maxScannedHeight
Expand All @@ -173,6 +167,8 @@ public enum ZcashErrorCode: String {
case rustLatestCachedBlockHeight = "ZRUST0054"
/// Rust layer's call ZcashRustBackend.getScanProgress returned values that after computation are outside of allowed range 0-100%.
case rustScanProgressOutOfRange = "ZRUST0055"
/// Error from rust layer when calling ZcashRustBackend.getWalletSummary
case rustGetWalletSummary = "ZRUST0056"
/// SQLite query failed when fetching all accounts from the database.
case accountDAOGetAll = "ZADAO0001"
/// Fetched accounts from SQLite but can't decode them.
Expand Down
9 changes: 5 additions & 4 deletions Sources/ZcashLightClientKit/Metrics/SDKMetrics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol SDKMetrics {
func actionStart(_ action: CBPState)
func actionDetail(_ detail: String, `for` action: CBPState)
func actionStop()
func logCBPOverviewReport(_ logger: Logger, shieldedBalance: WalletBalance) async
func logCBPOverviewReport(_ logger: Logger, walletSummary: WalletSummary?) async
}

final class SDKMetricsImpl: SDKMetrics {
Expand Down Expand Up @@ -102,15 +102,16 @@ final class SDKMetricsImpl: SDKMetrics {
}

// swiftlint:disable string_concatenation
func logCBPOverviewReport(_ logger: Logger, shieldedBalance: WalletBalance) async {
func logCBPOverviewReport(_ logger: Logger, walletSummary: WalletSummary?) async {
actionStop()

let accountBalance = walletSummary?.accountBalances[0]
logger.sync(
"""
SYNC (\(syncs)) REPORT
finished in: \(Date().timeIntervalSince1970 - cbpStartTime)
verified balance: \(shieldedBalance.verified.amount)
total balance: \(shieldedBalance.total.amount)
verified balance: \(accountBalance?.saplingBalance.spendableValue.amount ?? 0)
total balance: \(accountBalance?.saplingBalance.total().amount ?? 0)
"""
)

Expand Down
14 changes: 14 additions & 0 deletions Sources/ZcashLightClientKit/Model/ScanSummary.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// ScanSummary.swift
//
//
// Created by Jack Grigg on 26/01/2024.
//

import Foundation

struct ScanSummary: Equatable {
let scannedRange: Range<BlockHeight>
let spentSaplingNoteCount: UInt64
let receivedSaplingNoteCount: UInt64
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
//
// ScanProgress.swift
//
// WalletSummary.swift
//
//
// Created by Jack Grigg on 06/09/2023.
//

import Foundation

struct PoolBalance: Equatable {
let spendableValue: Zatoshi
let changePendingConfirmation: Zatoshi
let valuePendingSpendability: Zatoshi

func total() -> Zatoshi {
self.spendableValue + self.changePendingConfirmation + self.valuePendingSpendability
}
}

struct AccountBalance: Equatable {
let saplingBalance: PoolBalance
let unshielded: Zatoshi
}

struct ScanProgress: Equatable {
let numerator: UInt64
let denominator: UInt64
Expand All @@ -27,3 +42,11 @@ struct ScanProgress: Equatable {
return value
}
}

struct WalletSummary: Equatable {
let accountBalances: [UInt32: AccountBalance]
let chainTipHeight: BlockHeight
let fullyScannedHeight: BlockHeight
let scanProgress: ScanProgress?
let nextSaplingSubtreeIndex: UInt32
}
Loading

0 comments on commit 2a8f6c2

Please sign in to comment.