Skip to content

Commit

Permalink
update swift testing
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Jul 3, 2024
1 parent 3c369ee commit 86db549
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Blockchain/Sources/Blockchain/Types/primitives.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions JAMTests/Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "d0ba6136ea802783642249bb2acb6f690d9aaa061248c2475c631ac2b936925b",
"originHash" : "1a94da3c8aaf067c6334fb57eec4e914d1b1a9aa2bef87839b818335ab3fcc54",
"pins" : [
{
"identity" : "blake2.swift",
Expand Down Expand Up @@ -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"
Expand All @@ -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"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion JAMTests/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
193 changes: 189 additions & 4 deletions JAMTests/Tests/JAMTests/SafroleTests.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Blockchain
import Foundation
import ScaleCodec
import Testing
import Utils
Expand All @@ -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 {}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"originHash" : "693e5dac692252788cbed9d32807c9329cd36f901a527b7bc91afc36cc7393fb",
"originHash" : "8d5cb838cc09a6a6bc00190970a8db021b952fc8ccba1e187ef962bc6cc66278",
"pins" : [
{
"identity" : "blake2.swift",
Expand Down Expand Up @@ -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"
Expand All @@ -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"
}
},
{
Expand Down

0 comments on commit 86db549

Please sign in to comment.