Implement `BundleCheckpointSource` and add Tests
This commit is contained in:
parent
ea9d6fce95
commit
098eceec29
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
"""
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Reference in New Issue