[#1050] Implement ValidateServerAction

Closes #1050
This commit is contained in:
Michal Fousek 2023-05-10 14:17:12 +02:00
parent 4b366b68c0
commit 7ded055ef7
2 changed files with 36 additions and 25 deletions

View File

@ -8,35 +8,46 @@
import Foundation
class ValidateServerAction {
init(container: DIContainer) { }
let config: CompactBlockProcessorNG.Configuration
let rustBackend: ZcashRustBackendWelding
let service: LightWalletService
init(container: DIContainer, config: CompactBlockProcessorNG.Configuration) {
self.config = config
rustBackend = container.resolve(ZcashRustBackendWelding.self)
service = container.resolve(LightWalletService.self)
}
}
extension ValidateServerAction: Action {
func run(with context: ActionContext, didUpdate: @escaping (ActionProgress) async -> Void) async throws -> ActionContext {
let info = try await service.getInfo()
let localNetwork = config.network
let saplingActivation = config.saplingActivation
// // check network types
// guard let remoteNetworkType = NetworkType.forChainName(info.chainName) else {
// throw ZcashError.compactBlockProcessorChainName(info.chainName)
// }
//
// guard remoteNetworkType == localNetwork.networkType else {
// throw ZcashError.compactBlockProcessorNetworkMismatch(localNetwork.networkType, remoteNetworkType)
// }
//
// guard saplingActivation == info.saplingActivationHeight else {
// throw ZcashError.compactBlockProcessorSaplingActivationMismatch(saplingActivation, BlockHeight(info.saplingActivationHeight))
// }
//
// // check branch id
// let localBranch = try rustBackend.consensusBranchIdFor(height: Int32(info.blockHeight))
//
// guard let remoteBranchID = ConsensusBranchID.fromString(info.consensusBranchID) else {
// throw ZcashError.compactBlockProcessorConsensusBranchID
// }
//
// guard remoteBranchID == localBranch else {
// throw ZcashError.compactBlockProcessorWrongConsensusBranchId(localBranch, remoteBranchID)
// }
// check network types
guard let remoteNetworkType = NetworkType.forChainName(info.chainName) else {
throw ZcashError.compactBlockProcessorChainName(info.chainName)
}
guard remoteNetworkType == localNetwork.networkType else {
throw ZcashError.compactBlockProcessorNetworkMismatch(localNetwork.networkType, remoteNetworkType)
}
guard saplingActivation == info.saplingActivationHeight else {
throw ZcashError.compactBlockProcessorSaplingActivationMismatch(saplingActivation, BlockHeight(info.saplingActivationHeight))
}
// check branch id
let localBranch = try rustBackend.consensusBranchIdFor(height: Int32(info.blockHeight))
guard let remoteBranchID = ConsensusBranchID.fromString(info.consensusBranchID) else {
throw ZcashError.compactBlockProcessorConsensusBranchID
}
guard remoteBranchID == localBranch else {
throw ZcashError.compactBlockProcessorWrongConsensusBranchId(localBranch, remoteBranchID)
}
await context.update(state: .computeSyncRanges)
return context

View File

@ -126,7 +126,7 @@ class CompactBlockProcessorNG {
let action: Action
switch state {
case .validateServer:
action = ValidateServerAction(container: container)
action = ValidateServerAction(container: container, config: config)
case .computeSyncRanges:
action = ComputeSyncRangesAction(container: container)
case .checksBeforeSync: