Skip to content

Commit

Permalink
add some scale codec support (#21)
Browse files Browse the repository at this point in the history
* add some scale codec support

* implements codec
  • Loading branch information
xlc authored Jun 19, 2024
1 parent 8f2de7c commit bde54bd
Show file tree
Hide file tree
Showing 26 changed files with 773 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ScaleCodec
import Utils

public struct AvailabilitySpecifications {
Expand Down Expand Up @@ -36,3 +37,21 @@ extension AvailabilitySpecifications: Dummy {
)
}
}

extension AvailabilitySpecifications: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
workPackageHash: decoder.decode(),
length: decoder.decode(),
erasureRoot: decoder.decode(),
segmentRoot: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(workPackageHash)
try encoder.encode(length)
try encoder.encode(erasureRoot)
try encoder.encode(segmentRoot)
}
}
15 changes: 15 additions & 0 deletions Blockchain/Sources/Blockchain/Types/Block.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ScaleCodec
import Utils

public struct Block {
Expand All @@ -20,3 +21,17 @@ extension Block: Dummy {
)
}
}

extension Block: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
header: decoder.decode(),
extrinsic: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(header)
try encoder.encode(extrinsic)
}
}
21 changes: 21 additions & 0 deletions Blockchain/Sources/Blockchain/Types/Extrinsic.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ScaleCodec
import Utils

public struct Extrinsic {
Expand Down Expand Up @@ -44,3 +45,23 @@ extension Extrinsic: Dummy {
)
}
}

extension Extrinsic: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
tickets: decoder.decode(),
judgements: decoder.decode(),
preimages: decoder.decode(),
availability: decoder.decode(),
reports: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(tickets)
try encoder.encode(judgements)
try encoder.encode(preimages)
try encoder.encode(availability)
try encoder.encode(reports)
}
}
69 changes: 55 additions & 14 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicAvailability.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import Foundation
import ScaleCodec
import Utils

public struct ExtrinsicAvailability {
public typealias AssurancesList = LimitedSizeArray<
(
// a
parentHash: H256,

// f
assurance: Data, // bit string with length of Constants.TotalNumberOfCores
// TODO: use a BitString type

// v
validatorIndex: ValidatorIndex,

// s
signature: Ed25519Signature
),
AssuranceItem,
ConstInt0,
Constants.TotalNumberOfValidators
>
Expand All @@ -35,3 +23,56 @@ extension ExtrinsicAvailability: Dummy {
ExtrinsicAvailability(assurances: [])
}
}

extension ExtrinsicAvailability: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
assurances: decoder.decode()
)
}

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

public struct AssuranceItem {
// a
public var parentHash: H256
// f
public var assurance: Data // bit string with length of Constants.TotalNumberOfCores TODO: use a BitString type
// v
public var validatorIndex: ValidatorIndex
// s
public var signature: Ed25519Signature

public init(
parentHash: H256,
assurance: Data,
validatorIndex: ValidatorIndex,
signature: Ed25519Signature
) {
self.parentHash = parentHash
self.assurance = assurance
self.validatorIndex = validatorIndex
self.signature = signature
}
}

extension AssuranceItem: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
parentHash: decoder.decode(),
assurance: decoder.decode(),
validatorIndex: decoder.decode(),
signature: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(parentHash)
try encoder.encode(assurance)
try encoder.encode(validatorIndex)
try encoder.encode(signature)
}
}
69 changes: 59 additions & 10 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicGuarantees.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import ScaleCodec
import Utils

public struct ExtrinsicGuarantees {
public typealias GuaranteesList = LimitedSizeArray<
(
coreIndex: CoreIndex,
workReport: WorkReport,
timeslot: TimeslotIndex,
credential: LimitedSizeArray<
Ed25519Signature,
ConstInt2,
ConstInt3
>
),
GuaranteeItem,
ConstInt0,
Constants.TotalNumberOfCores
>
Expand All @@ -30,3 +22,60 @@ extension ExtrinsicGuarantees: Dummy {
ExtrinsicGuarantees(guarantees: [])
}
}

