forked from Electric-Coin-Company/zcash-swift-wallet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Electric-Coin-Company#1452] TX Resubmission-the wallet has to period…
…ically resubmit unmined transactions - functional version is done [Electric-Coin-Company#1452] TX Resubmission-the wallet has to periodically resubmit unmined transactions - code cleanup [Electric-Coin-Company#1452] TX Resubmission-the wallet has to periodically resubmit unmined transactions - changelog updated [Electric-Coin-Company#1452] TX Resubmission-the wallet has to periodically resubmit unmined transactions - unit tests fixed - mocks generated
- Loading branch information
1 parent
d9a706d
commit d6526c9
Showing
15 changed files
with
153 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
Sources/ZcashLightClientKit/Block/Actions/TxResubmissionAction.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// | ||
// TxResubmissionAction.swift | ||
// | ||
// | ||
// Created by Lukas Korba on 06-17-2024. | ||
// | ||
|
||
import Foundation | ||
|
||
final class TxResubmissionAction { | ||
private enum Constants { | ||
static let thresholdToTrigger = TimeInterval(300.0) | ||
} | ||
|
||
var latestResolvedTime: TimeInterval = 0 | ||
let transactionRepository: TransactionRepository | ||
let transactionEncoder: TransactionEncoder | ||
let logger: Logger | ||
|
||
init(container: DIContainer) { | ||
transactionRepository = container.resolve(TransactionRepository.self) | ||
transactionEncoder = container.resolve(TransactionEncoder.self) | ||
logger = container.resolve(Logger.self) | ||
} | ||
} | ||
|
||
extension TxResubmissionAction: Action { | ||
var removeBlocksCacheWhenFailed: Bool { true } | ||
|
||
func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessor.Event) async -> Void) async throws -> ActionContext { | ||
let latestBlockHeight = await context.syncControlData.latestBlockHeight | ||
|
||
// find all candidates for the resubmission | ||
do { | ||
let transactions = try await transactionRepository.findForResubmission(upTo: latestBlockHeight) | ||
|
||
// no candidates, update the time and continue with the next action | ||
if transactions.isEmpty { | ||
latestResolvedTime = Date().timeIntervalSince1970 | ||
} else { | ||
let now = Date().timeIntervalSince1970 | ||
let diff = now - latestResolvedTime | ||
|
||
// the last time resubmission was triggered is more than 5 minutes ago so try again | ||
if diff > Constants.thresholdToTrigger { | ||
// resubmission | ||
do { | ||
for transaction in transactions { | ||
let encodedTransaction = try transaction.encodedTransaction() | ||
|
||
try await transactionEncoder.submit(transaction: encodedTransaction) | ||
logger.info("TxResubmissionAction trying to resubmit transaction") | ||
} | ||
} catch { | ||
logger.error("TxResubmissionAction failed to resubmit candidates") | ||
} | ||
|
||
latestResolvedTime = Date().timeIntervalSince1970 | ||
} | ||
} | ||
} catch { | ||
logger.error("TxResubmissionAction failed to find candidates") | ||
} | ||
|
||
if await context.prevState == .enhance { | ||
await context.update(state: .updateChainTip) | ||
} else { | ||
await context.update(state: .finished) | ||
} | ||
return context | ||
} | ||
|
||
func stop() async { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters