-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1454 from LukasKorba/1452-TX-Resubmission-the-wal…
…let-has-to-periodically-resubmit-unmined-transactions [#1452] TX Resubmission-the wallet has to periodically resubmit unmined transactions
- Loading branch information
Showing
17 changed files
with
163 additions
and
11 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
75 changes: 75 additions & 0 deletions
75
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,75 @@ | ||
// | ||
// 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 { | ||
logger.info("TxResubmissionAction check started at \(latestBlockHeight) height.") | ||
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 { | ||
logger.info("TxResubmissionAction trying to resubmit transaction \(transaction.rawID.toHexStringTxId()).") | ||
let encodedTransaction = try transaction.encodedTransaction() | ||
|
||
try await transactionEncoder.submit(transaction: encodedTransaction) | ||
} | ||
} 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
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.