Skip to content

Commit

Permalink
TreeState support
Browse files Browse the repository at this point in the history
- @str4d please check this implementation, I believe it's what you were looking for.
- the new service's method getTreeState needs some documentation though
  • Loading branch information
LukasKorba committed Mar 15, 2024
1 parent 90dcb0e commit 78cf40f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
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))

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 @@ -196,4 +196,6 @@ protocol LightWalletService: AnyObject {
/// - Parameters:
/// - request: Request to send to GetSubtreeRoots.
func getSubtreeRoots(_ request: GetSubtreeRootsArg) -> AsyncThrowingStream<SubtreeRoot, Error>

func getTreeState(_ id: BlockID) async throws -> TreeState
}
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
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

0 comments on commit 78cf40f

Please sign in to comment.