parent
d7aca1fae2
commit
f2d15b2989
|
@ -8,7 +8,33 @@
|
||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class EnhanceAction {
|
class EnhanceAction {
|
||||||
init(container: DIContainer) { }
|
let blockEnhancer: BlockEnhancer
|
||||||
|
let config: CompactBlockProcessorNG.Configuration
|
||||||
|
let internalSyncProgress: InternalSyncProgress
|
||||||
|
let logger: Logger
|
||||||
|
let transactionRepository: TransactionRepository
|
||||||
|
init(container: DIContainer, config: CompactBlockProcessorNG.Configuration) {
|
||||||
|
blockEnhancer = container.resolve(BlockEnhancer.self)
|
||||||
|
self.config = config
|
||||||
|
internalSyncProgress = container.resolve(InternalSyncProgress.self)
|
||||||
|
logger = container.resolve(Logger.self)
|
||||||
|
transactionRepository = container.resolve(TransactionRepository.self)
|
||||||
|
}
|
||||||
|
|
||||||
|
func decideWhatToDoNext(context: ActionContext, lastScannedHeight: BlockHeight) async -> ActionContext {
|
||||||
|
guard let downloadAndScanRange = await context.syncRanges.downloadAndScanRange else {
|
||||||
|
await context.update(state: .clearCache)
|
||||||
|
return context
|
||||||
|
}
|
||||||
|
|
||||||
|
if lastScannedHeight >= downloadAndScanRange.upperBound {
|
||||||
|
await context.update(state: .clearCache)
|
||||||
|
} else {
|
||||||
|
await context.update(state: .download)
|
||||||
|
}
|
||||||
|
|
||||||
|
return context
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension EnhanceAction: Action {
|
extension EnhanceAction: Action {
|
||||||
|
@ -23,8 +49,25 @@ extension EnhanceAction: Action {
|
||||||
// If latestScannedHeight < context.scanRanges.downloadAndScanRange?.upperBound thne set state to `download` because there are blocks to
|
// If latestScannedHeight < context.scanRanges.downloadAndScanRange?.upperBound thne set state to `download` because there are blocks to
|
||||||
// download and scan.
|
// download and scan.
|
||||||
|
|
||||||
await context.update(state: .clearCache)
|
let lastScannedHeight = try await transactionRepository.lastScannedHeight()
|
||||||
return context
|
|
||||||
|
guard let range = await context.syncRanges.enhanceRange else {
|
||||||
|
return await decideWhatToDoNext(context: context, lastScannedHeight: lastScannedHeight)
|
||||||
|
}
|
||||||
|
|
||||||
|
let lastEnhancedHeight = await internalSyncProgress.load(.latestEnhancedHeight)
|
||||||
|
let enhanceRangeStart = max(range.lowerBound, lastEnhancedHeight)
|
||||||
|
let enhanceRangeEnd = min(range.upperBound, lastScannedHeight)
|
||||||
|
|
||||||
|
if enhanceRangeStart <= enhanceRangeEnd && lastEnhancedHeight - lastScannedHeight >= config.enhanceBatchSize {
|
||||||
|
let enhanceRange = enhanceRangeStart...enhanceRangeEnd
|
||||||
|
let transactions = try await blockEnhancer.enhance(at: enhanceRange) { progress in
|
||||||
|
await didUpdate(.progressUpdated(.enhance(progress)))
|
||||||
|
}
|
||||||
|
await didUpdate(.foundTransactions(transactions, enhanceRange))
|
||||||
|
}
|
||||||
|
|
||||||
|
return await decideWhatToDoNext(context: context, lastScannedHeight: lastScannedHeight)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop() async { }
|
func stop() async { }
|
||||||
|
|
|
@ -49,6 +49,7 @@ actor CompactBlockProcessorNG {
|
||||||
let dataDb: URL
|
let dataDb: URL
|
||||||
let spendParamsURL: URL
|
let spendParamsURL: URL
|
||||||
let outputParamsURL: URL
|
let outputParamsURL: URL
|
||||||
|
let enhanceBatchSize: Int
|
||||||
let batchSize: Int
|
let batchSize: Int
|
||||||
let retries: Int
|
let retries: Int
|
||||||
let maxBackoffInterval: TimeInterval
|
let maxBackoffInterval: TimeInterval
|
||||||
|
@ -72,6 +73,7 @@ actor CompactBlockProcessorNG {
|
||||||
spendParamsURL: URL,
|
spendParamsURL: URL,
|
||||||
outputParamsURL: URL,
|
outputParamsURL: URL,
|
||||||
saplingParamsSourceURL: SaplingParamsSourceURL,
|
saplingParamsSourceURL: SaplingParamsSourceURL,
|
||||||
|
enhanceBatchSize: Int = ZcashSDK.DefaultEnhanceBatch,
|
||||||
batchSize: Int = ZcashSDK.DefaultSyncBatch,
|
batchSize: Int = ZcashSDK.DefaultSyncBatch,
|
||||||
retries: Int = ZcashSDK.defaultRetries,
|
retries: Int = ZcashSDK.defaultRetries,
|
||||||
maxBackoffInterval: TimeInterval = ZcashSDK.defaultMaxBackOffInterval,
|
maxBackoffInterval: TimeInterval = ZcashSDK.defaultMaxBackOffInterval,
|
||||||
|
@ -87,6 +89,7 @@ actor CompactBlockProcessorNG {
|
||||||
self.outputParamsURL = outputParamsURL
|
self.outputParamsURL = outputParamsURL
|
||||||
self.saplingParamsSourceURL = saplingParamsSourceURL
|
self.saplingParamsSourceURL = saplingParamsSourceURL
|
||||||
self.network = network
|
self.network = network
|
||||||
|
self.enhanceBatchSize = enhanceBatchSize
|
||||||
self.batchSize = batchSize
|
self.batchSize = batchSize
|
||||||
self.retries = retries
|
self.retries = retries
|
||||||
self.maxBackoffInterval = maxBackoffInterval
|
self.maxBackoffInterval = maxBackoffInterval
|
||||||
|
@ -103,6 +106,7 @@ actor CompactBlockProcessorNG {
|
||||||
spendParamsURL: URL,
|
spendParamsURL: URL,
|
||||||
outputParamsURL: URL,
|
outputParamsURL: URL,
|
||||||
saplingParamsSourceURL: SaplingParamsSourceURL,
|
saplingParamsSourceURL: SaplingParamsSourceURL,
|
||||||
|
enhanceBatchSize: Int = ZcashSDK.DefaultEnhanceBatch,
|
||||||
batchSize: Int = ZcashSDK.DefaultSyncBatch,
|
batchSize: Int = ZcashSDK.DefaultSyncBatch,
|
||||||
retries: Int = ZcashSDK.defaultRetries,
|
retries: Int = ZcashSDK.defaultRetries,
|
||||||
maxBackoffInterval: TimeInterval = ZcashSDK.defaultMaxBackOffInterval,
|
maxBackoffInterval: TimeInterval = ZcashSDK.defaultMaxBackOffInterval,
|
||||||
|
@ -120,6 +124,7 @@ actor CompactBlockProcessorNG {
|
||||||
self.saplingActivation = network.constants.saplingActivationHeight
|
self.saplingActivation = network.constants.saplingActivationHeight
|
||||||
self.network = network
|
self.network = network
|
||||||
self.cacheDbURL = nil
|
self.cacheDbURL = nil
|
||||||
|
self.enhanceBatchSize = enhanceBatchSize
|
||||||
self.batchSize = batchSize
|
self.batchSize = batchSize
|
||||||
self.retries = retries
|
self.retries = retries
|
||||||
self.maxBackoffInterval = maxBackoffInterval
|
self.maxBackoffInterval = maxBackoffInterval
|
||||||
|
@ -208,7 +213,7 @@ actor CompactBlockProcessorNG {
|
||||||
case .clearAlreadyScannedBlocks:
|
case .clearAlreadyScannedBlocks:
|
||||||
action = ClearAlreadyScannedBlocksAction(container: container)
|
action = ClearAlreadyScannedBlocksAction(container: container)
|
||||||
case .enhance:
|
case .enhance:
|
||||||
action = EnhanceAction(container: container)
|
action = EnhanceAction(container: container, config: config)
|
||||||
case .fetchUTXO:
|
case .fetchUTXO:
|
||||||
action = FetchUTXOsAction(container: container)
|
action = FetchUTXOsAction(container: container)
|
||||||
case .handleSaplingParams:
|
case .handleSaplingParams:
|
||||||
|
|
|
@ -104,6 +104,9 @@ public enum ZcashSDK {
|
||||||
/// times three is held in memory at some point of the sync process.
|
/// times three is held in memory at some point of the sync process.
|
||||||
public static let DefaultSyncBatch = 100
|
public static let DefaultSyncBatch = 100
|
||||||
|
|
||||||
|
/// Default batch size for enhancing transactions for the compact block processor
|
||||||
|
public static let DefaultEnhanceBatch = 1000
|
||||||
|
|
||||||
/// Default amount of time, in in seconds, to poll for new blocks. Typically, this should be about half the average
|
/// Default amount of time, in in seconds, to poll for new blocks. Typically, this should be about half the average
|
||||||
/// block time.
|
/// block time.
|
||||||
public static let defaultPollInterval: TimeInterval = 20
|
public static let defaultPollInterval: TimeInterval = 20
|
||||||
|
|
Loading…
Reference in New Issue