2021-05-28 15:45:18 -07:00
|
|
|
//
|
|
|
|
// BlockStreamingTest.swift
|
|
|
|
// ZcashLightClientKit-Unit-Tests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 5/25/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
2022-02-28 09:03:20 -08:00
|
|
|
@testable import TestUtils
|
2021-05-28 15:45:18 -07:00
|
|
|
@testable import ZcashLightClientKit
|
2021-09-23 06:26:41 -07:00
|
|
|
|
|
|
|
// swiftlint:disable print_function_usage
|
2021-05-28 15:45:18 -07:00
|
|
|
class BlockStreamingTest: XCTestCase {
|
2023-02-02 08:58:12 -08:00
|
|
|
let testTempDirectory = URL(fileURLWithPath: NSString(
|
|
|
|
string: NSTemporaryDirectory()
|
|
|
|
)
|
|
|
|
.appendingPathComponent("tmp-\(Int.random(in: 0 ... .max))"))
|
|
|
|
|
|
|
|
let testFileManager = FileManager()
|
|
|
|
|
2021-05-28 15:45:18 -07:00
|
|
|
override func setUpWithError() throws {
|
2021-09-23 06:26:41 -07:00
|
|
|
try super.setUpWithError()
|
2023-02-02 08:58:12 -08:00
|
|
|
try self.testFileManager.createDirectory(at: self.testTempDirectory, withIntermediateDirectories: false)
|
2023-01-26 09:14:07 -08:00
|
|
|
logger = OSLogger(logLevel: .debug)
|
2021-05-28 15:45:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDownWithError() throws {
|
2021-09-23 06:26:41 -07:00
|
|
|
try super.tearDownWithError()
|
2021-06-02 14:32:05 -07:00
|
|
|
try? FileManager.default.removeItem(at: __dataDbURL())
|
2023-02-02 08:58:12 -08:00
|
|
|
try? testFileManager.removeItem(at: testTempDirectory)
|
2021-05-28 15:45:18 -07:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:05:11 -07:00
|
|
|
func testStream() async throws {
|
2023-02-03 02:23:35 -08:00
|
|
|
let endpoint = LightWalletEndpoint(
|
|
|
|
address: LightWalletEndpointBuilder.eccTestnet.host,
|
2021-09-23 06:26:41 -07:00
|
|
|
port: 9067,
|
|
|
|
secure: true,
|
2023-02-03 02:23:35 -08:00
|
|
|
singleCallTimeoutInMillis: 1000,
|
|
|
|
streamingCallTimeoutInMillis: 100000
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-02-03 02:23:35 -08:00
|
|
|
let service = LightWalletServiceFactory(endpoint: endpoint, connectionStateChange: { _, _ in }).make()
|
|
|
|
|
2021-05-28 15:45:18 -07:00
|
|
|
let latestHeight = try service.latestBlockHeight()
|
|
|
|
|
|
|
|
let startHeight = latestHeight - 100_000
|
2021-09-23 06:26:41 -07:00
|
|
|
var blocks: [ZcashCompactBlock] = []
|
2022-10-03 16:05:11 -07:00
|
|
|
let stream = service.blockStream(startHeight: startHeight, endHeight: latestHeight)
|
|
|
|
|
|
|
|
do {
|
|
|
|
for try await compactBlock in stream {
|
|
|
|
print("received block \(compactBlock.height)")
|
|
|
|
blocks.append(compactBlock)
|
|
|
|
print("progressHeight: \(compactBlock.height)")
|
|
|
|
print("startHeight: \(startHeight)")
|
|
|
|
print("targetHeight: \(latestHeight)")
|
2021-05-28 15:45:18 -07:00
|
|
|
}
|
2022-10-03 16:05:11 -07:00
|
|
|
} catch {
|
|
|
|
XCTFail("failed with error: \(error)")
|
2021-05-28 15:45:18 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testStreamCancellation() async throws {
|
2023-02-03 02:23:35 -08:00
|
|
|
let endpoint = LightWalletEndpoint(
|
|
|
|
address: LightWalletEndpointBuilder.eccTestnet.host,
|
2021-09-23 06:26:41 -07:00
|
|
|
port: 9067,
|
|
|
|
secure: true,
|
2023-02-03 02:23:35 -08:00
|
|
|
singleCallTimeoutInMillis: 10000,
|
|
|
|
streamingCallTimeoutInMillis: 10000
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-02-03 02:23:35 -08:00
|
|
|
let service = LightWalletServiceFactory(endpoint: endpoint, connectionStateChange: { _, _ in }).make()
|
2022-09-01 05:58:41 -07:00
|
|
|
|
2023-02-02 08:58:12 -08:00
|
|
|
let realRustBackend = ZcashRustBackend.self
|
|
|
|
|
|
|
|
let storage = FSCompactBlockRepository(
|
|
|
|
cacheDirectory: testTempDirectory,
|
|
|
|
metadataStore: FSMetadataStore.live(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
|
|
|
rustBackend: realRustBackend
|
|
|
|
),
|
|
|
|
blockDescriptor: .live,
|
|
|
|
contentProvider: DirectoryListingProviders.defaultSorted
|
|
|
|
)
|
|
|
|
|
|
|
|
try storage.create()
|
|
|
|
|
2022-12-01 08:57:13 -08:00
|
|
|
let latestBlockHeight = try service.latestBlockHeight()
|
|
|
|
let startHeight = latestBlockHeight - 100_000
|
2022-09-01 05:58:41 -07:00
|
|
|
let processorConfig = CompactBlockProcessor.Configuration.standard(
|
|
|
|
for: ZcashNetworkBuilder.network(for: .testnet),
|
|
|
|
walletBirthday: ZcashNetworkBuilder.network(for: .testnet).constants.saplingActivationHeight
|
|
|
|
)
|
|
|
|
|
|
|
|
let compactBlockProcessor = CompactBlockProcessor(
|
2021-09-23 06:26:41 -07:00
|
|
|
service: service,
|
|
|
|
storage: storage,
|
2023-02-02 08:58:12 -08:00
|
|
|
backend: realRustBackend,
|
2022-09-01 05:58:41 -07:00
|
|
|
config: processorConfig
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
let cancelableTask = Task {
|
|
|
|
do {
|
2023-01-31 02:11:00 -08:00
|
|
|
let downloadStream = try await compactBlockProcessor.blockDownloader.compactBlocksDownloadStream(
|
2022-12-01 08:57:13 -08:00
|
|
|
startHeight: startHeight,
|
|
|
|
targetHeight: latestBlockHeight
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
2022-12-01 08:57:13 -08:00
|
|
|
|
2023-01-31 02:11:00 -08:00
|
|
|
try await compactBlockProcessor.blockDownloader.downloadAndStoreBlocks(
|
2022-12-22 05:05:29 -08:00
|
|
|
using: downloadStream,
|
|
|
|
at: startHeight...latestBlockHeight,
|
|
|
|
maxBlockBufferSize: 10,
|
|
|
|
totalProgressRange: startHeight...latestBlockHeight
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
|
|
|
} catch {
|
2022-10-27 03:51:38 -07:00
|
|
|
XCTAssertTrue(Task.isCancelled)
|
2022-09-01 05:58:41 -07:00
|
|
|
}
|
2021-05-28 15:45:18 -07:00
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
try await Task.sleep(nanoseconds: 3_000_000_000)
|
|
|
|
cancelableTask.cancel()
|
2021-05-28 15:45:18 -07:00
|
|
|
}
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
func testStreamTimeout() async throws {
|
2023-02-03 02:23:35 -08:00
|
|
|
let endpoint = LightWalletEndpoint(
|
|
|
|
address: LightWalletEndpointBuilder.eccTestnet.host,
|
2021-09-23 06:26:41 -07:00
|
|
|
port: 9067,
|
|
|
|
secure: true,
|
2023-02-03 02:23:35 -08:00
|
|
|
singleCallTimeoutInMillis: 1000,
|
|
|
|
streamingCallTimeoutInMillis: 1000
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
2023-02-03 02:23:35 -08:00
|
|
|
let service = LightWalletServiceFactory(endpoint: endpoint, connectionStateChange: { _, _ in }).make()
|
2022-09-01 05:58:41 -07:00
|
|
|
|
2023-02-02 08:58:12 -08:00
|
|
|
let realRustBackend = ZcashRustBackend.self
|
|
|
|
|
|
|
|
let storage = FSCompactBlockRepository(
|
|
|
|
cacheDirectory: testTempDirectory,
|
|
|
|
metadataStore: FSMetadataStore.live(
|
|
|
|
fsBlockDbRoot: testTempDirectory,
|
|
|
|
rustBackend: realRustBackend
|
|
|
|
),
|
|
|
|
blockDescriptor: .live,
|
|
|
|
contentProvider: DirectoryListingProviders.defaultSorted
|
|
|
|
)
|
|
|
|
|
|
|
|
try storage.create()
|
|
|
|
|
2022-12-01 08:57:13 -08:00
|
|
|
let latestBlockHeight = try service.latestBlockHeight()
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2022-12-01 08:57:13 -08:00
|
|
|
let startHeight = latestBlockHeight - 100_000
|
2022-09-01 05:58:41 -07:00
|
|
|
|
|
|
|
let processorConfig = CompactBlockProcessor.Configuration.standard(
|
|
|
|
for: ZcashNetworkBuilder.network(for: .testnet),
|
|
|
|
walletBirthday: ZcashNetworkBuilder.network(for: .testnet).constants.saplingActivationHeight
|
|
|
|
)
|
|
|
|
|
|
|
|
let compactBlockProcessor = CompactBlockProcessor(
|
2021-09-23 06:26:41 -07:00
|
|
|
service: service,
|
|
|
|
storage: storage,
|
2023-02-02 08:58:12 -08:00
|
|
|
backend: realRustBackend,
|
2022-09-01 05:58:41 -07:00
|
|
|
config: processorConfig
|
2021-09-23 06:26:41 -07:00
|
|
|
)
|
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
let date = Date()
|
2021-05-28 15:45:18 -07:00
|
|
|
|
2022-09-01 05:58:41 -07:00
|
|
|
do {
|
2023-01-31 02:11:00 -08:00
|
|
|
let downloadStream = try await compactBlockProcessor.blockDownloader.compactBlocksDownloadStream(
|
2022-12-01 08:57:13 -08:00
|
|
|
startHeight: startHeight,
|
|
|
|
targetHeight: latestBlockHeight
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
2022-12-01 08:57:13 -08:00
|
|
|
|
2023-01-31 02:11:00 -08:00
|
|
|
try await compactBlockProcessor.blockDownloader.downloadAndStoreBlocks(
|
2022-12-22 05:05:29 -08:00
|
|
|
using: downloadStream,
|
|
|
|
at: startHeight...latestBlockHeight,
|
|
|
|
maxBlockBufferSize: 10,
|
|
|
|
totalProgressRange: startHeight...latestBlockHeight
|
2022-09-01 05:58:41 -07:00
|
|
|
)
|
|
|
|
} catch {
|
2021-05-28 15:45:18 -07:00
|
|
|
if let lwdError = error as? LightWalletServiceError {
|
|
|
|
switch lwdError {
|
|
|
|
case .timeOut:
|
|
|
|
XCTAssert(true)
|
|
|
|
default:
|
2022-09-01 05:58:41 -07:00
|
|
|
XCTFail("LWD Service error found, but should have been a timeLimit reached Error")
|
2021-05-28 15:45:18 -07:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
XCTFail("Error should have been a timeLimit reached Error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let now = Date()
|
|
|
|
|
2022-05-05 14:30:17 -07:00
|
|
|
let elapsed = now.timeIntervalSince(date)
|
2021-05-28 15:45:18 -07:00
|
|
|
print("took \(elapsed) seconds")
|
|
|
|
}
|
|
|
|
}
|