Skip to content

Commit

Permalink
rename (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc authored Jul 1, 2024
1 parent e603d92 commit c4f276b
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 71 deletions.
20 changes: 10 additions & 10 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicTickets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Utils

public struct ExtrinsicTickets: Sendable {
public struct TicketItem: Sendable {
public var ticketIndex: TicketIndex
public var proof: BandersnatchRintVRFProof
public var attempt: TicketIndex
public var signature: BandersnatchRintVRFProof

public init(
ticketIndex: TicketIndex,
proof: BandersnatchRintVRFProof
attempt: TicketIndex,
signature: BandersnatchRintVRFProof
) {
self.ticketIndex = ticketIndex
self.proof = proof
self.attempt = attempt
self.signature = signature
}
}

Expand All @@ -34,14 +34,14 @@ extension ExtrinsicTickets: Dummy {
extension ExtrinsicTickets.TicketItem: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
ticketIndex: decoder.decode(),
proof: decoder.decode()
attempt: decoder.decode(),
signature: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(ticketIndex)
try encoder.encode(proof)
try encoder.encode(attempt)
try encoder.encode(signature)
}
}

Expand Down
20 changes: 10 additions & 10 deletions Blockchain/Sources/Blockchain/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import Utils

public struct Header: Sendable {
public struct EpochMarker: Sendable {
public var randomness: Data32
public var keys: ConfigFixedSizeArray<
public var entropy: Data32
public var validators: ConfigFixedSizeArray<
BandersnatchPublicKey,
ProtocolConfig.TotalNumberOfValidators
>

public init(
randomness: Data32,
keys: ConfigFixedSizeArray<
entropy: Data32,
validators: ConfigFixedSizeArray<
BandersnatchPublicKey,
ProtocolConfig.TotalNumberOfValidators
>
) {
self.randomness = randomness
self.keys = keys
self.entropy = entropy
self.validators = validators
}
}

Expand Down Expand Up @@ -140,14 +140,14 @@ extension Header: ScaleCodec.Encodable {
extension Header.EpochMarker: ScaleCodec.Encodable {
public init(withConfig config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
randomness: decoder.decode(),
keys: ConfigFixedSizeArray(withConfig: config, from: &decoder)
entropy: decoder.decode(),
validators: ConfigFixedSizeArray(withConfig: config, from: &decoder)
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(randomness)
try encoder.encode(keys)
try encoder.encode(entropy)
try encoder.encode(validators)
}
}

Expand Down
48 changes: 24 additions & 24 deletions Blockchain/Sources/Blockchain/Types/SafroleState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import Utils

public struct SafroleState: Sendable {
// γk
public var pendingValidators: ConfigFixedSizeArray<
public var nextValidators: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
>

// γz
public var epochRoot: BandersnatchRingVRFRoot
public var ticketsVerifierKey: BandersnatchRingVRFRoot

// γs
public var slotSealerSeries: Either<
public var ticketsOrKeys: Either<
ConfigFixedSizeArray<
Ticket,
ProtocolConfig.EpochLength
Expand All @@ -23,18 +23,18 @@ public struct SafroleState: Sendable {
>

// γa
public var ticketAccumulator: ConfigLimitedSizeArray<
public var ticketsAccumulator: ConfigLimitedSizeArray<
Ticket,
ProtocolConfig.Int0,
ProtocolConfig.EpochLength
>

public init(
pendingValidators: ConfigFixedSizeArray<
nextValidators: ConfigFixedSizeArray<
ValidatorKey, ProtocolConfig.TotalNumberOfValidators
>,
epochRoot: BandersnatchRingVRFRoot,
slotSealerSeries: Either<
ticketsVerifierKey: BandersnatchRingVRFRoot,
ticketsOrKeys: Either<
ConfigFixedSizeArray<
Ticket,
ProtocolConfig.EpochLength
Expand All @@ -44,49 +44,49 @@ public struct SafroleState: Sendable {
ProtocolConfig.EpochLength
>
>,
ticketAccumulator: ConfigLimitedSizeArray<
ticketsAccumulator: ConfigLimitedSizeArray<
Ticket,
ProtocolConfig.Int0,
ProtocolConfig.EpochLength
>
) {
self.pendingValidators = pendingValidators
self.epochRoot = epochRoot
self.slotSealerSeries = slotSealerSeries
self.ticketAccumulator = ticketAccumulator
self.nextValidators = nextValidators
self.ticketsVerifierKey = ticketsVerifierKey
self.ticketsOrKeys = ticketsOrKeys
self.ticketsAccumulator = ticketsAccumulator
}
}

extension SafroleState: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(withConfig config: Config) -> SafroleState {
SafroleState(
pendingValidators: ConfigFixedSizeArray(withConfig: config, defaultValue: ValidatorKey.dummy(withConfig: config)),
epochRoot: BandersnatchRingVRFRoot(),
slotSealerSeries: .right(ConfigFixedSizeArray(withConfig: config, defaultValue: BandersnatchPublicKey())),
ticketAccumulator: ConfigLimitedSizeArray(withConfig: config)
nextValidators: ConfigFixedSizeArray(withConfig: config, defaultValue: ValidatorKey.dummy(withConfig: config)),
ticketsVerifierKey: BandersnatchRingVRFRoot(),
ticketsOrKeys: .right(ConfigFixedSizeArray(withConfig: config, defaultValue: BandersnatchPublicKey())),
ticketsAccumulator: ConfigLimitedSizeArray(withConfig: config)
)
}
}

extension SafroleState: ScaleCodec.Encodable {
public init(withConfig config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
pendingValidators: ConfigFixedSizeArray(withConfig: config, from: &decoder),
epochRoot: decoder.decode(),
slotSealerSeries: Either(
nextValidators: ConfigFixedSizeArray(withConfig: config, from: &decoder),
ticketsVerifierKey: decoder.decode(),
ticketsOrKeys: Either(
from: &decoder,
decodeLeft: { try ConfigFixedSizeArray(withConfig: config, from: &$0) },
decodeRight: { try ConfigFixedSizeArray(withConfig: config, from: &$0) }
),
ticketAccumulator: ConfigLimitedSizeArray(withConfig: config, from: &decoder)
ticketsAccumulator: ConfigLimitedSizeArray(withConfig: config, from: &decoder)
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(pendingValidators)
try encoder.encode(epochRoot)
try encoder.encode(slotSealerSeries)
try encoder.encode(ticketAccumulator)
try encoder.encode(nextValidators)
try encoder.encode(ticketsVerifierKey)
try encoder.encode(ticketsOrKeys)
try encoder.encode(ticketsAccumulator)
}
}
14 changes: 7 additions & 7 deletions Blockchain/Sources/Blockchain/Types/Ticket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@ import ScaleCodec
import Utils

public struct Ticket: Sendable {
public var identifier: Data32
public var entryIndex: TicketIndex
public var id: Data32
public var attempt: TicketIndex
}

extension Ticket: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(withConfig _: Config) -> Ticket {
Ticket(identifier: Data32(), entryIndex: 0)
Ticket(id: Data32(), attempt: 0)
}
}

extension Ticket: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
identifier: decoder.decode(),
entryIndex: decoder.decode()
id: decoder.decode(),
attempt: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(identifier)
try encoder.encode(entryIndex)
try encoder.encode(id)
try encoder.encode(attempt)
}
}
36 changes: 18 additions & 18 deletions Blockchain/Sources/Blockchain/Types/ValidatorKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import ScaleCodec
import Utils

public struct ValidatorKey: Sendable {
public var bandersnatchKey: BandersnatchPublicKey
public var ed25519Key: Ed25519PublicKey
public var blsKey: BLSKey
public var bandersnatch: BandersnatchPublicKey
public var ed25519: Ed25519PublicKey
public var bls: BLSKey
public var metadata: Data128

public init(
bandersnatchKey: BandersnatchPublicKey,
ed25519Key: Ed25519PublicKey,
blsKey: BLSKey,
bandersnatch: BandersnatchPublicKey,
ed25519: Ed25519PublicKey,
bls: BLSKey,
metadata: Data128
) {
self.bandersnatchKey = bandersnatchKey
self.ed25519Key = ed25519Key
self.blsKey = blsKey
self.bandersnatch = bandersnatch
self.ed25519 = ed25519
self.bls = bls
self.metadata = metadata
}
}
Expand All @@ -24,9 +24,9 @@ extension ValidatorKey: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(withConfig _: Config) -> ValidatorKey {
ValidatorKey(
bandersnatchKey: BandersnatchPublicKey(),
ed25519Key: Ed25519PublicKey(),
blsKey: BLSKey(),
bandersnatch: BandersnatchPublicKey(),
ed25519: Ed25519PublicKey(),
bls: BLSKey(),
metadata: Data128()
)
}
Expand All @@ -35,17 +35,17 @@ extension ValidatorKey: Dummy {
extension ValidatorKey: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
bandersnatchKey: decoder.decode(),
ed25519Key: decoder.decode(),
blsKey: decoder.decode(),
bandersnatch: decoder.decode(),
ed25519: decoder.decode(),
bls: decoder.decode(),
metadata: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(bandersnatchKey)
try encoder.encode(ed25519Key)
try encoder.encode(blsKey)
try encoder.encode(bandersnatch)
try encoder.encode(ed25519)
try encoder.encode(bls)
try encoder.encode(metadata)
}
}
4 changes: 2 additions & 2 deletions Blockchain/Sources/Blockchain/Types/primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public typealias Gas = UInt64
public typealias DataLength = UInt32
public typealias ValidatorIndex = UInt32 // TODO: confirm this
public typealias CoreIndex = UInt32 // TODO: confirm this
public typealias TicketIndex = UInt8 // TODO: confirm this
public typealias TicketIndex = UInt8

public typealias Ed25519PublicKey = Data32
public typealias Ed25519Signature = Data64
public typealias BandersnatchPublicKey = Data32
public typealias BandersnatchSignature = Data64
public typealias BandersnatchRintVRFProof = Data784
public typealias BandersnatchRingVRFRoot = Data32
public typealias BandersnatchRingVRFRoot = Data384
public typealias BLSKey = Data144
6 changes: 6 additions & 0 deletions Utils/Sources/Utils/ConstValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public enum ConstUInt144: ConstInt {
}
}

public enum ConstUInt384: ConstInt {
public static var value: Int {
384
}
}

public enum ConstUInt784: ConstInt {
public static var value: Int {
784
Expand Down
1 change: 1 addition & 0 deletions Utils/Sources/Utils/FixedSizeData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ public typealias Data64 = FixedSizeData<ConstInt64>
public typealias Data96 = FixedSizeData<ConstUInt96>
public typealias Data128 = FixedSizeData<ConstUInt128>
public typealias Data144 = FixedSizeData<ConstUInt144>
public typealias Data384 = FixedSizeData<ConstUInt384>
public typealias Data784 = FixedSizeData<ConstUInt784>

0 comments on commit c4f276b

Please sign in to comment.