[#475] NextStateHelper to async/await (#495)

- refactored to await/async
This commit is contained in:
Lukas Korba 2022-08-23 21:58:15 +02:00 committed by GitHub
parent 5d662dc98b
commit 6d0f241ed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 5 deletions

View File

@ -514,7 +514,7 @@ public class CompactBlockProcessor {
return
}
self.nextBatch()
self.nextBatchTask()
}
/**
@ -935,7 +935,7 @@ public class CompactBlockProcessor {
// process next batch
// processNewBlocks(range: Self.nextBatchBlockRange(latestHeight: latestBlockHeight, latestDownloadedHeight: try downloader.lastDownloadedBlockHeight(), walletBirthday: config.walletBirthday))
nextBatch()
nextBatchTask()
} catch {
self.fail(error)
}
@ -972,6 +972,7 @@ public class CompactBlockProcessor {
}
}
@available(*, deprecated, message: "This static method will be removed soon, use `nextBatchTask()` instead.")
private func nextBatch() {
self.state = .downloading
NextStateHelper.nextState(
@ -1009,6 +1010,40 @@ public class CompactBlockProcessor {
}
}
}
private func nextBatchTask() {
self.state = .downloading
Task { [self] in
do {
let nextState = try await NextStateHelper.nextStateAsync(
service: self.service,
downloader: self.downloader,
config: self.config,
rustBackend: self.rustBackend
)
switch nextState {
case .finishProcessing(let height):
self.latestBlockHeight = height
self.processingFinished(height: height)
case .processNewBlocks(let range):
self.latestBlockHeight = range.upperBound
self.lowerBoundHeight = range.lowerBound
self.processNewBlocks(range: range)
case let .wait(latestHeight, latestDownloadHeight):
// Lightwalletd might be syncing
self.lowerBoundHeight = latestDownloadHeight
self.latestBlockHeight = latestHeight
LoggerProxy.info(
"Lightwalletd might be syncing: latest downloaded block height is: \(latestDownloadHeight)" +
"while latest blockheight is reported at: \(latestHeight)"
)
self.processingFinished(height: latestDownloadHeight)
}
} catch {
self.severeFailure(error)
}
}
}
private func validationFailed(at height: BlockHeight) {
// cancel all Tasks
@ -1043,7 +1078,7 @@ public class CompactBlockProcessor {
)
// process next batch
self.nextBatch()
self.nextBatchTask()
} catch {
self.fail(error)
}
@ -1063,7 +1098,7 @@ public class CompactBlockProcessor {
return
}
nextBatch()
nextBatchTask()
}
private func processingFinished(height: BlockHeight) {
@ -1398,7 +1433,7 @@ extension CompactBlockProcessor {
result: @escaping (Result<FigureNextBatchOperation.NextState, Error>) -> Void
) {
let dispatchQueue = queue ?? DispatchQueue.global(qos: .userInitiated)
dispatchQueue.async {
do {
let nextResult = try self.nextState(
@ -1414,6 +1449,25 @@ extension CompactBlockProcessor {
}
}
static func nextStateAsync(
service: LightWalletService,
downloader: CompactBlockDownloading,
config: Configuration,
rustBackend: ZcashRustBackendWelding.Type
) async throws -> FigureNextBatchOperation.NextState {
let task = Task(priority: .userInitiated) {
// TODO: refactor to async call, issue 463, PR 493
// https://github.com/zcash/ZcashLightClientKit/issues/463
try nextState(
service: service,
downloader: downloader,
config: config,
rustBackend: rustBackend
)
}
return try await task.value
}
static func nextState(
service: LightWalletService,
downloader: CompactBlockDownloading,