2020-10-20 15:22:05 -07:00
|
|
|
//
|
|
|
|
// SychronizerDarksideTests.swift
|
|
|
|
// ZcashLightClientKit-Unit-Tests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 10/20/20.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2022-11-11 09:46:13 -08:00
|
|
|
import Combine
|
2022-02-28 09:03:20 -08:00
|
|
|
@testable import TestUtils
|
2020-10-20 15:22:05 -07:00
|
|
|
@testable import ZcashLightClientKit
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
class SynchronizerDarksideTests: ZcashTestCase {
|
2020-10-20 15:22:05 -07:00
|
|
|
let sendAmount: Int64 = 1000
|
|
|
|
let defaultLatestHeight: BlockHeight = 663175
|
2021-09-23 06:26:41 -07:00
|
|
|
let branchID = "2bb40e60"
|
|
|
|
let chainName = "main"
|
|
|
|
let network = DarksideWalletDNetwork()
|
|
|
|
|
|
|
|
var birthday: BlockHeight = 663150
|
2020-10-20 15:22:05 -07:00
|
|
|
var coordinator: TestCoordinator!
|
|
|
|
var syncedExpectation = XCTestExpectation(description: "synced")
|
|
|
|
var sentTransactionExpectation = XCTestExpectation(description: "sent")
|
|
|
|
var expectedReorgHeight: BlockHeight = 665188
|
|
|
|
var expectedRewindHeight: BlockHeight = 665188
|
2021-09-23 06:26:41 -07:00
|
|
|
var reorgExpectation = XCTestExpectation(description: "reorg")
|
2023-01-23 01:01:07 -08:00
|
|
|
var foundTransactions: [ZcashTransaction.Overview] = []
|
2023-02-20 01:53:04 -08:00
|
|
|
var cancellables: [AnyCancellable] = []
|
2023-04-07 05:02:05 -07:00
|
|
|
var idGenerator: MockSyncSessionIDGenerator!
|
2023-03-31 10:10:35 -07:00
|
|
|
|
2023-03-30 10:01:47 -07:00
|
|
|
override func setUp() async throws {
|
|
|
|
try await super.setUp()
|
2023-05-01 07:28:59 -07:00
|
|
|
let idGenerator = MockSyncSessionIDGenerator(ids: [.deadbeef])
|
|
|
|
mockContainer.mock(type: SyncSessionIDGenerator.self, isSingleton: false) { _ in idGenerator }
|
|
|
|
self.idGenerator = idGenerator
|
|
|
|
|
|
|
|
self.coordinator = try await TestCoordinator(
|
|
|
|
container: mockContainer,
|
|
|
|
walletBirthday: birthday,
|
|
|
|
network: network
|
|
|
|
)
|
|
|
|
|
2023-05-05 08:04:13 -07:00
|
|
|
try await coordinator.reset(saplingActivation: 663150, branchID: "e9ff75a6", chainName: "main")
|
2020-10-20 15:22:05 -07:00
|
|
|
}
|
2022-12-19 06:15:23 -08:00
|
|
|
|
2023-03-30 10:01:47 -07:00
|
|
|
override func tearDown() async throws {
|
|
|
|
try await super.tearDown()
|
|
|
|
let coordinator = self.coordinator!
|
|
|
|
self.coordinator = nil
|
|
|
|
foundTransactions = []
|
|
|
|
cancellables = []
|
|
|
|
|
|
|
|
try await coordinator.stop()
|
2023-02-02 08:58:12 -08:00
|
|
|
try? FileManager.default.removeItem(at: coordinator.databases.fsCacheDbRoot)
|
2020-10-20 15:22:05 -07:00
|
|
|
try? FileManager.default.removeItem(at: coordinator.databases.dataDB)
|
|
|
|
}
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
func testFoundTransactions() async throws {
|
2023-03-15 04:17:43 -07:00
|
|
|
coordinator.synchronizer.eventStream
|
|
|
|
.map { event in
|
|
|
|
guard case let .foundTransactions(transactions, _) = event else { return nil }
|
|
|
|
return transactions
|
|
|
|
}
|
|
|
|
.compactMap { $0 }
|
|
|
|
.sink(receiveValue: { [weak self] transactions in self?.handleFoundTransactions(transactions: transactions) })
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
2021-05-18 14:22:29 -07:00
|
|
|
try FakeChainBuilder.buildChain(darksideWallet: self.coordinator.service, branchID: branchID, chainName: chainName)
|
2020-10-20 15:22:05 -07:00
|
|
|
let receivedTxHeight: BlockHeight = 663188
|
|
|
|
|
|
|
|
try coordinator.applyStaged(blockheight: receivedTxHeight + 1)
|
|
|
|
|
|
|
|
sleep(2)
|
|
|
|
let preTxExpectation = XCTestExpectation(description: "pre receive")
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
try await coordinator.sync(
|
|
|
|
completion: { _ in
|
|
|
|
preTxExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
2020-10-20 15:22:05 -07:00
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [preTxExpectation], timeout: 5)
|
2020-10-20 15:22:05 -07:00
|
|
|
|
|
|
|
XCTAssertEqual(self.foundTransactions.count, 2)
|
|
|
|
}
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
func testFoundManyTransactions() async throws {
|
2023-04-07 05:02:05 -07:00
|
|
|
self.idGenerator.ids = [.deadbeef, .beefbeef, .beefdead]
|
2023-03-15 04:17:43 -07:00
|
|
|
coordinator.synchronizer.eventStream
|
|
|
|
.map { event in
|
|
|
|
guard case let .foundTransactions(transactions, _) = event else { return nil }
|
|
|
|
return transactions
|
|
|
|
}
|
|
|
|
.compactMap { $0 }
|
|
|
|
.sink(receiveValue: { [weak self] transactions in self?.handleFoundTransactions(transactions: transactions) })
|
|
|
|
.store(in: &cancellables)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
try FakeChainBuilder.buildChain(darksideWallet: self.coordinator.service, branchID: branchID, chainName: chainName, length: 1000)
|
2020-10-26 14:57:19 -07:00
|
|
|
let receivedTxHeight: BlockHeight = 663229
|
|
|
|
|
|
|
|
try coordinator.applyStaged(blockheight: receivedTxHeight + 1)
|
|
|
|
|
|
|
|
sleep(2)
|
|
|
|
let firsTxExpectation = XCTestExpectation(description: "first sync")
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
try await coordinator.sync(
|
|
|
|
completion: { _ in
|
|
|
|
firsTxExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [firsTxExpectation], timeout: 10)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
|
|
|
XCTAssertEqual(self.foundTransactions.count, 5)
|
|
|
|
|
|
|
|
self.foundTransactions.removeAll()
|
|
|
|
|
|
|
|
try coordinator.applyStaged(blockheight: 663900)
|
|
|
|
sleep(2)
|
|
|
|
|
|
|
|
let preTxExpectation = XCTestExpectation(description: "intermediate sync")
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
try await coordinator.sync(
|
|
|
|
completion: { _ in
|
|
|
|
preTxExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [preTxExpectation], timeout: 10)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertTrue(self.foundTransactions.isEmpty)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
|
|
|
let findManyTxExpectation = XCTestExpectation(description: "final sync")
|
|
|
|
|
|
|
|
try coordinator.applyStaged(blockheight: 664010)
|
|
|
|
sleep(2)
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
try await coordinator.sync(
|
|
|
|
completion: { _ in
|
|
|
|
findManyTxExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [findManyTxExpectation], timeout: 10)
|
2020-10-26 14:57:19 -07:00
|
|
|
|
|
|
|
XCTAssertEqual(self.foundTransactions.count, 2)
|
|
|
|
}
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2023-04-12 00:39:49 -07:00
|
|
|
func testLastStates() async throws {
|
|
|
|
self.idGenerator.ids = [.deadbeef, .beefbeef, .beefdead]
|
|
|
|
let uuids = self.idGenerator.ids
|
2023-04-07 05:02:05 -07:00
|
|
|
|
|
|
|
var cancellables: [AnyCancellable] = []
|
|
|
|
|
|
|
|
var states: [SynchronizerState] = []
|
|
|
|
|
|
|
|
try FakeChainBuilder.buildChain(darksideWallet: self.coordinator.service, branchID: branchID, chainName: chainName)
|
|
|
|
let receivedTxHeight: BlockHeight = 663188
|
|
|
|
|
|
|
|
try coordinator.applyStaged(blockheight: receivedTxHeight + 1)
|
|
|
|
|
|
|
|
sleep(2)
|
|
|
|
let preTxExpectation = XCTestExpectation(description: "pre receive")
|
|
|
|
|
|
|
|
coordinator.synchronizer.stateStream
|
|
|
|
.sink { state in
|
|
|
|
states.append(state)
|
|
|
|
}
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
|
|
|
try await coordinator.sync(
|
|
|
|
completion: { _ in
|
|
|
|
preTxExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [preTxExpectation], timeout: 5)
|
2023-04-07 05:02:05 -07:00
|
|
|
|
|
|
|
let expectedStates: [SynchronizerState] = [
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: .nullID,
|
|
|
|
shieldedBalance: .zero,
|
|
|
|
transparentBalance: .zero,
|
2023-05-24 06:24:09 -07:00
|
|
|
internalSyncStatus: .unprepared,
|
|
|
|
latestScannedHeight: 0,
|
2023-05-05 10:30:47 -07:00
|
|
|
latestBlockHeight: 0,
|
2023-05-24 06:24:09 -07:00
|
|
|
latestScannedTime: 0
|
2023-04-07 05:02:05 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[0],
|
|
|
|
shieldedBalance: .zero,
|
|
|
|
transparentBalance: .zero,
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(0),
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663150,
|
2023-05-05 10:30:47 -07:00
|
|
|
latestBlockHeight: 0,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedTime: 1576821833
|
2023-04-07 05:02:05 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[0],
|
2023-05-05 10:30:47 -07:00
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(100000), total: Zatoshi(200000)),
|
2023-04-07 05:02:05 -07:00
|
|
|
transparentBalance: .zero,
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(0.9),
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663189,
|
|
|
|
latestBlockHeight: 663189,
|
|
|
|
latestScannedTime: 1
|
2023-04-07 05:02:05 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[0],
|
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(100000), total: Zatoshi(200000)),
|
2023-04-14 00:08:37 -07:00
|
|
|
transparentBalance: .zero,
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(1.0),
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663189,
|
|
|
|
latestBlockHeight: 663189,
|
|
|
|
latestScannedTime: 1
|
2023-04-07 05:02:05 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[0],
|
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(100000), total: Zatoshi(200000)),
|
2023-04-14 00:08:37 -07:00
|
|
|
transparentBalance: .zero,
|
2023-04-28 10:13:21 -07:00
|
|
|
internalSyncStatus: .synced,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663189,
|
|
|
|
latestBlockHeight: 663189,
|
|
|
|
latestScannedTime: 1
|
2023-04-07 05:02:05 -07:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
XCTAssertEqual(states.count, expectedStates.count)
|
2023-05-01 07:28:59 -07:00
|
|
|
|
|
|
|
for (index, state) in states.enumerated() {
|
|
|
|
let expectedState = expectedStates[index]
|
|
|
|
XCTAssertEqual(state, expectedState, "Failed state comparison at index \(index).")
|
|
|
|
}
|
2023-04-07 05:02:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSyncSessionUpdates() async throws {
|
2023-03-15 04:17:43 -07:00
|
|
|
var cancellables: [AnyCancellable] = []
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2023-04-12 00:39:49 -07:00
|
|
|
self.idGenerator.ids = [.deadbeef, .beefbeef, .beefdead]
|
|
|
|
let uuids = idGenerator.ids
|
2023-04-07 05:02:05 -07:00
|
|
|
|
2023-03-15 04:17:43 -07:00
|
|
|
var states: [SynchronizerState] = []
|
2022-11-11 09:46:13 -08:00
|
|
|
|
|
|
|
try FakeChainBuilder.buildChain(darksideWallet: self.coordinator.service, branchID: branchID, chainName: chainName)
|
|
|
|
let receivedTxHeight: BlockHeight = 663188
|
|
|
|
|
|
|
|
try coordinator.applyStaged(blockheight: receivedTxHeight + 1)
|
|
|
|
|
|
|
|
sleep(2)
|
|
|
|
let preTxExpectation = XCTestExpectation(description: "pre receive")
|
|
|
|
|
2023-03-15 04:17:43 -07:00
|
|
|
coordinator.synchronizer.stateStream
|
2022-11-11 09:46:13 -08:00
|
|
|
.sink { state in
|
|
|
|
states.append(state)
|
|
|
|
}
|
2023-03-15 04:17:43 -07:00
|
|
|
.store(in: &cancellables)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
try await coordinator.sync(
|
|
|
|
completion: { _ in
|
|
|
|
preTxExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [preTxExpectation], timeout: 5)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2023-03-15 04:17:43 -07:00
|
|
|
let expectedStates: [SynchronizerState] = [
|
|
|
|
SynchronizerState(
|
2023-04-07 05:02:05 -07:00
|
|
|
syncSessionID: .nullID,
|
2023-03-15 04:17:43 -07:00
|
|
|
shieldedBalance: .zero,
|
|
|
|
transparentBalance: .zero,
|
2023-05-24 06:24:09 -07:00
|
|
|
internalSyncStatus: .unprepared,
|
|
|
|
latestScannedHeight: 0,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestBlockHeight: 0,
|
2023-05-24 06:24:09 -07:00
|
|
|
latestScannedTime: 0
|
2023-03-15 04:17:43 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
2023-04-07 05:02:05 -07:00
|
|
|
syncSessionID: uuids[0],
|
2023-03-15 04:17:43 -07:00
|
|
|
shieldedBalance: .zero,
|
|
|
|
transparentBalance: .zero,
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(0),
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663150,
|
|
|
|
latestBlockHeight: 0,
|
2023-05-05 10:30:47 -07:00
|
|
|
latestScannedTime: 1576821833.0
|
2023-03-15 04:17:43 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
2023-04-07 05:02:05 -07:00
|
|
|
syncSessionID: uuids[0],
|
2023-05-05 10:30:47 -07:00
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(100000), total: Zatoshi(200000)),
|
2022-11-11 09:46:13 -08:00
|
|
|
transparentBalance: .zero,
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(0.9),
|
2023-05-05 10:30:47 -07:00
|
|
|
latestScannedHeight: 663189,
|
|
|
|
latestBlockHeight: 663189,
|
|
|
|
latestScannedTime: 1
|
2022-11-11 09:46:13 -08:00
|
|
|
),
|
2023-03-15 04:17:43 -07:00
|
|
|
SynchronizerState(
|
2023-04-07 05:02:05 -07:00
|
|
|
syncSessionID: uuids[0],
|
2022-11-11 09:46:13 -08:00
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(100000), total: Zatoshi(200000)),
|
2023-05-05 08:04:13 -07:00
|
|
|
transparentBalance: .zero,
|
|
|
|
internalSyncStatus: .syncing(1.0),
|
2023-05-05 10:30:47 -07:00
|
|
|
latestScannedHeight: 663189,
|
|
|
|
latestBlockHeight: 663189,
|
|
|
|
latestScannedTime: 1
|
2023-03-15 04:17:43 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
2023-04-07 05:02:05 -07:00
|
|
|
syncSessionID: uuids[0],
|
2023-03-15 04:17:43 -07:00
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(100000), total: Zatoshi(200000)),
|
|
|
|
transparentBalance: WalletBalance(verified: Zatoshi(0), total: Zatoshi(0)),
|
2023-04-28 10:13:21 -07:00
|
|
|
internalSyncStatus: .synced,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663189,
|
2023-05-05 10:30:47 -07:00
|
|
|
latestBlockHeight: 663189,
|
|
|
|
latestScannedTime: 1
|
2022-11-11 09:46:13 -08:00
|
|
|
)
|
2023-03-15 04:17:43 -07:00
|
|
|
]
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
XCTAssertEqual(states.count, expectedStates.count)
|
2023-05-01 07:28:59 -07:00
|
|
|
|
|
|
|
for (index, state) in states.enumerated() {
|
|
|
|
let expectedState = expectedStates[index]
|
|
|
|
XCTAssertEqual(state, expectedState, "Failed state comparison at index \(index).")
|
|
|
|
}
|
2023-05-05 10:30:47 -07:00
|
|
|
|
2023-04-07 05:02:05 -07:00
|
|
|
try coordinator.service.applyStaged(nextLatestHeight: 663_200)
|
|
|
|
|
2023-04-12 00:39:49 -07:00
|
|
|
sleep(2)
|
2023-04-07 05:02:05 -07:00
|
|
|
|
|
|
|
states.removeAll()
|
|
|
|
|
|
|
|
let secondSyncExpectation = XCTestExpectation(description: "second sync")
|
|
|
|
|
|
|
|
try await coordinator.sync(
|
|
|
|
completion: { _ in
|
|
|
|
secondSyncExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [secondSyncExpectation], timeout: 5)
|
2023-04-07 05:02:05 -07:00
|
|
|
|
|
|
|
let secondBatchOfExpectedStates: [SynchronizerState] = [
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[1],
|
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(100000), total: Zatoshi(200000)),
|
|
|
|
transparentBalance: WalletBalance(verified: Zatoshi(0), total: Zatoshi(0)),
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(0),
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663189,
|
2023-05-05 10:30:47 -07:00
|
|
|
latestBlockHeight: 663189,
|
|
|
|
latestScannedTime: 1.0
|
2023-04-07 05:02:05 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[1],
|
2023-05-05 10:30:47 -07:00
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(200000), total: Zatoshi(200000)),
|
2023-04-07 05:02:05 -07:00
|
|
|
transparentBalance: WalletBalance(verified: Zatoshi(0), total: Zatoshi(0)),
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(0.9),
|
2023-05-05 10:30:47 -07:00
|
|
|
latestScannedHeight: 663200,
|
|
|
|
latestBlockHeight: 663200,
|
|
|
|
latestScannedTime: 1
|
2023-04-07 05:02:05 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[1],
|
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(200000), total: Zatoshi(200000)),
|
|
|
|
transparentBalance: WalletBalance(verified: Zatoshi(0), total: Zatoshi(0)),
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(1.0),
|
2023-05-05 10:30:47 -07:00
|
|
|
latestScannedHeight: 663200,
|
|
|
|
latestBlockHeight: 663200,
|
|
|
|
latestScannedTime: 1
|
2023-04-07 05:02:05 -07:00
|
|
|
),
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: uuids[1],
|
|
|
|
shieldedBalance: WalletBalance(verified: Zatoshi(200000), total: Zatoshi(200000)),
|
|
|
|
transparentBalance: WalletBalance(verified: Zatoshi(0), total: Zatoshi(0)),
|
2023-04-28 10:13:21 -07:00
|
|
|
internalSyncStatus: .synced,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: 663200,
|
2023-05-05 10:30:47 -07:00
|
|
|
latestBlockHeight: 663200,
|
|
|
|
latestScannedTime: 1
|
2023-04-07 05:02:05 -07:00
|
|
|
)
|
|
|
|
]
|
|
|
|
|
2023-05-05 10:30:47 -07:00
|
|
|
XCTAssertEqual(states.count, secondBatchOfExpectedStates.count)
|
2023-05-01 07:28:59 -07:00
|
|
|
|
|
|
|
for (index, state) in states.enumerated() {
|
|
|
|
let expectedState = secondBatchOfExpectedStates[index]
|
|
|
|
XCTAssertEqual(state, expectedState, "Failed state comparison at index \(index).")
|
|
|
|
}
|
2022-11-11 09:46:13 -08:00
|
|
|
}
|
2022-12-19 06:15:23 -08:00
|
|
|
|
2023-03-27 07:12:06 -07:00
|
|
|
func testSyncAfterWipeWorks() async throws {
|
2023-04-12 00:39:49 -07:00
|
|
|
idGenerator.ids = [.deadbeef, .beefbeef, .beefdead]
|
2022-12-19 06:15:23 -08:00
|
|
|
try FakeChainBuilder.buildChain(darksideWallet: self.coordinator.service, branchID: branchID, chainName: chainName)
|
|
|
|
let receivedTxHeight: BlockHeight = 663188
|
|
|
|
|
|
|
|
try coordinator.applyStaged(blockheight: receivedTxHeight + 1)
|
|
|
|
|
|
|
|
sleep(2)
|
|
|
|
|
|
|
|
let firsSyncExpectation = XCTestExpectation(description: "first sync")
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
try await coordinator.sync(
|
2022-12-19 06:15:23 -08:00
|
|
|
completion: { _ in
|
|
|
|
firsSyncExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [firsSyncExpectation], timeout: 10)
|
2022-12-19 06:15:23 -08:00
|
|
|
|
2023-02-20 01:53:04 -08:00
|
|
|
let wipeFinished = XCTestExpectation(description: "SynchronizerWipeFinished Expectation")
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [wipeFinished], timeout: 1)
|
2022-12-19 06:15:23 -08:00
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
_ = try await coordinator.prepare(seed: Environment.seedBytes)
|
2022-12-19 06:15:23 -08:00
|
|
|
|
|
|
|
let secondSyncExpectation = XCTestExpectation(description: "second sync")
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
try await coordinator.sync(
|
2022-12-19 06:15:23 -08:00
|
|
|
completion: { _ in
|
|
|
|
secondSyncExpectation.fulfill()
|
|
|
|
},
|
|
|
|
error: self.handleError
|
|
|
|
)
|
|
|
|
|
2023-04-03 06:30:08 -07:00
|
|
|
await fulfillment(of: [secondSyncExpectation], timeout: 10)
|
2022-12-19 06:15:23 -08:00
|
|
|
}
|
2020-10-26 14:57:19 -07:00
|
|
|
|
2023-03-15 04:17:43 -07:00
|
|
|
func handleFoundTransactions(transactions: [ZcashTransaction.Overview]) {
|
2020-10-20 15:22:05 -07:00
|
|
|
self.foundTransactions.append(contentsOf: transactions)
|
|
|
|
}
|
|
|
|
|
2023-03-16 02:11:18 -07:00
|
|
|
func handleError(_ error: Error?) async {
|
|
|
|
_ = try? await coordinator.stop()
|
2020-10-20 15:22:05 -07:00
|
|
|
guard let testError = error else {
|
|
|
|
XCTFail("failed with nil error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
XCTFail("Failed with error: \(testError)")
|
|
|
|
}
|
|
|
|
}
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2023-04-07 05:02:05 -07:00
|
|
|
extension UUID {
|
|
|
|
static let deadbeef = UUID(uuidString: "DEADBEEF-BEEF-FAFA-BEEF-FAFAFAFAFAFA")!
|
|
|
|
static let beefbeef = UUID(uuidString: "BEEFBEEF-BEEF-DEAD-BEEF-BEEFEBEEFEBE")!
|
|
|
|
static let beefdead = UUID(uuidString: "BEEFDEAD-BEEF-FAFA-DEAD-EAEAEAEAEAEA")!
|
|
|
|
}
|