Skip to content

Commit

Permalink
update judgements
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Aug 5, 2024
1 parent 31277d2 commit 319e2b3
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 63 deletions.
10 changes: 5 additions & 5 deletions Blockchain/Sources/Blockchain/Types/Extrinsic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public struct Extrinsic: Sendable, Equatable {
// permissioning of block authoring
public var tickets: ExtrinsicTickets

// EJ: Votes, by validators, on dispute(s) arising between them presently taking place
public var judgements: ExtrinsicJudgement
// ED: Votes, by validators, on dispute(s) arising between them presently taking place
public var judgements: ExtrinsicDisputes

// EP: Static data which is presently being requested to be available for workloads to be able to fetch on demand
public var preimages: ExtrinsicPreimages
Expand All @@ -21,7 +21,7 @@ public struct Extrinsic: Sendable, Equatable {

public init(
tickets: ExtrinsicTickets,
judgements: ExtrinsicJudgement,
judgements: ExtrinsicDisputes,
preimages: ExtrinsicPreimages,
availability: ExtrinsicAvailability,
reports: ExtrinsicGuarantees
Expand All @@ -39,7 +39,7 @@ extension Extrinsic: Dummy {
public static func dummy(config: Config) -> Extrinsic {
Extrinsic(
tickets: ExtrinsicTickets.dummy(config: config),
judgements: ExtrinsicJudgement.dummy(config: config),
judgements: ExtrinsicDisputes.dummy(config: config),
preimages: ExtrinsicPreimages.dummy(config: config),
availability: ExtrinsicAvailability.dummy(config: config),
reports: ExtrinsicGuarantees.dummy(config: config)
Expand All @@ -51,7 +51,7 @@ extension Extrinsic: ScaleCodec.Encodable {
public init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
tickets: ExtrinsicTickets(config: config, from: &decoder),
judgements: ExtrinsicJudgement(config: config, from: &decoder),
judgements: ExtrinsicDisputes(config: config, from: &decoder),
preimages: decoder.decode(),
availability: ExtrinsicAvailability(config: config, from: &decoder),
reports: ExtrinsicGuarantees(config: config, from: &decoder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import ScaleCodec
import Utils

public struct ExtrinsicJudgement: Sendable, Equatable {
public struct JudgementItem: Sendable, Equatable {
public struct ExtrinsicDisputes: Sendable, Equatable {
public struct VerdictItem: Sendable, Equatable {
public struct SignatureItem: Sendable, Equatable {
public var isValid: Bool
public var validatorIndex: ValidatorIndex
public var signature: BandersnatchSignature
public var signature: Ed25519Signature

public init(
isValid: Bool,
validatorIndex: ValidatorIndex,
signature: BandersnatchSignature
signature: Ed25519Signature
) {
self.isValid = isValid
self.validatorIndex = validatorIndex
Expand All @@ -37,37 +37,35 @@ public struct ExtrinsicJudgement: Sendable, Equatable {
}
}

public typealias JudgementsList = [JudgementItem]

public var judgements: JudgementsList
public var verdicts: [VerdictItem]

public init(
judgements: JudgementsList
verdicts: [VerdictItem]
) {
self.judgements = judgements
self.verdicts = verdicts
}
}

extension ExtrinsicJudgement: Dummy {
extension ExtrinsicDisputes: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(config _: Config) -> ExtrinsicJudgement {
ExtrinsicJudgement(judgements: [])
public static func dummy(config _: Config) -> ExtrinsicDisputes {
ExtrinsicDisputes(verdicts: [])
}
}

extension ExtrinsicJudgement: ScaleCodec.Encodable {
extension ExtrinsicDisputes: ScaleCodec.Encodable {
public init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
judgements: decoder.decode(.array { try JudgementItem(config: config, from: &$0) })
verdicts: decoder.decode(.array { try VerdictItem(config: config, from: &$0) })
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(judgements)
try encoder.encode(verdicts)
}
}

extension ExtrinsicJudgement.JudgementItem: ScaleCodec.Encodable {
extension ExtrinsicDisputes.VerdictItem: ScaleCodec.Encodable {
public init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
reportHash: decoder.decode(),
Expand All @@ -81,7 +79,7 @@ extension ExtrinsicJudgement.JudgementItem: ScaleCodec.Encodable {
}
}

extension ExtrinsicJudgement.JudgementItem.SignatureItem: ScaleCodec.Codable {
extension ExtrinsicDisputes.VerdictItem.SignatureItem: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
isValid: decoder.decode(),
Expand Down
32 changes: 19 additions & 13 deletions Blockchain/Sources/Blockchain/Types/JudgementsState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import ScaleCodec
import Utils

public struct JudgementsState: Sendable, Equatable {
// ψa: The allow-set contains the hashes of all work-reports which were disputed and judged to be accurate.
public var allowSet: Set<Data32>
// ψg: Work-reports judged to be correct
public var goodSet: Set<Data32>

// ψb: The ban-set contains the hashes of all work-reports which were disputed and whose accuracy
// could not be confidently confirmed.
// ψb: Work-reports judged to be incorrect
public var banSet: Set<Data32>

// ψp; he punish-set is a set of keys of Bandersnatch keys which were found to have guaranteed
// a report which was confidently found to be invalid.
public var punishSet: Set<BandersnatchPublicKey>
// ψw: Work-reports whose validity is judged to be unknowable
public var wonkySet: Set<Data32>

// ψo: Validators who made a judgement found to be incorrect
public var punishSet: Set<Ed25519PublicKey>

public init(
allowSet: Set<Data32>,
goodSet: Set<Data32>,
banSet: Set<Data32>,
punishSet: Set<BandersnatchPublicKey>
wonkySet: Set<Data32>,
punishSet: Set<Ed25519PublicKey>
) {
self.allowSet = allowSet
self.goodSet = goodSet
self.banSet = banSet
self.wonkySet = wonkySet
self.punishSet = punishSet
}
}
Expand All @@ -28,8 +31,9 @@ extension JudgementsState: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(config _: Config) -> JudgementsState {
JudgementsState(
allowSet: [],
goodSet: [],
banSet: [],
wonkySet: [],
punishSet: []
)
}
Expand All @@ -38,15 +42,17 @@ extension JudgementsState: Dummy {
extension JudgementsState: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
allowSet: decoder.decode(),
goodSet: decoder.decode(),
banSet: decoder.decode(),
wonkySet: decoder.decode(),
punishSet: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(allowSet)
try encoder.encode(goodSet)
try encoder.encode(banSet)
try encoder.encode(wonkySet)
try encoder.encode(punishSet)
}
}
81 changes: 53 additions & 28 deletions Blockchain/Sources/Blockchain/Types/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct State: Sendable {
>

// β: Information on the most recent βlocks.
public var lastBlock: BlockRef
public var lastBlock: Block

// γ: State concerning Safrole.
public var safroleState: SafroleState
Expand Down Expand Up @@ -85,9 +85,6 @@ public struct State: Sendable {
// ψ: past judgements
public var judgements: JudgementsState

// π: The activity statistics for the validators.
public var activityStatistics: ValidatorActivityStatistics

public init(
config: ProtocolConfigRef,
coreAuthorizationPool: ConfigFixedSizeArray<
Expand All @@ -98,7 +95,7 @@ public struct State: Sendable {
>,
ProtocolConfig.TotalNumberOfCores
>,
lastBlock: BlockRef,
lastBlock: Block,
safroleState: SafroleState,
serviceAccounts: [ServiceIdentifier: ServiceAccount],
entropyPool: (Data32, Data32, Data32, Data32),
Expand Down Expand Up @@ -128,8 +125,7 @@ public struct State: Sendable {
assign: ServiceIdentifier,
designate: ServiceIdentifier
),
judgements: JudgementsState,
activityStatistics: ValidatorActivityStatistics
judgements: JudgementsState
) {
self.config = config
self.coreAuthorizationPool = coreAuthorizationPool
Expand All @@ -145,7 +141,6 @@ public struct State: Sendable {
self.authorizationQueue = authorizationQueue
self.privilegedServiceIndices = privilegedServiceIndices
self.judgements = judgements
self.activityStatistics = activityStatistics
}
}

Expand All @@ -165,8 +160,7 @@ extension State: Equatable {
lhs.timeslot == rhs.timeslot &&
lhs.authorizationQueue == rhs.authorizationQueue &&
lhs.privilegedServiceIndices == rhs.privilegedServiceIndices &&
lhs.judgements == rhs.judgements &&
lhs.activityStatistics == rhs.activityStatistics
lhs.judgements == rhs.judgements
}
}

Expand All @@ -176,7 +170,7 @@ extension State: Dummy {
State(
config: config,
coreAuthorizationPool: ConfigFixedSizeArray(config: config, defaultValue: ConfigLimitedSizeArray(config: config)),
lastBlock: BlockRef.dummy(config: config),
lastBlock: Block.dummy(config: config),
safroleState: SafroleState.dummy(config: config),
serviceAccounts: [:],
entropyPool: (Data32(), Data32(), Data32(), Data32()),
Expand All @@ -194,8 +188,7 @@ extension State: Dummy {
assign: ServiceIdentifier(),
designate: ServiceIdentifier()
),
judgements: JudgementsState.dummy(config: config),
activityStatistics: ValidatorActivityStatistics.dummy(config: config)
judgements: JudgementsState.dummy(config: config)
)
}
}
Expand Down Expand Up @@ -223,7 +216,7 @@ extension State: ScaleCodec.Encodable {
coreAuthorizationPool: ConfigFixedSizeArray(config: config, from: &decoder) {
try ConfigLimitedSizeArray(config: config, from: &$0) { try $0.decode() }
},
lastBlock: Block(config: config, from: &decoder).asRef(),
lastBlock: Block(config: config, from: &decoder),
safroleState: SafroleState(config: config, from: &decoder),
serviceAccounts: decoder.decode(),
entropyPool: decoder.decode(),
Expand All @@ -236,14 +229,13 @@ extension State: ScaleCodec.Encodable {
try ConfigFixedSizeArray(config: config, from: &$0)
},
privilegedServiceIndices: decoder.decode(),
judgements: decoder.decode(),
activityStatistics: ValidatorActivityStatistics(config: config, from: &decoder)
judgements: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(coreAuthorizationPool)
try encoder.encode(lastBlock.value)
try encoder.encode(lastBlock)
try encoder.encode(safroleState)
try encoder.encode(serviceAccounts)
try encoder.encode(entropyPool)
Expand All @@ -255,7 +247,28 @@ extension State: ScaleCodec.Encodable {
try encoder.encode(authorizationQueue)
try encoder.encode(privilegedServiceIndices)
try encoder.encode(judgements)
try encoder.encode(activityStatistics)
}
}

extension State {
public func update(with block: Block) -> State {
let state = State(
config: config,
coreAuthorizationPool: coreAuthorizationPool,
lastBlock: block,
safroleState: safroleState,
serviceAccounts: serviceAccounts,
entropyPool: entropyPool,
validatorQueue: validatorQueue,
currentValidators: currentValidators,
previousValidators: previousValidators,
reports: reports,
timeslot: timeslot,
authorizationQueue: authorizationQueue,
privilegedServiceIndices: privilegedServiceIndices,
judgements: judgements
)
return state
}
}

Expand Down Expand Up @@ -283,15 +296,27 @@ extension State: Safrole {

public var ticketsVerifier: BandersnatchRingVRFRoot { safroleState.ticketsVerifier }

public mutating func mergeWith(postState: SafrolePostState) {
safroleState.nextValidators = postState.nextValidators
safroleState.ticketsVerifier = postState.ticketsVerifier
safroleState.ticketsOrKeys = postState.ticketsOrKeys
safroleState.ticketsAccumulator = postState.ticketsAccumulator
entropyPool = postState.entropyPool
validatorQueue = postState.validatorQueue
currentValidators = postState.currentValidators
previousValidators = postState.previousValidators
timeslot = postState.timeslot
public func mergeWith(postState: SafrolePostState) -> Self {
Self(
config: config,
coreAuthorizationPool: coreAuthorizationPool,
lastBlock: lastBlock,
safroleState: SafroleState(
nextValidators: postState.nextValidators,
ticketsVerifier: postState.ticketsVerifier,
ticketsOrKeys: postState.ticketsOrKeys,
ticketsAccumulator: postState.ticketsAccumulator
),
serviceAccounts: serviceAccounts,
entropyPool: postState.entropyPool,
validatorQueue: postState.validatorQueue,
currentValidators: postState.currentValidators,
previousValidators: postState.previousValidators,
reports: reports,
timeslot: postState.timeslot,
authorizationQueue: authorizationQueue,
privilegedServiceIndices: privilegedServiceIndices,
judgements: judgements
)
}
}

0 comments on commit 319e2b3

Please sign in to comment.