2019-10-18 11:45:19 -07:00
|
|
|
//
|
2020-01-14 14:25:14 -08:00
|
|
|
// FakeStorage.swift
|
2019-10-18 11:45:19 -07:00
|
|
|
// ZcashLightClientKit
|
|
|
|
//
|
|
|
|
// Created by Francisco Gindre on 12/09/2019.
|
|
|
|
// Copyright © 2019 Electric Coin Company. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
@testable import ZcashLightClientKit
|
|
|
|
|
2019-11-18 11:49:36 -08:00
|
|
|
class ZcashConsoleFakeStorage: CompactBlockRepository {
|
2022-08-26 10:52:12 -07:00
|
|
|
func latestHeightAsync() async throws -> BlockHeight {
|
|
|
|
latestBlockHeight
|
|
|
|
}
|
|
|
|
|
2022-10-03 16:05:11 -07:00
|
|
|
func write(blocks: [ZcashCompactBlock]) async throws {
|
2022-08-26 10:52:12 -07:00
|
|
|
fakeSave(blocks: blocks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func rewindAsync(to height: BlockHeight) async throws {
|
|
|
|
fakeRewind(to: height)
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
func latestHeight() throws -> Int {
|
|
|
|
return self.latestBlockHeight
|
|
|
|
}
|
2022-11-04 02:03:52 -07:00
|
|
|
|
|
|
|
func latestBlock() throws -> ZcashLightClientKit.ZcashCompactBlock {
|
|
|
|
return ZcashCompactBlock(height: latestBlockHeight, data: Data())
|
|
|
|
}
|
|
|
|
|
2019-10-18 11:45:19 -07:00
|
|
|
func rewind(to height: BlockHeight) throws {
|
|
|
|
fakeRewind(to: height)
|
|
|
|
}
|
|
|
|
|
|
|
|
var latestBlockHeight: BlockHeight = 0
|
|
|
|
var delay = DispatchTimeInterval.milliseconds(300)
|
|
|
|
|
|
|
|
init(latestBlockHeight: BlockHeight = 0) {
|
|
|
|
self.latestBlockHeight = latestBlockHeight
|
|
|
|
}
|
|
|
|
|
2021-09-23 06:26:41 -07:00
|
|
|
private func fakeSave(blocks: [ZcashCompactBlock]) {
|
2019-10-18 11:45:19 -07:00
|
|
|
blocks.forEach {
|
2020-03-09 13:25:27 -07:00
|
|
|
LoggerProxy.debug("saving block \($0)")
|
2019-10-18 11:45:19 -07:00
|
|
|
self.latestBlockHeight = $0.height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func fakeRewind(to height: BlockHeight) {
|
2020-03-09 13:25:27 -07:00
|
|
|
LoggerProxy.debug("rewind to \(height)")
|
2019-10-18 11:45:19 -07:00
|
|
|
self.latestBlockHeight = min(self.latestBlockHeight, height)
|
|
|
|
}
|
|
|
|
}
|