2019-10-18 11:45:19 -07:00
|
|
|
//
|
|
|
|
// CompactBlockProcessorTests.swift
|
|
|
|
// ZcashLightClientKitTests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 20/09/2019.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import ZcashLightClientKit
|
|
|
|
class CompactBlockProcessorTests: XCTestCase {
|
|
|
|
|
|
|
|
let processorConfig = CompactBlockProcessor.Configuration.standard
|
|
|
|
var processor: CompactBlockProcessor!
|
2019-10-30 13:18:57 -07:00
|
|
|
var downloadStartedExpect: XCTestExpectation!
|
|
|
|
var updatedNotificationExpectation: XCTestExpectation!
|
|
|
|
var stopNotificationExpectation: XCTestExpectation!
|
|
|
|
var startedScanningNotificationExpectation: XCTestExpectation!
|
|
|
|
var startedValidatingNotificationExpectation: XCTestExpectation!
|
|
|
|
var idleNotificationExpectation: XCTestExpectation!
|
|
|
|
let mockLatestHeight = 282_000
|
2019-10-31 10:33:21 -07:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
override func setUp() {
|
|
|
|
// Put setup code here. This method is called before the invocation of each test method in the class.
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
let service = MockLightWalletService(latestBlockHeight: mockLatestHeight)
|
|
|
|
let storage = CompactBlockStorage.init(connectionProvider: SimpleConnectionProvider(path: processorConfig.cacheDb.absoluteString))
|
|
|
|
try! storage.createTable()
|
2019-10-18 11:45:19 -07:00
|
|
|
let downloader = CompactBlockDownloader(service: service, storage: storage)
|
2019-10-30 13:18:57 -07:00
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
processor = CompactBlockProcessor(downloader: downloader,
|
|
|
|
backend: ZcashRustBackend.self,
|
2019-11-04 15:18:07 -08:00
|
|
|
config: processorConfig)
|
2019-10-30 13:18:57 -07:00
|
|
|
|
|
|
|
downloadStartedExpect = XCTestExpectation(description: self.description + " downloadStartedExpect")
|
|
|
|
stopNotificationExpectation = XCTestExpectation(description: self.description + " stopNotificationExpectation")
|
|
|
|
updatedNotificationExpectation = XCTestExpectation(description: self.description + " updatedNotificationExpectation")
|
|
|
|
startedValidatingNotificationExpectation = XCTestExpectation(description: self.description + " startedValidatingNotificationExpectation")
|
|
|
|
startedScanningNotificationExpectation = XCTestExpectation(description: self.description + " startedScanningNotificationExpectation")
|
|
|
|
idleNotificationExpectation = XCTestExpectation(description: self.description + " idleNotificationExpectation")
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(processorFailed(_:)), name: Notification.Name.blockProcessorFailed, object: processor)
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
try! FileManager.default.removeItem(at: processorConfig.cacheDb)
|
2019-11-01 12:59:16 -07:00
|
|
|
try? FileManager.default.removeItem(at: processorConfig.dataDb)
|
2019-10-30 13:18:57 -07:00
|
|
|
downloadStartedExpect.unsubscribeFromNotifications()
|
|
|
|
stopNotificationExpectation.unsubscribeFromNotifications()
|
|
|
|
updatedNotificationExpectation.unsubscribeFromNotifications()
|
|
|
|
startedScanningNotificationExpectation.unsubscribeFromNotifications()
|
|
|
|
startedValidatingNotificationExpectation.unsubscribeFromNotifications()
|
|
|
|
idleNotificationExpectation.unsubscribeFromNotifications()
|
|
|
|
NotificationCenter.default.removeObserver(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func processorFailed(_ notification: Notification) {
|
|
|
|
XCTAssertNotNil(notification.userInfo)
|
|
|
|
if let error = notification.userInfo?["error"] {
|
|
|
|
XCTFail("CompactBlockProcessor failed with Error: \(error)")
|
|
|
|
} else {
|
|
|
|
XCTFail("CompactBlockProcessor failed")
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-31 10:33:21 -07:00
|
|
|
fileprivate func startProcessing() {
|
2019-10-18 11:45:19 -07:00
|
|
|
XCTAssertNotNil(processor)
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
// Subscribe to notifications
|
|
|
|
downloadStartedExpect.subscribe(to: Notification.Name.blockProcessorStartedDownloading, object: processor)
|
|
|
|
stopNotificationExpectation.subscribe(to: Notification.Name.blockProcessorStopped, object: processor)
|
|
|
|
updatedNotificationExpectation.subscribe(to: Notification.Name.blockProcessorUpdated, object: processor)
|
|
|
|
startedValidatingNotificationExpectation.subscribe(to: Notification.Name.blockProcessorStartedValidating, object: processor)
|
|
|
|
startedScanningNotificationExpectation.subscribe(to: Notification.Name.blockProcessorStartedScanning, object: processor)
|
|
|
|
idleNotificationExpectation.subscribe(to: Notification.Name.blockProcessorIdle, object: processor)
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
XCTAssertNoThrow(try processor.start())
|
2019-10-31 10:33:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testStartNotifiesSuscriptors() {
|
|
|
|
|
|
|
|
startProcessing()
|
2019-10-30 13:18:57 -07:00
|
|
|
|
2019-10-31 10:33:21 -07:00
|
|
|
wait(for: [
|
|
|
|
downloadStartedExpect,
|
2019-10-30 13:18:57 -07:00
|
|
|
startedValidatingNotificationExpectation,
|
|
|
|
startedScanningNotificationExpectation,
|
|
|
|
idleNotificationExpectation,
|
2019-12-03 09:11:24 -08:00
|
|
|
], timeout: 260,enforceOrder: true)
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
|
|
|
|
2019-10-31 10:33:21 -07:00
|
|
|
func testProgressNotifications() {
|
|
|
|
|
|
|
|
let expectedUpdates = expectedBatches(currentHeight: processorConfig.walletBirthday, targetHeight: mockLatestHeight, batchSize: processorConfig.downloadBatchSize)
|
|
|
|
updatedNotificationExpectation.expectedFulfillmentCount = expectedUpdates
|
|
|
|
|
|
|
|
startProcessing()
|
2019-12-03 09:11:24 -08:00
|
|
|
wait(for: [updatedNotificationExpectation], timeout: 300)
|
2019-10-31 10:33:21 -07:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private func expectedBatches(currentHeight: BlockHeight, targetHeight: BlockHeight, batchSize: Int) -> Int {
|
|
|
|
(abs(currentHeight-targetHeight)/batchSize)
|
|
|
|
}
|
|
|
|
|
2019-10-30 13:18:57 -07:00
|
|
|
func testNextBatchBlockRange() {
|
|
|
|
|
|
|
|
// test first range
|
|
|
|
var latestDownloadedHeight = processorConfig.walletBirthday // this can be either this or Wallet Birthday.
|
|
|
|
var latestBlockchainHeight = BlockHeight(281_000)
|
|
|
|
|
|
|
|
var expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight, upper:latestDownloadedHeight + processorConfig.downloadBatchSize - 1))
|
|
|
|
|
|
|
|
XCTAssertEqual(expectedBatchRange, processor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight))
|
|
|
|
|
|
|
|
// Test mid-range
|
|
|
|
latestDownloadedHeight = BlockHeight(280_100)
|
|
|
|
latestBlockchainHeight = BlockHeight(281_000)
|
|
|
|
|
|
|
|
expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight + 1, upper:latestDownloadedHeight + processorConfig.downloadBatchSize))
|
|
|
|
|
|
|
|
XCTAssertEqual(expectedBatchRange, processor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight))
|
|
|
|
|
|
|
|
latestDownloadedHeight = BlockHeight(280_950)
|
|
|
|
latestBlockchainHeight = BlockHeight(281_000)
|
|
|
|
|
|
|
|
// Test last batch range
|
|
|
|
|
|
|
|
latestDownloadedHeight = BlockHeight(280_950)
|
|
|
|
latestBlockchainHeight = BlockHeight(281_000)
|
|
|
|
|
|
|
|
expectedBatchRange = CompactBlockRange(uncheckedBounds: (lower: latestDownloadedHeight + 1, upper: latestBlockchainHeight))
|
|
|
|
|
|
|
|
XCTAssertEqual(expectedBatchRange, processor.nextBatchBlockRange(latestHeight: latestBlockchainHeight, latestDownloadedHeight: latestDownloadedHeight))
|
|
|
|
}
|
2020-01-29 19:11:40 -08:00
|
|
|
|
|
|
|
func testDetermineLowerBoundPastBirthday() {
|
|
|
|
let errorHeight = 781_906
|
|
|
|
|
|
|
|
let walletBirthday = 781_900
|
|
|
|
|
|
|
|
let result = processor.determineLowerBound(errorHeight: errorHeight, consecutiveErrors: 1, walletBirthday: walletBirthday)
|
2020-02-07 13:04:21 -08:00
|
|
|
let expected = 781_886
|
2020-01-29 19:11:40 -08:00
|
|
|
|
|
|
|
XCTAssertEqual(result, expected)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func testDetermineLowerBound() {
|
|
|
|
let errorHeight = 781_906
|
|
|
|
|
|
|
|
let walletBirthday = 780_900
|
|
|
|
|
|
|
|
let result = processor.determineLowerBound(errorHeight: errorHeight, consecutiveErrors: 0, walletBirthday: walletBirthday)
|
|
|
|
let expected = 781_896
|
|
|
|
|
|
|
|
XCTAssertEqual(result, expected)
|
|
|
|
|
|
|
|
}
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|