2019-10-18 11:45:19 -07:00
|
|
|
//
|
|
|
|
// BlockDownloaderTests.swift
|
|
|
|
// ZcashLightClientKitTests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 18/09/2019.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
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-02-02 08:58:12 -08:00
|
|
|
// swiftlint:disable implicitly_unwrapped_optional force_cast
|
2019-10-18 11:45:19 -07:00
|
|
|
class BlockDownloaderTests: XCTestCase {
|
2021-09-23 06:26:41 -07:00
|
|
|
let branchID = "2bb40e60"
|
|
|
|
let chainName = "main"
|
2023-02-02 08:58:12 -08:00
|
|
|
let testTempDirectory = URL(fileURLWithPath: NSString(
|
|
|
|
string: NSTemporaryDirectory()
|
|
|
|
)
|
|
|
|
.appendingPathComponent("tmp-\(Int.random(in: 0 ... .max))"))
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2023-02-02 08:58:12 -08:00
|
|
|
let testFileManager = FileManager()
|
2021-09-23 06:26:41 -07:00
|
|
|
var darksideWalletService: DarksideWalletService!
|
2023-01-31 02:11:00 -08:00
|
|
|
var downloader: BlockDownloaderService!
|
2019-10-18 11:45:19 -07:00
|
|
|
var service: LightWalletService!
|
2019-11-18 11:49:36 -08:00
|
|
|
var storage: CompactBlockRepository!
|
2021-07-28 09:59:10 -07:00
|
|
|
var network = DarksideWalletDNetwork()
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2021-07-28 09:59:10 -07:00
|
|
|
override func setUpWithError() throws {
|
2021-09-23 06:26:41 -07:00
|
|
|
try super.setUpWithError()
|
2021-05-07 11:50:50 -07:00
|
|
|
service = LightWalletGRPCService(endpoint: LightWalletEndpointBuilder.default)
|
2023-02-02 08:58:12 -08:00
|
|
|
try self.testFileManager.createDirectory(at: self.testTempDirectory, withIntermediateDirectories: false)
|
|
|
|
storage = FSCompactBlockRepository(
|
|
|
|
cacheDirectory: testTempDirectory,
|
|
|
|
metadataStore: FSMetadataStore.live(fsBlockDbRoot: testTempDirectory, rustBackend: ZcashRustBackend.self),
|
|
|
|
blockDescriptor: .live,
|
|
|
|
contentProvider: DirectoryListingProviders.defaultSorted
|
|
|
|
)
|
2023-01-31 02:11:00 -08:00
|
|
|
downloader = BlockDownloaderServiceImpl(service: service, storage: storage)
|
2021-07-28 09:59:10 -07:00
|
|
|
darksideWalletService = DarksideWalletService(service: service as! LightWalletGRPCService)
|
|
|
|
|
|
|
|
try FakeChainBuilder.buildChain(darksideWallet: darksideWalletService, branchID: branchID, chainName: chainName)
|
|
|
|
try darksideWalletService.applyStaged(nextLatestHeight: 663250)
|
2022-10-28 03:18:19 -07:00
|
|
|
|
|
|
|
sleep(2)
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
2023-01-18 08:09:04 -08:00
|
|
|
super.tearDown()
|
2023-02-02 08:58:12 -08:00
|
|
|
try? testFileManager.removeItem(at: testTempDirectory)
|
2019-10-18 11:45:19 -07:00
|
|
|
service = nil
|
|
|
|
storage = nil
|
|
|
|
downloader = nil
|
|
|
|
}
|
|
|
|
|
2022-08-29 12:31:01 -07:00
|
|
|
func testSmallDownloadAsync() async {
|
2021-09-15 05:21:29 -07:00
|
|
|
let lowerRange: BlockHeight = self.network.constants.saplingActivationHeight
|
|
|
|
let upperRange: BlockHeight = self.network.constants.saplingActivationHeight + 99
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
let range = CompactBlockRange(uncheckedBounds: (lowerRange, upperRange))
|
2022-08-29 12:31:01 -07:00
|
|
|
do {
|
2022-10-03 16:05:11 -07:00
|
|
|
try await downloader.downloadBlockRange(range)
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2022-08-29 12:31:01 -07:00
|
|
|
// check what was 'stored'
|
2023-02-02 08:58:12 -08:00
|
|
|
let latestHeight = await self.storage.latestHeightAsync()
|
2022-08-29 12:31:01 -07:00
|
|
|
XCTAssertEqual(latestHeight, upperRange)
|
|
|
|
|
|
|
|
let resultHeight = try await self.downloader.lastDownloadedBlockHeightAsync()
|
|
|
|
XCTAssertEqual(resultHeight, upperRange)
|
|
|
|
} catch {
|
|
|
|
XCTFail("testSmallDownloadAsync() shouldn't fail")
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-29 12:31:01 -07:00
|
|
|
func testFailure() async {
|
2023-01-31 02:11:00 -08:00
|
|
|
let awfulDownloader = BlockDownloaderServiceImpl(
|
2021-09-23 06:26:41 -07:00
|
|
|
service: AwfulLightWalletService(
|
|
|
|
latestBlockHeight: self.network.constants.saplingActivationHeight + 1000,
|
|
|
|
service: darksideWalletService
|
|
|
|
),
|
|
|
|
storage: ZcashConsoleFakeStorage()
|
|
|
|
)
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2021-09-15 05:21:29 -07:00
|
|
|
let lowerRange: BlockHeight = self.network.constants.saplingActivationHeight
|
|
|
|
let upperRange: BlockHeight = self.network.constants.saplingActivationHeight + 99
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
let range = CompactBlockRange(uncheckedBounds: (lowerRange, upperRange))
|
2022-08-29 12:31:01 -07:00
|
|
|
|
|
|
|
do {
|
2022-10-03 16:05:11 -07:00
|
|
|
try await awfulDownloader.downloadBlockRange(range)
|
2022-08-29 12:31:01 -07:00
|
|
|
} catch {
|
2019-10-18 11:45:19 -07:00
|
|
|
XCTAssertNotNil(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper functions
|
|
|
|
|
|
|
|
extension BlockDownloaderTests {
|
2021-09-23 06:26:41 -07:00
|
|
|
func validate(result: Result<BlockHeight, Error>, against height: BlockHeight) -> Bool {
|
2019-10-18 11:45:19 -07:00
|
|
|
switch result {
|
|
|
|
case .success(let resultHeight):
|
|
|
|
return resultHeight == height
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|