Skip to content

Commit

Permalink
Merge branch 'master' into dev-guaranteeringservice
Browse files Browse the repository at this point in the history
* master:
  encoder cleanup (#251)
  update work item (#250)
  • Loading branch information
MacOMNI committed Dec 18, 2024
2 parents d1bbfdb + 25e6b5a commit 30cf6d7
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 31 deletions.
8 changes: 4 additions & 4 deletions Blockchain/Sources/Blockchain/Types/Extrinsic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ extension Extrinsic: Validate {}
extension Extrinsic {
public func hash() -> Data32 {
do {
return try JamEncoder.encode([
return try JamEncoder.encode(
JamEncoder.encode(tickets).blake2b256hash(),
JamEncoder.encode(preimages).blake2b256hash(),
JamEncoder.encode(reports.guarantees.array.map { item in
try JamEncoder.encode(item.workReport.hash()) + JamEncoder.encode(item.timeslot) + JamEncoder.encode(item.credential)
try JamEncoder.encode(item.workReport.hash(), item.timeslot, item.credential)
}).blake2b256hash(),
JamEncoder.encode(availability).blake2b256hash(),
JamEncoder.encode(disputes).blake2b256hash(),
]).blake2b256hash()
JamEncoder.encode(disputes).blake2b256hash()
).blake2b256hash()
} catch {
logger.error("Failed to encode extrinsic, returning empty hash", metadata: ["error": "\(error)"])
return Data32()
Expand Down
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 @@ import Utils
// 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) {
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)
}

// 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
}
}
}

// s
Expand All @@ -23,7 +57,10 @@ public struct WorkItem: Sendable, Equatable, Codable {
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 @@ public struct WorkItem: Sendable, Equatable, Codable {
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
self.inputs = inputs
self.outputs = outputs
self.outputDataSegmentsCount = outputDataSegmentsCount
Expand All @@ -60,7 +99,8 @@ extension WorkItem: Dummy {
serviceIndex: 0,
codeHash: Data32(),
payloadBlob: Data(),
gasLimit: Gas(0),
refineGasLimit: Gas(0),
accumulateGasLimit: Gas(0),
inputs: [],
outputs: [],
outputDataSegmentsCount: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@ public class Invoke: HostCall {
let engine = Engine(config: DefaultPvmConfig())
let exitReason = await engine.execute(program: program, state: vm)

try state.writeMemory(address: startAddr, values: JamEncoder.encode(vm.getGas()) + JamEncoder.encode(vm.getRegisters()))
try state.writeMemory(address: startAddr, values: JamEncoder.encode(vm.getGas(), vm.getRegisters()))
context.pvms[pvmIndex]?.memory = vm.getMemoryUnsafe()

switch exitReason {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension AccumulateFunction {
y: resultCtx
)
let ctx = AccumulateContext(context: &contextContent, config: config, timeslot: timeslot)
let argument = try JamEncoder.encode(timeslot) + JamEncoder.encode(serviceIndex) + JamEncoder.encode(arguments)
let argument = try JamEncoder.encode(timeslot, serviceIndex, arguments)

let (exitReason, gas, output) = await invokePVM(
config: config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public protocol IsAuthorizedFunction {
}

extension IsAuthorizedFunction {
public func invoke(config: ProtocolConfigRef, package: WorkPackage,
coreIndex: CoreIndex) async throws -> Result<Data, WorkResultError>
{
let args = try JamEncoder.encode(package) + JamEncoder.encode(coreIndex)
public func invoke(
config: ProtocolConfigRef,
package: WorkPackage,
coreIndex: CoreIndex
) async throws -> Result<Data, WorkResultError> {
let args = try JamEncoder.encode(package, coreIndex)
let ctx = IsAuthorizedContext(config: config)

let (exitReason, _, output) = await invokePVM(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extension OnTransferFunction {
var contextContent = OnTransferContext.ContextType(service, serviceAccounts)
let ctx = OnTransferContext(context: &contextContent, config: config)
let gasLimitSum = transfers.reduce(Balance(0)) { $0 + $1.gasLimit }
let argument = try JamEncoder.encode(timeslot) + JamEncoder.encode(service) + JamEncoder.encode(transfers)
let argument = try JamEncoder.encode(timeslot, service, transfers)

_ = await invokePVM(
config: config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ extension RefineInvocation {
return (.failure(.codeTooLarge), [])
}

let argumentData = try JamEncoder.encode(service) +
JamEncoder.encode(workPayload) +
JamEncoder.encode(workPackageHash) +
JamEncoder.encode(refinementCtx) +
JamEncoder.encode(authorizerHash) +
JamEncoder.encode(authorizationOutput) +
JamEncoder.encode(extrinsicDataBlobs)
let argumentData = try JamEncoder.encode(
service,
workPayload,
workPackageHash,
refinementCtx,
authorizerHash,
authorizationOutput,
extrinsicDataBlobs
)
let ctx = RefineContext(
config: config,
context: (pvms: [:], exports: []),
Expand Down
8 changes: 8 additions & 0 deletions Codec/Sources/Codec/JamEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ public class JamEncoder {
return encoder.data
}

public static func encode(_ values: any Encodable...) throws -> Data {
let encoder = JamEncoder()
for value in values {
try encoder.encode(value)
}
return encoder.data
}

public var data: Data {
encoder.data
}
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 @@ struct CodecTests {

@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)
}
}

@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)
}
}

@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

0 comments on commit 30cf6d7

Please sign in to comment.