-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1316 from pacu/custom-checkpoint-provisioning
Create a Dependency that can load checkpoints from bundle
- Loading branch information
Showing
12 changed files
with
338 additions
and
60 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
Sources/ZcashLightClientKit/Checkpoint/BundleCheckpointSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// BundleCheckpointSource.swift | ||
// | ||
// | ||
// Created by Francisco Gindre on 2023-10-30. | ||
// | ||
|
||
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 | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
Sources/ZcashLightClientKit/Checkpoint/BundleCheckpointURLProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// BundleCheckpointURLProvider.swift | ||
// | ||
// | ||
// Created by Francisco Gindre on 2023-10-30. | ||
// | ||
|
||
import Foundation | ||
|
||
struct BundleCheckpointURLProvider { | ||
var url: (NetworkType) -> URL | ||
} | ||
|
||
extension BundleCheckpointURLProvider { | ||
/// Attempts to resolve the platform. `#if os(macOS)` implies that the build is for a macOS | ||
/// target, otherwise we assume the build is for an iOS target. | ||
static let `default` = BundleCheckpointURLProvider { networkType in | ||
#if os(macOS) | ||
Self.macOS.url(networkType) | ||
#else | ||
Self.iOS.url(networkType) | ||
#endif | ||
} | ||
|
||
static let iOS = BundleCheckpointURLProvider(url: { networkType in | ||
switch networkType { | ||
case .mainnet: | ||
return Checkpoint.mainnetCheckpointDirectory | ||
case .testnet: | ||
return Checkpoint.testnetCheckpointDirectory | ||
} | ||
}) | ||
|
||
/// This variant attempts to retrieve the saplingActivation checkpoint for the given network | ||
/// type using `Bundle.module.url(forResource:withExtension:subdirectory:localization)`. | ||
/// If not found it will return `WalletBirthday.mainnetCheckpointDirectory` or | ||
/// `WalletBirthday.testnetCheckpointDirectory`. This responds to tests failing on a macOS | ||
/// target because the checkpoint resources would not be found. | ||
static let macOS = BundleCheckpointURLProvider(url: { networkType in | ||
switch networkType { | ||
case .mainnet: | ||
return Bundle.module.url( | ||
forResource: "419200", | ||
withExtension: "json", | ||
subdirectory: "checkpoints/mainnet/", | ||
localization: nil | ||
)? | ||
.deletingLastPathComponent() ?? Checkpoint.mainnetCheckpointDirectory | ||
case .testnet: | ||
return Bundle.module.url( | ||
forResource: "280000", | ||
withExtension: "json", | ||
subdirectory: "checkpoints/testnet/", | ||
localization: nil | ||
)? | ||
.deletingLastPathComponent() ?? Checkpoint.testnetCheckpointDirectory | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
34 changes: 34 additions & 0 deletions
34
Sources/ZcashLightClientKit/Checkpoint/CheckpointSource.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// CheckpointSource.swift | ||
// | ||
// | ||
// Created by Francisco Gindre on 2023-10-30. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A protocol that abstracts the requirements around obtaining wallet checkpoints | ||
/// (also known as TreeStates). | ||
protocol CheckpointSource { | ||
/// `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 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 | ||
|
||
/// Obtain a `Checkpoint` in terms of a "wallet birthday". Wallet birthday | ||
/// is estimated to be the latest height of the Zcash blockchain at the moment when the wallet was | ||
/// created. | ||
/// - Parameter height: Estimated or effective height known for the wallet birthday | ||
/// - Returns: a `Checkpoint` that will allow the wallet to manage funds from the given `height` | ||
/// onwards. | ||
/// - Note: When the user knows the exact height of the first received funds for a wallet, | ||
/// the effective birthday of that wallet is `transaction.height - 1`. | ||
func birthday(for height: BlockHeight) -> Checkpoint | ||
} |
14 changes: 14 additions & 0 deletions
14
Sources/ZcashLightClientKit/Checkpoint/CheckpointSourceFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// CheckpointSourceFactory.swift | ||
// | ||
// | ||
// Created by Francisco Gindre on 2023-10-30. | ||
// | ||
|
||
import Foundation | ||
|
||
struct CheckpointSourceFactory { | ||
static func fromBundle(for network: NetworkType) -> CheckpointSource { | ||
BundleCheckpointSource(network: network) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.