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
|
|
|
|
@testable import ZcashLightClientKit
|
|
|
|
class BlockDownloaderTests: XCTestCase {
|
|
|
|
|
|
|
|
var downloader: CompactBlockDownloading!
|
|
|
|
var service: LightWalletService!
|
2019-11-18 11:49:36 -08:00
|
|
|
var storage: CompactBlockRepository!
|
2019-11-01 12:59:16 -07:00
|
|
|
var cacheDB = try! __cacheDbURL()
|
2021-07-28 09:59:10 -07:00
|
|
|
var network = DarksideWalletDNetwork()
|
|
|
|
var darksideWalletService: DarksideWalletService!
|
|
|
|
let branchID = "2bb40e60"
|
|
|
|
let chainName = "main"
|
|
|
|
override func setUpWithError() throws {
|
2021-05-07 11:50:50 -07:00
|
|
|
service = LightWalletGRPCService(endpoint: LightWalletEndpointBuilder.default)
|
2019-11-01 12:59:16 -07:00
|
|
|
storage = try! TestDbBuilder.diskCompactBlockStorage(at: cacheDB)
|
2019-10-18 11:45:19 -07:00
|
|
|
downloader = CompactBlockDownloader(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)
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
|
|
|
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
|
|
|
service = nil
|
|
|
|
storage = nil
|
|
|
|
downloader = nil
|
2019-11-01 12:59:16 -07:00
|
|
|
try? FileManager.default.removeItem(at: cacheDB)
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSmallDownloadAsync() {
|
|
|
|
|
|
|
|
let expect = XCTestExpectation(description: self.description)
|
|
|
|
expect.expectedFulfillmentCount = 3
|
2021-07-28 09:59:10 -07:00
|
|
|
let lowerRange: BlockHeight = self.network.constants.SAPLING_ACTIVATION_HEIGHT
|
|
|
|
let upperRange: BlockHeight = self.network.constants.SAPLING_ACTIVATION_HEIGHT + 99
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
let range = CompactBlockRange(uncheckedBounds: (lowerRange,upperRange))
|
|
|
|
downloader.downloadBlockRange(range) { (error) in
|
|
|
|
expect.fulfill()
|
|
|
|
XCTAssertNil(error)
|
|
|
|
|
|
|
|
// check what was 'stored'
|
|
|
|
self.storage.latestHeight { (result) in
|
|
|
|
expect.fulfill()
|
|
|
|
|
|
|
|
XCTAssertTrue(self.validate(result: result, against: upperRange))
|
|
|
|
|
2019-11-04 15:18:07 -08:00
|
|
|
self.downloader.lastDownloadedBlockHeight { (resultHeight) in
|
2019-10-18 11:45:19 -07:00
|
|
|
expect.fulfill()
|
|
|
|
XCTAssertTrue(self.validate(result: resultHeight, against: upperRange))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(for: [expect], timeout: 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSmallDownload() {
|
|
|
|
|
2021-07-28 09:59:10 -07:00
|
|
|
let lowerRange: BlockHeight = self.network.constants.SAPLING_ACTIVATION_HEIGHT
|
|
|
|
let upperRange: BlockHeight = self.network.constants.SAPLING_ACTIVATION_HEIGHT + 99
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
let range = CompactBlockRange(uncheckedBounds: (lowerRange,upperRange))
|
|
|
|
var latest: BlockHeight = 0
|
|
|
|
|
|
|
|
do {
|
2019-11-04 15:18:07 -08:00
|
|
|
latest = try downloader.lastDownloadedBlockHeight()
|
2019-10-18 11:45:19 -07:00
|
|
|
} catch {
|
|
|
|
XCTFail(error.localizedDescription)
|
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(latest, BlockHeight.empty())
|
|
|
|
XCTAssertNoThrow(try downloader.downloadBlockRange(range))
|
|
|
|
|
|
|
|
var currentLatest: BlockHeight = 0
|
|
|
|
do {
|
2019-11-04 15:18:07 -08:00
|
|
|
currentLatest = try downloader.lastDownloadedBlockHeight()
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
} catch {
|
|
|
|
XCTFail("latest block failed")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
XCTAssertEqual(currentLatest,upperRange )
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testFailure() {
|
2021-07-28 15:25:47 -07:00
|
|
|
let awfulDownloader = CompactBlockDownloader(service: AwfulLightWalletService(latestBlockHeight: self.network.constants.SAPLING_ACTIVATION_HEIGHT + 1000, service: darksideWalletService), storage: ZcashConsoleFakeStorage())
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
let expect = XCTestExpectation(description: self.description)
|
|
|
|
expect.expectedFulfillmentCount = 1
|
2021-07-28 09:59:10 -07:00
|
|
|
let lowerRange: BlockHeight = self.network.constants.SAPLING_ACTIVATION_HEIGHT
|
|
|
|
let upperRange: BlockHeight = self.network.constants.SAPLING_ACTIVATION_HEIGHT + 99
|
2019-10-18 11:45:19 -07:00
|
|
|
|
|
|
|
let range = CompactBlockRange(uncheckedBounds: (lowerRange,upperRange))
|
|
|
|
|
|
|
|
awfulDownloader.downloadBlockRange(range) { (error) in
|
|
|
|
expect.fulfill()
|
|
|
|
XCTAssertNotNil(error)
|
|
|
|
}
|
|
|
|
wait(for: [expect], timeout: 2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper functions
|
|
|
|
|
|
|
|
extension BlockDownloaderTests {
|
|
|
|
func validate(result: Result<BlockHeight,Error> ,against height: BlockHeight) -> Bool {
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success(let resultHeight):
|
|
|
|
return resultHeight == height
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|