[#470] CompactBlockStreamDownloadOperation to async (#506)

- using new sync APIs for storage and service
- whole logic wrapped in the Task

Closes #470
This commit is contained in:
Lukas Korba 2022-08-29 21:53:49 +02:00 committed by GitHub
parent 16d1948b5b
commit be24044b51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 42 deletions

View File

@ -71,7 +71,6 @@ class CompactBlockStreamDownloadOperation: ZcashOperation {
private var storage: CompactBlockStorage
private var service: LightWalletService
private var done = false
private var cancelable: CancellableCall?
private var cancelableTask: Task<Void, Error>?
private var startHeight: BlockHeight?
private var targetHeight: BlockHeight?
@ -120,14 +119,15 @@ class CompactBlockStreamDownloadOperation: ZcashOperation {
}
self.startedHandler?()
cancelableTask = Task {
do {
if self.targetHeight == nil {
self.targetHeight = try service.latestBlockHeight()
self.targetHeight = try await service.latestBlockHeightAsync()
}
guard let latestHeight = self.targetHeight else {
throw LightWalletServiceError.generalError(message: "missing target height on block stream operation")
}
let latestDownloaded = try storage.latestHeight()
let latestDownloaded = try await storage.latestHeightAsync()
let startHeight = max(self.startHeight ?? BlockHeight.empty(), latestDownloaded)
let stream = service.blockStream(
@ -135,8 +135,6 @@ class CompactBlockStreamDownloadOperation: ZcashOperation {
endHeight: latestHeight
)
cancelableTask = Task {
do {
for try await zcashCompactBlock in stream {
try self.cache(zcashCompactBlock, flushCache: false)
let progress = BlockProgress(
@ -160,19 +158,14 @@ class CompactBlockStreamDownloadOperation: ZcashOperation {
while !done && !isCancelled {
sleep(1)
}
} catch {
self.fail(error: error)
}
}
override func fail(error: Error? = nil) {
self.cancelable?.cancel()
self.cancelableTask?.cancel()
super.fail(error: error)
}
override func cancel() {
self.cancelable?.cancel()
self.cancelableTask?.cancel()
super.cancel()
}