[#474] FetchUnspentTxOutputsOperation to async/await (#514)

- wrapped to Task
- downloader uses the new async API
This commit is contained in:
Lukas Korba 2022-08-30 16:00:48 +02:00 committed by GitHub
parent 29f159845d
commit b684a2f486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 43 additions and 22 deletions

View File

@ -24,7 +24,9 @@ class FetchUnspentTxOutputsOperation: ZcashOperation {
private var startHeight: BlockHeight private var startHeight: BlockHeight
private var network: NetworkType private var network: NetworkType
private var dataDb: URL private var dataDb: URL
private var cancelableTask: Task<Void, Error>?
private var done = false
init( init(
accountRepository: AccountRepository, accountRepository: AccountRepository,
downloader: CompactBlockDownloading, downloader: CompactBlockDownloading,
@ -49,32 +51,41 @@ class FetchUnspentTxOutputsOperation: ZcashOperation {
self.startedHandler?() self.startedHandler?()
do { cancelableTask = Task {
let tAddresses = try accountRepository.getAll().map({ $0.transparentAddress })
do { do {
for tAddress in tAddresses { let tAddresses = try accountRepository.getAll().map({ $0.transparentAddress })
guard try self.rustbackend.clearUtxos( do {
dbData: dataDb, for tAddress in tAddresses {
address: tAddress, guard try self.rustbackend.clearUtxos(
sinceHeight: startHeight - 1, dbData: dataDb,
networkType: network address: tAddress,
) >= 0 else { sinceHeight: startHeight - 1,
throw rustbackend.lastError() ?? RustWeldingError.genericError(message: "attempted to clear utxos but -1 was returned") networkType: network
) >= 0 else {
throw rustbackend.lastError() ?? RustWeldingError.genericError(message: "attempted to clear utxos but -1 was returned")
}
} }
} catch {
throw FetchUTXOError.clearingFailed(error)
} }
var utxos: [UnspentTransactionOutputEntity] = []
let stream: AsyncThrowingStream<UnspentTransactionOutputEntity, Error> = downloader.fetchUnspentTransactionOutputs(tAddresses: tAddresses, startHeight: startHeight)
for try await transaction in stream {
utxos.append(transaction)
}
let result = storeUTXOs(utxos, in: dataDb)
self.fetchedUTXOsHandler?(result)
self.done = true
} catch { } catch {
throw FetchUTXOError.clearingFailed(error) self.fail(error: error)
} }
}
// TODO: will be replaced by new async API, issue 474
// https://github.com/zcash/ZcashLightClientKit/issues/474 while !done && !isCancelled {
let utxos: [UnspentTransactionOutputEntity] = try downloader.fetchUnspentTransactionOutputs(tAddresses: tAddresses, startHeight: startHeight) sleep(1)
let result = storeUTXOs(utxos, in: dataDb)
self.fetchedUTXOsHandler?(result)
} catch {
self.fail(error: error)
} }
} }
@ -102,4 +113,14 @@ class FetchUnspentTxOutputsOperation: ZcashOperation {
return (inserted: refreshed, skipped: skipped) return (inserted: refreshed, skipped: skipped)
} }
override func fail(error: Error? = nil) {
self.cancelableTask?.cancel()
super.fail(error: error)
}
override func cancel() {
self.cancelableTask?.cancel()
super.cancel()
}
} }