[483] OutboundTransactionManager To Async/Await

- protocol methods updated from closure to async APIs
- PersistentTransactionManager updated to implement async APIs
- PersistentTransactionManager deleted closure APIs
- SDKSynchronizer updated to use async APIs
This commit is contained in:
Lukas Korba 2022-09-12 13:27:52 +02:00
parent 7b90e598ad
commit 4a2fcde353
3 changed files with 133 additions and 186 deletions

View File

@ -527,27 +527,18 @@ public class SDKSynchronizer: Synchronizer {
let shieldingSpend = try transactionManager.initSpend(zatoshi: tBalance.verified, toAddress: zAddr, memo: memo, from: 0)
transactionManager.encodeShieldingTransaction(
// TODO: Task will be removed when this method is changed to async, issue 487, https://github.com/zcash/ZcashLightClientKit/issues/487
Task {
do {
let transaction = try await transactionManager.encodeShieldingTransaction(
spendingKey: spendingKey,
tsk: transparentSecretKey,
pendingTransaction: shieldingSpend
) { [weak self] result in
guard let self = self else { return }
)
switch result {
case .success(let transaction):
self.transactionManager.submit(pendingTransaction: transaction) { submitResult in
switch submitResult {
case .success(let submittedTx):
let submittedTx = try await transactionManager.submit(pendingTransaction: transaction)
resultBlock(.success(submittedTx))
case .failure(let submissionError):
DispatchQueue.main.async {
resultBlock(.failure(submissionError))
}
}
}
case .failure(let error):
} catch {
resultBlock(.failure(error))
}
}
@ -574,22 +565,16 @@ public class SDKSynchronizer: Synchronizer {
from: accountIndex
)
transactionManager.encode(spendingKey: spendingKey, pendingTransaction: spend) { [weak self] result in
guard let self = self else { return }
switch result {
case .success(let transaction):
self.transactionManager.submit(pendingTransaction: transaction) { submitResult in
switch submitResult {
case .success(let submittedTx):
// TODO: Task will be removed when this method is changed to async, issue 487, https://github.com/zcash/ZcashLightClientKit/issues/487
Task {
do {
let transaction = try await transactionManager.encode(
spendingKey: spendingKey,
pendingTransaction: spend
)
let submittedTx = try await transactionManager.submit(pendingTransaction: transaction)
resultBlock(.success(submittedTx))
case .failure(let submissionError):
DispatchQueue.main.async {
resultBlock(.failure(submissionError))
}
}
}
case .failure(let error):
} catch {
resultBlock(.failure(error))
}
}

View File

@ -67,33 +67,22 @@ class PersistentTransactionManager: OutboundTransactionManager {
func encodeShieldingTransaction(
spendingKey: String,
tsk: String,
pendingTransaction: PendingTransactionEntity,
result: @escaping (Result<PendingTransactionEntity, Error>) -> Void
) {
queue.async { [weak self] in
guard let self = self else { return }
pendingTransaction: PendingTransactionEntity
) async throws -> PendingTransactionEntity {
let derivationTool = DerivationTool(networkType: self.network)
guard
let viewingKey = try? derivationTool.deriveViewingKey(spendingKey: spendingKey),
let zAddr = try? derivationTool.deriveShieldedAddress(viewingKey: viewingKey)
else {
result(
.failure(
TransactionManagerError.shieldingEncodingFailed(
throw TransactionManagerError.shieldingEncodingFailed(
pendingTransaction,
reason: "There was an error Deriving your keys"
)
)
)
return
}
guard pendingTransaction.toAddress == zAddr else {
result(
.failure(
TransactionManagerError.shieldingEncodingFailed(
throw TransactionManagerError.shieldingEncodingFailed(
pendingTransaction,
reason: """
the recipient address does not match your
@ -104,10 +93,8 @@ class PersistentTransactionManager: OutboundTransactionManager {
of funds
"""
)
)
)
return
}
do {
let encodedTransaction = try self.encoder.createShieldingTransaction(
spendingKey: spendingKey,
@ -126,26 +113,18 @@ class PersistentTransactionManager: OutboundTransactionManager {
try self.repository.update(pending)
result(.success(pending))
return pending
} catch StorageError.updateFailed {
DispatchQueue.main.async {
result(.failure(TransactionManagerError.updateFailed(pendingTransaction)))
}
throw TransactionManagerError.updateFailed(pendingTransaction)
} catch {
DispatchQueue.main.async {
result(.failure(error))
}
}
throw error
}
}
func encode(
spendingKey: String,
pendingTransaction: PendingTransactionEntity,
result: @escaping (Result<PendingTransactionEntity, Error>) -> Void
) {
queue.async { [weak self] in
guard let self = self else { return }
pendingTransaction: PendingTransactionEntity
) async throws -> PendingTransactionEntity {
do {
let encodedTransaction = try self.encoder.createTransaction(
spendingKey: spendingKey,
@ -165,69 +144,52 @@ class PersistentTransactionManager: OutboundTransactionManager {
try self.repository.update(pending)
result(.success(pending))
return pending
} catch StorageError.updateFailed {
DispatchQueue.main.async {
result(.failure(TransactionManagerError.updateFailed(pendingTransaction)))
}
throw TransactionManagerError.updateFailed(pendingTransaction)
} catch {
do {
try self.updateOnFailure(transaction: pendingTransaction, error: error)
} catch {
DispatchQueue.main.async {
result(.failure(TransactionManagerError.updateFailed(pendingTransaction)))
}
}
DispatchQueue.main.async {
result(.failure(error))
}
throw TransactionManagerError.updateFailed(pendingTransaction)
}
throw error
}
}
func submit(
pendingTransaction: PendingTransactionEntity,
result: @escaping (Result<PendingTransactionEntity, Error>) -> Void
) {
pendingTransaction: PendingTransactionEntity
) async throws -> PendingTransactionEntity {
guard let txId = pendingTransaction.id else {
result(.failure(TransactionManagerError.notPending(pendingTransaction)))// this transaction is not stored
return
throw TransactionManagerError.notPending(pendingTransaction) // this transaction is not stored
}
queue.async { [weak self] in
guard let self = self else { return }
do {
guard let storedTx = try self.repository.find(by: txId) else {
result(.failure(TransactionManagerError.notPending(pendingTransaction)))
return
throw TransactionManagerError.notPending(pendingTransaction)
}
guard !storedTx.isCancelled else {
LoggerProxy.debug("ignoring cancelled transaction \(storedTx)")
result(.failure(TransactionManagerError.cancelled(storedTx)))
return
throw TransactionManagerError.cancelled(storedTx)
}
guard let raw = storedTx.raw else {
LoggerProxy.debug("INCONSISTENCY: attempt to send pending transaction \(txId) that has not raw data")
result(.failure(TransactionManagerError.internalInconsistency(storedTx)))
return
throw TransactionManagerError.internalInconsistency(storedTx)
}
let response = try self.service.submit(spendTransaction: raw)
let transaction = try self.update(transaction: storedTx, on: response)
guard response.errorCode >= 0 else {
result(.failure(TransactionManagerError.submitFailed(transaction, errorCode: Int(response.errorCode))))
return
throw TransactionManagerError.submitFailed(transaction, errorCode: Int(response.errorCode))
}
result(.success(transaction))
return transaction
} catch {
try? self.updateOnFailure(transaction: pendingTransaction, error: error)
result(.failure(error))
}
throw error
}
}

View File

@ -16,11 +16,11 @@ transactions through to completion.
protocol OutboundTransactionManager {
func initSpend(zatoshi: Zatoshi, toAddress: String, memo: String?, from accountIndex: Int) throws -> PendingTransactionEntity
func encodeShieldingTransaction(spendingKey: String, tsk: String, pendingTransaction: PendingTransactionEntity, result: @escaping (Result<PendingTransactionEntity, Error>) -> Void)
func encodeShieldingTransaction(spendingKey: String, tsk: String, pendingTransaction: PendingTransactionEntity) async throws -> PendingTransactionEntity
func encode(spendingKey: String, pendingTransaction: PendingTransactionEntity, result: @escaping (Result<PendingTransactionEntity, Error>) -> Void)
func encode(spendingKey: String, pendingTransaction: PendingTransactionEntity) async throws -> PendingTransactionEntity
func submit(pendingTransaction: PendingTransactionEntity, result: @escaping (Result<PendingTransactionEntity, Error>) -> Void)
func submit(pendingTransaction: PendingTransactionEntity) async throws -> PendingTransactionEntity
func applyMinedHeight(pendingTransaction: PendingTransactionEntity, minedHeight: BlockHeight) throws -> PendingTransactionEntity