Implement `BundleCheckpointSource` and add Tests

This commit is contained in:
Francisco Gindre 2023-10-30 18:00:13 -03:00
parent ea9d6fce95
commit 098eceec29
No known key found for this signature in database
GPG Key ID: 6B61CD8DAA2862B4
4 changed files with 42 additions and 53 deletions

View File

@ -0,0 +1,38 @@
//
// BundleCheckpointSource.swift
//
//
// Created by Francisco Gindre on 10/30/23.
//
import Foundation
struct BundleCheckpointSource: CheckpointSource {
var network: NetworkType
var saplingActivation: Checkpoint
init(network: NetworkType) {
self.network = network
self.saplingActivation = switch network {
case .mainnet:
Checkpoint.mainnetMin
case .testnet:
Checkpoint.testnetMin
}
}
func latestKnownCheckpoint() -> Checkpoint {
Checkpoint.birthday(
with: .max,
checkpointDirectory: BundleCheckpointURLProvider.default.url(self.network)
) ?? saplingActivation
}
func birthday(for height: BlockHeight) -> Checkpoint {
Checkpoint.birthday(
with: height,
checkpointDirectory: BundleCheckpointURLProvider.default.url(self.network)
) ?? saplingActivation
}
}

View File

@ -10,14 +10,14 @@ import Foundation
/// A protocol that abstracts the requirements around obtaining wallet checkpoints
/// (also known as TreeStates).
protocol CheckpointSource {
/// network type of this Checkpoint source
/// `NetworkType` of this Checkpoint source
var network: NetworkType { get }
/// The `Checkpoint` that represents the block in which Sapling was activated
var saplingActivation: Checkpoint { get }
/// Obtain the latest `Checkpoint` in terms of block height known by
/// this `CheckpointSource`. It is possible that the returned checkpoints
/// this `CheckpointSource`. It is possible that the returned checkpoint
/// is not the latest checkpoint that exists in the blockchain.
/// - Returns a `Checkpoint` with the highest height known by this source
func latestKnownCheckpoint() -> Checkpoint

View File

@ -9,55 +9,6 @@ import Foundation
struct CheckpointSourceFactory {
static func fromBundle(for network: NetworkType) -> CheckpointSource {
Placeholder()
BundleCheckpointSource(network: network)
}
}
// TODO: Remove this
struct Placeholder: CheckpointSource {
var network: NetworkType { .mainnet }
var saplingActivation: Checkpoint {
Checkpoint(
height: 1,
hash: "deadbeef",
time: 1,
saplingTree: """
000000
""",
orchardTree: """
000000
"""
)
}
func latestKnownCheckpoint() -> ZcashLightClientKit.Checkpoint {
Checkpoint(
height: 1,
hash: "deadbeef",
time: 1,
saplingTree: """
000000
""",
orchardTree: """
000000
"""
)
}
func birthday(for height: BlockHeight) -> Checkpoint {
Checkpoint(
height: 1,
hash: "deadbeef",
time: 1,
saplingTree: """
000000
""",
orchardTree: """
000000
"""
)
}
}

View File

@ -93,7 +93,7 @@ class CheckpointSourceTests: XCTestCase {
let source = CheckpointSourceFactory.fromBundle(for: .mainnet)
let birthday = source.birthday(for: 4)
let birthday = source.birthday(for: activationHeight)
XCTAssertEqual(birthday.height, activationHeight)
XCTAssertEqual(birthday.orchardTree, "000000")