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

encoder cleanup #251

Merged
merged 1 commit into from
Dec 17, 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
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 {
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)

Check warning on line 62 in Blockchain/Sources/Blockchain/Types/Extrinsic.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/Types/Extrinsic.swift#L62

Added line #L62 was not covered by tests
}).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
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,7 @@
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()))

Check warning on line 946 in Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/HostCall/HostCalls.swift#L946

Added line #L946 was not covered by tests
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 @@
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)

Check warning on line 37 in Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/Invocations/AccumulateInvocation.swift#L37

Added line #L37 was not covered by tests

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

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)

Check warning on line 19 in Blockchain/Sources/Blockchain/VMInvocations/Invocations/IsAuthorizedInvocation.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/Invocations/IsAuthorizedInvocation.swift#L18-L19

Added lines #L18 - L19 were not covered by tests
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 @@
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)

Check warning on line 27 in Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/Invocations/OnTransferInvocation.swift#L27

Added line #L27 was not covered by tests

_ = await invokePVM(
config: config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@
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
)

Check warning on line 62 in Blockchain/Sources/Blockchain/VMInvocations/Invocations/RefineInvocation.swift

View check run for this annotation

Codecov / codecov/patch

Blockchain/Sources/Blockchain/VMInvocations/Invocations/RefineInvocation.swift#L54-L62

Added lines #L54 - L62 were not covered by tests
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