Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TreeState support #1394

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Sources/ZcashLightClientKit/Block/Scan/BlockScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protocol BlockScanner {
struct BlockScannerImpl {
let config: BlockScannerConfig
let rustBackend: ZcashRustBackendWelding
let service: LightWalletService
let transactionRepository: TransactionRepository
let metrics: SDKMetrics
let logger: Logger
Expand Down Expand Up @@ -56,7 +57,9 @@ extension BlockScannerImpl: BlockScanner {
let scanSummary: ScanSummary
let scanStartTime = Date()
do {
scanSummary = try await self.rustBackend.scanBlocks(fromHeight: Int32(startHeight), limit: batchSize)
let fromState = try await service.getTreeState(BlockID(height: startHeight - 1))

scanSummary = try await self.rustBackend.scanBlocks(fromHeight: Int32(startHeight), fromState: fromState, limit: batchSize)
} catch {
logger.debug("block scanning failed with error: \(String(describing: error))")
throw error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,10 @@ extension LightWalletGRPCService: LightWalletService {
}
}
}

func getTreeState(_ id: BlockID) async throws -> TreeState {
try await compactTxStreamer.getTreeState(id)
}

func closeConnection() {
_ = channel.close()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
}

extension LightWalletServiceError: Equatable {
// swiftlint:disable cyclomatic_complexity

Check warning on line 27 in Sources/ZcashLightClientKit/Modules/Service/LightWalletService.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Blanket Disable Command Violation: The disabled 'cyclomatic_complexity' rule should be re-enabled before the end of the file (blanket_disable_command)
public static func == (lhs: Self, rhs: Self) -> Bool {
switch lhs {
case .generalError(let message):
Expand Down Expand Up @@ -196,4 +196,6 @@
/// - Parameters:
/// - request: Request to send to GetSubtreeRoots.
func getSubtreeRoots(_ request: GetSubtreeRootsArg) -> AsyncThrowingStream<SubtreeRoot, Error>

func getTreeState(_ id: BlockID) async throws -> TreeState
}
1 change: 1 addition & 0 deletions Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Created by Jack Grigg on 5/8/19.
// Copyright © 2019 Electric Coin Company. All rights reserved.
//
// swiftlint:disable type_body_length

Check warning on line 8 in Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Blanket Disable Command Violation: The disabled 'type_body_length' rule should be re-enabled before the end of the file (blanket_disable_command)
import Foundation
import libzcashlc

Expand Down Expand Up @@ -628,7 +628,7 @@
fromStateBytes,
UInt(fromStateBytes.count),
limit,
networkType.networkId)

Check warning on line 631 in Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Multiline Arguments Brackets Violation: Multiline arguments should have their surrounding brackets in a new line (multiline_arguments_brackets)
globalDBLock.unlock()

guard let summaryPtr else {
Expand Down Expand Up @@ -859,6 +859,7 @@
}
}

// swiftlint:disable large_tuple line_length

Check warning on line 862 in Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Blanket Disable Command Violation: The disabled 'large_tuple' rule should be re-enabled before the end of the file (blanket_disable_command)

Check warning on line 862 in Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Blanket Disable Command Violation: The disabled 'line_length' rule should be re-enabled before the end of the file (blanket_disable_command)
struct FfiTxId {
var tuple: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8)
var array: [UInt8] {
Expand Down
2 changes: 2 additions & 0 deletions Sources/ZcashLightClientKit/Synchronizer/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ enum Dependencies {
}

container.register(type: BlockScanner.self, isSingleton: true) { di in
let service = di.resolve(LightWalletService.self)
let rustBackend = di.resolve(ZcashRustBackendWelding.self)
let transactionRepository = di.resolve(TransactionRepository.self)
let metrics = di.resolve(SDKMetrics.self)
Expand All @@ -133,6 +134,7 @@ enum Dependencies {
return BlockScannerImpl(
config: blockScannerConfig,
rustBackend: rustBackend,
service: service,
transactionRepository: transactionRepository,
metrics: metrics,
logger: logger
Expand Down
4 changes: 4 additions & 0 deletions Tests/TestUtils/DarkSideWalletService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ class DarksideWalletService: LightWalletService {
func getSubtreeRoots(_ request: ZcashLightClientKit.GetSubtreeRootsArg) -> AsyncThrowingStream<ZcashLightClientKit.SubtreeRoot, Error> {
service.getSubtreeRoots(request)
}

func getTreeState(_ id: BlockID) async throws -> TreeState {
try await service.getTreeState(id)
}
}

enum DarksideWalletDConstants: NetworkConstants {
Expand Down
4 changes: 4 additions & 0 deletions Tests/TestUtils/FakeService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,8 @@ class MockLightWalletService: LightWalletService {
func getSubtreeRoots(_ request: ZcashLightClientKit.GetSubtreeRootsArg) -> AsyncThrowingStream<ZcashLightClientKit.SubtreeRoot, Error> {
service.getSubtreeRoots(request)
}

func getTreeState(_ id: BlockID) async throws -> TreeState {
try await service.getTreeState(id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,30 @@ class LightWalletServiceMock: LightWalletService {
}
}

// MARK: - getTreeState

var getTreeStateThrowableError: Error?
var getTreeStateCallsCount = 0
var getTreeStateCalled: Bool {
return getTreeStateCallsCount > 0
}
var getTreeStateReceivedId: BlockID?
var getTreeStateReturnValue: TreeState!
var getTreeStateClosure: ((BlockID) async throws -> TreeState)?

func getTreeState(_ id: BlockID) async throws -> TreeState {
if let error = getTreeStateThrowableError {
throw error
}
getTreeStateCallsCount += 1
getTreeStateReceivedId = id
if let closure = getTreeStateClosure {
return try await closure(id)
} else {
return getTreeStateReturnValue
}
}

}
class LightWalletdInfoMock: LightWalletdInfo {

Expand Down
4 changes: 2 additions & 2 deletions Tests/TestUtils/Stubs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class RustBackendMockHelper {
try await rustBackend.suggestScanRanges()
}

await rustBackendMock.setScanBlocksFromHeightLimitClosure() { fromHeight, limit in
try await rustBackend.scanBlocks(fromHeight: fromHeight, limit: limit)
await rustBackendMock.setScanBlocksFromHeightFromStateLimitClosure { fromHeight, fromState, limit in
try await rustBackend.scanBlocks(fromHeight: fromHeight, fromState: fromState, limit: limit)
}
}

Expand Down
Loading