From 86db549ca938c013df367fabbaf5ff19d7794d57 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 3 Jul 2024 15:14:54 +1200 Subject: [PATCH] update swift testing --- .../Sources/Blockchain/Types/primitives.swift | 2 +- JAMTests/Package.resolved | 8 +- JAMTests/Package.swift | 2 +- JAMTests/Tests/JAMTests/SafroleTests.swift | 193 +++++++++++++++++- .../xcshareddata/swiftpm/Package.resolved | 8 +- 5 files changed, 199 insertions(+), 14 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Types/primitives.swift b/Blockchain/Sources/Blockchain/Types/primitives.swift index ee2ef32d..9c5a61b7 100644 --- a/Blockchain/Sources/Blockchain/Types/primitives.swift +++ b/Blockchain/Sources/Blockchain/Types/primitives.swift @@ -14,5 +14,5 @@ public typealias Ed25519Signature = Data64 public typealias BandersnatchPublicKey = Data32 public typealias BandersnatchSignature = Data64 public typealias BandersnatchRintVRFProof = Data784 -public typealias BandersnatchRingVRFRoot = Data384 +public typealias BandersnatchRingVRFRoot = Data144 public typealias BLSKey = Data144 diff --git a/JAMTests/Package.resolved b/JAMTests/Package.resolved index 94cfe76e..2c7986c5 100644 --- a/JAMTests/Package.resolved +++ b/JAMTests/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "d0ba6136ea802783642249bb2acb6f690d9aaa061248c2475c631ac2b936925b", + "originHash" : "1a94da3c8aaf067c6334fb57eec4e914d1b1a9aa2bef87839b818335ab3fcc54", "pins" : [ { "identity" : "blake2.swift", @@ -31,7 +31,7 @@ { "identity" : "swift-syntax", "kind" : "remoteSourceControl", - "location" : "https://github.com/swiftlang/swift-syntax.git", + "location" : "https://github.com/apple/swift-syntax.git", "state" : { "revision" : "4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c", "version" : "600.0.0-prerelease-2024-06-12" @@ -42,8 +42,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-testing.git", "state" : { - "branch" : "main", - "revision" : "0e2d9cbb553aba8b9a8acf3d56d1bf042360855f" + "branch" : "0.10.0", + "revision" : "69d59cfc76e5daf498ca61f5af409f594768eef9" } }, { diff --git a/JAMTests/Package.swift b/JAMTests/Package.swift index 794ba28c..e936bf76 100644 --- a/JAMTests/Package.swift +++ b/JAMTests/Package.swift @@ -18,7 +18,7 @@ let package = Package( dependencies: [ .package(path: "../Utils"), .package(path: "../Blockchain"), - .package(url: "https://github.com/apple/swift-testing.git", branch: "main"), + .package(url: "https://github.com/apple/swift-testing.git", branch: "0.10.0"), .package(url: "https://github.com/tesseract-one/ScaleCodec.swift.git", from: "0.3.0"), ], targets: [ diff --git a/JAMTests/Tests/JAMTests/SafroleTests.swift b/JAMTests/Tests/JAMTests/SafroleTests.swift index 23b3ae3a..a30bf375 100644 --- a/JAMTests/Tests/JAMTests/SafroleTests.swift +++ b/JAMTests/Tests/JAMTests/SafroleTests.swift @@ -1,4 +1,5 @@ import Blockchain +import Foundation import ScaleCodec import Testing import Utils @@ -11,10 +12,194 @@ struct SafroleInput { var extrinsics: ExtrinsicTickets } +extension SafroleInput: ScaleCodec.Encodable { + init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws { + try self.init( + slot: decoder.decode(), + entropy: decoder.decode(), + extrinsics: ExtrinsicTickets(config: config, from: &decoder) + ) + } + + func encode(in encoder: inout some ScaleCodec.Encoder) throws { + try encoder.encode(slot) + try encoder.encode(entropy) + try encoder.encode(extrinsics) + } +} + +struct OutputMarks { + var epochMark: Header.EpochMarker? + var ticketsMark: ConfigFixedSizeArray< + Ticket, + ProtocolConfig.EpochLength + >? +} + +extension OutputMarks: ScaleCodec.Encodable { + init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws { + try self.init( + epochMark: Header.EpochMarker(config: config, from: &decoder), + ticketsMark: ConfigFixedSizeArray(config: config, from: &decoder) + ) + } + + func encode(in encoder: inout some ScaleCodec.Encoder) throws { + try encoder.encode(epochMark) + try encoder.encode(ticketsMark) + } +} + +enum SafroleOutput { + case ok(OutputMarks) + case err(UInt8) +} + +extension SafroleOutput: ScaleCodec.Encodable { + init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws { + let id = try decoder.decode(.enumCaseId) + switch id { + case 0: + self = try .ok(OutputMarks(config: config, from: &decoder)) + case 1: + self = try .err(decoder.decode()) + default: + throw decoder.enumCaseError(for: id) + } + } + + func encode(in encoder: inout some ScaleCodec.Encoder) throws { + switch self { + case let .ok(marks): + try encoder.encode(0, .enumCaseId) + try marks.encode(in: &encoder) + case let .err(error): + try encoder.encode(1, .enumCaseId) + try encoder.encode(error) + } + } +} + +struct SafroleState { + var tau: UInt32 + var eta: (Data32, Data32, Data32, Data32) + var lambda: ConfigFixedSizeArray< + ValidatorKey, ProtocolConfig.TotalNumberOfValidators + > + var kappa: ConfigFixedSizeArray< + ValidatorKey, ProtocolConfig.TotalNumberOfValidators + > + var gammaK: ConfigFixedSizeArray< + ValidatorKey, ProtocolConfig.TotalNumberOfValidators + > + var iota: ConfigFixedSizeArray< + ValidatorKey, ProtocolConfig.TotalNumberOfValidators + > + var gammaA: ConfigLimitedSizeArray< + Ticket, + ProtocolConfig.Int0, + ProtocolConfig.EpochLength + > + var gammaS: Either< + ConfigFixedSizeArray< + Ticket, + ProtocolConfig.EpochLength + >, + ConfigFixedSizeArray< + BandersnatchPublicKey, + ProtocolConfig.EpochLength + > + > + var gammaZ: BandersnatchRingVRFRoot +} + +extension SafroleState: ScaleCodec.Encodable { + init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws { + try self.init( + tau: decoder.decode(), + eta: decoder.decode(), + lambda: ConfigFixedSizeArray(config: config, from: &decoder), + kappa: ConfigFixedSizeArray(config: config, from: &decoder), + gammaK: ConfigFixedSizeArray(config: config, from: &decoder), + iota: ConfigFixedSizeArray(config: config, from: &decoder), + gammaA: ConfigLimitedSizeArray(config: config, from: &decoder), + gammaS: Either( + from: &decoder, + decodeLeft: { try ConfigFixedSizeArray(config: config, from: &$0) }, + decodeRight: { try ConfigFixedSizeArray(config: config, from: &$0) } + ), + gammaZ: decoder.decode() + ) + } + + func encode(in encoder: inout some ScaleCodec.Encoder) throws { + try encoder.encode(tau) + try encoder.encode(eta) + try encoder.encode(lambda) + try encoder.encode(kappa) + try encoder.encode(gammaK) + try encoder.encode(iota) + try encoder.encode(gammaA) + try encoder.encode(gammaS) + try encoder.encode(gammaZ) + } +} + +struct SafroleTestcase { + var input: SafroleInput + var preState: SafroleState + var output: SafroleOutput + var postState: SafroleState +} + +extension SafroleTestcase: ScaleCodec.Encodable { + init(config: ProtocolConfigRef, from decoder: inout some ScaleCodec.Decoder) throws { + try self.init( + input: SafroleInput(config: config, from: &decoder), + preState: SafroleState(config: config, from: &decoder), + output: SafroleOutput(config: config, from: &decoder), + postState: SafroleState(config: config, from: &decoder) + ) + } + + func encode(in encoder: inout some ScaleCodec.Encoder) throws { + try encoder.encode(input) + try encoder.encode(preState) + try encoder.encode(output) + try encoder.encode(postState) + } +} + +enum SafroleTestVariants: String, CaseIterable { + case tiny + case full + + static let tinyConfig = { + var config = ProtocolConfigRef.mainnet.value + config.totalNumberOfValidators = 6 + config.epochLength = 12 + return Ref(config) + }() + + var config: ProtocolConfigRef { + switch self { + case .tiny: + Self.tinyConfig + case .full: + ProtocolConfigRef.mainnet + } + } +} + struct SafroleTests { - @Test func works() throws { - let tinyTests = try TestLoader.getTestFiles(path: "safrole/tiny", extension: "scale") - print(tinyTests) - #expect(1 + 1 == 2) + static func loadTests(variant: SafroleTestVariants) throws -> [SafroleTestcase] { + let tests = try TestLoader.getTestFiles(path: "safrole/\(variant)", extension: "scale") + return try tests.map { path in + let data = try Data(contentsOf: URL(fileURLWithPath: path)) + var decoder = decoder(from: data) + return try SafroleTestcase(config: variant.config, from: &decoder) + } } + + @Test func tinyTests() throws {} } diff --git a/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index fadaa2c7..f6dfbd6a 100644 --- a/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/boka.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "693e5dac692252788cbed9d32807c9329cd36f901a527b7bc91afc36cc7393fb", + "originHash" : "8d5cb838cc09a6a6bc00190970a8db021b952fc8ccba1e187ef962bc6cc66278", "pins" : [ { "identity" : "blake2.swift", @@ -40,7 +40,7 @@ { "identity" : "swift-syntax", "kind" : "remoteSourceControl", - "location" : "https://github.com/swiftlang/swift-syntax.git", + "location" : "https://github.com/apple/swift-syntax.git", "state" : { "revision" : "4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c", "version" : "600.0.0-prerelease-2024-06-12" @@ -51,8 +51,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-testing.git", "state" : { - "branch" : "main", - "revision" : "0e2d9cbb553aba8b9a8acf3d56d1bf042360855f" + "branch" : "0.10.0", + "revision" : "69d59cfc76e5daf498ca61f5af409f594768eef9" } }, {