Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update work item #250

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions Blockchain/Sources/Blockchain/Types/WorkItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,47 @@
// I
public struct WorkItem: Sendable, Equatable, Codable {
public struct ImportedDataSegment: Sendable, Equatable, Codable {
public var root: Data32
public enum DataSegmentRootKind: Sendable, Equatable {
case segmentRoot(Data32)
case workPackageHash(Data32)
}

public var root: DataSegmentRootKind
public var index: UInt16

public init(root: Data32, index: UInt16) {
public init(root: DataSegmentRootKind, index: UInt16) {

Check warning on line 15 in Blockchain/Sources/Blockchain/Types/WorkItem.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/Types/WorkItem.swift#L15

Added line #L15 was not covered by tests
self.root = root
self.index = index
}

// Encodable
public func encode(to encoder: Encoder) throws {
var container = encoder.unkeyedContainer()
var indexValue = index
switch root {
case let .segmentRoot(root):
try container.encode(root)
case let .workPackageHash(hash):
try container.encode(hash)
indexValue |= 1 << 15
}
try container.encode(indexValue)
}

Check warning on line 32 in Blockchain/Sources/Blockchain/Types/WorkItem.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/Types/WorkItem.swift#L21-L32

Added lines #L21 - L32 were not covered by tests

// Decodable
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
let root = try container.decode(Data32.self)
let index = try container.decode(UInt16.self)
let flag = index >> 15
if flag == 0 {
self.root = .segmentRoot(root)
self.index = index
} else {
self.root = .workPackageHash(root)
self.index = index & 0x7FFF
}
}

Check warning on line 47 in Blockchain/Sources/Blockchain/Types/WorkItem.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/Types/WorkItem.swift#L35-L47

Added lines #L35 - L47 were not covered by tests
}

// s
Expand All @@ -23,7 +57,10 @@
public var payloadBlob: Data

// g
public var gasLimit: Gas
public var refineGasLimit: Gas

// a
public var accumulateGasLimit: Gas

// i: a sequence of imported data segments which identify a prior exported segment through an index
public var inputs: [ImportedDataSegment]
Expand All @@ -38,15 +75,17 @@
serviceIndex: ServiceIndex,
codeHash: Data32,
payloadBlob: Data,
gasLimit: Gas,
refineGasLimit: Gas,
accumulateGasLimit: Gas,
inputs: [ImportedDataSegment],
outputs: [HashAndLength],
outputDataSegmentsCount: UInt16
) {
self.serviceIndex = serviceIndex
self.codeHash = codeHash
self.payloadBlob = payloadBlob
self.gasLimit = gasLimit
self.refineGasLimit = refineGasLimit
self.accumulateGasLimit = accumulateGasLimit

Check warning on line 88 in Blockchain/Sources/Blockchain/Types/WorkItem.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/Types/WorkItem.swift#L87-L88

Added lines #L87 - L88 were not covered by tests
self.inputs = inputs
self.outputs = outputs
self.outputDataSegmentsCount = outputDataSegmentsCount
Expand All @@ -60,7 +99,8 @@
serviceIndex: 0,
codeHash: Data32(),
payloadBlob: Data(),
gasLimit: Gas(0),
refineGasLimit: Gas(0),
accumulateGasLimit: Gas(0),

Check warning on line 103 in Blockchain/Sources/Blockchain/Types/WorkItem.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/Types/WorkItem.swift#L102-L103

Added lines #L102 - L103 were not covered by tests
inputs: [],
outputs: [],
outputDataSegmentsCount: 0
Expand Down
12 changes: 8 additions & 4 deletions JAMTests/Tests/JAMTests/CodecTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,18 @@

@Test
func work_item() throws {
let (actual, expected) = try Self.test(WorkItem.self, path: "work_item")
#expect(actual == expected)
withKnownIssue("need bump test vectors") {
let (actual, expected) = try Self.test(WorkItem.self, path: "work_item")
#expect(actual == expected)
}

Check warning on line 303 in JAMTests/Tests/JAMTests/CodecTests.swift

View check run for this annotation

Codecov / codecov/patch

JAMTests/Tests/JAMTests/CodecTests.swift#L302-L303

Added lines #L302 - L303 were not covered by tests
}

@Test
func work_package() throws {
let (actual, expected) = try Self.test(WorkPackage.self, path: "work_package")
#expect(actual == expected)
withKnownIssue("need bump test vectors") {
let (actual, expected) = try Self.test(WorkPackage.self, path: "work_package")
#expect(actual == expected)
}

Check warning on line 311 in JAMTests/Tests/JAMTests/CodecTests.swift

View check run for this annotation

Codecov / codecov/patch

JAMTests/Tests/JAMTests/CodecTests.swift#L310-L311

Added lines #L310 - L311 were not covered by tests
}

@Test
Expand Down
6 changes: 3 additions & 3 deletions Utils/Sources/Utils/AnyCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
/// A type-erased codable value.
///
/// An `AnyCodable` value forwards encoding and decoding operations to the underlying base.
public struct AnyCodable: Codable, CustomDebugStringConvertible {
public struct AnyCodable: Codable, CustomDebugStringConvertible, Sendable {
/// The base encodable value.
public var value: Encodable
public var value: Encodable & Sendable

/// Creates a codable value that wraps the given base.
public init(_ encodable: Encodable) {
public init(_ encodable: Encodable & Sendable) {
value = encodable
}

Expand Down