ZcashLightClientKit/Tests/DarksideTests/SynchronizerTests.swift

333 lines
12 KiB
Swift
Raw Normal View History

//
// SynchronizerTests.swift
// DarksideTests
//
// Created by Francisco Gindre on 9/16/22.
//
import Combine
import XCTest
@testable import TestUtils
@testable import ZcashLightClientKit
final class SynchronizerTests: ZcashTestCase {
let sendAmount = Zatoshi(1000)
var birthday: BlockHeight = 663150
let defaultLatestHeight: BlockHeight = 663175
var coordinator: TestCoordinator!
var expectedReorgHeight: BlockHeight = 665188
var expectedRewindHeight: BlockHeight = 665188
var reorgExpectation = XCTestExpectation(description: "reorg")
let branchID = "2bb40e60"
let chainName = "main"
let network = DarksideWalletDNetwork()
var cancellables: [AnyCancellable] = []
var sdkSynchronizerInternalSyncStatusHandler: SDKSynchronizerInternalSyncStatusHandler! = SDKSynchronizerInternalSyncStatusHandler()
2023-03-30 10:01:47 -07:00
override func setUp() async throws {
try await super.setUp()
2023-03-30 10:01:47 -07:00
// don't use an exact birthday, users never do.
self.coordinator = try await TestCoordinator(
container: mockContainer,
walletBirthday: birthday + 50,
network: network
)
try await coordinator.reset(saplingActivation: 663150, startSaplingTreeSize: 128607, startOrchardTreeSize: 0, branchID: self.branchID, chainName: self.chainName)
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
let eventClosure: CompactBlockProcessor.EventClosure = { [weak self] event in
switch event {
case .handledReorg: self?.handleReorg(event: event)
default: break
}
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
}
2023-03-30 10:01:47 -07:00
await self.coordinator.synchronizer.blockProcessor.updateEventClosure(identifier: "tests", closure: eventClosure)
}
2023-03-30 10:01:47 -07:00
override func tearDown() async throws {
try await super.tearDown()
let coordinator = self.coordinator!
self.coordinator = nil
sdkSynchronizerInternalSyncStatusHandler = nil
2023-03-30 10:01:47 -07:00
cancellables = []
try await coordinator.stop()
- [#679] Implementation of the File-system based block cache (#679) Closes https://github.com/zcash/ZcashLightClientKit/issues/697 Closes https://github.com/zcash/ZcashLightClientKit/issues/720 Closes https://github.com/zcash/ZcashLightClientKit/issues/587 Closes https://github.com/zcash/ZcashLightClientKit/issues/667 Closes https://github.com/zcash/ZcashLightClientKit/issues/443 Closes https://github.com/zcash/ZcashLightClientKit/issues/754 - [#790] Fix ShieldFundsTests Closes #790 Removes comments on `ShieldFundsTests` since those issues have been fixed Depends on zcash-light-client-ffi changes that adopt newer versions of librustzcash crates `zcash_primitives 0.10`, `zcash_client_backend 0.7`, `zcash_proofs 0.10`, `zcash_client_sqlite 0.5.0`. Also allows wallets to define a shielding_threshold and will set foundations to customize minimum confirmations for balances, spends and shielding operations. **Test Bootstrapping** - `ZcashCompactBlockDescriptor`: struct that holds functions to describe blocks as filenames and compare those filenames `ZcashCompactBlockDescriptor.live` has the actual implementation but it can be replaced by mocks if needed on Tests main implementations are held under `FSCompactBlockRepository.filenameDescription` and `FSCompactBlockRepository.filenameComparison` on a separate extention `DirectoryListingProviders` provide two default implementations of listing a directory deterministically. `FileManager` does not define a sorting and needs to be done in-memory by calling `.sorted()` on the resulting collection. If this is a big toll on performance it can be changed to a POSIX implementation but this is good for now. `ZcashCompactBlockDescriptor` adds a `height` helper function to turn a filename into the height of the block stored. Implemented `func latestHeight() throws -> BlockHeight ` that returns the blockheight by querying the cache directory in a sorted fashion and getting the last value and turning the filename into a `BlockHeight` Added `Meta` struct to ZcashCompactBlock. Tests implemented: - `filterBlockFiles` - `testClearTheCache` - `testLatestHeightEmptyCacheThrows` - `testLatestHeightEmptyCacheThrowsAsync` - `testRewindEmptyCacheDoesNothing` - `testRewindEmptyCacheDoesNothingAsync` - `testWhenBlockIsStoredItFollowsTheDescribedFormat` - `testWhenBlockIsStoredItFollowsTheFilenameConvention` - `testGetLatestHeight` - `testRewindDeletesTheRightBlocks` test - `testPerformanceExample` test. This isn't a real performance test because the API doesn't work with async/await yet adopts `shield_funds` shielding threshold parameter Implements `initBlockMetadataDb` and fix tests Renames dbCache parameter to `fsBlockDbRoot`. Builds but tests don't pass. Removes cacheDb uses from code. Testing utilities still persist. Added needed information in MIGRATING and CHANGELOG. Added helper to perform deletion of legacy db and creation a the new file system backed cache. Renames parameters and changes code where needed. Network Constants turned into `enum` with static methods. DeletelastDownloadedBlock helper from initializer Removes CompactBlockStorage and CompactBlockEntity. Implements `latestCachedBlockHeight` on rustbackend. *Replaces dependencies on ZcashRustWelding with `FSMetadataStore`* This allows the tests to not depend in a particular implementation of either the MockRustBackend of or ZcashRustBackend. Also provides a way to test errors properly and switch implementations of critical areas like `writeBlocks`.
2023-02-02 08:58:12 -08:00
try? FileManager.default.removeItem(at: coordinator.databases.fsCacheDbRoot)
try? FileManager.default.removeItem(at: coordinator.databases.dataDB)
}
func handleReorg(event: CompactBlockProcessor.Event) {
guard case let .handledReorg(reorgHeight, rewindHeight) = event else { return XCTFail("empty reorg notification") }
[#209] Add support for multiple instances of the SDKSynchronizer Closes #209. [#845] Introduce ZcashSynchronizerAlias enum Closes #845. [#852] SDKSynchronizer using queues label based on the alias Closes #852. [#847] Remove posibility to use DatabaseStorageManager as singleton Closes #847. [#850] Remove synchronizerConnectionStateChanged notification Closes #850. [#855] Add check if the Alias is already used Closes #855 - Added `UsedAliasesChecker` utility which is used to register aliases that are in use. - `prepare()` and `wipe()` methods now check if the current alias can't be used and if not then `InitializerError.aliasAlreadyInUse` is thrown/emitted. - Some public methods that could cause harm if used with the Alias that is already in use now throw `SynchronizerError.notPrepared`. Thanks to this the client app is forced to call `prepare()` first. And `prepare()` does check for the Alias. - Added tests for new conditions. [#849] Make InternalSyncProgress aware of the Alias Closes #849. [#853] Only instance with default Alias migrates legacy cache DB Closes #853. [#851] Apply the Alias to the URLs Closes #851. - `Initializer` now updates paths according to alias before paths are used anywhere in the SDK. - Paths update can fail. It would be incovenient for the client apps to handle errors thrown from `Initiliazer` constructor. So the error is then handled in `SDKSynchronizer.prepare()` or `SDKSynchronizer.wipe()`. [#846] Stop using SDKMetrics as singleton (#862) - metrics are not longer a singleton - tests fixed - metrics outside init of the synchronizer [#848] Make logger aware of the alias - logger is now an instance passed throughout the sdk instead of a static proxy [#848] Make logger aware of the alias (#868) - comments addressed [#848] Make logger aware of the alias (#868) - returning protocol back Fix typos [#856] Add possibility to test multiple synchronizers in the sample app Closes #856. - Added `alias` property to `Synchronizer`. - Added `SyncBlocksListViewController` which provides UI to use multiple synchronizers at once. [#209] Add changelog - Add changelog for #209. - Overall improve readability of the rendered changelog. Tickets references are now prefixed with `###` instead of `- `. Fix compilation
2023-03-22 05:47:32 -07:00
logger.debug("--- REORG DETECTED \(reorgHeight)--- RewindHeight: \(rewindHeight)", file: #file, function: #function, line: #line)
XCTAssertEqual(reorgHeight, expectedReorgHeight)
reorgExpectation.fulfill()
}
func testSynchronizerStops() async throws {
/*
1. create fake chain
*/
let fullSyncLength = 100_000
try FakeChainBuilder.buildChain(darksideWallet: coordinator.service, branchID: branchID, chainName: chainName, length: fullSyncLength)
try coordinator.applyStaged(blockheight: birthday + fullSyncLength)
sleep(10)
let syncStoppedExpectation = XCTestExpectation(description: "SynchronizerStopped Expectation")
sdkSynchronizerInternalSyncStatusHandler.subscribe(
to: coordinator.synchronizer.stateStream,
expectations: [.stopped: syncStoppedExpectation]
)
/*
sync to latest height
*/
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
try await coordinator.sync(
completion: { _ in
XCTFail("Sync should have stopped")
},
error: self.handleError
)
try await Task.sleep(nanoseconds: 5_000_000_000)
[#1001] Remove PendingDb in favor of `v_transactions` and `v_tx_output` Views (#1001) Removes `PendingTransactionEntity` and all of its related components. Pending items are still tracked and visualized by the existing APIs but they are retrieved from the `TransactionRepository` instead by returning `ZcashTransaction.Overview` instead. `pendingDbURL` is removed from every place it was required. Its deletion is responsibility of wallet developers. `ClearedTransactions` are now just `transactions`. `MigrationManager` is deleted. Now all migrations are in charge of the rust welding layer. `PendingTransactionDao.swift` is removed. Implementation of `AccountEntity` called `Account` is now `DbAccount` `ZcashTransaction.Overview` can be checked for "pending-ness" by calling `.isPending(latestHeight:)` latest height must be provided so that minedHeight can be compared with the lastest and the `defaultStaleTolerance` constant. `TransactionRecipient` is now a public type. protocol `PendingTransactionRepository` is removed. `TransactionManagerError` and `PersistentTransactionManager` are deleted. `OutboundTransactionManager` is deleted and replaced by `TransactionEncoder` which now incorporates `submit(encoded:)` functionality `WalletTransactionEncoder` now uses a `LightWalletService` to submit the encoded transactions. Add changelog changes Delete references to PendingDb from tests and documentation. Fixes some typos. Adds the ability to trace transaction repository SQL queries from test Fix rebase conflicts and generate code [#837] Memo tests regarding transparent address Closes #837 Add model for transaction output Point to FFI branch Fix issue where sync wouldn't resume after wipe. Becasue GRPC channel would be closed Fix Tests Fix testPendingTransactionMinedHeightUpdated Fix testLastStates [#921] Fix broken SynchronizerDarksideTests Add ZcashTransaction.Output API to Synchronizer Changelog + comment fix Add Assertions for transaction outputs and recipients Point to FFI 0.3.1 Fix Demo App Compiler errors Fix Demo App Compiler errors fix cacheDb warnings Fix Tests and compiler errors of rebase build demo app Remove `ZcashTransaction.Sent` and `.Received`. Add `.State` and tests Fix SPM warning PR Suggestions Removes errors that are not used anymore fix warnings
2023-05-05 10:30:47 -07:00
self.coordinator.synchronizer.stop()
await fulfillment(of: [syncStoppedExpectation], timeout: 6)
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
let status = await coordinator.synchronizer.status
XCTAssertEqual(status, .stopped)
}
// MARK: Wipe tests
func testWipeCalledWhichSyncDoesntRun() async throws {
/*
create fake chain
*/
let fullSyncLength = 1000
try FakeChainBuilder.buildChain(darksideWallet: coordinator.service, branchID: branchID, chainName: chainName, length: fullSyncLength)
try coordinator.applyStaged(blockheight: birthday + fullSyncLength)
sleep(2)
let syncFinished = XCTestExpectation(description: "SynchronizerSyncFinished Expectation")
/*
sync to latest height
*/
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
try await coordinator.sync(
completion: { _ in
syncFinished.fulfill()
},
error: handleError
)
await fulfillment(of: [syncFinished], timeout: 3)
let wipeFinished = XCTestExpectation(description: "SynchronizerWipeFinished Expectation")
/*
Call wipe
*/
coordinator.synchronizer.wipe()
.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
wipeFinished.fulfill()
case .failure(let error):
XCTFail("Wipe should finish successfully. \(error)")
}
},
receiveValue: {
XCTFail("No no value should be received from wipe.")
}
)
.store(in: &cancellables)
await fulfillment(of: [wipeFinished], timeout: 1)
/*
Check that wipe cleared everything that is expected
*/
try await checkThatWipeWorked()
}
func testWipeCalledWhileSyncRuns() async throws {
/*
1. create fake chain
*/
let fullSyncLength = 50_000
try FakeChainBuilder.buildChain(darksideWallet: coordinator.service, branchID: branchID, chainName: chainName, length: fullSyncLength)
try coordinator.applyStaged(blockheight: birthday + fullSyncLength)
sleep(5)
/*
Start sync
*/
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
try await coordinator.sync(
completion: { _ in
XCTFail("Sync should have stopped")
},
error: self.handleError
)
try await Task.sleep(nanoseconds: 2_000_000_000)
// Just to be sure that blockProcessor is still syncing and that this test does what it should.
[#700] CompactBlockProcessor as state machine proof of concept - For now I created `CompactBlockProcessorNG` where I started with implementation of the state machine. I did it to not break the rest of the SDK. This change will be merged to the feature branch. And before it is merged to `main` branch code from `CompactBlockProcessorNG` will be moved to `CompactBlockProcessor`. - The new code is not used. It just shows and explains how it is done. It is proof of concept. - I did put either commented current code or comment to some places to explain what should be done there. - New important data types: - `ActionContext` is context that can hold any data that needs to be shared between actions. For example sync ranges or current state. - `CBPState` is state of the `CompactBlockProcessor`. Each state is handled by one action. This doesn't apply to terminal states like `finished` or `failed`. - `ActionProgress` is very similar to `CompactBlockProgress`. Different actions reports progress differently and `ActionProgress` represents this. - `Action` is protocol that defines API of an action. It has one run method that executes the code of the action - CBP first creates actions for (almost) each state in `makeActions()` method. Then the "magic" is done in `CompactBlockProcessorNG.run()` method. Here is main loop which takes action for current state and execute it. It's expected that action does it's work and then updates the context with new state. And this happens until some terminal state (`finished`, `failed`, `stopped`) is reached. - After the transition to state machine API of the `CompactBlockProcessor` should stay the same. No changes should be required in `SDKSynchronizer`. [#700] Add documentation for CompactBlockProcessor state machine - plantuml tool is used to generate diagram. [#1054] Add config to state machine CBP Closes #1054 [#1043] Implement DownloadAction Closes #1043 [#1049] Implement ValidateAction Closes #1049 [#1050] Implement ValidateServerAction Closes #1050 [#1056] Add constructors to state machine CBP Closes #1056 [#1061] Add failure methods for state machine CBP Closes #1061 [#1055] Implement retry timer to state machine CBP Closes #1055 [#1057] Implement start for state machine CBP Closes #1057 [#1058] Implement stop for state machine CBP Closes #1058 [#1052] Implement AfterSyncHooksManager when using state machine CBP Closes #1052 [#1060] Implement wipe for state machine CBP Closes #1060 [#1059] Implement rewind for state machine CBP Closes #1059 [#700] Add idle state to CBP state machine This is required so the CBP can detect start of the sync process. [#700] Implement sending of some events from CompactBlockProcessorNG [#700] Implement progress reporting in state machine CBP [#1045] Implement FetchUTXOsAction - draft of the fetching [#1045] Implement FetchUTXOsAction - updated the way Actions communicate data back to the CBP - used this mechanism to pass result of utxos fetch so it's passed to the SDKSynchronizer as an Event [#1042] Implement ComputeSyncRangesAction Closes #1042 [#700] Implement cache clearing when some actions fail [#1043] Fix batch range computation in DownloadAction [#1046] Implement SaplingParamsAction - action for sapling param files finished [#1048] Implement ScanDownloadedButUnscannedAction - scan downloaded but unscanned blocks [#1047] Implement ScanAction - scan action with the proper ranges computed [#1047] Implement ScanAction (#1085) - fixed logger message [#1044] Implement EnhanceAction Closes #1044 [#1041] Implement ClearCacheAction Closes #1041 [#1040] Implement ClearAlreadyScannedBlocksAction Closes #1040 [#1039] Implement ChecksBeforeSyncAction Closes #1039 [#700] Make CBP state machine work [#1050] Implement ValidateServerAction - broken tests commented out and tracked in the tickets - new test for ValidateServerAction [#1051] Update how progress is computed after switch to state machine Closes #1051 - new proposal for the progress computation - OverallProgress value is passed to the Synchronizer as a Float - OverallProgress is a result of fetch, scan and enhance operations - Order of actions no longer influences the computation - Actions report partial updates, CompactBlockProgress actor holds current state and computes the OverallProgress [#1049] Implement ValidateAction - synchronizer offline tests updated so it compiles, review is requested in a different ticket - ValidateAction tests added - BlockValidator mock generated [#1047] Implement ScanAction - ScanAction tests - refactor of validateAction -> validateServerAction - generated few more mocks for the DI [#1045] Implement FetchUTXOsAction - FetchUTXOsAction tests - UTXOFetcher mocks [#1045] Implement FetchUTXOsAction - enhanced with mocked values and more checks [#1046] Implement SaplingParamsAction - SaplingParamsAction tests - added TODO for TestCoordinator reset() - SaplingParametersHandler mock added [#1046] Implement SaplingParamsAction - SaplingParamsAction tests - added TODO for TestCoordinator reset() - SaplingParametersHandler mock added [#1046] Implement SaplingParamsAction - rebased so I get functionality of improved mock checks - enhanced SaplingParamsAction tests - enhanced ValidateAction tests - enhanced ScanAction tests [#1046] Implement SaplingParamsAction - scanAction tests more checks added [#1044] Implement EnhanceAction - EnhanceAction tests focused on 2 different methods: - decideWhatToDoNext covered separately, decisions where the state machine goes next - run tests for different cases - new mocks generated for enhacer - some typos fixed [#1044] Implement EnhanceAction (#1107) - empty assert messages fixed [#700] Get rid of ScanDownloadedButUnscannedAction Before the state machine download and scan was called in one loop. And processing range for one batch was same for both of them. Therefore there was code which scanned downloaded but not scanned blocks. But now download and scan are independent. So it is possible to remove `ScanDownloadedButUnscannedAction`. [#700] Make NetworkTests compilable Some tests are disabled for now (list is in #1115). And `NetworkTests` can be compiled and all the enabled tests work. [#1043] Implement DownloadAction - DownloadAction tests - BlockDownloader mock [#1043] Implement DownloadAction (#1110) - support functions set to private [#1039] Implement ChecksBeforeSyncAction - ChecksBeforeSyncAction tests - all support functions in Action tests are set to private - let _ = -> _ = refactor - CompactBlockRepository mock added [#1040] Implement ClearAlreadyScannedBlocksAction - Implement ClearAlreadyScannedBlocksAction tests - CompactBlockRepository mock added [#1041] Implement ClearCacheAction - ClearCacheAction tests - CompactBlockRepository mock added [#1042] Implement ComputeSyncRangesAction - ComputeSyncRangesAction tests - fixed all tests after merge of latest SDK changes related InternalSyncProgress - all actions marked as final class [#1042] Implement ComputeSyncRangesAction (#1120) - Custom LatestBlocksDataProviderMock removed from the project [#1122] Implement FileManager protocol and dependency - ZcashFileManager implemented - MigrateLegacyCacheDBAction refactored to be dependent on ZcashFileManager - ZcashFileManager mock added [#1122] Implement FileManager protocol and dependency (#1124) - code cleanup [#1121] Implement MigrateLegacyCacheDBAction - MigrateLegacyCacheDBAction tests WIP - tests naming cleanup [#1121] Implement MigrateLegacyCacheDBAction - MigrateLegacyCacheDBAction tests finished [#700] Fix DarksideTests Closes #1102 Some tests that can't be compiled are disabled for now. List is in #1126. This PR contains multiple fixes. Most of the fixes is done in the code. Not in the tests. That is good news. Fixes: - `config` inside `CompactBlockProcessor` can be updated during the tests. And it must be also updated inside the actions. So `ConfigProvider` is added and it is wrapper for config that is passed to any instance of `Action` and provides updated config. - Fixed `EnhanceAction`. Now it should update all the blocks in the enhance range even when the remaining count of blocks is lower than 1000. - Fixed `fail()` and `validationFailed()`. These two were canceling `syncTask`. But that stopped run loop in a bad way. [#1129] Final check of all State Machine Action tests - XTCAsset messages checked - test naming checked and fixed [#1126] Fix DarksideTests in state machine branch Closes #1126 Fix offline tests Closes #1098 Closes #1095 Closes #1094 Most of the tests is removed. Either the code that was tested doesn't exists. Or now tests for state machine actions do this work. [#1115] Fix NetworkTests in state machine branch Closes #1115 [#700] Fix progress reporting Some actions in the sync process may not run. For example there are no transactions to enhance and therefore there is no enhance progress. And in cases like this computation of final progress won't work properly. So let's fake 100% progress at the end of the sync process.
2023-05-05 08:04:13 -07:00
let synchronizerState = coordinator.synchronizer.latestState.syncStatus
switch synchronizerState {
case .syncing:
break
default:
XCTFail("Synchornizer should be in syncing state.")
}
let wipeFinished = XCTestExpectation(description: "SynchronizerWipeFinished Expectation")
/*
Call wipe
*/
coordinator.synchronizer.wipe()
.sink(
receiveCompletion: { completion in
switch completion {
case .finished:
wipeFinished.fulfill()
case .failure(let error):
XCTFail("Wipe should finish successfully. \(error)")
}
},
receiveValue: {
XCTFail("No no value should be received from wipe.")
}
)
.store(in: &cancellables)
await fulfillment(of: [wipeFinished], timeout: 6)
/*
Check that wipe cleared everything that is expected
*/
try await checkThatWipeWorked()
}
private func checkThatWipeWorked() async throws {
let storage = await self.coordinator.synchronizer.blockProcessor.storage as! FSCompactBlockRepository
let fm = FileManager.default
[#700] CompactBlockProcessor as state machine proof of concept - For now I created `CompactBlockProcessorNG` where I started with implementation of the state machine. I did it to not break the rest of the SDK. This change will be merged to the feature branch. And before it is merged to `main` branch code from `CompactBlockProcessorNG` will be moved to `CompactBlockProcessor`. - The new code is not used. It just shows and explains how it is done. It is proof of concept. - I did put either commented current code or comment to some places to explain what should be done there. - New important data types: - `ActionContext` is context that can hold any data that needs to be shared between actions. For example sync ranges or current state. - `CBPState` is state of the `CompactBlockProcessor`. Each state is handled by one action. This doesn't apply to terminal states like `finished` or `failed`. - `ActionProgress` is very similar to `CompactBlockProgress`. Different actions reports progress differently and `ActionProgress` represents this. - `Action` is protocol that defines API of an action. It has one run method that executes the code of the action - CBP first creates actions for (almost) each state in `makeActions()` method. Then the "magic" is done in `CompactBlockProcessorNG.run()` method. Here is main loop which takes action for current state and execute it. It's expected that action does it's work and then updates the context with new state. And this happens until some terminal state (`finished`, `failed`, `stopped`) is reached. - After the transition to state machine API of the `CompactBlockProcessor` should stay the same. No changes should be required in `SDKSynchronizer`. [#700] Add documentation for CompactBlockProcessor state machine - plantuml tool is used to generate diagram. [#1054] Add config to state machine CBP Closes #1054 [#1043] Implement DownloadAction Closes #1043 [#1049] Implement ValidateAction Closes #1049 [#1050] Implement ValidateServerAction Closes #1050 [#1056] Add constructors to state machine CBP Closes #1056 [#1061] Add failure methods for state machine CBP Closes #1061 [#1055] Implement retry timer to state machine CBP Closes #1055 [#1057] Implement start for state machine CBP Closes #1057 [#1058] Implement stop for state machine CBP Closes #1058 [#1052] Implement AfterSyncHooksManager when using state machine CBP Closes #1052 [#1060] Implement wipe for state machine CBP Closes #1060 [#1059] Implement rewind for state machine CBP Closes #1059 [#700] Add idle state to CBP state machine This is required so the CBP can detect start of the sync process. [#700] Implement sending of some events from CompactBlockProcessorNG [#700] Implement progress reporting in state machine CBP [#1045] Implement FetchUTXOsAction - draft of the fetching [#1045] Implement FetchUTXOsAction - updated the way Actions communicate data back to the CBP - used this mechanism to pass result of utxos fetch so it's passed to the SDKSynchronizer as an Event [#1042] Implement ComputeSyncRangesAction Closes #1042 [#700] Implement cache clearing when some actions fail [#1043] Fix batch range computation in DownloadAction [#1046] Implement SaplingParamsAction - action for sapling param files finished [#1048] Implement ScanDownloadedButUnscannedAction - scan downloaded but unscanned blocks [#1047] Implement ScanAction - scan action with the proper ranges computed [#1047] Implement ScanAction (#1085) - fixed logger message [#1044] Implement EnhanceAction Closes #1044 [#1041] Implement ClearCacheAction Closes #1041 [#1040] Implement ClearAlreadyScannedBlocksAction Closes #1040 [#1039] Implement ChecksBeforeSyncAction Closes #1039 [#700] Make CBP state machine work [#1050] Implement ValidateServerAction - broken tests commented out and tracked in the tickets - new test for ValidateServerAction [#1051] Update how progress is computed after switch to state machine Closes #1051 - new proposal for the progress computation - OverallProgress value is passed to the Synchronizer as a Float - OverallProgress is a result of fetch, scan and enhance operations - Order of actions no longer influences the computation - Actions report partial updates, CompactBlockProgress actor holds current state and computes the OverallProgress [#1049] Implement ValidateAction - synchronizer offline tests updated so it compiles, review is requested in a different ticket - ValidateAction tests added - BlockValidator mock generated [#1047] Implement ScanAction - ScanAction tests - refactor of validateAction -> validateServerAction - generated few more mocks for the DI [#1045] Implement FetchUTXOsAction - FetchUTXOsAction tests - UTXOFetcher mocks [#1045] Implement FetchUTXOsAction - enhanced with mocked values and more checks [#1046] Implement SaplingParamsAction - SaplingParamsAction tests - added TODO for TestCoordinator reset() - SaplingParametersHandler mock added [#1046] Implement SaplingParamsAction - SaplingParamsAction tests - added TODO for TestCoordinator reset() - SaplingParametersHandler mock added [#1046] Implement SaplingParamsAction - rebased so I get functionality of improved mock checks - enhanced SaplingParamsAction tests - enhanced ValidateAction tests - enhanced ScanAction tests [#1046] Implement SaplingParamsAction - scanAction tests more checks added [#1044] Implement EnhanceAction - EnhanceAction tests focused on 2 different methods: - decideWhatToDoNext covered separately, decisions where the state machine goes next - run tests for different cases - new mocks generated for enhacer - some typos fixed [#1044] Implement EnhanceAction (#1107) - empty assert messages fixed [#700] Get rid of ScanDownloadedButUnscannedAction Before the state machine download and scan was called in one loop. And processing range for one batch was same for both of them. Therefore there was code which scanned downloaded but not scanned blocks. But now download and scan are independent. So it is possible to remove `ScanDownloadedButUnscannedAction`. [#700] Make NetworkTests compilable Some tests are disabled for now (list is in #1115). And `NetworkTests` can be compiled and all the enabled tests work. [#1043] Implement DownloadAction - DownloadAction tests - BlockDownloader mock [#1043] Implement DownloadAction (#1110) - support functions set to private [#1039] Implement ChecksBeforeSyncAction - ChecksBeforeSyncAction tests - all support functions in Action tests are set to private - let _ = -> _ = refactor - CompactBlockRepository mock added [#1040] Implement ClearAlreadyScannedBlocksAction - Implement ClearAlreadyScannedBlocksAction tests - CompactBlockRepository mock added [#1041] Implement ClearCacheAction - ClearCacheAction tests - CompactBlockRepository mock added [#1042] Implement ComputeSyncRangesAction - ComputeSyncRangesAction tests - fixed all tests after merge of latest SDK changes related InternalSyncProgress - all actions marked as final class [#1042] Implement ComputeSyncRangesAction (#1120) - Custom LatestBlocksDataProviderMock removed from the project [#1122] Implement FileManager protocol and dependency - ZcashFileManager implemented - MigrateLegacyCacheDBAction refactored to be dependent on ZcashFileManager - ZcashFileManager mock added [#1122] Implement FileManager protocol and dependency (#1124) - code cleanup [#1121] Implement MigrateLegacyCacheDBAction - MigrateLegacyCacheDBAction tests WIP - tests naming cleanup [#1121] Implement MigrateLegacyCacheDBAction - MigrateLegacyCacheDBAction tests finished [#700] Fix DarksideTests Closes #1102 Some tests that can't be compiled are disabled for now. List is in #1126. This PR contains multiple fixes. Most of the fixes is done in the code. Not in the tests. That is good news. Fixes: - `config` inside `CompactBlockProcessor` can be updated during the tests. And it must be also updated inside the actions. So `ConfigProvider` is added and it is wrapper for config that is passed to any instance of `Action` and provides updated config. - Fixed `EnhanceAction`. Now it should update all the blocks in the enhance range even when the remaining count of blocks is lower than 1000. - Fixed `fail()` and `validationFailed()`. These two were canceling `syncTask`. But that stopped run loop in a bad way. [#1129] Final check of all State Machine Action tests - XTCAsset messages checked - test naming checked and fixed [#1126] Fix DarksideTests in state machine branch Closes #1126 Fix offline tests Closes #1098 Closes #1095 Closes #1094 Most of the tests is removed. Either the code that was tested doesn't exists. Or now tests for state machine actions do this work. [#1115] Fix NetworkTests in state machine branch Closes #1115 [#700] Fix progress reporting Some actions in the sync process may not run. For example there are no transactions to enhance and therefore there is no enhance progress. And in cases like this computation of final progress won't work properly. So let's fake 100% progress at the end of the sync process.
2023-05-05 08:04:13 -07:00
XCTAssertFalse(fm.fileExists(atPath: coordinator.synchronizer.initializer.dataDbURL.path), "Data DB should be deleted.")
XCTAssertTrue(fm.fileExists(atPath: storage.blocksDirectory.path), "FS Cache directory should exist")
XCTAssertEqual(try fm.contentsOfDirectory(atPath: storage.blocksDirectory.path), [], "FS Cache directory should be empty")
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
let status = await coordinator.synchronizer.status
XCTAssertEqual(status, .unprepared, "SDKSynchronizer state should be unprepared")
}
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
func handleError(_ error: Error?) async {
_ = try? await coordinator.stop()
guard let testError = error else {
XCTFail("failed with nil error")
return
}
XCTFail("Failed with error: \(testError)")
}
// MARK: Rewind tests
func testRewindCalledWhileSyncRuns() async throws {
// 1 sync and get spendable funds
try FakeChainBuilder.buildChain(darksideWallet: coordinator.service, branchID: branchID, chainName: chainName)
[#1001] Remove PendingDb in favor of `v_transactions` and `v_tx_output` Views (#1001) Removes `PendingTransactionEntity` and all of its related components. Pending items are still tracked and visualized by the existing APIs but they are retrieved from the `TransactionRepository` instead by returning `ZcashTransaction.Overview` instead. `pendingDbURL` is removed from every place it was required. Its deletion is responsibility of wallet developers. `ClearedTransactions` are now just `transactions`. `MigrationManager` is deleted. Now all migrations are in charge of the rust welding layer. `PendingTransactionDao.swift` is removed. Implementation of `AccountEntity` called `Account` is now `DbAccount` `ZcashTransaction.Overview` can be checked for "pending-ness" by calling `.isPending(latestHeight:)` latest height must be provided so that minedHeight can be compared with the lastest and the `defaultStaleTolerance` constant. `TransactionRecipient` is now a public type. protocol `PendingTransactionRepository` is removed. `TransactionManagerError` and `PersistentTransactionManager` are deleted. `OutboundTransactionManager` is deleted and replaced by `TransactionEncoder` which now incorporates `submit(encoded:)` functionality `WalletTransactionEncoder` now uses a `LightWalletService` to submit the encoded transactions. Add changelog changes Delete references to PendingDb from tests and documentation. Fixes some typos. Adds the ability to trace transaction repository SQL queries from test Fix rebase conflicts and generate code [#837] Memo tests regarding transparent address Closes #837 Add model for transaction output Point to FFI branch Fix issue where sync wouldn't resume after wipe. Becasue GRPC channel would be closed Fix Tests Fix testPendingTransactionMinedHeightUpdated Fix testLastStates [#921] Fix broken SynchronizerDarksideTests Add ZcashTransaction.Output API to Synchronizer Changelog + comment fix Add Assertions for transaction outputs and recipients Point to FFI 0.3.1 Fix Demo App Compiler errors Fix Demo App Compiler errors fix cacheDb warnings Fix Tests and compiler errors of rebase build demo app Remove `ZcashTransaction.Sent` and `.Received`. Add `.State` and tests Fix SPM warning PR Suggestions Removes errors that are not used anymore fix warnings
2023-05-05 10:30:47 -07:00
try coordinator.applyStaged(blockheight: 663200)
let initialVerifiedBalance: Zatoshi = try await coordinator.synchronizer.getShieldedVerifiedBalance()
let initialTotalBalance: Zatoshi = try await coordinator.synchronizer.getShieldedBalance()
sleep(1)
let firstSyncExpectation = XCTestExpectation(description: "first sync expectation")
do {
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
try await coordinator.sync(
completion: { _ in
firstSyncExpectation.fulfill()
},
error: self.handleError
)
} catch {
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
await handleError(error)
}
await fulfillment(of: [firstSyncExpectation], timeout: 12)
[#1001] Remove PendingDb in favor of `v_transactions` and `v_tx_output` Views (#1001) Removes `PendingTransactionEntity` and all of its related components. Pending items are still tracked and visualized by the existing APIs but they are retrieved from the `TransactionRepository` instead by returning `ZcashTransaction.Overview` instead. `pendingDbURL` is removed from every place it was required. Its deletion is responsibility of wallet developers. `ClearedTransactions` are now just `transactions`. `MigrationManager` is deleted. Now all migrations are in charge of the rust welding layer. `PendingTransactionDao.swift` is removed. Implementation of `AccountEntity` called `Account` is now `DbAccount` `ZcashTransaction.Overview` can be checked for "pending-ness" by calling `.isPending(latestHeight:)` latest height must be provided so that minedHeight can be compared with the lastest and the `defaultStaleTolerance` constant. `TransactionRecipient` is now a public type. protocol `PendingTransactionRepository` is removed. `TransactionManagerError` and `PersistentTransactionManager` are deleted. `OutboundTransactionManager` is deleted and replaced by `TransactionEncoder` which now incorporates `submit(encoded:)` functionality `WalletTransactionEncoder` now uses a `LightWalletService` to submit the encoded transactions. Add changelog changes Delete references to PendingDb from tests and documentation. Fixes some typos. Adds the ability to trace transaction repository SQL queries from test Fix rebase conflicts and generate code [#837] Memo tests regarding transparent address Closes #837 Add model for transaction output Point to FFI branch Fix issue where sync wouldn't resume after wipe. Becasue GRPC channel would be closed Fix Tests Fix testPendingTransactionMinedHeightUpdated Fix testLastStates [#921] Fix broken SynchronizerDarksideTests Add ZcashTransaction.Output API to Synchronizer Changelog + comment fix Add Assertions for transaction outputs and recipients Point to FFI 0.3.1 Fix Demo App Compiler errors Fix Demo App Compiler errors fix cacheDb warnings Fix Tests and compiler errors of rebase build demo app Remove `ZcashTransaction.Sent` and `.Received`. Add `.State` and tests Fix SPM warning PR Suggestions Removes errors that are not used anymore fix warnings
2023-05-05 10:30:47 -07:00
let verifiedBalance: Zatoshi = try await coordinator.synchronizer.getShieldedVerifiedBalance()
let totalBalance: Zatoshi = try await coordinator.synchronizer.getShieldedBalance()
// 2 check that there are no unconfirmed funds
XCTAssertTrue(verifiedBalance > network.constants.defaultFee(for: defaultLatestHeight))
XCTAssertEqual(verifiedBalance, totalBalance)
// Add more blocks to the chain so the long sync can start.
try FakeChainBuilder.buildChain(darksideWallet: coordinator.service, branchID: branchID, chainName: chainName, length: 10000)
try coordinator.applyStaged(blockheight: birthday + 10000)
sleep(2)
do {
// Start the long sync.
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
try await coordinator.sync(
completion: { _ in },
error: self.handleError
)
} catch {
[#831] Add SDKSynchronizer wrappers for non-async API This change introduces two new protocols: `ClosureSynchronizer` and `CombineSynchronizer`. These two protocols define API that doesn't use `async`. So the client can choose exactly which API it wants to use. This change also introduces two new objects: `ClosureSDKSynchronizer` and `CombineSDKSynchronizer`. These two implement the respective protocols mentioned above. Both are structures. Neither of these two keeps any state. Thanks to this each is very cheap to create. And usage of these two isn't mutually exclusive. So devs can really choose the best SDK API for each part of the client app. [#831] Use async inside of the SDKSynchronizer - In general lot of methods inside the `SDKSynchronizer` and `CompactBlockProcessoer` which weren't async are now async. And other changes are made because of this change. - `CompactBlockProcessor` no longer uses Combine to communicate with `SDKSynchronizer`. Reason for this is that Combine doesn't play great with async. Closure passed to `sink` isn't async. - Because of this and because of how our tests work (receiving signals from CBP directly) `CompactBlockProcessor` must be able to handle more event closures. Not just one. So it now has `eventClosures` dictionary. It's little bit strange but it works fine. - `SyncStatus` inside the `SDKSynchronizer` was previously protected by lock. Now it's protected by simple actor wrapper. - Changes in tests are minimal. Changes were mady only because `CompactBlockProcessor` changes from Combine to closures. [#831] Add tests for ClosureSDKSynchronizer - Added tests are testing in general if the `ClosuresSDKSynchronizer` is correctly calling `Synchronizer` and if the values are correctly returned. - `ClosuresSDKSynchronizer` doesn't contain any logic but it is public API and we should be sure that it works correctly. [#831] Add tests for CombineSDKSynchronizer [#831] Add changelog
2023-03-16 02:11:18 -07:00
await handleError(error)
}
// Wait 0.5 second and then start rewind while sync is in progress.
let waitExpectation = XCTestExpectation()
DispatchQueue.global().asyncAfter(deadline: .now() + 0.5) {
waitExpectation.fulfill()
}
await fulfillment(of: [waitExpectation], timeout: 1)
let rewindExpectation = XCTestExpectation(description: "RewindExpectation")
// rewind to birthday
coordinator.synchronizer.rewind(.birthday)
.sink(
receiveCompletion: { result in
switch result {
case .finished:
break
case let .failure(error):
XCTFail("Rewind failed with error: \(error)")
}
rewindExpectation.fulfill()
},
[#1001] Remove PendingDb in favor of `v_transactions` and `v_tx_output` Views (#1001) Removes `PendingTransactionEntity` and all of its related components. Pending items are still tracked and visualized by the existing APIs but they are retrieved from the `TransactionRepository` instead by returning `ZcashTransaction.Overview` instead. `pendingDbURL` is removed from every place it was required. Its deletion is responsibility of wallet developers. `ClearedTransactions` are now just `transactions`. `MigrationManager` is deleted. Now all migrations are in charge of the rust welding layer. `PendingTransactionDao.swift` is removed. Implementation of `AccountEntity` called `Account` is now `DbAccount` `ZcashTransaction.Overview` can be checked for "pending-ness" by calling `.isPending(latestHeight:)` latest height must be provided so that minedHeight can be compared with the lastest and the `defaultStaleTolerance` constant. `TransactionRecipient` is now a public type. protocol `PendingTransactionRepository` is removed. `TransactionManagerError` and `PersistentTransactionManager` are deleted. `OutboundTransactionManager` is deleted and replaced by `TransactionEncoder` which now incorporates `submit(encoded:)` functionality `WalletTransactionEncoder` now uses a `LightWalletService` to submit the encoded transactions. Add changelog changes Delete references to PendingDb from tests and documentation. Fixes some typos. Adds the ability to trace transaction repository SQL queries from test Fix rebase conflicts and generate code [#837] Memo tests regarding transparent address Closes #837 Add model for transaction output Point to FFI branch Fix issue where sync wouldn't resume after wipe. Becasue GRPC channel would be closed Fix Tests Fix testPendingTransactionMinedHeightUpdated Fix testLastStates [#921] Fix broken SynchronizerDarksideTests Add ZcashTransaction.Output API to Synchronizer Changelog + comment fix Add Assertions for transaction outputs and recipients Point to FFI 0.3.1 Fix Demo App Compiler errors Fix Demo App Compiler errors fix cacheDb warnings Fix Tests and compiler errors of rebase build demo app Remove `ZcashTransaction.Sent` and `.Received`. Add `.State` and tests Fix SPM warning PR Suggestions Removes errors that are not used anymore fix warnings
2023-05-05 10:30:47 -07:00
receiveValue: { _ in rewindExpectation.fulfill() }
)
.store(in: &cancellables)
await fulfillment(of: [rewindExpectation], timeout: 5)
// assert that after the new height is
let lastScannedHeight = try await coordinator.synchronizer.initializer.transactionRepository.lastScannedHeight()
XCTAssertEqual(lastScannedHeight, self.birthday)
// check that the balance is cleared
let expectedVerifiedBalance = try await coordinator.synchronizer.getShieldedVerifiedBalance()
let expectedBalance = try await coordinator.synchronizer.getShieldedBalance()
XCTAssertEqual(initialVerifiedBalance, expectedVerifiedBalance)
XCTAssertEqual(initialTotalBalance, expectedBalance)
}
}