From 8e445f8b090532a17f110c87e8957cc12fd90d79 Mon Sep 17 00:00:00 2001 From: Michal Fousek Date: Fri, 3 Mar 2023 18:58:53 +0100 Subject: [PATCH] [#613] Adopt ZcashLightClientKit version 0.19.0-beta (#616) Closes #613 - Only change was in usage of rewind function. So it's changed. - And when rewind is done then start of the sync process is called. So rewind now really works without app restart. --- secant.xcodeproj/project.pbxproj | 4 +- .../xcshareddata/swiftpm/Package.resolved | 3 +- .../SDKSynchronizerInterface.swift | 2 +- .../SDKSynchronizer/SDKSynchronizerLive.swift | 4 +- .../SDKSynchronizerMocks.swift | 2 +- .../SDKSynchronizer/SDKSynchronizerTest.swift | 8 ++-- secant/Features/Root/RootDebug.swift | 48 ++++++++++++------- secantTests/RootTests/DebugTests.swift | 4 ++ 8 files changed, 47 insertions(+), 28 deletions(-) diff --git a/secant.xcodeproj/project.pbxproj b/secant.xcodeproj/project.pbxproj index b493fc79..018c159f 100644 --- a/secant.xcodeproj/project.pbxproj +++ b/secant.xcodeproj/project.pbxproj @@ -3567,8 +3567,8 @@ isa = XCRemoteSwiftPackageReference; repositoryURL = "https://github.com/zcash/ZcashLightClientKit/"; requirement = { - kind = revision; - revision = 9bfbdcb4c5ce315beb2e6bf94e982e7e10703707; + kind = exactVersion; + version = "0.19.0-beta"; }; }; 6654C7382715A38000901167 /* XCRemoteSwiftPackageReference "swift-composable-architecture" */ = { diff --git a/secant.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/secant.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index cd591fb6..e2a6ea59 100644 --- a/secant.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/secant.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -338,7 +338,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/zcash/ZcashLightClientKit/", "state" : { - "revision" : "9bfbdcb4c5ce315beb2e6bf94e982e7e10703707" + "revision" : "763d3eaeee333b1c7a592d8c2810847d00a50ee1", + "version" : "0.19.0-beta" } } ], diff --git a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerInterface.swift b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerInterface.swift index c6175aa8..e617b623 100644 --- a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerInterface.swift +++ b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerInterface.swift @@ -44,7 +44,7 @@ protocol SDKSynchronizerClient { func isSyncing() -> Bool func isInitialized() -> Bool - func rewind(_ policy: RewindPolicy) async throws + func rewind(_ policy: RewindPolicy) -> AnyPublisher? func getShieldedBalance() -> WalletBalance? func getTransparentBalance() -> WalletBalance? diff --git a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerLive.swift b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerLive.swift index 76f6756f..664afdd5 100644 --- a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerLive.swift +++ b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerLive.swift @@ -109,8 +109,8 @@ class LiveSDKSynchronizerClient: SDKSynchronizerClient { return SyncStatusSnapshot.snapshotFor(state: synchronizer.status) } - func rewind(_ policy: RewindPolicy) async throws { - try await synchronizer?.rewind(policy) + func rewind(_ policy: RewindPolicy) -> AnyPublisher? { + return synchronizer?.rewind(policy) } func getShieldedBalance() -> WalletBalance? { diff --git a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerMocks.swift b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerMocks.swift index 4222c575..7ab38d69 100644 --- a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerMocks.swift +++ b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerMocks.swift @@ -41,7 +41,7 @@ class MockSDKSynchronizerClient: SDKSynchronizerClient { func statusSnapshot() -> SyncStatusSnapshot { .default } - func rewind(_ policy: RewindPolicy) throws { } + func rewind(_ policy: RewindPolicy) -> AnyPublisher? { Empty().eraseToAnyPublisher() } func getShieldedBalance() -> WalletBalance? { WalletBalance(verified: Zatoshi(12345000), total: Zatoshi(12345000)) diff --git a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerTest.swift b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerTest.swift index 9b96b62e..40945866 100644 --- a/secant/Dependencies/SDKSynchronizer/SDKSynchronizerTest.swift +++ b/secant/Dependencies/SDKSynchronizer/SDKSynchronizerTest.swift @@ -5,9 +5,9 @@ // Created by Lukáš Korba on 15.11.2022. // -import Foundation import Combine import ComposableArchitecture +import Foundation import ZcashLightClientKit extension SDKSynchronizerDependency: TestDependencyKey { @@ -40,8 +40,8 @@ class NoopSDKSynchronizer: SDKSynchronizerClient { func statusSnapshot() -> SyncStatusSnapshot { .default } - func rewind(_ policy: RewindPolicy) async throws { } - + func rewind(_ policy: RewindPolicy) -> AnyPublisher? { Empty().eraseToAnyPublisher() } + func getShieldedBalance() -> WalletBalance? { nil } func getTransparentBalance() -> WalletBalance? { nil } @@ -104,7 +104,7 @@ class TestSDKSynchronizerClient: SDKSynchronizerClient { func statusSnapshot() -> SyncStatusSnapshot { .default } - func rewind(_ policy: RewindPolicy) throws { } + func rewind(_ policy: RewindPolicy) -> AnyPublisher? { nil } func getShieldedBalance() -> WalletBalance? { nil } diff --git a/secant/Features/Root/RootDebug.swift b/secant/Features/Root/RootDebug.swift index f0cc459e..a18bf342 100644 --- a/secant/Features/Root/RootDebug.swift +++ b/secant/Features/Root/RootDebug.swift @@ -5,8 +5,9 @@ // Created by Lukáš Korba on 02.03.2023. // -import Foundation +import Combine import ComposableArchitecture +import Foundation import ZcashLightClientKit /// In this file is a collection of helpers that control all state and action related operations @@ -54,25 +55,11 @@ extension RootReducer { case .debug(.quickRescan): state.destinationState.destination = .home - return .run { send in - do { - try await sdkSynchronizer.rewind(.quick) - await send(.debug(.rewindDone(nil, .debug(.quickRescan)))) - } catch { - await send(.debug(.rewindDone(error.localizedDescription, .debug(.quickRescan)))) - } - } + return rewind(policy: .quick, sourceAction: .quickRescan) case .debug(.fullRescan): state.destinationState.destination = .home - return .run { send in - do { - try await sdkSynchronizer.rewind(.birthday) - await send(.debug(.rewindDone(nil, .debug(.fullRescan)))) - } catch { - await send(.debug(.rewindDone(error.localizedDescription, .debug(.fullRescan)))) - } - } + return rewind(policy: .birthday, sourceAction: .fullRescan) case let .debug(.rewindDone(errorDescription, _)): if let errorDescription { @@ -82,7 +69,19 @@ extension RootReducer { message: TextState("Error: \(errorDescription)"), dismissButton: .default(TextState("Ok"), action: .send(.dismissAlert)) ) + } else { + do { + try sdkSynchronizer.start() + } catch { + // TODO: [#221] Handle error more properly (https://github.com/zcash/secant-ios-wallet/issues/221) + state.alert = AlertState( + title: TextState("Can't start sync process after rewind"), + message: TextState("Error: \(error.localizedDescription)"), + dismissButton: .default(TextState("Ok"), action: .send(.dismissAlert)) + ) + } } + return .none case let .debug(.updateFlag(flag, isEnabled)): @@ -106,6 +105,21 @@ extension RootReducer { } } } + + private func rewind(policy: RewindPolicy, sourceAction: DebugAction) -> EffectPublisher { + guard let rewindPublisher = sdkSynchronizer.rewind(policy) else { + return EffectTask(value: .debug(.rewindDone("SDKSynchronizer not initilized. rewindPublisher is nil", .debug(sourceAction)))) + } + return rewindPublisher + .replaceEmpty(with: Void()) + .map { _ in return RootReducer.Action.debug(.rewindDone(nil, .debug(sourceAction))) } + .catch { error in + return Just(RootReducer.Action.debug(.rewindDone(error.localizedDescription, .debug(sourceAction)))).eraseToAnyPublisher() + } + .receive(on: mainQueue) + .eraseToEffect() + .cancellable(id: SynchronizerCancelId.self, cancelInFlight: true) + } } // MARK: Placeholders diff --git a/secantTests/RootTests/DebugTests.swift b/secantTests/RootTests/DebugTests.swift index 883f9d51..30ad36d3 100644 --- a/secantTests/RootTests/DebugTests.swift +++ b/secantTests/RootTests/DebugTests.swift @@ -71,6 +71,8 @@ class DebugTests: XCTestCase { reducer: RootReducer() ) + store.dependencies.mainQueue = .immediate + await store.send(.debug(.quickRescan)) { state in state.destinationState.internalDestination = .home state.destinationState.previousDestination = .welcome @@ -96,6 +98,8 @@ class DebugTests: XCTestCase { initialState: mockState, reducer: RootReducer() ) + + store.dependencies.mainQueue = .immediate await store.send(.debug(.fullRescan)) { state in state.destinationState.internalDestination = .home