From 3137c5e256858231595222b7a07a645139a73dc8 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 17 Dec 2024 14:57:11 +1300 Subject: [PATCH] encoder cleanup --- .../Sources/Blockchain/Types/Extrinsic.swift | 8 ++++---- .../VMInvocations/HostCall/HostCalls.swift | 2 +- .../Invocations/AccumulateInvocation.swift | 2 +- .../Invocations/IsAuthorizedInvocation.swift | 10 ++++++---- .../Invocations/OnTransferInvocation.swift | 2 +- .../Invocations/RefineInvocation.swift | 16 +++++++++------- Codec/Sources/Codec/JamEncoder.swift | 8 ++++++++ 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Blockchain/Sources/Blockchain/Types/Extrinsic.swift b/Blockchain/Sources/Blockchain/Types/Extrinsic.swift index 21ed6be4..5a690b36 100644 --- a/Blockchain/Sources/Blockchain/Types/Extrinsic.swift +++ b/Blockchain/Sources/Blockchain/Types/Extrinsic.swift @@ -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() diff --git a/Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift b/Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift index 8d1170c9..d09455a3 100644 --- a/Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift +++ b/Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift @@ -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 { diff --git a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift index caba9677..0b35c913 100644 --- a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift +++ b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift @@ -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, diff --git a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/IsAuthorizedInvocation.swift b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/IsAuthorizedInvocation.swift index cbf1dde7..51bbda0d 100644 --- a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/IsAuthorizedInvocation.swift +++ b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/IsAuthorizedInvocation.swift @@ -11,10 +11,12 @@ public protocol IsAuthorizedFunction { } extension IsAuthorizedFunction { - public func invoke(config: ProtocolConfigRef, package: WorkPackage, - coreIndex: CoreIndex) async throws -> Result - { - let args = try JamEncoder.encode(package) + JamEncoder.encode(coreIndex) + public func invoke( + config: ProtocolConfigRef, + package: WorkPackage, + coreIndex: CoreIndex + ) async throws -> Result { + let args = try JamEncoder.encode(package, coreIndex) let ctx = IsAuthorizedContext(config: config) let (exitReason, _, output) = await invokePVM( diff --git a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift index 64b5004c..451d5226 100644 --- a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift +++ b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift @@ -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, diff --git a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/RefineInvocation.swift b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/RefineInvocation.swift index 2906dd4c..722e1c85 100644 --- a/Blockchain/Sources/Blockchain/VMInvocations/Invocations/RefineInvocation.swift +++ b/Blockchain/Sources/Blockchain/VMInvocations/Invocations/RefineInvocation.swift @@ -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: []), diff --git a/Codec/Sources/Codec/JamEncoder.swift b/Codec/Sources/Codec/JamEncoder.swift index 59796f1e..36bee04e 100644 --- a/Codec/Sources/Codec/JamEncoder.swift +++ b/Codec/Sources/Codec/JamEncoder.swift @@ -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 }