2019-10-18 11:45:19 -07:00
|
|
|
//
|
2022-09-01 05:58:41 -07:00
|
|
|
// DownloadTests.swift
|
2019-10-18 11:45:19 -07:00
|
|
|
// ZcashLightClientKitTests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 10/16/19.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
import SQLite
|
2022-02-28 09:03:20 -08:00
|
|
|
@testable import TestUtils
|
2019-10-18 11:45:19 -07:00
|
|
|
@testable import ZcashLightClientKit
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
class DownloadTests: ZcashTestCase {
|
2023-02-02 08:58:12 -08:00
|
|
|
let testFileManager = FileManager()
|
2021-07-28 09:59:10 -07:00
|
|
|
var network = ZcashNetworkBuilder.network(for: .testnet)
|
2023-03-31 10:10:35 -07:00
|
|
|
var testTempDirectory: URL!
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2023-03-29 11:28:24 -07:00
|
|
|
override func setUp() async throws {
|
|
|
|
try await super.setUp()
|
2023-03-31 10:10:35 -07:00
|
|
|
testTempDirectory = Environment.uniqueTestTempDirectory
|
2023-03-29 11:28:24 -07:00
|
|
|
try? FileManager.default.removeItem(at: testTempDirectory)
|
|
|
|
await InternalSyncProgress(alias: .default, storage: UserDefaults.standard, logger: logger).rewind(to: 0)
|
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
try self.testFileManager.createDirectory(at: testTempDirectory, withIntermediateDirectories: false)
|
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-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
|
|
|
testTempDirectory = nil
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testSingleDownload() async throws {
|
2023-05-01 07:28:59 -07:00
|
|
|
mockContainer.mock(type: ZcashRustBackendWelding.self, isSingleton: true) { _ in
|
|
|
|
ZcashRustBackend.makeForTests(fsBlockDbRoot: self.testTempDirectory, networkType: self.network.networkType)
|
|
|
|
}
|
|
|
|
mockContainer.mock(type: LightWalletService.self, isSingleton: true) { _ in
|
|
|
|
LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.eccTestnet).make()
|
|
|
|
}
|
|
|
|
let storage = mockContainer.resolve(CompactBlockRepository.self)
|
2023-03-30 03:49:28 -07:00
|
|
|
try await storage.create()
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
let blockCount = 100
|
2021-09-15 05:21:29 -07:00
|
|
|
let activationHeight = network.constants.saplingActivationHeight
|
2021-07-28 09:59:10 -07:00
|
|
|
let range = activationHeight ... activationHeight + blockCount
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
let processorConfig = CompactBlockProcessor.Configuration.standard(
|
|
|
|
for: network,
|
|
|
|
walletBirthday: network.constants.saplingActivationHeight
|
|
|
|
)
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-05-01 07:28:59 -07:00
|
|
|
let compactBlockProcessor = CompactBlockProcessor(container: mockContainer, config: processorConfig)
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
do {
|
2023-01-31 02:11:00 -08:00
|
|
|
try await compactBlockProcessor.blockDownloaderService.downloadBlockRange(range)
|
2022-09-01 05:58:41 -07:00
|
|
|
} catch {
|
|
|
|
XCTFail("Download failed with error: \(error)")
|
2019-10-30 13:18:57 -07:00
|
|
|
}
|
2023-03-27 05:26:47 -07:00
|
|
|
|
|
|
|
let latestHeight = await storage.latestHeight()
|
|
|
|
XCTAssertEqual(latestHeight, range.upperBound)
|
2023-03-29 11:28:24 -07:00
|
|
|
|
|
|
|
await compactBlockProcessor.stop()
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
}
|