diff --git a/Sources/ZcashLightClientKit/Block/Scan/BlockScanner.swift b/Sources/ZcashLightClientKit/Block/Scan/BlockScanner.swift index cf203f6b..42cd4d95 100644 --- a/Sources/ZcashLightClientKit/Block/Scan/BlockScanner.swift +++ b/Sources/ZcashLightClientKit/Block/Scan/BlockScanner.swift @@ -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 @@ -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 diff --git a/Sources/ZcashLightClientKit/Modules/Service/GRPC/LightWalletGRPCService.swift b/Sources/ZcashLightClientKit/Modules/Service/GRPC/LightWalletGRPCService.swift index c430786e..4794b108 100644 --- a/Sources/ZcashLightClientKit/Modules/Service/GRPC/LightWalletGRPCService.swift +++ b/Sources/ZcashLightClientKit/Modules/Service/GRPC/LightWalletGRPCService.swift @@ -277,6 +277,10 @@ extension LightWalletGRPCService: LightWalletService { } } } + + func getTreeState(_ id: BlockID) async throws -> TreeState { + try await compactTxStreamer.getTreeState(id) + } func closeConnection() { _ = channel.close() diff --git a/Sources/ZcashLightClientKit/Modules/Service/LightWalletService.swift b/Sources/ZcashLightClientKit/Modules/Service/LightWalletService.swift index b6f3cf60..b5b4fe8e 100644 --- a/Sources/ZcashLightClientKit/Modules/Service/LightWalletService.swift +++ b/Sources/ZcashLightClientKit/Modules/Service/LightWalletService.swift @@ -196,4 +196,6 @@ protocol LightWalletService: AnyObject { /// - Parameters: /// - request: Request to send to GetSubtreeRoots. func getSubtreeRoots(_ request: GetSubtreeRootsArg) -> AsyncThrowingStream + + func getTreeState(_ id: BlockID) async throws -> TreeState } diff --git a/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift b/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift index 1f0af88d..ea586648 100644 --- a/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift +++ b/Sources/ZcashLightClientKit/Rust/ZcashRustBackend.swift @@ -859,6 +859,7 @@ extension FfiScanProgress { } } +// swiftlint:disable large_tuple line_length 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] { diff --git a/Sources/ZcashLightClientKit/Synchronizer/Dependencies.swift b/Sources/ZcashLightClientKit/Synchronizer/Dependencies.swift index 05c48053..40b2ad76 100644 --- a/Sources/ZcashLightClientKit/Synchronizer/Dependencies.swift +++ b/Sources/ZcashLightClientKit/Synchronizer/Dependencies.swift @@ -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) @@ -133,6 +134,7 @@ enum Dependencies { return BlockScannerImpl( config: blockScannerConfig, rustBackend: rustBackend, + service: service, transactionRepository: transactionRepository, metrics: metrics, logger: logger diff --git a/Tests/TestUtils/DarkSideWalletService.swift b/Tests/TestUtils/DarkSideWalletService.swift index 60e3f90e..7cc86a86 100644 --- a/Tests/TestUtils/DarkSideWalletService.swift +++ b/Tests/TestUtils/DarkSideWalletService.swift @@ -190,6 +190,10 @@ class DarksideWalletService: LightWalletService { func getSubtreeRoots(_ request: ZcashLightClientKit.GetSubtreeRootsArg) -> AsyncThrowingStream { service.getSubtreeRoots(request) } + + func getTreeState(_ id: BlockID) async throws -> TreeState { + try await service.getTreeState(id) + } } enum DarksideWalletDConstants: NetworkConstants { diff --git a/Tests/TestUtils/FakeService.swift b/Tests/TestUtils/FakeService.swift index 07e2d74b..3358f519 100644 --- a/Tests/TestUtils/FakeService.swift +++ b/Tests/TestUtils/FakeService.swift @@ -82,4 +82,8 @@ class MockLightWalletService: LightWalletService { func getSubtreeRoots(_ request: ZcashLightClientKit.GetSubtreeRootsArg) -> AsyncThrowingStream { service.getSubtreeRoots(request) } + + func getTreeState(_ id: BlockID) async throws -> TreeState { + try await service.getTreeState(id) + } } diff --git a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift index f8606891..1f319796 100644 --- a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift +++ b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift @@ -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 { diff --git a/Tests/TestUtils/Stubs.swift b/Tests/TestUtils/Stubs.swift index f5f33521..b552b079 100644 --- a/Tests/TestUtils/Stubs.swift +++ b/Tests/TestUtils/Stubs.swift @@ -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) } }