Skip to content
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/FluffyLabs/typeberry/blob/main/packages/pvm/host-calls/host-calls-manager.ts#L10

Take a look at this code it's doing exactly the thing that you are doing here with a series of if-elses and handles a missing host call by returning some info back to the PVM.
You might consider using this instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, I'll take some pieces from this when working on the import/export host calls.

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
Loading