From b9e00055e982ecc0f42b679447907255f4be946e Mon Sep 17 00:00:00 2001 From: Francisco Gindre Date: Wed, 9 Nov 2022 18:01:43 -0300 Subject: [PATCH] [#614] make prepare(with:) and get{pool_type}Address() synchronous MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #614 fixes travis ci Removed `setStartHeight` function I’m going back in time and this setHeight is not something we are using anywhere else than in the prepare function. This comes from the times we had `initialize(seed:birthday) and we don’t have that anymore See diff https://github.com/zcash/ZcashLightClientKit/commit/246d10edaa50bee0a2b5efb6be5aa5a9d7e0afdd#diff-414771774e10bc81260094ffcdc7b310a3be48758b2abd2bfae831c83912c02c The `func setStartHeight(_ startHeight: BlockHeight)` function assumes that there might not be a wallet birthday set up or that it might be changed in-flight which are things that are no longer happening. remove test warning Fix test compiler error --- .travis.yml | 2 +- Package.resolved | 4 ++-- .../Processor/CompactBlockProcessor.swift | 15 --------------- .../Block/Processor/FigureNextBatch.swift | 2 +- Sources/ZcashLightClientKit/Initializer.swift | 12 ++++++++++++ .../Synchronizer/SDKSynchronizer.swift | 19 ++++++++++++++++--- .../CompactBlockProcessorTests.swift | 6 +----- .../BlockBatchValidationTests.swift | 5 +++-- 8 files changed, 36 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7e704432..6730f163 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ os: osx osx_image: xcode14 xcode_project: ./Example/ZcashLightClientSample/ZcashLightClientSample.xcodeproj xcode_scheme: ZcashLightClientSample -xcode_destination: platform=iOS Simulator,OS=15.5,name=iPhone 8 +xcode_destination: platform=iOS Simulator,OS=16.0,name=iPhone 14 addons: homebrew: packages: diff --git a/Package.resolved b/Package.resolved index 33336565..3b45ffd6 100644 --- a/Package.resolved +++ b/Package.resolved @@ -86,8 +86,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/zcash-hackworks/zcash-light-client-ffi", "state" : { - "revision" : "70bf6ae6538943f2b8453f424e607a05f4dc7be6", - "version" : "0.1.0-beta.2" + "revision" : "7febbfd74a7d963ec206e01f8ef6090bb8cee523", + "version" : "0.1.0-beta.3" } } ], diff --git a/Sources/ZcashLightClientKit/Block/Processor/CompactBlockProcessor.swift b/Sources/ZcashLightClientKit/Block/Processor/CompactBlockProcessor.swift index 4b993940..b9e9ecc1 100644 --- a/Sources/ZcashLightClientKit/Block/Processor/CompactBlockProcessor.swift +++ b/Sources/ZcashLightClientKit/Block/Processor/CompactBlockProcessor.swift @@ -560,21 +560,6 @@ public actor CompactBlockProcessor { self.lowerBoundHeight = try? downloader.lastDownloadedBlockHeight() return BlockHeight(rewindHeight) } - /** - changes the wallet birthday in configuration. Use this method when wallet birthday is not available and the processor can't be lazy initialized. - - Note: this does not rewind your chain state - - Parameter startHeight: the wallet birthday for this compact block processor - - Throws CompactBlockProcessorError.invalidConfiguration if block height is invalid or if processor is already started - */ - func setStartHeight(_ startHeight: BlockHeight) throws { - guard self.state == .stopped, startHeight >= config.network.constants.saplingActivationHeight else { - throw CompactBlockProcessorError.invalidConfiguration - } - - var config = self.config - config.walletBirthday = startHeight - self.config = config - } func validateServer() async { do { diff --git a/Sources/ZcashLightClientKit/Block/Processor/FigureNextBatch.swift b/Sources/ZcashLightClientKit/Block/Processor/FigureNextBatch.swift index 2b65c04c..bd62a32c 100644 --- a/Sources/ZcashLightClientKit/Block/Processor/FigureNextBatch.swift +++ b/Sources/ZcashLightClientKit/Block/Processor/FigureNextBatch.swift @@ -8,7 +8,7 @@ import Foundation extension CompactBlockProcessor { - enum NextState { + enum NextState: Equatable { case finishProcessing(height: BlockHeight) case processNewBlocks(range: CompactBlockRange, latestBlockHeight: BlockHeight) case wait(latestHeight: BlockHeight, latestDownloadHeight: BlockHeight) diff --git a/Sources/ZcashLightClientKit/Initializer.swift b/Sources/ZcashLightClientKit/Initializer.swift index f3ea6183..933645a6 100644 --- a/Sources/ZcashLightClientKit/Initializer.swift +++ b/Sources/ZcashLightClientKit/Initializer.swift @@ -384,3 +384,15 @@ extension InitializerError: LocalizedError { } } } + + +/// Synchronous helpers that support clients that don't use structured concurrency yet +extension Initializer { + func getCurrentAddress(accountIndex: Int) -> UnifiedAddress? { + try? self.rustBackend.getCurrentAddress( + dbData: self.dataDbURL, + account: Int32(accountIndex), + networkType: self.network.networkType + ) + } +} diff --git a/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift b/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift index e24a4207..f6d83a05 100644 --- a/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift +++ b/Sources/ZcashLightClientKit/Synchronizer/SDKSynchronizer.swift @@ -156,12 +156,11 @@ public class SDKSynchronizer: Synchronizer { } } - public func prepare(with seed: [UInt8]?) async throws -> Initializer.InitializationResult { + public func prepare(with seed: [UInt8]?) throws -> Initializer.InitializationResult { if case .seedRequired = try self.initializer.initialize(with: seed) { return .seedRequired } - try await self.blockProcessor.setStartHeight(initializer.walletBirthday) self.status = .disconnected return .success @@ -624,7 +623,6 @@ public class SDKSynchronizer: Synchronizer { await blockProcessor.getTransparentAddress(accountIndex: accountIndex) } - /// Returns the last stored transparent balance public func getTransparentBalance(accountIndex: Int) async throws -> WalletBalance { try await blockProcessor.getTransparentBalance(accountIndex: accountIndex) @@ -838,6 +836,21 @@ extension SDKSynchronizer { } } +extension SDKSynchronizer { + public func getUnifiedAddress(accountIndex: Int) -> UnifiedAddress? { + self.initializer.getCurrentAddress(accountIndex: accountIndex) + } + + public func getSaplingAddress(accountIndex: Int) -> SaplingAddress? { + self.getUnifiedAddress(accountIndex: accountIndex)?.saplingReceiver() + } + + public func getTransparentAddress(accountIndex: Int) -> TransparentAddress? { + self.getUnifiedAddress(accountIndex: accountIndex)?.transparentReceiver() + } + +} + import GRPC extension ConnectionState { init(_ connectivityState: ConnectivityState) { diff --git a/Tests/NetworkTests/CompactBlockProcessorTests.swift b/Tests/NetworkTests/CompactBlockProcessorTests.swift index d9bec25a..514a740a 100644 --- a/Tests/NetworkTests/CompactBlockProcessorTests.swift +++ b/Tests/NetworkTests/CompactBlockProcessorTests.swift @@ -113,11 +113,7 @@ class CompactBlockProcessorTests: XCTestCase { startedScanningNotificationExpectation.subscribe(to: Notification.Name.blockProcessorStartedScanning, object: processor) idleNotificationExpectation.subscribe(to: Notification.Name.blockProcessorIdle, object: processor) - do { - try await processor.start() - } catch { - XCTFail("shouldn't fail") - } + await processor.start() } // FIXME: disabled see https://github.com/zcash/ZcashLightClientKit/issues/590 diff --git a/Tests/OfflineTests/BlockBatchValidationTests.swift b/Tests/OfflineTests/BlockBatchValidationTests.swift index ec8178eb..24742b73 100644 --- a/Tests/OfflineTests/BlockBatchValidationTests.swift +++ b/Tests/OfflineTests/BlockBatchValidationTests.swift @@ -315,7 +315,8 @@ class BlockBatchValidationTests: XCTestCase { latestHeight: expectedLatestHeight, latestDownloadedHeight: expectedStoreLatestHeight, walletBirthday: walletBirthday - ) + ), + latestBlockHeight: expectedLatestHeight ) let storage = try! TestDbBuilder.inMemoryCompactBlockStorage() let repository = ZcashConsoleFakeStorage(latestBlockHeight: expectedStoreLatestHeight) @@ -368,7 +369,7 @@ class BlockBatchValidationTests: XCTestCase { XCTAssertTrue( { switch nextBatch { - case .processNewBlocks(range: CompactBlockRange(uncheckedBounds: (expectedStoreLatestHeight + 1, expectedLatestHeight))): + case .processNewBlocks(range: CompactBlockRange(uncheckedBounds: (expectedStoreLatestHeight + 1, expectedLatestHeight)), latestBlockHeight: expectedLatestHeight): return true default: return false