Skip to content

Commit

Permalink
Merge branch 'master' into dev_mackun
Browse files Browse the repository at this point in the history
* master:
  JAM Codec tests and fixes (#97)
  • Loading branch information
MacOMNI committed Sep 4, 2024
2 parents b606458 + 9304473 commit d40ed96
Show file tree
Hide file tree
Showing 19 changed files with 484 additions and 95 deletions.
49 changes: 48 additions & 1 deletion Blockchain/Sources/Blockchain/Types/Header.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Utils

private let logger = Logger(label: "Header")

public struct Header: Sendable, Equatable, Codable {
public struct Header: Sendable, Equatable {
public struct Unsigned: Sendable, Equatable, Codable {
// Hp: parent hash
public var parentHash: Data32
Expand Down Expand Up @@ -80,6 +80,53 @@ public struct Header: Sendable, Equatable, Codable {
}
}

extension Header: Codable {
enum CodingKeys: String, CodingKey {
case parentHash
case priorStateRoot
case extrinsicsHash
case timeslot
case epoch
case winningTickets
case offendersMarkers
case authorIndex
case vrfSignature
case seal
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
try self.init(
unsigned: Unsigned(
parentHash: container.decode(Data32.self, forKey: .parentHash),
priorStateRoot: container.decode(Data32.self, forKey: .priorStateRoot),
extrinsicsHash: container.decode(Data32.self, forKey: .extrinsicsHash),
timeslot: container.decode(UInt32.self, forKey: .timeslot),
epoch: container.decode(EpochMarker?.self, forKey: .epoch),
winningTickets: container.decode(ConfigFixedSizeArray<Ticket, ProtocolConfig.EpochLength>?.self, forKey: .winningTickets),
offendersMarkers: container.decode([Ed25519PublicKey].self, forKey: .offendersMarkers),
authorIndex: container.decode(ValidatorIndex.self, forKey: .authorIndex),
vrfSignature: container.decode(BandersnatchSignature.self, forKey: .vrfSignature)
),
seal: container.decode(BandersnatchSignature.self, forKey: .seal)
)
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(unsigned.parentHash, forKey: .parentHash)
try container.encode(unsigned.priorStateRoot, forKey: .priorStateRoot)
try container.encode(unsigned.extrinsicsHash, forKey: .extrinsicsHash)
try container.encode(unsigned.timeslot, forKey: .timeslot)
try container.encodeIfPresent(unsigned.epoch, forKey: .epoch)
try container.encodeIfPresent(unsigned.winningTickets, forKey: .winningTickets)
try container.encode(unsigned.offendersMarkers, forKey: .offendersMarkers)
try container.encode(unsigned.authorIndex, forKey: .authorIndex)
try container.encode(unsigned.vrfSignature, forKey: .vrfSignature)
try container.encode(seal, forKey: .seal)
}
}

extension Header {
public func asRef() -> HeaderRef {
HeaderRef(self)
Expand Down
10 changes: 5 additions & 5 deletions Blockchain/Sources/Blockchain/Types/WorkOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,18 @@ extension WorkOutput: Codable {
var container = encoder.unkeyedContainer()
switch result {
case let .success(success):
try container.encode(0)
try container.encode(UInt8(0))
try container.encode(success)
case let .failure(failure):
switch failure {
case .outOfGas:
try container.encode(1)
try container.encode(UInt8(1))
case .panic:
try container.encode(2)
try container.encode(UInt8(2))
case .invalidCode:
try container.encode(3)
try container.encode(UInt8(3))
case .codeTooLarge:
try container.encode(4)
try container.encode(UInt8(4))
}
}
} else {
Expand Down
16 changes: 7 additions & 9 deletions Blockchain/Sources/Blockchain/Types/WorkReport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ import Foundation
import Utils

public struct WorkReport: Sendable, Equatable, Codable {
// the order is based on the Block Serialization section
// s: package specification
public var packageSpecification: AvailabilitySpecifications

// a: authorizer hash
public var authorizerHash: Data32
// x: refinement context
public var refinementContext: RefinementContext

// c: the core-index
public var coreIndex: CoreIndex

// a: authorizer hash
public var authorizerHash: Data32

// o: output
public var output: Data

// x: refinement context
public var refinementContext: RefinementContext

// s: package specification
public var packageSpecification: AvailabilitySpecifications

// r: the results of the evaluation of each of the items in the package
public var results: ConfigLimitedSizeArray<
WorkResult,
Expand Down
7 changes: 3 additions & 4 deletions Codec/Sources/Codec/JamDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private class DecodeContext: Decoder {
}

fileprivate func decodeData(codingPath: @autoclosure () -> [CodingKey]) throws -> Data {
guard let length = data.decodeScale() else {
guard let length = data.decode() else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: codingPath(),
Expand All @@ -131,7 +131,7 @@ private class DecodeContext: Decoder {
}

fileprivate func decodeData(codingPath: @autoclosure () -> [CodingKey]) throws -> [UInt8] {
guard let length = data.decodeScale() else {
guard let length = data.decode() else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: codingPath(),
Expand All @@ -153,15 +153,14 @@ private class DecodeContext: Decoder {
}

fileprivate func decodeArray<T: ArrayWrapper>(_ type: T.Type, key: CodingKey?) throws -> T {
guard let length = data.decodeScale() else {
guard let length = data.decode(), length < 0xFFFFFF else {
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: codingPath,
debugDescription: "Unable to decode array length"
)
)
}
assert(length < 0xFFFFFF)
var array = [T.Element]()
array.reserveCapacity(Int(length))
for _ in 0 ..< length {
Expand Down
45 changes: 0 additions & 45 deletions Codec/Sources/Codec/ScaleIntegerCodec.swift

This file was deleted.

6 changes: 3 additions & 3 deletions Codec/Tests/CodecTests/SortedSetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ struct SortedSetTests {
}

@Test func decode() throws {
let decoded = try JamDecoder.decode(SortedSet<UInt8>.self, from: Data([12, 1, 2, 3]))
let decoded = try JamDecoder.decode(SortedSet<UInt8>.self, from: Data([3, 1, 2, 3]))
#expect(decoded.alias == [1, 2, 3])
}

@Test func invalidData() throws {
#expect(throws: DecodingError.self) {
try JamDecoder.decode(SortedSet<UInt>.self, from: Data([12, 1, 2, 2]))
try JamDecoder.decode(SortedSet<UInt>.self, from: Data([3, 1, 2, 2]))
}

#expect(throws: DecodingError.self) {
try JamDecoder.decode(SortedSet<UInt>.self, from: Data([12, 3, 2, 1]))
try JamDecoder.decode(SortedSet<UInt>.self, from: Data([3, 3, 2, 1]))
}
}
}
5 changes: 5 additions & 0 deletions JAMTests/Sources/JAMTests/TestLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@ enum TestLoader {
return Testcase(description: $0, data: data)
}
}

static func getFile(path: String, extension ext: String) throws -> Data {
let path = Bundle.module.resourcePath! + "/jamtestvectors/\(path).\(ext)"
return try Data(contentsOf: URL(fileURLWithPath: path))
}
}
Loading

0 comments on commit d40ed96

Please sign in to comment.