From ffc7fd1be9c922a3ce4f06267558f12eb1819aa1 Mon Sep 17 00:00:00 2001 From: Lukas Korba Date: Tue, 16 Apr 2024 16:48:20 +0200 Subject: [PATCH] [#1412] Review wipe function and in memory values - The wipe() is now resetting local in memory values of latestBlocksDataProvider. In some case this could cause a reset of CBP's state machine context sooner than it was supposed to, resulting in corrupted sync. --- CHANGELOG.md | 6 ++++++ .../Block/CompactBlockProcessor.swift | 2 ++ .../Providers/LatestBlocksDataProvider.swift | 10 +++++++++- .../GeneratedMocks/AutoMockable.generated.swift | 13 +++++++++++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a04ed1bd..759eba67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,12 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +## Changed +- The database locking mechanism has been changed to use async/await concurrency approach - the DBActor. + +## Fixed +- Call of wipe() resets local (in memory) values. + # 2.1.3 - 2024-03-28 ## Fixed diff --git a/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift b/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift index 81f48917..4fc6d056 100644 --- a/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift +++ b/Sources/ZcashLightClientKit/Block/CompactBlockProcessor.swift @@ -386,6 +386,8 @@ extension CompactBlockProcessor { try await rewindDownloadBlockAction(to: nil) + await latestBlocksDataProvider.reset() + await context.completion(nil) } catch { await context.completion(error) diff --git a/Sources/ZcashLightClientKit/Providers/LatestBlocksDataProvider.swift b/Sources/ZcashLightClientKit/Providers/LatestBlocksDataProvider.swift index d526bf61..3e0ed850 100644 --- a/Sources/ZcashLightClientKit/Providers/LatestBlocksDataProvider.swift +++ b/Sources/ZcashLightClientKit/Providers/LatestBlocksDataProvider.swift @@ -12,7 +12,8 @@ protocol LatestBlocksDataProvider { var maxScannedHeight: BlockHeight { get async } var latestBlockHeight: BlockHeight { get async } var walletBirthday: BlockHeight { get async } - + + func reset() async func updateScannedData() async func updateBlockData() async func updateWalletBirthday(_ walletBirthday: BlockHeight) async @@ -36,6 +37,13 @@ actor LatestBlocksDataProviderImpl: LatestBlocksDataProvider { self.rustBackend = rustBackend } + func reset() async { + fullyScannedHeight = .zero + maxScannedHeight = .zero + latestBlockHeight = .zero + walletBirthday = .zero + } + func updateScannedData() async { fullyScannedHeight = (try? await rustBackend.fullyScannedHeight()) ?? walletBirthday maxScannedHeight = (try? await rustBackend.maxScannedHeight()) ?? walletBirthday diff --git a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift index 77c6fb5e..655c250a 100644 --- a/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift +++ b/Tests/TestUtils/Sourcery/GeneratedMocks/AutoMockable.generated.swift @@ -671,6 +671,19 @@ class LatestBlocksDataProviderMock: LatestBlocksDataProvider { } var underlyingWalletBirthday: BlockHeight! + // MARK: - reset + + var resetCallsCount = 0 + var resetCalled: Bool { + return resetCallsCount > 0 + } + var resetClosure: (() async -> Void)? + + func reset() async { + resetCallsCount += 1 + await resetClosure!() + } + // MARK: - updateScannedData var updateScannedDataCallsCount = 0