From c9d0fffb57929536535dbc66108e4491b371ea2e Mon Sep 17 00:00:00 2001 From: Krystian Fras Date: Wed, 22 Jan 2025 16:15:35 +0100 Subject: [PATCH] Add gas host call (#303) * Add gas host call * fix conditions --- .../web-worker/command-handlers/host-call.ts | 22 ++++++++++++++----- src/store/workers/workersSlice.ts | 5 ++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/packages/web-worker/command-handlers/host-call.ts b/src/packages/web-worker/command-handlers/host-call.ts index 8c9640e..3d2ca39 100644 --- a/src/packages/web-worker/command-handlers/host-call.ts +++ b/src/packages/web-worker/command-handlers/host-call.ts @@ -1,6 +1,6 @@ import { HostCallIdentifiers } from "@/types/pvm"; import { CommandStatus, PvmApiInterface, Storage } from "../types"; -import { read, Registers, write, Memory } from "@typeberry/jam-host-calls"; +import { read, Registers, write, Memory, gas } from "@typeberry/jam-host-calls"; import { WriteAccounts } from "@/packages/host-calls/write"; import { isInternalPvm } from "../utils"; import { ReadAccounts } from "@/packages/host-calls/read"; @@ -146,10 +146,14 @@ const hostCall = async ({ }: { pvm: PvmApiInterface; hostCallIdentifier: HostCallIdentifiers; - storage: Storage; + storage: Storage | null; serviceId: number; }): Promise => { if (hostCallIdentifier === HostCallIdentifiers.READ) { + if (storage === null) { + throw new Error("Storage is uninitialized."); + } + const readAccounts = new ReadAccounts(storage); const jamHostCall = new read.Read(readAccounts); // TODO the types are the same, but exported from different packages and lost track of the type @@ -159,12 +163,22 @@ const hostCall = async ({ return { hostCallIdentifier, status: CommandStatus.SUCCESS }; } else if (hostCallIdentifier === HostCallIdentifiers.WRITE) { + if (storage === null) { + throw new Error("Storage is uninitialized."); + } + const writeAccounts = new WriteAccounts(storage); const jamHostCall = new write.Write(writeAccounts); await jamHostCall.execute(getGasCounter(pvm), getRegisters(pvm), getMemory(pvm)); return { hostCallIdentifier, storage, status: CommandStatus.SUCCESS }; + } else if (hostCallIdentifier === HostCallIdentifiers.GAS) { + const jamHostCall = new gas.Gas(); + + await jamHostCall.execute(getGasCounter(pvm), getRegisters(pvm)); + + return { hostCallIdentifier, status: CommandStatus.SUCCESS }; } return { hostCallIdentifier, status: CommandStatus.ERROR, error: new Error("Unknown host call identifier") }; @@ -180,10 +194,6 @@ export const runHostCall = async ({ throw new Error("PVM is uninitialized."); } - if (storage === null) { - throw new Error("Storage is uninitialized."); - } - if (serviceId === null) { throw new Error("Service ID is uninitialized."); } diff --git a/src/store/workers/workersSlice.ts b/src/store/workers/workersSlice.ts index 34478c8..03374b8 100644 --- a/src/store/workers/workersSlice.ts +++ b/src/store/workers/workersSlice.ts @@ -282,7 +282,10 @@ export const handleHostCall = createAsyncThunk( async ({ workerId }: { workerId?: string }, { getState, dispatch }) => { const state = getState() as RootState; - if (state.debugger.storage === null) { + if ( + state.debugger.storage === null && + state.workers.findIndex((worker: WorkerState) => worker.exitArg === HostCallIdentifiers.READ) !== -1 + ) { return dispatch(setHasHostCallOpen(true)); } else { const previousInstruction = nextInstruction(