diff --git a/Sources/ZcashLightClientKit/Block/Actions/ComputeSyncRangesAction.swift b/Sources/ZcashLightClientKit/Block/Actions/ComputeSyncRangesAction.swift index 2abc2086..710a88f1 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/ComputeSyncRangesAction.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/ComputeSyncRangesAction.swift @@ -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) diff --git a/Sources/ZcashLightClientKit/Block/Actions/DownloadAction.swift b/Sources/ZcashLightClientKit/Block/Actions/DownloadAction.swift index a957755d..b366e1cc 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/DownloadAction.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/DownloadAction.swift @@ -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) diff --git a/Sources/ZcashLightClientKit/Block/Actions/FetchUTXOsAction.swift b/Sources/ZcashLightClientKit/Block/Actions/FetchUTXOsAction.swift index f75da77b..531ad628 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/FetchUTXOsAction.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/FetchUTXOsAction.swift @@ -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)) } diff --git a/Sources/ZcashLightClientKit/Block/Actions/SaplingParamsAction.swift b/Sources/ZcashLightClientKit/Block/Actions/SaplingParamsAction.swift index 0a0cef96..d61b0699 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/SaplingParamsAction.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/SaplingParamsAction.swift @@ -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 diff --git a/Sources/ZcashLightClientKit/Block/Actions/ScanAction.swift b/Sources/ZcashLightClientKit/Block/Actions/ScanAction.swift index 003c97da..7af5558f 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/ScanAction.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/ScanAction.swift @@ -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 } diff --git a/Sources/ZcashLightClientKit/Block/Actions/ValidateAction.swift b/Sources/ZcashLightClientKit/Block/Actions/ValidateAction.swift index 02505fc4..4c29939d 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/ValidateAction.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/ValidateAction.swift @@ -9,6 +9,7 @@ import Foundation class ValidateAction { let validator: BlockValidator + init(container: DIContainer) { validator = container.resolve(BlockValidator.self) }