[#1047] Implement ScanAction

- scan action with the proper ranges computed

[#1047] Implement ScanAction (#1085)

- fixed logger message
This commit is contained in:
Lukas Korba 2023-05-11 15:20:17 +02:00 committed by Michal Fousek
parent 0fee045ca9
commit 16fb61c774
6 changed files with 51 additions and 3 deletions

View File

@ -13,6 +13,7 @@ class ComputeSyncRangesAction {
let internalSyncProgress: InternalSyncProgress
let latestBlocksDataProvider: LatestBlocksDataProvider
let logger: Logger
init(container: DIContainer, config: CompactBlockProcessorNG.Configuration) {
self.config = config
downloaderService = container.resolve(BlockDownloaderService.self)

View File

@ -11,10 +11,13 @@ class DownloadAction {
let config: CompactBlockProcessorNG.Configuration
let downloader: BlockDownloader
let transactionRepository: TransactionRepository
let logger: Logger
init(container: DIContainer, config: CompactBlockProcessorNG.Configuration) {
self.config = config
downloader = container.resolve(BlockDownloader.self)
transactionRepository = container.resolve(TransactionRepository.self)
logger = container.resolve(Logger.self)
}
private func update(context: ActionContext) async -> ActionContext {
@ -38,6 +41,7 @@ extension DownloadAction: Action {
let batchRange = batchRangeStart...batchRangeStart + config.batchSize
let downloadLimit = batchRange.upperBound + (2 * config.batchSize)
logger.debug("Starting download with range: \(batchRange.lowerBound)...\(batchRange.upperBound)")
try await downloader.setSyncRange(downloadRange)
await downloader.setDownloadLimit(downloadLimit)

View File

@ -9,8 +9,11 @@ import Foundation
class FetchUTXOsAction {
let utxoFetcher: UTXOFetcher
let logger: Logger
init(container: DIContainer) {
utxoFetcher = container.resolve(UTXOFetcher.self)
logger = container.resolve(Logger.self)
}
}
@ -19,6 +22,7 @@ extension FetchUTXOsAction: Action {
func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessorNG.Event) async -> Void) async throws -> ActionContext {
if let range = await context.syncRanges.fetchUTXORange {
logger.debug("Fetching UTXO with range: \(range.lowerBound)...\(range.upperBound)")
let result = try await utxoFetcher.fetch(at: range)
await didUpdate(.storedUTXOs(result))
}

View File

@ -9,8 +9,11 @@ import Foundation
class SaplingParamsAction {
let saplingParametersHandler: SaplingParametersHandler
let logger: Logger
init(container: DIContainer) {
saplingParametersHandler = container.resolve(SaplingParametersHandler.self)
logger = container.resolve(Logger.self)
}
}
@ -18,6 +21,7 @@ extension SaplingParamsAction: Action {
var removeBlocksCacheWhenFailed: Bool { false }
func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessorNG.Event) async -> Void) async throws -> ActionContext {
logger.debug("Fetching sapling parameters")
try await saplingParametersHandler.handleIfNeeded()
await context.update(state: .scanDownloaded)
return context

View File

@ -8,15 +8,49 @@
import Foundation
class ScanAction {
init(container: DIContainer) { }
let config: CompactBlockProcessorNG.Configuration
let blockScanner: BlockScanner
let logger: Logger
let transactionRepository: TransactionRepository
init(container: DIContainer, config: CompactBlockProcessorNG.Configuration) {
self.config = config
blockScanner = container.resolve(BlockScanner.self)
transactionRepository = container.resolve(TransactionRepository.self)
logger = container.resolve(Logger.self)
}
private func update(context: ActionContext) async -> ActionContext {
await context.update(state: .clearAlreadyScannedBlocks)
return context
}
}
extension ScanAction: Action {
var removeBlocksCacheWhenFailed: Bool { true }
func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessorNG.Event) async -> Void) async throws -> ActionContext {
// Scan in range latestScannedHeight...latestScannedHeight+batchSize.
guard let scanRange = await context.syncRanges.downloadAndScanRange else {
return await update(context: context)
}
let lastScannedHeight = try await transactionRepository.lastScannedHeight()
// This action is executed for each batch (batch size is 100 blocks by default) until all the blocks in whole `scanRange` are scanned.
// So the right range for this batch must be computed.
let batchRangeStart = max(scanRange.lowerBound, lastScannedHeight)
let batchRange = batchRangeStart...batchRangeStart + config.batchSize
logger.debug("Starting scan blocks with range: \(batchRange.lowerBound)...\(batchRange.upperBound)")
let totalProgressRange = await context.totalProgressRange
try await blockScanner.scanBlocks(at: batchRange, totalProgressRange: totalProgressRange) { [weak self] lastScannedHeight in
let progress = BlockProgress(
startHeight: totalProgressRange.lowerBound,
targetHeight: totalProgressRange.upperBound,
progressHeight: lastScannedHeight
)
self?.logger.debug("progress: \(progress)")
await didUpdate(.progressUpdated(.syncing(progress)))
}
await context.update(state: .clearAlreadyScannedBlocks)
return context
}

View File

@ -9,6 +9,7 @@ import Foundation
class ValidateAction {
let validator: BlockValidator
init(container: DIContainer) {
validator = container.resolve(BlockValidator.self)
}