Skip to content

Commit

Permalink
Add gas host call (#303)
Browse files Browse the repository at this point in the history
* Add gas host call

* fix conditions
  • Loading branch information
krystian50 authored Jan 22, 2025
1 parent ec8a53c commit c9d0fff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 16 additions & 6 deletions src/packages/web-worker/command-handlers/host-call.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -146,10 +146,14 @@ const hostCall = async ({
}: {
pvm: PvmApiInterface;
hostCallIdentifier: HostCallIdentifiers;
storage: Storage;
storage: Storage | null;
serviceId: number;
}): Promise<HostCallResponse> => {
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
Expand All @@ -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") };
Expand All @@ -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.");
}
Expand Down
5 changes: 4 additions & 1 deletion src/store/workers/workersSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit c9d0fff

Please sign in to comment.