extension ExtrinsicGuarantees: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
guarantees: decoder.decode()
)
}

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

public struct GuaranteeItem {
public var coreIndex: CoreIndex
public var workReport: WorkReport
public var timeslot: TimeslotIndex
public var credential: LimitedSizeArray<
Ed25519Signature,
ConstInt2,
ConstInt3
>

public init(
coreIndex: CoreIndex,
workReport: WorkReport,
timeslot: TimeslotIndex,
credential: LimitedSizeArray<
Ed25519Signature,
ConstInt2,
ConstInt3
>
) {
self.coreIndex = coreIndex
self.workReport = workReport
self.timeslot = timeslot
self.credential = credential
}
}

extension GuaranteeItem: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
coreIndex: decoder.decode(),
workReport: decoder.decode(),
timeslot: decoder.decode(),
credential: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(coreIndex)
try encoder.encode(workReport)
try encoder.encode(timeslot)
try encoder.encode(credential)
}
}
92 changes: 79 additions & 13 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicJudgement.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import ScaleCodec
import Utils

public struct ExtrinsicJudgement {
public typealias JudgementsList = [
(
reportHash: H256,
signatures: FixedSizeArray<
(
isValid: Bool,
validatorIndex: ValidatorIndex,
signature: BandersnatchSignature
),
Constants.TwoThirdValidatorsPlusOne
>
)
]
public typealias JudgementsList = [JudgementItem]

public var judgements: JudgementsList

Expand All @@ -29,3 +18,80 @@ extension ExtrinsicJudgement: Dummy {
ExtrinsicJudgement(judgements: [])
}
}

extension ExtrinsicJudgement: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
judgements: decoder.decode()
)
}

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

public struct JudgementItem {
public var reportHash: H256
public var signatures: FixedSizeArray<
SignatureItem,
Constants.TwoThirdValidatorsPlusOne
>

public init(
reportHash: H256,
signatures: FixedSizeArray<
SignatureItem,
Constants.TwoThirdValidatorsPlusOne
>
) {
self.reportHash = reportHash
self.signatures = signatures
}

public struct SignatureItem {
public var isValid: Bool
public var validatorIndex: ValidatorIndex
public var signature: BandersnatchSignature

public init(
isValid: Bool,
validatorIndex: ValidatorIndex,
signature: BandersnatchSignature
) {
self.isValid = isValid
self.validatorIndex = validatorIndex
self.signature = signature
}
}
}

extension JudgementItem: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
reportHash: decoder.decode(),
signatures: decoder.decode()
)
}

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

extension JudgementItem.SignatureItem: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
isValid: decoder.decode(),
validatorIndex: decoder.decode(),
signature: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(isValid)
try encoder.encode(validatorIndex)
try encoder.encode(signature)
}
}
41 changes: 39 additions & 2 deletions Blockchain/Sources/Blockchain/Types/ExtrinsicPreimages.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import Foundation
import ScaleCodec
import Utils

public struct ExtrinsicPreimages {
public var preimages: [(size: DataLength, data: Data)]
public var preimages: [SizeAndData]

public init(
preimages: [(size: DataLength, data: Data)]
preimages: [SizeAndData]
) {
self.preimages = preimages
}
Expand All @@ -16,3 +17,39 @@ extension ExtrinsicPreimages: Dummy {
ExtrinsicPreimages(preimages: [])
}
}

extension ExtrinsicPreimages: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
preimages: decoder.decode()
)
}

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

public struct SizeAndData {
public var size: DataLength
public var data: Data

public init(size: DataLength, data: Data) {
self.size = size
self.data = data
}
}

extension SizeAndData: ScaleCodec.Codable {
public init(from decoder: inout some ScaleCodec.Decoder) throws {
try self.init(
size: decoder.decode(),
data: decoder.decode()
)
}

public func encode(in encoder: inout some ScaleCodec.Encoder) throws {
try encoder.encode(size)
try encoder.encode(data)
}
}
Loading

0 comments on commit bde54bd

Please sign in to comment.