Removed confirmed pending transactions (#52)

This commit is contained in:
Francisco Gindre 2019-12-17 18:16:26 -03:00 committed by GitHub
parent 97ab864cbe
commit 5ad77abf46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 16 deletions

View File

@ -175,6 +175,15 @@ class PersistentTransactionManager: OutboundTransactionManager {
try repository.update(tx)
return tx
}
func delete(pendingTransaction: PendingTransactionEntity) throws {
do {
try repository.delete(pendingTransaction)
} catch {
throw TransactionManagerError.notPending(tx: pendingTransaction)
}
}
}
class OutboundTransactionManagerBuilder {

View File

@ -31,5 +31,10 @@ protocol OutboundTransactionManager {
func allPendingTransactions() throws -> [PendingTransactionEntity]?
func handleReorg(at: BlockHeight) throws
func handleReorg(at: BlockHeight) throws
/**
deletes a pending transaction from the database
*/
func delete(pendingTransaction: PendingTransactionEntity) throws
}

View File

@ -244,9 +244,12 @@ public class SDKSynchronizer: Synchronizer {
DispatchQueue.main.async { self.status = .disconnected }
}
@objc func processorFinished(_ notification: Notification) {
refreshPendingTransactions()
DispatchQueue.main.async {
self.status = .synced }
DispatchQueue.global().async {
self.refreshPendingTransactions()
DispatchQueue.main.async {
self.status = .synced
}
}
}
@objc func processorTransitionUnknown(_ notification: Notification) {
@ -382,20 +385,30 @@ public class SDKSynchronizer: Synchronizer {
}
// MARK: book keeping
private func updateMinedTransactions() throws {
try transactionManager.allPendingTransactions()?.filter( { $0.isSubmitSuccess && !$0.isMined } ).forEach( { pendingTx in
guard let rawId = pendingTx.rawTransactionId else { return }
let tx = try transactionRepository.findBy(rawId: rawId)
guard let minedHeight = tx?.minedHeight else { return }
let minedTx = try transactionManager.applyMinedHeight(pendingTransaction: pendingTx, minedHeight: minedHeight)
notifyMinedTransaction(minedTx)
})
}
private func removeConfirmedTransactions() throws {
let latestHeight = try transactionRepository.lastScannedHeight()
try transactionManager.allPendingTransactions()?.filter( { abs($0.minedHeight - latestHeight) >= DEFAULT_REWIND_DISTANCE } ).forEach( { try transactionManager.delete(pendingTransaction: $0) } )
}
private func refreshPendingTransactions() {
do {
try transactionManager.allPendingTransactions()?.filter({ $0.isSubmitSuccess && !$0.isMined }).forEach( { pendingTx in
guard let rawId = pendingTx.rawTransactionId else { return }
let tx = try transactionRepository.findBy(rawId: rawId)
guard let minedHeight = tx?.minedHeight else { return }
let minedTx = try transactionManager.applyMinedHeight(pendingTransaction: pendingTx, minedHeight: minedHeight)
notifyMinedTransaction(minedTx)
})
try updateMinedTransactions()
try removeConfirmedTransactions()
} catch {
print("error refreshing pending transactions: \(error)")
}