From 4a78b4bd16dd8442786be5006d0cfac2590d5ed7 Mon Sep 17 00:00:00 2001 From: Lukas Korba Date: Tue, 1 Aug 2023 13:04:50 +0200 Subject: [PATCH] [#1167] Step 3 - Download chain tip metadata from lightwalletd - update chain tip action added [#1167] Step 3 - Download chain tip metadata from lightwalletd - roots removed from the ActionContext [#1167] Step 3 - Download chain tip metadata from lightwalletd - fallback to linear sync until next step is implemented [#1167] Step 3 - Download chain tip metadata from lightwalletd - offline tests fixed --- .../Block/Actions/Action.swift | 1 + .../Block/Actions/UpdateChainTipAction.swift | 39 +++++++++++++++++++ .../Actions/UpdateSubtreeRootsAction.swift | 4 +- .../Block/CompactBlockProcessor.swift | 4 ++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 Sources/ZcashLightClientKit/Block/Actions/UpdateChainTipAction.swift diff --git a/Sources/ZcashLightClientKit/Block/Actions/Action.swift b/Sources/ZcashLightClientKit/Block/Actions/Action.swift index c643166f..de822fda 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/Action.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/Action.swift @@ -35,6 +35,7 @@ enum CBPState: CaseIterable { case migrateLegacyCacheDB case validateServer case updateSubtreeRoots + case updateChainTip case computeSyncControlData case download case scan diff --git a/Sources/ZcashLightClientKit/Block/Actions/UpdateChainTipAction.swift b/Sources/ZcashLightClientKit/Block/Actions/UpdateChainTipAction.swift new file mode 100644 index 00000000..03e1d476 --- /dev/null +++ b/Sources/ZcashLightClientKit/Block/Actions/UpdateChainTipAction.swift @@ -0,0 +1,39 @@ +// +// UpdateChainTipAction.swift +// +// +// Created by Lukáš Korba on 01.08.2023. +// + +import Foundation + +final class UpdateChainTipAction { + let rustBackend: ZcashRustBackendWelding + let service: LightWalletService + let logger: Logger + + init(container: DIContainer) { + service = container.resolve(LightWalletService.self) + rustBackend = container.resolve(ZcashRustBackendWelding.self) + logger = container.resolve(Logger.self) + } +} + +extension UpdateChainTipAction: Action { + var removeBlocksCacheWhenFailed: Bool { false } + + func run(with context: ActionContext, didUpdate: @escaping (CompactBlockProcessor.Event) async -> Void) async throws -> ActionContext { + let latestBlockHeight = try await service.latestBlockHeight() + + logger.info("Latest block height is \(latestBlockHeight)") + try await rustBackend.updateChainTip(height: Int32(latestBlockHeight)) + + // TODO: [#1169] Switching back to linear sync for now before step 5 & 6 are implemented + // https://github.com/zcash/ZcashLightClientKit/issues/1169 + await context.update(state: .computeSyncControlData) + + return context + } + + func stop() async { } +} diff --git a/Sources/ZcashLightClientKit/Block/Actions/UpdateSubtreeRootsAction.swift b/Sources/ZcashLightClientKit/Block/Actions/UpdateSubtreeRootsAction.swift index efb7d035..c5703760 100644 --- a/Sources/ZcashLightClientKit/Block/Actions/UpdateSubtreeRootsAction.swift +++ b/Sources/ZcashLightClientKit/Block/Actions/UpdateSubtreeRootsAction.swift @@ -52,9 +52,7 @@ extension UpdateSubtreeRootsAction: Action { do { try await rustBackend.putSaplingSubtreeRoots(startIndex: UInt64(request.startIndex), roots: roots) - // TODO: [#1167] Switching back to linear sync for now before step 3 & 4 are implemented - // https://github.com/zcash/ZcashLightClientKit/issues/1167 - await context.update(state: .computeSyncControlData) + await context.update(state: .updateChainTip) } catch { logger.debug("putSaplingSubtreeRoots failed with error \(error.localizedDescription)") throw ZcashError.compactBlockProcessorPutSaplingSubtreeRoots(error) diff --git a/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift b/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift index 95972243..12f2da76 100644 --- a/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift +++ b/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift @@ -216,6 +216,8 @@ actor CompactBlockProcessor { action = ValidateServerAction(container: container, configProvider: configProvider) case .updateSubtreeRoots: action = UpdateSubtreeRootsAction(container: container) + case .updateChainTip: + action = UpdateChainTipAction(container: container) case .computeSyncControlData: action = ComputeSyncControlDataAction(container: container, configProvider: configProvider) case .download: @@ -589,6 +591,8 @@ extension CompactBlockProcessor { break case .updateSubtreeRoots: break + case .updateChainTip: + break case .computeSyncControlData: break case .download: