Merge pull request #1195 from LukasKorba/1189-Implement-continuity-check-and-RewindAction
[#1189] implement continuity check and rewind action
This commit is contained in:
commit
97201f1314
|
@ -13,6 +13,7 @@ actor ActionContext {
|
||||||
var syncControlData: SyncControlData
|
var syncControlData: SyncControlData
|
||||||
let preferredSyncAlgorithm: SyncAlgorithm
|
let preferredSyncAlgorithm: SyncAlgorithm
|
||||||
var supportedSyncAlgorithm: SyncAlgorithm?
|
var supportedSyncAlgorithm: SyncAlgorithm?
|
||||||
|
var requestedRewindHeight: BlockHeight?
|
||||||
var totalProgressRange: CompactBlockRange = 0...0
|
var totalProgressRange: CompactBlockRange = 0...0
|
||||||
var lastScannedHeight: BlockHeight?
|
var lastScannedHeight: BlockHeight?
|
||||||
var lastDownloadedHeight: BlockHeight?
|
var lastDownloadedHeight: BlockHeight?
|
||||||
|
@ -34,6 +35,7 @@ actor ActionContext {
|
||||||
func update(lastDownloadedHeight: BlockHeight) async { self.lastDownloadedHeight = lastDownloadedHeight }
|
func update(lastDownloadedHeight: BlockHeight) async { self.lastDownloadedHeight = lastDownloadedHeight }
|
||||||
func update(lastEnhancedHeight: BlockHeight?) async { self.lastEnhancedHeight = lastEnhancedHeight }
|
func update(lastEnhancedHeight: BlockHeight?) async { self.lastEnhancedHeight = lastEnhancedHeight }
|
||||||
func update(supportedSyncAlgorithm: SyncAlgorithm) async { self.supportedSyncAlgorithm = supportedSyncAlgorithm }
|
func update(supportedSyncAlgorithm: SyncAlgorithm) async { self.supportedSyncAlgorithm = supportedSyncAlgorithm }
|
||||||
|
func update(requestedRewindHeight: BlockHeight) async { self.requestedRewindHeight = requestedRewindHeight }
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CBPState: CaseIterable {
|
enum CBPState: CaseIterable {
|
||||||
|
@ -43,6 +45,7 @@ enum CBPState: CaseIterable {
|
||||||
case updateSubtreeRoots
|
case updateSubtreeRoots
|
||||||
case updateChainTip
|
case updateChainTip
|
||||||
case processSuggestedScanRanges
|
case processSuggestedScanRanges
|
||||||
|
case rewind
|
||||||
case computeSyncControlData
|
case computeSyncControlData
|
||||||
case download
|
case download
|
||||||
case scan
|
case scan
|
||||||
|
|
|
@ -27,13 +27,6 @@ extension ProcessSuggestedScanRangesAction: Action {
|
||||||
let scanRanges = try await rustBackend.suggestScanRanges()
|
let scanRanges = try await rustBackend.suggestScanRanges()
|
||||||
|
|
||||||
if let firstRange = scanRanges.first {
|
if let firstRange = scanRanges.first {
|
||||||
// If there is a range of blocks that needs to be verified, it will always
|
|
||||||
// be returned as the first element of the vector of suggested ranges.
|
|
||||||
if firstRange.priority == .verify {
|
|
||||||
// TODO: [#1189] handle rewind, https://github.com/zcash/ZcashLightClientKit/issues/1189
|
|
||||||
// REWIND to download.start height HERE
|
|
||||||
}
|
|
||||||
|
|
||||||
let lowerBound = firstRange.range.lowerBound - 1
|
let lowerBound = firstRange.range.lowerBound - 1
|
||||||
let upperBound = firstRange.range.upperBound - 1
|
let upperBound = firstRange.range.upperBound - 1
|
||||||
|
|
||||||
|
@ -55,7 +48,14 @@ extension ProcessSuggestedScanRangesAction: Action {
|
||||||
await context.update(syncControlData: syncControlData)
|
await context.update(syncControlData: syncControlData)
|
||||||
await context.update(totalProgressRange: lowerBound...upperBound)
|
await context.update(totalProgressRange: lowerBound...upperBound)
|
||||||
|
|
||||||
|
// If there is a range of blocks that needs to be verified, it will always
|
||||||
|
// be returned as the first element of the vector of suggested ranges.
|
||||||
|
if firstRange.priority == .verify {
|
||||||
|
await context.update(requestedRewindHeight: lowerBound + 1)
|
||||||
|
await context.update(state: .rewind)
|
||||||
|
} else {
|
||||||
await context.update(state: .download)
|
await context.update(state: .download)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await context.update(state: .finished)
|
await context.update(state: .finished)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
//
|
||||||
|
// RewindAction.swift
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// Created by Lukáš Korba on 09.08.2023.
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
final class RewindAction {
|
||||||
|
let downloader: BlockDownloader
|
||||||
|
let rustBackend: ZcashRustBackendWelding
|
||||||
|
let downloaderService: BlockDownloaderService
|
||||||
|
let logger: Logger
|
||||||
|
|
||||||
|
init(container: DIContainer) {
|
||||||
|
downloader = container.resolve(BlockDownloader.self)
|
||||||
|
rustBackend = container.resolve(ZcashRustBackendWelding.self)
|
||||||
|
downloaderService = container.resolve(BlockDownloaderService.self)
|
||||||
|
logger = container.resolve(Logger.self)
|
||||||
|
}
|
||||||
|
|
||||||
|
private func update(context: ActionContext) async -> ActionContext {
|
||||||
|
await context.update(state: .download)
|
||||||
|
return context
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension RewindAction: Action {
|
||||||
|
var removeBlocksCacheWhenFailed: Bool { false }
|
||||||
|
|
||||||
|
func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessor.Event) async -> Void) async throws -> ActionContext {
|
||||||
|
guard let rewindHeight = await context.requestedRewindHeight else {
|
||||||
|
return await update(context: context)
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.debug("Executing rewind.")
|
||||||
|
await downloader.rewind(latestDownloadedBlockHeight: rewindHeight)
|
||||||
|
try await rustBackend.rewindToHeight(height: Int32(rewindHeight))
|
||||||
|
|
||||||
|
// clear cache
|
||||||
|
try await downloaderService.rewind(to: rewindHeight)
|
||||||
|
|
||||||
|
return await update(context: context)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop() async { }
|
||||||
|
}
|
|
@ -63,9 +63,15 @@ extension ScanAction: Action {
|
||||||
// ScanAction is controlled locally so it must report back the updated scanned height
|
// ScanAction is controlled locally so it must report back the updated scanned height
|
||||||
await context.update(lastScannedHeight: lastScannedHeight)
|
await context.update(lastScannedHeight: lastScannedHeight)
|
||||||
}
|
}
|
||||||
|
} catch ZcashError.rustScanBlocks(let errorMsg) {
|
||||||
|
if isContinuityError(errorMsg) {
|
||||||
|
await context.update(requestedRewindHeight: batchRange.lowerBound - 10)
|
||||||
|
await context.update(state: .download)
|
||||||
|
return context
|
||||||
|
} else {
|
||||||
|
throw ZcashError.rustScanBlocks(errorMsg)
|
||||||
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// TODO: [#1189] check isContinuityError, https://github.com/zcash/ZcashLightClientKit/issues/1189
|
|
||||||
// if YES, REWIND to height at what error occured - at least 1 block
|
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,3 +80,11 @@ extension ScanAction: Action {
|
||||||
|
|
||||||
func stop() async { }
|
func stop() async { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private extension ScanAction {
|
||||||
|
func isContinuityError(_ errorMsg: String) -> Bool {
|
||||||
|
errorMsg.contains("The parent hash of proposed block does not correspond to the block hash at height")
|
||||||
|
|| errorMsg.contains("Block height discontinuity at height")
|
||||||
|
|| errorMsg.contains("note commitment tree size provided by a compact block did not match the expected size at height")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -230,6 +230,8 @@ actor CompactBlockProcessor {
|
||||||
action = UpdateChainTipAction(container: container)
|
action = UpdateChainTipAction(container: container)
|
||||||
case .processSuggestedScanRanges:
|
case .processSuggestedScanRanges:
|
||||||
action = ProcessSuggestedScanRangesAction(container: container)
|
action = ProcessSuggestedScanRangesAction(container: container)
|
||||||
|
case .rewind:
|
||||||
|
action = RewindAction(container: container)
|
||||||
case .computeSyncControlData:
|
case .computeSyncControlData:
|
||||||
action = ComputeSyncControlDataAction(container: container, configProvider: configProvider)
|
action = ComputeSyncControlDataAction(container: container, configProvider: configProvider)
|
||||||
case .download:
|
case .download:
|
||||||
|
@ -607,6 +609,8 @@ extension CompactBlockProcessor {
|
||||||
break
|
break
|
||||||
case .processSuggestedScanRanges:
|
case .processSuggestedScanRanges:
|
||||||
break
|
break
|
||||||
|
case .rewind:
|
||||||
|
break
|
||||||
case .computeSyncControlData:
|
case .computeSyncControlData:
|
||||||
break
|
break
|
||||||
case .download:
|
case .download:
|
||||||
|
|
Loading…
Reference in New Issue