2021-06-17 16:58:30 -07:00
|
|
|
//
|
|
|
|
// BlockBatchValidationTests.swift
|
|
|
|
// ZcashLightClientKit-Unit-Tests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 6/17/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2022-02-28 09:03:20 -08:00
|
|
|
@testable import TestUtils
|
2021-06-17 16:58:30 -07:00
|
|
|
@testable import ZcashLightClientKit
|
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
class BlockBatchValidationTests: ZcashTestCase {
|
2023-02-02 08:58:12 -08:00
|
|
|
let testFileManager = FileManager()
|
2023-03-31 10:10:35 -07:00
|
|
|
var rustBackend: ZcashRustBackendWelding!
|
|
|
|
var testTempDirectory: URL!
|
2023-02-02 08:58:12 -08:00
|
|
|
|
|
|
|
override func setUpWithError() throws {
|
|
|
|
try super.setUpWithError()
|
2023-03-31 10:10:35 -07:00
|
|
|
testTempDirectory = Environment.uniqueTestTempDirectory
|
2023-05-01 07:28:59 -07:00
|
|
|
|
|
|
|
Dependencies.setup(
|
|
|
|
in: mockContainer,
|
|
|
|
urls: Initializer.URLs(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
|
|
|
dataDbURL: try! __dataDbURL(),
|
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL()
|
|
|
|
),
|
|
|
|
alias: .default,
|
|
|
|
networkType: .testnet,
|
|
|
|
endpoint: LightWalletEndpointBuilder.default,
|
|
|
|
loggingPolicy: .default(.debug)
|
|
|
|
)
|
|
|
|
|
|
|
|
mockContainer.mock(type: LatestBlocksDataProvider.self, isSingleton: true) { _ in LatestBlocksDataProviderMock() }
|
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
try self.testFileManager.createDirectory(at: testTempDirectory, withIntermediateDirectories: false)
|
|
|
|
rustBackend = ZcashRustBackend.makeForTests(fsBlockDbRoot: testTempDirectory, networkType: .testnet)
|
2023-02-02 08:58:12 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDownWithError() throws {
|
|
|
|
try super.tearDownWithError()
|
|
|
|
try? testFileManager.removeItem(at: testTempDirectory)
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend = nil
|
|
|
|
testTempDirectory = nil
|
2023-02-02 08:58:12 -08:00
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testBranchIdFailure() async throws {
|
2021-07-28 09:59:10 -07:00
|
|
|
let network = ZcashNetworkBuilder.network(for: .mainnet)
|
2021-09-23 06:26:41 -07:00
|
|
|
let service = MockLightWalletService(
|
|
|
|
latestBlockHeight: 1210000,
|
2023-03-22 05:47:32 -07:00
|
|
|
service: LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: LightWalletService.self, isSingleton: true) { _ in service }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
|
|
|
let storage = FSCompactBlockRepository(
|
2023-02-09 04:58:49 -08:00
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-02-02 08:58:12 -08:00
|
|
|
metadataStore: FSMetadataStore.live(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend: rustBackend,
|
2023-03-22 05:47:32 -07:00
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
),
|
|
|
|
blockDescriptor: .live,
|
2023-03-22 05:47:32 -07:00
|
|
|
contentProvider: DirectoryListingProviders.defaultSorted,
|
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: CompactBlockRepository.self, isSingleton: true) { _ in storage }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-03-30 03:49:28 -07:00
|
|
|
try await storage.create()
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: BlockDownloaderService.self, isSingleton: true) { _ in
|
|
|
|
let repository = ZcashConsoleFakeStorage(latestBlockHeight: 1220000)
|
|
|
|
return BlockDownloaderServiceImpl(service: service, storage: repository)
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
let config = CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: .default,
|
2023-02-02 08:58:12 -08:00
|
|
|
fsBlockCacheRoot: testTempDirectory,
|
2021-09-23 06:26:41 -07:00
|
|
|
dataDb: try! __dataDbURL(),
|
2022-11-28 22:38:28 -08:00
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL(),
|
2023-02-16 08:27:49 -08:00
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2021-09-23 06:26:41 -07:00
|
|
|
downloadBatchSize: 100,
|
|
|
|
retries: 5,
|
|
|
|
maxBackoffInterval: 10,
|
|
|
|
rewindDistance: 100,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { 1210000 },
|
2021-09-23 06:26:41 -07:00
|
|
|
saplingActivation: network.constants.saplingActivationHeight,
|
|
|
|
network: network
|
|
|
|
)
|
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
var info = LightdInfo()
|
|
|
|
info.blockHeight = 130000
|
|
|
|
info.branch = "d34db33f"
|
|
|
|
info.chainName = "main"
|
|
|
|
info.buildUser = "test user"
|
|
|
|
info.consensusBranchID = "d34db33f"
|
2021-09-15 05:21:29 -07:00
|
|
|
info.saplingActivationHeight = UInt64(network.constants.saplingActivationHeight)
|
2021-06-17 16:58:30 -07:00
|
|
|
service.mockLightDInfo = info
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
let mockBackend = await RustBackendMockHelper(rustBackend: rustBackend, consensusBranchID: Int32(0xd34d))
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: ZcashRustBackendWelding.self, isSingleton: true) { _ in mockBackend.rustBackendMock }
|
2022-09-01 05:58:41 -07:00
|
|
|
|
|
|
|
let compactBlockProcessor = CompactBlockProcessor(
|
2023-05-01 07:28:59 -07:00
|
|
|
container: mockContainer,
|
|
|
|
config: config
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
do {
|
2023-05-01 07:28:59 -07:00
|
|
|
try await compactBlockProcessor.figureNextBatch(downloaderService: mockContainer.resolve(BlockDownloaderService.self))
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTAssertFalse(Task.isCancelled)
|
|
|
|
} catch {
|
2021-06-17 16:58:30 -07:00
|
|
|
switch error {
|
2023-04-24 14:15:20 -07:00
|
|
|
case ZcashError.compactBlockProcessorWrongConsensusBranchId:
|
2021-06-17 16:58:30 -07:00
|
|
|
break
|
|
|
|
default:
|
2023-04-24 14:15:20 -07:00
|
|
|
XCTFail("Expected ZcashError.compactBlockProcessorWrongConsensusBranchId but found \(error)")
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testBranchNetworkMismatchFailure() async throws {
|
2021-07-28 09:59:10 -07:00
|
|
|
let network = ZcashNetworkBuilder.network(for: .mainnet)
|
2021-09-23 06:26:41 -07:00
|
|
|
let service = MockLightWalletService(
|
|
|
|
latestBlockHeight: 1210000,
|
2023-03-22 05:47:32 -07:00
|
|
|
service: LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: LightWalletService.self, isSingleton: true) { _ in service }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
|
|
|
let storage = FSCompactBlockRepository(
|
2023-02-09 04:58:49 -08:00
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-02-02 08:58:12 -08:00
|
|
|
metadataStore: FSMetadataStore.live(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend: rustBackend,
|
2023-03-22 05:47:32 -07:00
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
),
|
|
|
|
blockDescriptor: .live,
|
2023-03-22 05:47:32 -07:00
|
|
|
contentProvider: DirectoryListingProviders.defaultSorted,
|
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: CompactBlockRepository.self, isSingleton: true) { _ in storage }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-03-30 03:49:28 -07:00
|
|
|
try await storage.create()
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: BlockDownloaderService.self, isSingleton: true) { _ in
|
|
|
|
let repository = ZcashConsoleFakeStorage(latestBlockHeight: 1220000)
|
|
|
|
return BlockDownloaderServiceImpl(service: service, storage: repository)
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
let config = CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: .default,
|
2023-02-02 08:58:12 -08:00
|
|
|
fsBlockCacheRoot: testTempDirectory,
|
2021-09-23 06:26:41 -07:00
|
|
|
dataDb: try! __dataDbURL(),
|
2022-11-28 22:38:28 -08:00
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL(),
|
2023-02-16 08:27:49 -08:00
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2021-09-23 06:26:41 -07:00
|
|
|
downloadBatchSize: 100,
|
|
|
|
retries: 5,
|
|
|
|
maxBackoffInterval: 10,
|
|
|
|
rewindDistance: 100,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { 1210000 },
|
2021-09-23 06:26:41 -07:00
|
|
|
saplingActivation: network.constants.saplingActivationHeight,
|
|
|
|
network: network
|
|
|
|
)
|
2021-06-17 16:58:30 -07:00
|
|
|
var info = LightdInfo()
|
|
|
|
info.blockHeight = 130000
|
|
|
|
info.branch = "d34db33f"
|
|
|
|
info.chainName = "test"
|
|
|
|
info.buildUser = "test user"
|
|
|
|
info.consensusBranchID = "d34db4d"
|
2021-09-15 05:21:29 -07:00
|
|
|
info.saplingActivationHeight = UInt64(network.constants.saplingActivationHeight)
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
service.mockLightDInfo = info
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
let mockBackend = await RustBackendMockHelper(rustBackend: rustBackend, consensusBranchID: 0xd34db4d)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: ZcashRustBackendWelding.self, isSingleton: true) { _ in mockBackend.rustBackendMock }
|
2022-09-01 05:58:41 -07:00
|
|
|
|
|
|
|
let compactBlockProcessor = CompactBlockProcessor(
|
2023-05-01 07:28:59 -07:00
|
|
|
container: mockContainer,
|
|
|
|
config: config
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
do {
|
2023-05-01 07:28:59 -07:00
|
|
|
try await compactBlockProcessor.figureNextBatch(downloaderService: mockContainer.resolve(BlockDownloaderService.self))
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTAssertFalse(Task.isCancelled)
|
|
|
|
} catch {
|
2021-06-17 16:58:30 -07:00
|
|
|
switch error {
|
2023-04-24 14:15:20 -07:00
|
|
|
case ZcashError.compactBlockProcessorNetworkMismatch(.mainnet, .testnet):
|
2021-06-17 16:58:30 -07:00
|
|
|
break
|
|
|
|
default:
|
2023-04-24 14:15:20 -07:00
|
|
|
XCTFail("Expected ZcashError.compactBlockProcessorNetworkMismatch but found \(error)")
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testBranchNetworkTypeWrongFailure() async throws {
|
2021-07-28 09:59:10 -07:00
|
|
|
let network = ZcashNetworkBuilder.network(for: .testnet)
|
2021-09-23 06:26:41 -07:00
|
|
|
let service = MockLightWalletService(
|
|
|
|
latestBlockHeight: 1210000,
|
2023-03-22 05:47:32 -07:00
|
|
|
service: LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: LightWalletService.self, isSingleton: true) { _ in service }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
|
|
|
let storage = FSCompactBlockRepository(
|
2023-02-09 04:58:49 -08:00
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-02-02 08:58:12 -08:00
|
|
|
metadataStore: FSMetadataStore.live(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend: rustBackend,
|
2023-03-22 05:47:32 -07:00
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
),
|
|
|
|
blockDescriptor: .live,
|
2023-03-22 05:47:32 -07:00
|
|
|
contentProvider: DirectoryListingProviders.defaultSorted,
|
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: CompactBlockRepository.self, isSingleton: true) { _ in storage }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-03-30 03:49:28 -07:00
|
|
|
try await storage.create()
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: BlockDownloaderService.self, isSingleton: true) { _ in
|
|
|
|
let repository = ZcashConsoleFakeStorage(latestBlockHeight: 1220000)
|
|
|
|
return BlockDownloaderServiceImpl(service: service, storage: repository)
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
let config = CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: .default,
|
2023-02-02 08:58:12 -08:00
|
|
|
fsBlockCacheRoot: testTempDirectory,
|
2021-09-23 06:26:41 -07:00
|
|
|
dataDb: try! __dataDbURL(),
|
2022-11-28 22:38:28 -08:00
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL(),
|
2023-02-16 08:27:49 -08:00
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2021-09-23 06:26:41 -07:00
|
|
|
downloadBatchSize: 100,
|
|
|
|
retries: 5,
|
|
|
|
maxBackoffInterval: 10,
|
|
|
|
rewindDistance: 100,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { 1210000 },
|
2021-09-23 06:26:41 -07:00
|
|
|
saplingActivation: network.constants.saplingActivationHeight,
|
|
|
|
network: network
|
|
|
|
)
|
2021-06-17 16:58:30 -07:00
|
|
|
var info = LightdInfo()
|
|
|
|
info.blockHeight = 130000
|
|
|
|
info.branch = "d34db33f"
|
|
|
|
info.chainName = "another"
|
|
|
|
info.buildUser = "test user"
|
|
|
|
info.consensusBranchID = "d34db4d"
|
2021-09-15 05:21:29 -07:00
|
|
|
info.saplingActivationHeight = UInt64(network.constants.saplingActivationHeight)
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
service.mockLightDInfo = info
|
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
let mockBackend = await RustBackendMockHelper(rustBackend: rustBackend, consensusBranchID: 0xd34db4d)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: ZcashRustBackendWelding.self, isSingleton: true) { _ in mockBackend.rustBackendMock }
|
2022-09-01 05:58:41 -07:00
|
|
|
|
|
|
|
let compactBlockProcessor = CompactBlockProcessor(
|
2023-05-01 07:28:59 -07:00
|
|
|
container: mockContainer,
|
|
|
|
config: config
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
do {
|
2023-05-01 07:28:59 -07:00
|
|
|
try await compactBlockProcessor.figureNextBatch(downloaderService: mockContainer.resolve(BlockDownloaderService.self))
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTAssertFalse(Task.isCancelled)
|
|
|
|
} catch {
|
2021-06-17 16:58:30 -07:00
|
|
|
switch error {
|
2023-04-24 14:15:20 -07:00
|
|
|
case ZcashError.compactBlockProcessorChainName:
|
2021-06-17 16:58:30 -07:00
|
|
|
break
|
|
|
|
default:
|
2023-04-24 14:15:20 -07:00
|
|
|
XCTFail("Expected ZcashError.compactBlockProcessorChainName but found \(error)")
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testSaplingActivationHeightMismatch() async throws {
|
2021-07-28 09:59:10 -07:00
|
|
|
let network = ZcashNetworkBuilder.network(for: .mainnet)
|
2021-09-23 06:26:41 -07:00
|
|
|
let service = MockLightWalletService(
|
|
|
|
latestBlockHeight: 1210000,
|
2023-03-22 05:47:32 -07:00
|
|
|
service: LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: LightWalletService.self, isSingleton: true) { _ in service }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
|
|
|
let storage = FSCompactBlockRepository(
|
2023-02-09 04:58:49 -08:00
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-02-02 08:58:12 -08:00
|
|
|
metadataStore: FSMetadataStore.live(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend: rustBackend,
|
2023-03-22 05:47:32 -07:00
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
),
|
|
|
|
blockDescriptor: .live,
|
2023-03-22 05:47:32 -07:00
|
|
|
contentProvider: DirectoryListingProviders.defaultSorted,
|
|
|
|
logger: logger
|
2023-02-02 08:58:12 -08:00
|
|
|
)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: CompactBlockRepository.self, isSingleton: true) { _ in storage }
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-03-30 03:49:28 -07:00
|
|
|
try await storage.create()
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: BlockDownloaderService.self, isSingleton: true) { _ in
|
|
|
|
let repository = ZcashConsoleFakeStorage(latestBlockHeight: 1220000)
|
|
|
|
return BlockDownloaderServiceImpl(service: service, storage: repository)
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
let config = CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: .default,
|
2023-02-02 08:58:12 -08:00
|
|
|
fsBlockCacheRoot: testTempDirectory,
|
2021-09-23 06:26:41 -07:00
|
|
|
dataDb: try! __dataDbURL(),
|
2022-11-28 22:38:28 -08:00
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL(),
|
2023-02-16 08:27:49 -08:00
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2021-09-23 06:26:41 -07:00
|
|
|
downloadBatchSize: 100,
|
|
|
|
retries: 5,
|
|
|
|
maxBackoffInterval: 10,
|
|
|
|
rewindDistance: 100,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { 1210000 },
|
2021-09-23 06:26:41 -07:00
|
|
|
saplingActivation: network.constants.saplingActivationHeight,
|
|
|
|
network: network
|
|
|
|
)
|
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
var info = LightdInfo()
|
|
|
|
info.blockHeight = 130000
|
|
|
|
info.branch = "d34db33f"
|
|
|
|
info.chainName = "main"
|
|
|
|
info.buildUser = "test user"
|
|
|
|
info.consensusBranchID = "d34db4d"
|
|
|
|
info.saplingActivationHeight = UInt64(3434343)
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
service.mockLightDInfo = info
|
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
let mockBackend = await RustBackendMockHelper(rustBackend: rustBackend, consensusBranchID: 0xd34db4d)
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: ZcashRustBackendWelding.self, isSingleton: true) { _ in mockBackend.rustBackendMock }
|
2022-09-01 05:58:41 -07:00
|
|
|
|
|
|
|
let compactBlockProcessor = CompactBlockProcessor(
|
2023-05-01 07:28:59 -07:00
|
|
|
container: mockContainer,
|
|
|
|
config: config
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
do {
|
2023-05-01 07:28:59 -07:00
|
|
|
try await compactBlockProcessor.figureNextBatch(downloaderService: mockContainer.resolve(BlockDownloaderService.self))
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTAssertFalse(Task.isCancelled)
|
|
|
|
} catch {
|
2021-06-17 16:58:30 -07:00
|
|
|
switch error {
|
2023-04-24 14:15:20 -07:00
|
|
|
case ZcashError.compactBlockProcessorSaplingActivationMismatch(
|
|
|
|
network.constants.saplingActivationHeight,
|
|
|
|
BlockHeight(info.saplingActivationHeight)
|
2021-09-23 06:26:41 -07:00
|
|
|
):
|
2021-06-17 16:58:30 -07:00
|
|
|
break
|
|
|
|
default:
|
2023-04-24 14:15:20 -07:00
|
|
|
XCTFail("Expected ZcashError.compactBlockProcessorSaplingActivationMismatch but found \(error)")
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testResultIsWait() async throws {
|
2021-07-28 09:59:10 -07:00
|
|
|
let network = ZcashNetworkBuilder.network(for: .mainnet)
|
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
let expectedLatestHeight = BlockHeight(1210000)
|
2021-09-23 06:26:41 -07:00
|
|
|
let service = MockLightWalletService(
|
|
|
|
latestBlockHeight: expectedLatestHeight,
|
2023-03-22 05:47:32 -07:00
|
|
|
service: LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2022-11-23 10:05:49 -08:00
|
|
|
let expectedStoredLatestHeight = BlockHeight(1220000)
|
2022-09-01 05:58:41 -07:00
|
|
|
let expectedResult = CompactBlockProcessor.NextState.wait(
|
2021-09-23 06:26:41 -07:00
|
|
|
latestHeight: expectedLatestHeight,
|
2022-11-23 10:05:49 -08:00
|
|
|
latestDownloadHeight: expectedStoredLatestHeight
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2022-11-23 10:05:49 -08:00
|
|
|
let repository = ZcashConsoleFakeStorage(latestBlockHeight: expectedStoredLatestHeight)
|
2023-01-31 02:11:00 -08:00
|
|
|
let downloaderService = BlockDownloaderServiceImpl(service: service, storage: repository)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
let config = CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: .default,
|
2023-02-02 08:58:12 -08:00
|
|
|
fsBlockCacheRoot: testTempDirectory,
|
2021-09-23 06:26:41 -07:00
|
|
|
dataDb: try! __dataDbURL(),
|
2022-11-28 22:38:28 -08:00
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL(),
|
2023-02-16 08:27:49 -08:00
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2021-09-23 06:26:41 -07:00
|
|
|
downloadBatchSize: 100,
|
|
|
|
retries: 5,
|
|
|
|
maxBackoffInterval: 10,
|
|
|
|
rewindDistance: 100,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { 1210000 },
|
2021-09-23 06:26:41 -07:00
|
|
|
saplingActivation: network.constants.saplingActivationHeight,
|
|
|
|
network: network
|
|
|
|
)
|
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
var info = LightdInfo()
|
|
|
|
info.blockHeight = UInt64(expectedLatestHeight)
|
|
|
|
info.branch = "d34db33f"
|
|
|
|
info.chainName = "main"
|
|
|
|
info.buildUser = "test user"
|
|
|
|
info.consensusBranchID = "d34db4d"
|
2021-09-15 05:21:29 -07:00
|
|
|
info.saplingActivationHeight = UInt64(network.constants.saplingActivationHeight)
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
service.mockLightDInfo = info
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
let mockBackend = await RustBackendMockHelper(rustBackend: rustBackend, consensusBranchID: 0xd34db4d)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
var nextBatch: CompactBlockProcessor.NextState?
|
|
|
|
do {
|
2023-03-27 05:26:47 -07:00
|
|
|
nextBatch = try await CompactBlockProcessor.NextStateHelper.nextState(
|
2022-11-11 09:46:13 -08:00
|
|
|
service: service,
|
2023-01-31 02:11:00 -08:00
|
|
|
downloaderService: downloaderService,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestBlocksDataProvider: LatestBlocksDataProviderMock(
|
|
|
|
latestScannedHeight: expectedStoredLatestHeight,
|
|
|
|
latestBlockHeight: expectedLatestHeight
|
|
|
|
),
|
2022-11-11 09:46:13 -08:00
|
|
|
config: config,
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend: mockBackend.rustBackendMock,
|
2023-03-22 05:47:32 -07:00
|
|
|
internalSyncProgress: InternalSyncProgress(
|
|
|
|
alias: .default,
|
|
|
|
storage: InternalSyncProgressMemoryStorage(),
|
|
|
|
logger: logger
|
|
|
|
)
|
2022-11-11 09:46:13 -08:00
|
|
|
)
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTAssertFalse(Task.isCancelled)
|
|
|
|
} catch {
|
2021-07-28 09:59:10 -07:00
|
|
|
XCTFail("this shouldn't happen: \(error)")
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 08:09:04 -08:00
|
|
|
guard nextBatch != nil else {
|
2021-06-17 16:58:30 -07:00
|
|
|
XCTFail("result should not be nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertTrue(
|
|
|
|
{
|
2022-11-23 10:05:49 -08:00
|
|
|
switch (nextBatch, expectedResult) {
|
2023-01-18 08:09:04 -08:00
|
|
|
case let (.wait(latestHeight, latestDownloadHeight), .wait(expectedLatestHeight, exectedLatestDownloadHeight)):
|
2022-11-23 10:05:49 -08:00
|
|
|
return latestHeight == expectedLatestHeight && latestDownloadHeight == exectedLatestDownloadHeight
|
2021-09-23 06:26:41 -07:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}(),
|
2022-09-01 05:58:41 -07:00
|
|
|
"Expected \(expectedResult) got: \(String(describing: nextBatch))"
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testResultProcessNew() async throws {
|
2021-07-28 09:59:10 -07:00
|
|
|
let network = ZcashNetworkBuilder.network(for: .mainnet)
|
2021-06-17 16:58:30 -07:00
|
|
|
let expectedLatestHeight = BlockHeight(1230000)
|
2021-09-23 06:26:41 -07:00
|
|
|
let service = MockLightWalletService(
|
|
|
|
latestBlockHeight: expectedLatestHeight,
|
2023-03-22 05:47:32 -07:00
|
|
|
service: LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-04-14 00:08:37 -07:00
|
|
|
let expectedStoredLatestHeight = BlockHeight(1220000)
|
2021-06-17 16:58:30 -07:00
|
|
|
let walletBirthday = BlockHeight(1210000)
|
2022-11-23 10:05:49 -08:00
|
|
|
|
|
|
|
let ranges = SyncRanges(
|
|
|
|
latestBlockHeight: expectedLatestHeight,
|
2022-12-01 08:57:13 -08:00
|
|
|
downloadedButUnscannedRange: nil,
|
2023-04-14 00:08:37 -07:00
|
|
|
downloadAndScanRange: expectedStoredLatestHeight + 1...expectedLatestHeight,
|
2022-11-23 10:05:49 -08:00
|
|
|
enhanceRange: walletBirthday...expectedLatestHeight,
|
2023-03-09 10:40:06 -08:00
|
|
|
fetchUTXORange: walletBirthday...expectedLatestHeight,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestScannedHeight: expectedStoredLatestHeight,
|
|
|
|
latestDownloadedBlockHeight: expectedStoredLatestHeight
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2022-11-23 10:05:49 -08:00
|
|
|
let expectedResult = CompactBlockProcessor.NextState.processNewBlocks(ranges: ranges)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2023-04-14 00:08:37 -07:00
|
|
|
let repository = ZcashConsoleFakeStorage(latestBlockHeight: expectedStoredLatestHeight)
|
2023-01-31 02:11:00 -08:00
|
|
|
let downloaderService = BlockDownloaderServiceImpl(service: service, storage: repository)
|
2021-09-23 06:26:41 -07:00
|
|
|
let config = CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: .default,
|
2023-02-02 08:58:12 -08:00
|
|
|
fsBlockCacheRoot: testTempDirectory,
|
2021-09-23 06:26:41 -07:00
|
|
|
dataDb: try! __dataDbURL(),
|
2022-11-28 22:38:28 -08:00
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL(),
|
2023-02-16 08:27:49 -08:00
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2021-09-23 06:26:41 -07:00
|
|
|
downloadBatchSize: 100,
|
|
|
|
retries: 5,
|
|
|
|
maxBackoffInterval: 10,
|
|
|
|
rewindDistance: 100,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { walletBirthday },
|
2021-09-23 06:26:41 -07:00
|
|
|
saplingActivation: network.constants.saplingActivationHeight,
|
|
|
|
network: network
|
|
|
|
)
|
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
var info = LightdInfo()
|
|
|
|
info.blockHeight = UInt64(expectedLatestHeight)
|
|
|
|
info.branch = "d34db33f"
|
|
|
|
info.chainName = "main"
|
|
|
|
info.buildUser = "test user"
|
|
|
|
info.consensusBranchID = "d34db4d"
|
2021-09-15 05:21:29 -07:00
|
|
|
info.saplingActivationHeight = UInt64(network.constants.saplingActivationHeight)
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
service.mockLightDInfo = info
|
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
let mockBackend = await RustBackendMockHelper(rustBackend: rustBackend, consensusBranchID: 0xd34db4d)
|
2022-09-01 05:58:41 -07:00
|
|
|
|
|
|
|
var nextBatch: CompactBlockProcessor.NextState?
|
|
|
|
do {
|
2023-03-27 05:26:47 -07:00
|
|
|
nextBatch = try await CompactBlockProcessor.NextStateHelper.nextState(
|
2022-11-11 09:46:13 -08:00
|
|
|
service: service,
|
2023-01-31 02:11:00 -08:00
|
|
|
downloaderService: downloaderService,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestBlocksDataProvider: LatestBlocksDataProviderMock(
|
|
|
|
latestScannedHeight: expectedStoredLatestHeight,
|
|
|
|
latestBlockHeight: expectedLatestHeight
|
|
|
|
),
|
2022-11-11 09:46:13 -08:00
|
|
|
config: config,
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend: mockBackend.rustBackendMock,
|
2023-03-22 05:47:32 -07:00
|
|
|
internalSyncProgress: InternalSyncProgress(
|
|
|
|
alias: .default,
|
|
|
|
storage: InternalSyncProgressMemoryStorage(),
|
|
|
|
logger: logger
|
|
|
|
)
|
2022-11-11 09:46:13 -08:00
|
|
|
)
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTAssertFalse(Task.isCancelled)
|
|
|
|
} catch {
|
|
|
|
XCTFail("this shouldn't happen: \(error)")
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 08:09:04 -08:00
|
|
|
guard nextBatch != nil else {
|
2021-06-17 16:58:30 -07:00
|
|
|
XCTFail("result should not be nil")
|
|
|
|
return
|
|
|
|
}
|
2022-11-23 10:05:49 -08:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertTrue(
|
|
|
|
{
|
2023-01-18 08:09:04 -08:00
|
|
|
switch (nextBatch, expectedResult) {
|
|
|
|
case let (.processNewBlocks(ranges), .processNewBlocks(expectedRanges)):
|
2022-11-23 10:05:49 -08:00
|
|
|
return ranges == expectedRanges
|
2021-09-23 06:26:41 -07:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}(),
|
2022-09-01 05:58:41 -07:00
|
|
|
"Expected \(expectedResult) got: \(String(describing: nextBatch))"
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testResultProcessorFinished() async throws {
|
2021-07-28 09:59:10 -07:00
|
|
|
let network = ZcashNetworkBuilder.network(for: .mainnet)
|
2021-06-17 16:58:30 -07:00
|
|
|
let expectedLatestHeight = BlockHeight(1230000)
|
2021-09-23 06:26:41 -07:00
|
|
|
let service = MockLightWalletService(
|
|
|
|
latestBlockHeight: expectedLatestHeight,
|
2023-03-22 05:47:32 -07:00
|
|
|
service: LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-04-14 00:08:37 -07:00
|
|
|
let expectedStoredLatestHeight = BlockHeight(1230000)
|
2021-06-17 16:58:30 -07:00
|
|
|
let walletBirthday = BlockHeight(1210000)
|
2023-04-14 00:08:37 -07:00
|
|
|
let expectedResult = CompactBlockProcessor.NextState.finishProcessing(height: expectedStoredLatestHeight)
|
|
|
|
let repository = ZcashConsoleFakeStorage(latestBlockHeight: expectedStoredLatestHeight)
|
2023-01-31 02:11:00 -08:00
|
|
|
let downloaderService = BlockDownloaderServiceImpl(service: service, storage: repository)
|
2021-09-23 06:26:41 -07:00
|
|
|
let config = CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: .default,
|
2023-02-02 08:58:12 -08:00
|
|
|
fsBlockCacheRoot: testTempDirectory,
|
2021-09-23 06:26:41 -07:00
|
|
|
dataDb: try! __dataDbURL(),
|
2022-11-28 22:38:28 -08:00
|
|
|
spendParamsURL: try! __spendParamsURL(),
|
|
|
|
outputParamsURL: try! __outputParamsURL(),
|
2023-02-16 08:27:49 -08:00
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2021-09-23 06:26:41 -07:00
|
|
|
downloadBatchSize: 100,
|
|
|
|
retries: 5,
|
|
|
|
maxBackoffInterval: 10,
|
|
|
|
rewindDistance: 100,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { walletBirthday },
|
2021-09-23 06:26:41 -07:00
|
|
|
saplingActivation: network.constants.saplingActivationHeight,
|
|
|
|
network: network
|
|
|
|
)
|
|
|
|
|
2023-03-22 05:47:32 -07:00
|
|
|
let internalSyncProgress = InternalSyncProgress(
|
|
|
|
alias: .default,
|
|
|
|
storage: InternalSyncProgressMemoryStorage(),
|
|
|
|
logger: logger
|
|
|
|
)
|
2023-04-14 00:08:37 -07:00
|
|
|
await internalSyncProgress.set(expectedStoredLatestHeight, .latestEnhancedHeight)
|
|
|
|
await internalSyncProgress.set(expectedStoredLatestHeight, .latestUTXOFetchedHeight)
|
2022-11-11 09:46:13 -08:00
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
var info = LightdInfo()
|
|
|
|
info.blockHeight = UInt64(expectedLatestHeight)
|
|
|
|
info.branch = "d34db33f"
|
|
|
|
info.chainName = "main"
|
|
|
|
info.buildUser = "test user"
|
|
|
|
info.consensusBranchID = "d34db4d"
|
2021-09-15 05:21:29 -07:00
|
|
|
info.saplingActivationHeight = UInt64(network.constants.saplingActivationHeight)
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2021-06-17 16:58:30 -07:00
|
|
|
service.mockLightDInfo = info
|
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
let mockBackend = await RustBackendMockHelper(rustBackend: rustBackend, consensusBranchID: 0xd34db4d)
|
2022-09-01 05:58:41 -07:00
|
|
|
|
|
|
|
var nextBatch: CompactBlockProcessor.NextState?
|
|
|
|
do {
|
2023-03-27 05:26:47 -07:00
|
|
|
nextBatch = try await CompactBlockProcessor.NextStateHelper.nextState(
|
2022-11-11 09:46:13 -08:00
|
|
|
service: service,
|
2023-01-31 02:11:00 -08:00
|
|
|
downloaderService: downloaderService,
|
2023-04-14 00:08:37 -07:00
|
|
|
latestBlocksDataProvider: LatestBlocksDataProviderMock(
|
|
|
|
latestScannedHeight: expectedStoredLatestHeight,
|
|
|
|
latestBlockHeight: expectedLatestHeight
|
|
|
|
),
|
2022-11-11 09:46:13 -08:00
|
|
|
config: config,
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend: mockBackend.rustBackendMock,
|
2022-11-23 10:05:49 -08:00
|
|
|
internalSyncProgress: internalSyncProgress
|
2022-11-11 09:46:13 -08:00
|
|
|
)
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTAssertFalse(Task.isCancelled)
|
|
|
|
} catch {
|
|
|
|
XCTFail("this shouldn't happen: \(error)")
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 08:09:04 -08:00
|
|
|
guard nextBatch != nil else {
|
2021-06-17 16:58:30 -07:00
|
|
|
XCTFail("result should not be nil")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
XCTAssertTrue(
|
|
|
|
{
|
2022-11-23 10:05:49 -08:00
|
|
|
switch (nextBatch, expectedResult) {
|
2023-01-18 08:09:04 -08:00
|
|
|
case let (.finishProcessing(height), .finishProcessing(expectedHeight)):
|
2022-11-23 10:05:49 -08:00
|
|
|
return height == expectedHeight
|
2021-09-23 06:26:41 -07:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}(),
|
2022-09-01 05:58:41 -07:00
|
|
|
"Expected \(expectedResult) got: \(String(describing: nextBatch))"
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2021-06-17 16:58:30 -07:00
|
|
|
}
|
|
|
|
}
|