From a7ef46eba9dadd5e632e320715125ad88fc13f68 Mon Sep 17 00:00:00 2001 From: Kutsal Kaan Bilgin Date: Fri, 24 Jun 2022 09:05:49 +0300 Subject: [PATCH] Improve diff apply function for network status and implement it for validator summary and details too. --- .../SubVTData/Model/App/NetworkStatus.swift | 132 +++++------------- .../Model/App/ValidatorDetails.swift | 92 ++++++++---- .../Model/App/ValidatorSummary.swift | 65 ++++++--- 3 files changed, 141 insertions(+), 148 deletions(-) diff --git a/Sources/SubVTData/Model/App/NetworkStatus.swift b/Sources/SubVTData/Model/App/NetworkStatus.swift index f33b3c3..be0c06e 100644 --- a/Sources/SubVTData/Model/App/NetworkStatus.swift +++ b/Sources/SubVTData/Model/App/NetworkStatus.swift @@ -2,22 +2,22 @@ Returned by the network status service right after the initial subscription. */ public struct NetworkStatus: Hashable { - public let finalizedBlockNumber: UInt64 - public let finalizedBlockHash: String - public let bestBlockNumber: UInt64 - public let bestBlockHash: String - public let activeEra: Era - public let currentEpoch: Epoch - public let activeValidatorCount: Int - public let inactiveValidatorCount: Int - public let lastEraTotalReward: Balance - public let totalStake: Balance - public let returnRatePerMillion: Int - public let minStake: Balance - public let maxStake: Balance - public let averageStake: Balance - public let medianStake: Balance - public let eraRewardPoints: UInt64 + public private(set) var finalizedBlockNumber: UInt64 + public private(set) var finalizedBlockHash: String + public private(set) var bestBlockNumber: UInt64 + public private(set) var bestBlockHash: String + public private(set) var activeEra: Era + public private(set) var currentEpoch: Epoch + public private(set) var activeValidatorCount: Int + public private(set) var inactiveValidatorCount: Int + public private(set) var lastEraTotalReward: Balance + public private(set) var totalStake: Balance + public private(set) var returnRatePerMillion: Int + public private(set) var minStake: Balance + public private(set) var maxStake: Balance + public private(set) var averageStake: Balance + public private(set) var medianStake: Balance + public private(set) var eraRewardPoints: UInt64 public init( finalizedBlockNumber: UInt64, @@ -59,89 +59,23 @@ public struct NetworkStatus: Hashable { extension NetworkStatus: Codable {} extension NetworkStatus { - public func apply(diff: NetworkStatusDiff) -> NetworkStatus { - var finalizedBlockNumber = self.finalizedBlockNumber - if let newFinalizedBlockNumber = diff.finalizedBlockNumber { - finalizedBlockNumber = newFinalizedBlockNumber - } - var finalizedBlockHash = self.finalizedBlockHash - if let newFinalizedBlockHash = diff.finalizedBlockHash { - finalizedBlockHash = newFinalizedBlockHash - } - var bestBlockNumber = self.bestBlockNumber - if let newBestBlockNumber = diff.bestBlockNumber { - bestBlockNumber = newBestBlockNumber - } - var bestBlockHash = self.bestBlockHash - if let newBestBlockHash = diff.bestBlockHash { - bestBlockHash = newBestBlockHash - } - var activeEra = self.activeEra - if let newActiveEra = diff.activeEra { - activeEra = newActiveEra - } - var currentEpoch = self.currentEpoch - if let newCurrentEpoch = diff.currentEpoch { - currentEpoch = newCurrentEpoch - } - var activeValidatorCount = self.activeValidatorCount - if let newActiveValidatorCount = diff.activeValidatorCount { - activeValidatorCount = newActiveValidatorCount - } - var inactiveValidatorCount = self.inactiveValidatorCount - if let newInactiveValidatorCount = diff.inactiveValidatorCount { - inactiveValidatorCount = newInactiveValidatorCount - } - var lastEraTotalReward = self.lastEraTotalReward - if let newLastEraTotalReward = diff.lastEraTotalReward { - lastEraTotalReward = newLastEraTotalReward - } - var totalStake = self.totalStake - if let newTotalStake = diff.totalStake { - totalStake = newTotalStake - } - var returnRatePerMillion = self.returnRatePerMillion - if let newReturnRatePerMillion = diff.returnRatePerMillion { - returnRatePerMillion = newReturnRatePerMillion - } - var minStake = self.minStake - if let newMinStake = diff.minStake { - minStake = newMinStake - } - var maxStake = self.maxStake - if let newMaxStake = diff.maxStake { - maxStake = newMaxStake - } - var averageStake = self.averageStake - if let newAverageStake = diff.averageStake { - averageStake = newAverageStake - } - var medianStake = self.medianStake - if let newMedianStake = diff.medianStake { - medianStake = newMedianStake - } - var eraRewardPoints = self.eraRewardPoints - if let newEraRewardPoints = diff.eraRewardPoints { - eraRewardPoints = newEraRewardPoints - } - return NetworkStatus( - finalizedBlockNumber: finalizedBlockNumber, - finalizedBlockHash: finalizedBlockHash, - bestBlockNumber: bestBlockNumber, - bestBlockHash: bestBlockHash, - activeEra: activeEra, - currentEpoch: currentEpoch, - activeValidatorCount: activeValidatorCount, - inactiveValidatorCount: inactiveValidatorCount, - lastEraTotalReward: lastEraTotalReward, - totalStake: totalStake, - returnRatePerMillion: returnRatePerMillion, - minStake: minStake, - maxStake: maxStake, - averageStake: averageStake, - medianStake: medianStake, - eraRewardPoints: eraRewardPoints - ) + public mutating func apply(diff: NetworkStatusDiff) { + self.finalizedBlockNumber = diff.finalizedBlockNumber ?? self.finalizedBlockNumber + self.finalizedBlockHash = diff.finalizedBlockHash ?? self.finalizedBlockHash + self.bestBlockNumber = diff.bestBlockNumber ?? self.bestBlockNumber + self.bestBlockHash = diff.bestBlockHash ?? self.bestBlockHash + self.activeEra = diff.activeEra ?? self.activeEra + self.currentEpoch = diff.currentEpoch ?? self.currentEpoch + self.activeValidatorCount = diff.activeValidatorCount ?? self.activeValidatorCount + self.inactiveValidatorCount = diff.inactiveValidatorCount ?? self.inactiveValidatorCount + self.lastEraTotalReward = diff.lastEraTotalReward ?? self.lastEraTotalReward + self.totalStake = diff.totalStake ?? self.totalStake + self.returnRatePerMillion = diff.returnRatePerMillion ?? self.returnRatePerMillion + self.minStake = diff.minStake ?? self.minStake + self.maxStake = diff.maxStake ?? self.maxStake + self.averageStake = diff.averageStake ?? self.averageStake + self.medianStake = diff.medianStake ?? self.medianStake + self.eraRewardPoints = diff.eraRewardPoints ?? self.eraRewardPoints } } diff --git a/Sources/SubVTData/Model/App/ValidatorDetails.swift b/Sources/SubVTData/Model/App/ValidatorDetails.swift index 8304532..02fb918 100644 --- a/Sources/SubVTData/Model/App/ValidatorDetails.swift +++ b/Sources/SubVTData/Model/App/ValidatorDetails.swift @@ -2,35 +2,35 @@ Returned by the validatod details service right after the initial subscription. */ public struct ValidatorDetails: Hashable { - public let account: Account - public let controllerAccountId: AccountId - public let preferences: ValidatorPreferences - public let selfStake: Stake - public let rewardDestination: RewardDestination - public let nextSessionKeys: String - public let isActive: Bool - public let activeNextSession: Bool - public let nominations: [Nomination] - public let oversubscribed: Bool - public let activeEraCount: UInt32 - public let inactiveEraCount: UInt32 - public let slashCount: UInt32 - public let offlineOffenceCount: UInt32 - public let unclaimedEraIndices: [UInt32] - public let isParaValidator: Bool - public let paraCoreAssignment: ParaCoreAssignment? - public let returnRatePerBillion: UInt64? - public let blocksAuthored: UInt32? - public let rewardPoints: UInt64? - public let heartbeatReceived: Bool? - public let validatorStake: ValidatorStake? - public let onekvCandidateRecordId: UInt64? - public let onekvBinaryVersion: String? - public let onekvRank: UInt32? - public let onekvLocation: String? - public let onekvIsValid: Bool? - public let onekvOnlineSince: UInt64? - public let onekvOfflineSince: UInt64? + public private(set) var account: Account + public private(set) var controllerAccountId: AccountId + public private(set) var preferences: ValidatorPreferences + public private(set) var selfStake: Stake + public private(set) var rewardDestination: RewardDestination + public private(set) var nextSessionKeys: String + public private(set) var isActive: Bool + public private(set) var activeNextSession: Bool + public private(set) var nominations: [Nomination] + public private(set) var oversubscribed: Bool + public private(set) var activeEraCount: UInt32 + public private(set) var inactiveEraCount: UInt32 + public private(set) var slashCount: UInt32 + public private(set) var offlineOffenceCount: UInt32 + public private(set) var unclaimedEraIndices: [UInt32] + public private(set) var isParaValidator: Bool + public private(set) var paraCoreAssignment: ParaCoreAssignment? + public private(set) var returnRatePerBillion: UInt64? + public private(set) var blocksAuthored: UInt32? + public private(set) var rewardPoints: UInt64? + public private(set) var heartbeatReceived: Bool? + public private(set) var validatorStake: ValidatorStake? + public private(set) var onekvCandidateRecordId: UInt64? + public private(set) var onekvBinaryVersion: String? + public private(set) var onekvRank: UInt32? + public private(set) var onekvLocation: String? + public private(set) var onekvIsValid: Bool? + public private(set) var onekvOnlineSince: UInt64? + public private(set) var onekvOfflineSince: UInt64? public init( account: Account, @@ -97,6 +97,40 @@ public struct ValidatorDetails: Hashable { extension ValidatorDetails: Codable {} +extension ValidatorDetails { + public mutating func apply(diff: ValidatorDetailsDiff) { + self.account = diff.account + self.controllerAccountId = diff.controllerAccountId ?? self.controllerAccountId + self.preferences = diff.preferences ?? self.preferences + self.selfStake = diff.selfStake ?? self.selfStake + self.rewardDestination = diff.rewardDestination ?? self.rewardDestination + self.nextSessionKeys = diff.nextSessionKeys ?? self.nextSessionKeys + self.isActive = diff.isActive ?? self.isActive + self.activeNextSession = diff.activeNextSession ?? self.activeNextSession + self.nominations = diff.nominations ?? self.nominations + self.oversubscribed = diff.oversubscribed ?? self.oversubscribed + self.activeEraCount = diff.activeEraCount ?? self.activeEraCount + self.inactiveEraCount = diff.inactiveEraCount ?? self.inactiveEraCount + self.slashCount = diff.slashCount ?? self.slashCount + self.offlineOffenceCount = diff.offlineOffenceCount ?? self.offlineOffenceCount + self.unclaimedEraIndices = diff.unclaimedEraIndices ?? self.unclaimedEraIndices + self.isParaValidator = diff.isParaValidator ?? self.isParaValidator + self.paraCoreAssignment = diff.paraCoreAssignment ?? self.paraCoreAssignment + self.returnRatePerBillion = diff.returnRatePerBillion ?? self.returnRatePerBillion + self.blocksAuthored = diff.blocksAuthored ?? self.blocksAuthored + self.rewardPoints = diff.rewardPoints ?? self.rewardPoints + self.heartbeatReceived = diff.heartbeatReceived ?? self.heartbeatReceived + self.validatorStake = diff.validatorStake ?? self.validatorStake + self.onekvCandidateRecordId = diff.onekvCandidateRecordId ?? self.onekvCandidateRecordId + self.onekvBinaryVersion = diff.onekvBinaryVersion ?? self.onekvBinaryVersion + self.onekvRank = diff.onekvRank ?? self.onekvRank + self.onekvLocation = diff.onekvLocation ?? self.onekvLocation + self.onekvIsValid = diff.onekvIsValid ?? self.onekvIsValid + self.onekvOnlineSince = diff.onekvOnlineSince ?? self.onekvOnlineSince + self.onekvOfflineSince = diff.onekvOfflineSince ?? self.onekvOfflineSince + } +} + /** Subsequent data from the validator details service, reflecting the changes to the previous state. diff --git a/Sources/SubVTData/Model/App/ValidatorSummary.swift b/Sources/SubVTData/Model/App/ValidatorSummary.swift index d4737be..646dbfb 100644 --- a/Sources/SubVTData/Model/App/ValidatorSummary.swift +++ b/Sources/SubVTData/Model/App/ValidatorSummary.swift @@ -3,26 +3,26 @@ */ public struct ValidatorSummary: Codable, Hashable { public let accountId: AccountId - public let controllerAccountId: AccountId? - public let display: String? - public let parentDisplay: String? - public let childDisplay: String? - public let confirmed: Bool - public let preferences: ValidatorPreferences - public let selfStake: StakeSummary - public let isActive: Bool - public let activeNextSession: Bool - public let inactiveNominations: InactiveNominationsSummary - public let oversubscribed: Bool - public let slashCount: Int - public let isEnrolledIn1Kv: Bool - public let isParaValidator: Bool - public let paraId: Int? - public let returnRatePerBillion: UInt64? - public let blocksAuthored: Int? - public let rewardPoints: UInt64? - public let heartbeatReceived: Bool? - public let validatorStake: ValidatorStakeSummary? + public private(set) var controllerAccountId: AccountId? + public private(set) var display: String? + public private(set) var parentDisplay: String? + public private(set) var childDisplay: String? + public private(set) var confirmed: Bool + public private(set) var preferences: ValidatorPreferences + public private(set) var selfStake: StakeSummary + public private(set) var isActive: Bool + public private(set) var activeNextSession: Bool + public private(set) var inactiveNominations: InactiveNominationsSummary + public private(set) var oversubscribed: Bool + public private(set) var slashCount: Int + public private(set) var isEnrolledIn1Kv: Bool + public private(set) var isParaValidator: Bool + public private(set) var paraId: Int? + public private(set) var returnRatePerBillion: UInt64? + public private(set) var blocksAuthored: Int? + public private(set) var rewardPoints: UInt64? + public private(set) var heartbeatReceived: Bool? + public private(set) var validatorStake: ValidatorStakeSummary? public init( accountId: AccountId, @@ -71,6 +71,31 @@ public struct ValidatorSummary: Codable, Hashable { } } +extension ValidatorSummary { + public mutating func apply(diff: ValidatorSummaryDiff) { + self.controllerAccountId = diff.controllerAccountId ?? self.controllerAccountId + self.display = diff.display ?? self.display + self.parentDisplay = diff.parentDisplay ?? self.parentDisplay + self.childDisplay = diff.childDisplay ?? self.childDisplay + self.confirmed = diff.confirmed ?? self.confirmed + self.preferences = diff.preferences ?? self.preferences + self.selfStake = diff.selfStake ?? self.selfStake + self.isActive = diff.isActive ?? self.isActive + self.activeNextSession = diff.activeNextSession ?? self.activeNextSession + self.inactiveNominations = diff.inactiveNominations ?? self.inactiveNominations + self.oversubscribed = diff.oversubscribed ?? self.oversubscribed + self.slashCount = diff.slashCount ?? self.slashCount + self.isEnrolledIn1Kv = diff.isEnrolledIn1Kv ?? self.isEnrolledIn1Kv + self.isParaValidator = diff.isParaValidator ?? self.isParaValidator + self.paraId = diff.paraId ?? self.paraId + self.returnRatePerBillion = diff.returnRatePerBillion ?? self.returnRatePerBillion + self.blocksAuthored = diff.blocksAuthored ?? self.blocksAuthored + self.rewardPoints = diff.rewardPoints ?? self.rewardPoints + self.heartbeatReceived = diff.heartbeatReceived ?? self.heartbeatReceived + self.validatorStake = diff.validatorStake ?? self.validatorStake + } +} + public struct ValidatorSummaryDiff: Codable, Hashable { public let accountId: AccountId public let controllerAccountId: AccountId?