[#473] CompactBlockEnhancementOperation to async/await (#513)

- Wrapped to Task
- Downloader APIs upgraded to async one
This commit is contained in:
Lukas Korba 2022-08-30 16:07:36 +02:00 committed by GitHub
parent b684a2f486
commit 87f50a796c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 35 deletions

View File

@ -29,7 +29,9 @@ class CompactBlockEnhancementOperation: ZcashOperation {
private(set) var network: NetworkType private(set) var network: NetworkType
private weak var progressDelegate: CompactBlockProgressDelegate? private weak var progressDelegate: CompactBlockProgressDelegate?
private var dataDb: URL private var dataDb: URL
private var cancelableTask: Task<Void, Error>?
private var done = false
init( init(
rustWelding: ZcashRustBackendWelding.Type, rustWelding: ZcashRustBackendWelding.Type,
dataDb: URL, dataDb: URL,
@ -58,44 +60,50 @@ class CompactBlockEnhancementOperation: ZcashOperation {
self.startedHandler?() self.startedHandler?()
// fetch transactions cancelableTask = Task {
do { // fetch transactions
guard let transactions = try repository.findTransactions(in: self.range, limit: Int.max), !transactions.isEmpty else { do {
LoggerProxy.debug("no transactions detected on range: \(range.printRange)") guard let transactions = try repository.findTransactions(in: self.range, limit: Int.max), !transactions.isEmpty else {
return LoggerProxy.debug("no transactions detected on range: \(range.printRange)")
} return
}
for index in 0 ..< transactions.count {
let transaction = transactions[index] for index in 0 ..< transactions.count {
var retry = true let transaction = transactions[index]
var retry = true
while retry && self.retries < maxRetries {
do { while retry && self.retries < maxRetries {
let confirmedTx = try enhance(transaction: transaction) do {
retry = false let confirmedTx = try await enhance(transaction: transaction)
self.reportProgress( retry = false
totalTransactions: transactions.count, self.reportProgress(
enhanced: index + 1, totalTransactions: transactions.count,
txEnhanced: confirmedTx enhanced: index + 1,
) txEnhanced: confirmedTx
} catch { )
self.retries += 1 } catch {
LoggerProxy.error("could not enhance txId \(transaction.transactionId.toHexStringTxId()) - Error: \(error)") self.retries += 1
if retries > maxRetries { LoggerProxy.error("could not enhance txId \(transaction.transactionId.toHexStringTxId()) - Error: \(error)")
throw error if retries > maxRetries {
throw error
}
} }
} }
} }
} catch {
LoggerProxy.error("error enhancing transactions! \(error)")
self.fail(error: error)
return
} }
} catch {
LoggerProxy.error("error enhancing transactions! \(error)") if let handler = self.txFoundHandler, let foundTxs = try? repository.findConfirmedTransactions(in: self.range, offset: 0, limit: Int.max) {
self.error = error handler(foundTxs, self.range)
self.fail() }
return self.done = true
} }
if let handler = self.txFoundHandler, let foundTxs = try? repository.findConfirmedTransactions(in: self.range, offset: 0, limit: Int.max) { while !done && !isCancelled {
handler(foundTxs, self.range) sleep(1)
} }
} }
@ -112,10 +120,10 @@ class CompactBlockEnhancementOperation: ZcashOperation {
) )
} }
func enhance(transaction: TransactionEntity) throws -> ConfirmedTransactionEntity { func enhance(transaction: TransactionEntity) async throws -> ConfirmedTransactionEntity {
LoggerProxy.debug("Zoom.... Enhance... Tx: \(transaction.transactionId.toHexStringTxId())") LoggerProxy.debug("Zoom.... Enhance... Tx: \(transaction.transactionId.toHexStringTxId())")
let transaction = try downloader.fetchTransaction(txId: transaction.transactionId) let transaction = try await downloader.fetchTransactionAsync(txId: transaction.transactionId)
let transactionID = transaction.transactionId.toHexStringTxId() let transactionID = transaction.transactionId.toHexStringTxId()
let block = String(describing: transaction.minedHeight) let block = String(describing: transaction.minedHeight)
@ -148,6 +156,16 @@ class CompactBlockEnhancementOperation: ZcashOperation {
} }
return confirmedTx return confirmedTx
} }
override func fail(error: Error? = nil) {
self.cancelableTask?.cancel()
super.fail(error: error)
}
override func cancel() {
self.cancelableTask?.cancel()
super.cancel()
}
} }
private extension BlockRange { private extension BlockRange {