- refactored to await/async
This commit is contained in:
parent
5d662dc98b
commit
6d0f241ed6
|
@ -514,7 +514,7 @@ public class CompactBlockProcessor {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
self.nextBatch()
|
self.nextBatchTask()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -935,7 +935,7 @@ public class CompactBlockProcessor {
|
||||||
|
|
||||||
// process next batch
|
// process next batch
|
||||||
// processNewBlocks(range: Self.nextBatchBlockRange(latestHeight: latestBlockHeight, latestDownloadedHeight: try downloader.lastDownloadedBlockHeight(), walletBirthday: config.walletBirthday))
|
// processNewBlocks(range: Self.nextBatchBlockRange(latestHeight: latestBlockHeight, latestDownloadedHeight: try downloader.lastDownloadedBlockHeight(), walletBirthday: config.walletBirthday))
|
||||||
nextBatch()
|
nextBatchTask()
|
||||||
} catch {
|
} catch {
|
||||||
self.fail(error)
|
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() {
|
private func nextBatch() {
|
||||||
self.state = .downloading
|
self.state = .downloading
|
||||||
NextStateHelper.nextState(
|
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) {
|
private func validationFailed(at height: BlockHeight) {
|
||||||
// cancel all Tasks
|
// cancel all Tasks
|
||||||
|
@ -1043,7 +1078,7 @@ public class CompactBlockProcessor {
|
||||||
)
|
)
|
||||||
|
|
||||||
// process next batch
|
// process next batch
|
||||||
self.nextBatch()
|
self.nextBatchTask()
|
||||||
} catch {
|
} catch {
|
||||||
self.fail(error)
|
self.fail(error)
|
||||||
}
|
}
|
||||||
|
@ -1063,7 +1098,7 @@ public class CompactBlockProcessor {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
nextBatch()
|
nextBatchTask()
|
||||||
}
|
}
|
||||||
|
|
||||||
private func processingFinished(height: BlockHeight) {
|
private func processingFinished(height: BlockHeight) {
|
||||||
|
@ -1398,7 +1433,7 @@ extension CompactBlockProcessor {
|
||||||
result: @escaping (Result<FigureNextBatchOperation.NextState, Error>) -> Void
|
result: @escaping (Result<FigureNextBatchOperation.NextState, Error>) -> Void
|
||||||
) {
|
) {
|
||||||
let dispatchQueue = queue ?? DispatchQueue.global(qos: .userInitiated)
|
let dispatchQueue = queue ?? DispatchQueue.global(qos: .userInitiated)
|
||||||
|
|
||||||
dispatchQueue.async {
|
dispatchQueue.async {
|
||||||
do {
|
do {
|
||||||
let nextResult = try self.nextState(
|
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(
|
static func nextState(
|
||||||
service: LightWalletService,
|
service: LightWalletService,
|
||||||
downloader: CompactBlockDownloading,
|
downloader: CompactBlockDownloading,
|
||||||
|
|
Loading…
Reference in New Issue