Skip to content

Commit

Permalink
update disputes and verdicts (#61)
Browse files Browse the repository at this point in the history
* update and run foramt

* fix
  • Loading branch information
qiweiii authored Aug 19, 2024
1 parent 22e15e0 commit 5a69280
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 11 deletions.
98 changes: 90 additions & 8 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicDisputes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
)
}

Expand All @@ -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)
}
}

Expand All @@ -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)
}
}
6 changes: 3 additions & 3 deletions Blockchain/Sources/Blockchain/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Blockchain/Sources/Blockchain/Types/primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5a69280

Please sign in to comment.