2019-10-18 11:45:19 -07:00
|
|
|
//
|
|
|
|
// Stubs.swift
|
|
|
|
// ZcashLightClientKitTests
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 18/09/2019.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
2020-04-09 15:25:43 -07:00
|
|
|
import GRPC
|
2019-12-16 14:25:45 -08:00
|
|
|
import SwiftProtobuf
|
2019-11-14 06:38:54 -08:00
|
|
|
@testable import ZcashLightClientKit
|
2019-10-18 11:45:19 -07:00
|
|
|
|
2023-04-24 14:15:20 -07:00
|
|
|
extension String: Error { }
|
|
|
|
|
2019-12-16 14:25:45 -08:00
|
|
|
class AwfulLightWalletService: MockLightWalletService {
|
2023-03-27 05:26:47 -07:00
|
|
|
override func latestBlockHeight() async throws -> BlockHeight {
|
2023-04-24 14:15:20 -07:00
|
|
|
throw ZcashError.serviceLatestBlockFailed(.criticalError)
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
2023-03-27 05:26:47 -07:00
|
|
|
|
2022-08-26 10:52:12 -07:00
|
|
|
override func blockRange(_ range: CompactBlockRange) -> AsyncThrowingStream<ZcashCompactBlock, Error> {
|
2023-04-24 14:15:20 -07:00
|
|
|
AsyncThrowingStream { continuation in continuation.finish(throwing: ZcashError.serviceSubmitFailed(.invalidBlock)) }
|
2022-08-26 10:52:12 -07:00
|
|
|
}
|
|
|
|
|
2022-10-03 16:05:11 -07:00
|
|
|
/// Submits a raw transaction over lightwalletd.
|
|
|
|
override func submit(spendTransaction: Data) async throws -> LightWalletServiceResponse {
|
2023-04-24 14:15:20 -07:00
|
|
|
throw ZcashError.serviceSubmitFailed(.invalidBlock)
|
2019-12-03 07:19:44 -08:00
|
|
|
}
|
2019-10-18 11:45:19 -07:00
|
|
|
}
|
2019-11-14 06:38:54 -08:00
|
|
|
|
2022-10-03 16:05:11 -07:00
|
|
|
extension LightWalletServiceMockResponse: Error { }
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2022-10-03 16:05:11 -07:00
|
|
|
class SlightlyBadLightWalletService: MockLightWalletService {
|
|
|
|
/// Submits a raw transaction over lightwalletd.
|
|
|
|
override func submit(spendTransaction: Data) async throws -> LightWalletServiceResponse {
|
|
|
|
throw LightWalletServiceMockResponse.error
|
2019-12-16 14:25:45 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension LightWalletServiceMockResponse {
|
|
|
|
static var error: LightWalletServiceMockResponse {
|
2021-09-23 06:26:41 -07:00
|
|
|
LightWalletServiceMockResponse(
|
|
|
|
errorCode: -100,
|
|
|
|
errorMessage: "Ohhh this is bad, really bad, you lost all your internet money",
|
|
|
|
unknownFields: UnknownStorage()
|
|
|
|
)
|
2019-12-16 14:25:45 -08:00
|
|
|
}
|
2021-09-23 06:26:41 -07:00
|
|
|
|
2019-12-16 14:25:45 -08:00
|
|
|
static var success: LightWalletServiceMockResponse {
|
|
|
|
LightWalletServiceMockResponse(errorCode: 0, errorMessage: "", unknownFields: UnknownStorage())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
class RustBackendMockHelper {
|
|
|
|
let rustBackendMock: ZcashRustBackendWeldingMock
|
|
|
|
|
|
|
|
init(
|
|
|
|
rustBackend: ZcashRustBackendWelding,
|
2023-09-13 00:24:26 -07:00
|
|
|
consensusBranchID: Int32? = nil
|
2023-03-31 10:10:35 -07:00
|
|
|
) async {
|
|
|
|
self.rustBackendMock = ZcashRustBackendWeldingMock(
|
|
|
|
consensusBranchIdForHeightClosure: { height in
|
|
|
|
if let consensusBranchID {
|
|
|
|
return consensusBranchID
|
|
|
|
} else {
|
|
|
|
return try rustBackend.consensusBranchIdFor(height: height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2023-09-13 00:24:26 -07:00
|
|
|
await setupDefaultMock(rustBackend: rustBackend)
|
2020-06-09 17:23:46 -07:00
|
|
|
}
|
2023-02-02 08:58:12 -08:00
|
|
|
|
2023-09-13 00:24:26 -07:00
|
|
|
private func setupDefaultMock(rustBackend: ZcashRustBackendWelding) async {
|
2023-03-31 10:10:35 -07:00
|
|
|
await rustBackendMock.setLatestCachedBlockHeightReturnValue(.empty())
|
|
|
|
await rustBackendMock.setInitBlockMetadataDbClosure() { }
|
|
|
|
await rustBackendMock.setWriteBlocksMetadataBlocksClosure() { _ in }
|
|
|
|
await rustBackendMock.setGetTransparentBalanceAccountReturnValue(0)
|
|
|
|
await rustBackendMock.setListTransparentReceiversAccountReturnValue([])
|
2023-04-24 14:15:20 -07:00
|
|
|
await rustBackendMock.setGetCurrentAddressAccountThrowableError(ZcashError.rustGetCurrentAddress("mocked error"))
|
|
|
|
await rustBackendMock.setGetNextAvailableAddressAccountThrowableError(ZcashError.rustGetNextAvailableAddress("mocked error"))
|
2023-09-06 04:28:25 -07:00
|
|
|
await rustBackendMock.setCreateAccountSeedTreeStateRecoverUntilThrowableError(ZcashError.rustInitAccountsTableViewingKeyCotainsNullBytes)
|
2023-08-07 17:47:22 -07:00
|
|
|
await rustBackendMock.setGetMemoTxIdOutputIndexReturnValue(nil)
|
2023-03-31 10:10:35 -07:00
|
|
|
await rustBackendMock.setInitDataDbSeedReturnValue(.seedRequired)
|
|
|
|
await rustBackendMock.setGetNearestRewindHeightHeightReturnValue(-1)
|
|
|
|
await rustBackendMock.setPutUnspentTransparentOutputTxidIndexScriptValueHeightClosure() { _, _, _, _, _ in }
|
2024-01-26 19:07:18 -08:00
|
|
|
await rustBackendMock.setProposeTransferAccountToValueMemoThrowableError(ZcashError.rustCreateToAddress("mocked error"))
|
|
|
|
await rustBackendMock.setProposeShieldingAccountMemoShieldingThresholdThrowableError(ZcashError.rustShieldFunds("mocked error"))
|
|
|
|
await rustBackendMock.setCreateProposedTransactionProposalUskThrowableError(ZcashError.rustCreateToAddress("mocked error"))
|
2023-04-24 14:15:20 -07:00
|
|
|
await rustBackendMock.setDecryptAndStoreTransactionTxBytesMinedHeightThrowableError(ZcashError.rustDecryptAndStoreTransaction("mock fail"))
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
await rustBackendMock.setInitDataDbSeedClosure() { seed in
|
|
|
|
return try await rustBackend.initDataDb(seed: seed)
|
2019-11-14 06:38:54 -08:00
|
|
|
}
|
2022-10-02 19:11:17 -07:00
|
|
|
|
2023-03-31 10:10:35 -07:00
|
|
|
await rustBackendMock.setRewindToHeightHeightClosure() { height in
|
|
|
|
try await rustBackend.rewindToHeight(height: height)
|
2019-11-14 06:38:54 -08:00
|
|
|
}
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
await rustBackendMock.setRewindCacheToHeightHeightClosure() { _ in }
|
|
|
|
|
2023-07-19 10:13:59 -07:00
|
|
|
await rustBackendMock.setSuggestScanRangesClosure() {
|
|
|
|
try await rustBackend.suggestScanRanges()
|
|
|
|
}
|
|
|
|
|
2023-08-04 00:56:57 -07:00
|
|
|
await rustBackendMock.setScanBlocksFromHeightLimitClosure() { fromHeight, limit in
|
2023-07-19 10:13:59 -07:00
|
|
|
try await rustBackend.scanBlocks(fromHeight: fromHeight, limit: limit)
|
2019-11-14 06:38:54 -08:00
|
|
|
}
|
|
|
|
}
|
2023-03-31 10:10:35 -07:00
|
|
|
|
|
|
|
private static func shouldSucceed(successRate: Float) -> Bool {
|
2019-11-14 06:38:54 -08:00
|
|
|
let random = Float.random(in: 0.0...1.0)
|
|
|
|
return random <= successRate
|
|
|
|
}
|
|
|
|
}
|
2023-02-16 08:27:49 -08:00
|
|
|
|
|
|
|
extension SaplingParamsSourceURL {
|
2023-04-24 14:15:20 -07:00
|
|
|
static let tests = SaplingParamsSourceURL(
|
2023-02-16 08:27:49 -08:00
|
|
|
spendParamFileURL: Bundle.module.url(forResource: "sapling-spend", withExtension: "params")!,
|
|
|
|
outputParamFileURL: Bundle.module.url(forResource: "sapling-output", withExtension: "params")!
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
extension CompactBlockProcessor.Configuration {
|
|
|
|
/// Standard configuration for most compact block processors
|
2023-03-22 05:47:32 -07:00
|
|
|
static func standard(
|
|
|
|
alias: ZcashSynchronizerAlias = .default,
|
|
|
|
for network: ZcashNetwork,
|
|
|
|
walletBirthday: BlockHeight
|
|
|
|
) -> CompactBlockProcessor.Configuration {
|
2023-02-16 08:27:49 -08:00
|
|
|
let pathProvider = DefaultResourceProvider(network: network)
|
|
|
|
return CompactBlockProcessor.Configuration(
|
2023-03-22 05:47:32 -07:00
|
|
|
alias: alias,
|
2023-02-16 08:27:49 -08:00
|
|
|
fsBlockCacheRoot: pathProvider.fsCacheURL,
|
|
|
|
dataDb: pathProvider.dataDbURL,
|
|
|
|
spendParamsURL: pathProvider.spendParamsURL,
|
|
|
|
outputParamsURL: pathProvider.outputParamsURL,
|
|
|
|
saplingParamsSourceURL: SaplingParamsSourceURL.tests,
|
2023-03-10 03:58:28 -08:00
|
|
|
walletBirthdayProvider: { walletBirthday },
|
2023-02-16 08:27:49 -08:00
|
|
|
network: network
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-04-14 00:08:37 -07:00
|
|
|
|
|
|
|
extension SynchronizerState {
|
|
|
|
static var mock: SynchronizerState {
|
|
|
|
SynchronizerState(
|
|
|
|
syncSessionID: .nullID,
|
2024-02-06 01:38:38 -08:00
|
|
|
accountBalance: AccountBalance(saplingBalance: .zero, unshielded: Zatoshi(200)),
|
2023-05-05 08:04:13 -07:00
|
|
|
internalSyncStatus: .syncing(0),
|
2023-09-07 03:54:47 -07:00
|
|
|
latestBlockHeight: 222222
|
2023-04-14 00:08:37 -07:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|