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
|
|
|
|
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 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()
|
2023-03-31 10:10:35 -07:00
|
|
|
var rustBackend: ZcashRustBackendWelding!
|
|
|
|
var testTempDirectory: URL!
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2023-03-30 03:49:28 -07:00
|
|
|
override func setUp() async throws {
|
|
|
|
try await super.setUp()
|
2023-03-31 10:10:35 -07:00
|
|
|
testTempDirectory = Environment.uniqueTestTempDirectory
|
|
|
|
|
2023-03-22 05:47:32 -07:00
|
|
|
service = LightWalletServiceFactory(endpoint: LightWalletEndpointBuilder.default).make()
|
2023-02-17 02:02:24 -08:00
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend = ZcashRustBackend.makeForTests(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
|
|
|
networkType: network.networkType
|
|
|
|
)
|
|
|
|
|
2023-02-02 08:58:12 -08:00
|
|
|
storage = FSCompactBlockRepository(
|
2023-02-09 04:58:49 -08:00
|
|
|
fsBlockDbRoot: testTempDirectory,
|
2023-03-22 05:47:32 -07: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-03-30 03:49:28 -07:00
|
|
|
try await storage.create()
|
2023-02-17 02:02:24 -08:00
|
|
|
|
2023-01-31 02:11:00 -08:00
|
|
|
downloader = BlockDownloaderServiceImpl(service: service, storage: storage)
|
2023-03-28 06:30:03 -07:00
|
|
|
darksideWalletService = DarksideWalletService(endpoint: LightWalletEndpointBuilder.default, service: service as! LightWalletGRPCService)
|
2021-07-28 09:59:10 -07:00
|
|
|
|
|
|
|
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)
|
2023-02-16 08:14:31 -08:00
|
|
|
darksideWalletService = nil
|
2019-10-18 11:45:19 -07:00
|
|
|
service = nil
|
|
|
|
storage = nil
|
|
|
|
downloader = nil
|
2023-03-31 10:10:35 -07:00
|
|
|
rustBackend = nil
|
|
|
|
testTempDirectory = nil
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
2023-02-14 02:30:08 -08:00
|
|
|
|
2023-03-27 05:26:47 -07:00
|
|
|
func testSmallDownload() 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-03-27 05:26:47 -07:00
|
|
|
let latestHeight = await self.storage.latestHeight()
|
2022-08-29 12:31:01 -07:00
|
|
|
XCTAssertEqual(latestHeight, upperRange)
|
|
|
|
|
2023-03-27 04:40:32 -07:00
|
|
|
let resultHeight = try await self.downloader.lastDownloadedBlockHeight()
|
2022-08-29 12:31:01 -07:00
|
|
|
XCTAssertEqual(resultHeight, upperRange)
|
|
|
|
} catch {
|
2023-03-27 05:26:47 -07:00
|
|
|
XCTFail("testSmallDownload() shouldn't fail \(error)")
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|