From 5a69280c4a9923866d0320da37c7147fde835f29 Mon Sep 17 00:00:00 2001 From: Qiwei Yang Date: Mon, 19 Aug 2024 09:46:24 +0800 Subject: [PATCH] update disputes and verdicts (#61) * update and run foramt * fix --- .../Blockchain/Types/ExtrinsicDisputes.swift | 98 +++++++++++++++++-- .../Sources/Blockchain/Types/Header.swift | 6 +- .../Sources/Blockchain/Types/primitives.swift | 1 + 3 files changed, 94 insertions(+), 11 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Types/ExtrinsicDisputes.swift b/Blockchain/Sources/Blockchain/Types/ExtrinsicDisputes.swift index 68351026..7bd85d16 100644 --- a/Blockchain/Sources/Blockchain/Types/ExtrinsicDisputes.swift +++ b/Blockchain/Sources/Blockchain/Types/ExtrinsicDisputes.swift @@ -20,43 +20,89 @@ public struct ExtrinsicDisputes: Sendable, Equatable { } public var reportHash: Data32 - public var signatures: ConfigFixedSizeArray< + public var epoch: EpochIndex + public var judgements: ConfigFixedSizeArray< SignatureItem, ProtocolConfig.TwoThirdValidatorsPlusOne > public init( reportHash: Data32, - signatures: ConfigFixedSizeArray< + epoch: EpochIndex, + judgements: ConfigFixedSizeArray< SignatureItem, ProtocolConfig.TwoThirdValidatorsPlusOne > ) { self.reportHash = reportHash - self.signatures = signatures + self.epoch = epoch + self.judgements = judgements + } + } + + public struct CulpritItem: Sendable, Equatable { + public var reportHash: Data32 + public var validatorKey: Ed25519PublicKey + public var signature: Ed25519Signature + + public init( + reportHash: Data32, + validatorKey: Ed25519PublicKey, + signature: Ed25519Signature + ) { + self.reportHash = reportHash + self.validatorKey = validatorKey + self.signature = signature + } + } + + public struct FaultItem: Sendable, Equatable { + public var reportHash: Data32 + public var vote: Bool + public var validatorKey: Ed25519PublicKey + public var signature: Ed25519Signature + + public init( + reportHash: Data32, + vote: Bool, + validatorKey: Ed25519PublicKey, + signature: Ed25519Signature + ) { + self.reportHash = reportHash + self.vote = vote + self.validatorKey = validatorKey + self.signature = signature } } public var verdicts: [VerdictItem] + public var culprits: [CulpritItem] + public var faults: [FaultItem] public init( - verdicts: [VerdictItem] + verdicts: [VerdictItem], + culprits: [CulpritItem], + faults: [FaultItem] ) { self.verdicts = verdicts + self.culprits = culprits + self.faults = faults } } extension ExtrinsicDisputes: Dummy { public typealias Config = ProtocolConfigRef public static func dummy(config _: Config) -> ExtrinsicDisputes { - ExtrinsicDisputes(verdicts: []) + ExtrinsicDisputes(verdicts: [], culprits: [], faults: []) } } extension ExtrinsicDisputes: ScaleCodec.Encodable { public init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws { try self.init( - verdicts: decoder.decode(.array { try VerdictItem(config: config, from: &$0) }) + verdicts: decoder.decode(.array { try VerdictItem(config: config, from: &$0) }), + culprits: decoder.decode(.array { try CulpritItem(from: &$0) }), + faults: decoder.decode(.array { try FaultItem(from: &$0) }) ) } @@ -69,13 +115,15 @@ extension ExtrinsicDisputes.VerdictItem: ScaleCodec.Encodable { public init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws { try self.init( reportHash: decoder.decode(), - signatures: ConfigFixedSizeArray(config: config, from: &decoder) + epoch: decoder.decode(), + judgements: ConfigFixedSizeArray(config: config, from: &decoder) ) } public func encode(in encoder: inout some ScaleCodec.Encoder) throws { try encoder.encode(reportHash) - try encoder.encode(signatures) + try encoder.encode(epoch) + try encoder.encode(judgements) } } @@ -94,3 +142,37 @@ extension ExtrinsicDisputes.VerdictItem.SignatureItem: ScaleCodec.Codable { try encoder.encode(signature) } } + +extension ExtrinsicDisputes.CulpritItem: ScaleCodec.Codable { + public init(from decoder: inout some ScaleCodec.Decoder) throws { + try self.init( + reportHash: decoder.decode(), + validatorKey: decoder.decode(), + signature: decoder.decode() + ) + } + + public func encode(in encoder: inout some ScaleCodec.Encoder) throws { + try encoder.encode(reportHash) + try encoder.encode(validatorKey) + try encoder.encode(signature) + } +} + +extension ExtrinsicDisputes.FaultItem: ScaleCodec.Codable { + public init(from decoder: inout some ScaleCodec.Decoder) throws { + try self.init( + reportHash: decoder.decode(), + vote: decoder.decode(), + validatorKey: decoder.decode(), + signature: decoder.decode() + ) + } + + public func encode(in encoder: inout some ScaleCodec.Encoder) throws { + try encoder.encode(reportHash) + try encoder.encode(vote) + try encoder.encode(validatorKey) + try encoder.encode(signature) + } +} diff --git a/Blockchain/Sources/Blockchain/Types/Header.swift b/Blockchain/Sources/Blockchain/Types/Header.swift index e518b312..39557940 100644 --- a/Blockchain/Sources/Blockchain/Types/Header.swift +++ b/Blockchain/Sources/Blockchain/Types/Header.swift @@ -29,11 +29,11 @@ public struct Header: Sendable, Equatable { ProtocolConfig.EpochLength >? - // Hj: The judgement marker must contain exactly the sequence of report hashes judged not as - // confidently valid (i.e. either controversial or invalid). + // Hj: The verdicts markers must contain exactly the sequence of report hashes of all new + // bad & wonky verdicts. public var judgementsMarkers: [Data32] - // Ho: offenders markers + // Ho: The offenders markers must contain exactly the sequence of keys of all new offenders. public var offendersMarkers: [Ed25519PublicKey] // Hi: block author index diff --git a/Blockchain/Sources/Blockchain/Types/primitives.swift b/Blockchain/Sources/Blockchain/Types/primitives.swift index 85446f9c..4f81431c 100644 --- a/Blockchain/Sources/Blockchain/Types/primitives.swift +++ b/Blockchain/Sources/Blockchain/Types/primitives.swift @@ -8,6 +8,7 @@ public typealias DataLength = UInt32 public typealias ValidatorIndex = UInt32 // TODO: confirm this public typealias CoreIndex = UInt32 // TODO: confirm this public typealias TicketIndex = UInt8 +public typealias EpochIndex = UInt32 public typealias Ed25519PublicKey = Data32 public typealias Ed25519Signature = Data64