Skip to content

Commit

Permalink
Merge branch 'master' of github.com:AcalaNetwork/boka
Browse files Browse the repository at this point in the history
* 'master' of github.com:AcalaNetwork/boka:
  Finish Block Serialization section (#71)
  • Loading branch information
MacOMNI committed Aug 22, 2024
2 parents b0804b5 + e6ccd04 commit 98897c7
Show file tree
Hide file tree
Showing 19 changed files with 442 additions and 401 deletions.
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Runtime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public final class Runtime {
newState.lastBlock = block

let res = newState.updateSafrole(
slot: block.header.timeslotIndex, entropy: newState.entropyPool.t0, extrinsics: block.extrinsic.tickets
config: config, slot: block.header.timeslotIndex, entropy: newState.entropyPool.t0, extrinsics: block.extrinsic.tickets
)
switch res {
case let .success((state: postState, epochMark: _, ticketsMark: _)):
Expand Down
5 changes: 2 additions & 3 deletions Blockchain/Sources/Blockchain/Safrole.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public struct SafrolePostState: Sendable, Equatable {
}

public protocol Safrole {
var config: ProtocolConfigRef { get }
var timeslot: TimeslotIndex { get }
var entropyPool: EntropyPool { get }
var previousValidators: ConfigFixedSizeArray<
Expand Down Expand Up @@ -152,7 +151,7 @@ public protocol Safrole {
> { get }
var ticketsVerifier: BandersnatchRingVRFRoot { get }

func updateSafrole(slot: TimeslotIndex, entropy: Data32, extrinsics: ExtrinsicTickets)
func updateSafrole(config: ProtocolConfigRef, slot: TimeslotIndex, entropy: Data32, extrinsics: ExtrinsicTickets)
-> Result<
(
state: SafrolePostState,
Expand Down Expand Up @@ -211,7 +210,7 @@ func pickFallbackValidators(
}

extension Safrole {
public func updateSafrole(slot: TimeslotIndex, entropy: Data32, extrinsics: ExtrinsicTickets)
public func updateSafrole(config: ProtocolConfigRef, slot: TimeslotIndex, entropy: Data32, extrinsics: ExtrinsicTickets)
-> Result<
(
state: SafrolePostState,
Expand Down
3 changes: 3 additions & 0 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicDisputes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ public struct ExtrinsicDisputes: Sendable, Equatable, Codable {
}
}

// v
public var verdicts: [VerdictItem]
// c
public var culprits: [CulpritItem]
// f
public var faults: [FaultItem]

public init(
Expand Down
7 changes: 2 additions & 5 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicGuarantees.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import Utils

public struct ExtrinsicGuarantees: Sendable, Equatable, Codable {
public struct IndexAndSignature: Sendable, Equatable, Codable {
public var index: UInt32
public var index: ValidatorIndex
public var signature: Ed25519Signature

public init(
index: UInt32,
index: ValidatorIndex,
signature: Ed25519Signature
) {
self.index = index
Expand All @@ -15,7 +15,6 @@ public struct ExtrinsicGuarantees: Sendable, Equatable, Codable {
}

public struct GuaranteeItem: Sendable, Equatable, Codable {
public var coreIndex: CoreIndex
public var workReport: WorkReport
public var timeslot: TimeslotIndex
public var credential: LimitedSizeArray<
Expand All @@ -25,7 +24,6 @@ public struct ExtrinsicGuarantees: Sendable, Equatable, Codable {
>

public init(
coreIndex: CoreIndex,
workReport: WorkReport,
timeslot: TimeslotIndex,
credential: LimitedSizeArray<
Expand All @@ -34,7 +32,6 @@ public struct ExtrinsicGuarantees: Sendable, Equatable, Codable {
ConstInt3
>
) {
self.coreIndex = coreIndex
self.workReport = workReport
self.timeslot = timeslot
self.credential = credential
Expand Down
6 changes: 3 additions & 3 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicPreimages.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Utils

public struct ExtrinsicPreimages: Sendable, Equatable, Codable {
public struct PreimageItem: Sendable, Equatable, Codable {
public var serviceIndices: ServiceIndices
public var serviceIndex: ServiceIndex
public var data: Data

public init(serviceIndices: ServiceIndices, data: Data) {
self.serviceIndices = serviceIndices
public init(serviceIndex: ServiceIndex, data: Data) {
self.serviceIndex = serviceIndex
self.data = data
}
}
Expand Down
21 changes: 21 additions & 0 deletions Blockchain/Sources/Blockchain/Types/HashAndLength.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import Utils

public struct HashAndLength: Sendable, Codable {
public var hash: Data32
public var length: DataLength

public init(hash: Data32, length: DataLength) {
self.hash = hash
self.length = length
}
}

extension HashAndLength: Hashable {
public func hash(into hasher: inout Hasher) {
// we assume hash is alraedy a high quality hash
// and we know the output is 32 bytes
// so we can just take the first 4 bytes and should be good enough
// NOTE: we will never use the Hashable protocol for any critical operations
hasher.combine(hash.data[0 ..< 4])
}
}
164 changes: 96 additions & 68 deletions Blockchain/Sources/Blockchain/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,83 @@ import Codec
import Utils

public struct Header: Sendable, Equatable, Codable {
// Hp: parent hash
public var parentHash: Data32

// Hr: prior state root
public var priorStateRoot: Data32 // state root of the after parent block execution

// Hx: extrinsic hash
public var extrinsicsRoot: Data32

// Ht: timeslot index
public var timeslotIndex: TimeslotIndex

// He: the epoch
// the header’s epoch marker He is either empty or, if the block is the first in a new epoch,
// then a tuple of the epoch randomness and a sequence of Bandersnatch keys
// defining the Bandersnatch validator keys (kb) beginning in the next epoch
public var epoch: EpochMarker?

// Hw: winning-tickets
// The winning-tickets marker Hw is either empty or,
// if the block is the first after the end of the submission period
// for tickets and if the ticket accumulator is saturated, then the final sequence of ticket identifiers
public var winningTickets: ConfigFixedSizeArray<
Ticket,
ProtocolConfig.EpochLength
>?

// Hj: The verdicts markers must contain exactly the sequence of report hashes of all new
// bad & wonky verdicts.
public var judgementsMarkers: [Data32]

// Ho: The offenders markers must contain exactly the sequence of keys of all new offenders.
public var offendersMarkers: [Ed25519PublicKey]

// Hi: block author index
public var authorIndex: ValidatorIndex
public struct Unsigned: Sendable, Equatable, Codable {
// Hp: parent hash
public var parentHash: Data32

// Hr: prior state root
public var priorStateRoot: Data32 // state root of the after parent block execution

// Hx: extrinsic hash
public var extrinsicsRoot: Data32

// Ht: timeslot index
public var timeslotIndex: TimeslotIndex

// He: the epoch
// the header’s epoch marker He is either empty or, if the block is the first in a new epoch,
// then a tuple of the epoch randomness and a sequence of Bandersnatch keys
// defining the Bandersnatch validator keys (kb) beginning in the next epoch
public var epoch: EpochMarker?

// Hw: winning-tickets
// The winning-tickets marker Hw is either empty or,
// if the block is the first after the end of the submission period
// for tickets and if the ticket accumulator is saturated, then the final sequence of ticket identifiers
public var winningTickets:
ConfigFixedSizeArray<
Ticket,
ProtocolConfig.EpochLength
>?

// Hj: The verdicts markers must contain exactly the sequence of report hashes of all new
// bad & wonky verdicts.
public var judgementsMarkers: [Data32]

// Ho: The offenders markers must contain exactly the sequence of keys of all new offenders.
public var offendersMarkers: [Ed25519PublicKey]

// Hi: block author index
public var authorIndex: ValidatorIndex

// Hv: the entropy-yielding vrf signature
public var vrfSignature: BandersnatchSignature

public init(
parentHash: Data32,
priorStateRoot: Data32,
extrinsicsRoot: Data32,
timeslotIndex: TimeslotIndex,
epoch: EpochMarker?,
winningTickets: ConfigFixedSizeArray<
Ticket,
ProtocolConfig.EpochLength
>?,
judgementsMarkers: [Data32],
offendersMarkers: [Ed25519PublicKey],
authorIndex: ValidatorIndex,
vrfSignature: BandersnatchSignature
) {
self.parentHash = parentHash
self.priorStateRoot = priorStateRoot
self.extrinsicsRoot = extrinsicsRoot
self.timeslotIndex = timeslotIndex
self.epoch = epoch
self.winningTickets = winningTickets
self.judgementsMarkers = judgementsMarkers
self.offendersMarkers = offendersMarkers
self.authorIndex = authorIndex
self.vrfSignature = vrfSignature
}
}

// Hv: the entropy-yielding vrf signature
public var vrfSignature: BandersnatchSignature
public var unsigned: Unsigned

// Hs: block seal
public var seal: BandersnatchSignature

public init(
parentHash: Data32,
priorStateRoot: Data32,
extrinsicsRoot: Data32,
timeslotIndex: TimeslotIndex,
epoch: EpochMarker?,
winningTickets: ConfigFixedSizeArray<
Ticket,
ProtocolConfig.EpochLength
>?,
judgementsMarkers: [Data32],
offendersMarkers: [Ed25519PublicKey],
authorIndex: UInt32,
vrfSignature: BandersnatchSignature,
seal: BandersnatchSignature
) {
self.parentHash = parentHash
self.priorStateRoot = priorStateRoot
self.extrinsicsRoot = extrinsicsRoot
self.timeslotIndex = timeslotIndex
self.epoch = epoch
self.winningTickets = winningTickets
self.judgementsMarkers = judgementsMarkers
self.offendersMarkers = offendersMarkers
self.authorIndex = authorIndex
self.vrfSignature = vrfSignature
public init(unsigned: Unsigned, seal: BandersnatchSignature) {
self.unsigned = unsigned
self.seal = seal
}
}
Expand All @@ -83,10 +91,10 @@ extension Header {

public typealias HeaderRef = Ref<Header>

extension Header: Dummy {
extension Header.Unsigned: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(config _: Config) -> Header {
Header(
public static func dummy(config _: Config) -> Header.Unsigned {
Header.Unsigned(
parentHash: Data32(),
priorStateRoot: Data32(),
extrinsicsRoot: Data32(),
Expand All @@ -96,7 +104,16 @@ extension Header: Dummy {
judgementsMarkers: [],
offendersMarkers: [],
authorIndex: 0,
vrfSignature: BandersnatchSignature(),
vrfSignature: BandersnatchSignature()
)
}
}

extension Header: Dummy {
public typealias Config = ProtocolConfigRef
public static func dummy(config: Config) -> Header {
Header(
unsigned: Header.Unsigned.dummy(config: config),
seal: BandersnatchSignature()
)
}
Expand All @@ -110,4 +127,15 @@ extension Header {
fatalError("Failed to hash header: \(e)")
}
}

public var parentHash: Data32 { unsigned.parentHash }
public var priorStateRoot: Data32 { unsigned.priorStateRoot }
public var extrinsicsRoot: Data32 { unsigned.extrinsicsRoot }
public var timeslotIndex: TimeslotIndex { unsigned.timeslotIndex }
public var epoch: EpochMarker? { unsigned.epoch }
public var winningTickets: ConfigFixedSizeArray<Ticket, ProtocolConfig.EpochLength>? { unsigned.winningTickets }
public var judgementsMarkers: [Data32] { unsigned.judgementsMarkers }
public var offendersMarkers: [Ed25519PublicKey] { unsigned.offendersMarkers }
public var authorIndex: ValidatorIndex { unsigned.authorIndex }
public var vrfSignature: BandersnatchSignature { unsigned.vrfSignature }
}
6 changes: 6 additions & 0 deletions Blockchain/Sources/Blockchain/Types/RefinementContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import Utils
// at the point that the report’s corresponding work-package was evaluated.
public struct RefinementContext: Sendable, Equatable, Codable {
public struct Anchor: Sendable, Equatable, Codable {
// a
public var headerHash: Data32
// s
public var stateRoot: Data32
// b
public var beefyRoot: Data32

public init(
Expand All @@ -20,7 +23,9 @@ public struct RefinementContext: Sendable, Equatable, Codable {
}

public struct LokupAnchor: Sendable, Equatable, Codable {
// l
public var headerHash: Data32
// t
public var timeslot: TimeslotIndex

public init(
Expand All @@ -36,6 +41,7 @@ public struct RefinementContext: Sendable, Equatable, Codable {

public var lokupAnchor: LokupAnchor

// p
public var prerequistieWorkPackage: Data32?

public init(anchor: Anchor, lokupAnchor: LokupAnchor, prerequistieWorkPackage: Data32?) {
Expand Down
10 changes: 0 additions & 10 deletions Blockchain/Sources/Blockchain/Types/ServiceAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@ import Foundation
import Utils

public struct ServiceAccount: Sendable, Equatable, Codable {
public struct HashAndLength: Sendable, Hashable, Codable {
public var hash: Data32
public var length: DataLength

public init(hash: Data32, length: DataLength) {
self.hash = hash
self.length = length
}
}

// s
public var storage: [Data32: Data]

Expand Down
Loading

0 comments on commit 98897c7

Please sign in to comment.