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
4 changes: 2 additions & 2 deletions src/components/HostCalls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export const HostCalls = () => {
setError("");

try {
dispatch(setStorage(newStorage || []));
await dispatch(setAllWorkersStorage()).unwrap();
dispatch(setStorage({ storage: newStorage, isUserProvided: true }));
await dispatch(setAllWorkersStorage({ storage: newStorage || null })).unwrap();
try {
if (isOnEcalli) {
await dispatch(handleHostCall({})).unwrap();
Expand Down
11 changes: 9 additions & 2 deletions src/components/HostCalls/trie-input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { hasPVMGeneratedStorage } from "@/store/debugger/debuggerSlice";
import { useAppSelector } from "@/store/hooks";
import { bytes, hash } from "@typeberry/jam-host-calls";
import { cloneDeep } from "lodash";
import { PlusIcon, XIcon } from "lucide-react";
Expand Down Expand Up @@ -35,6 +37,7 @@ type StorageItemProps = {
};

const StorageItem = ({ row, index, handleRemoveRow, handleKeyChange, handleValueChange }: StorageItemProps) => {
const isDisabled = useAppSelector(hasPVMGeneratedStorage);
const backgroundClass = index % 2 === 0 ? "bg-white" : "bg-gray-100";

// Cover PVM generated hash
Expand All @@ -49,13 +52,14 @@ const StorageItem = ({ row, index, handleRemoveRow, handleKeyChange, handleValue
placeholder="Key"
value={row.key || row.keyHash}
onChange={(e) => handleKeyChange(index, e.target.value)}
disabled={isDisabled}
/>
<span className="text-xs py-1 ml-2">
Key Hash: {isKeySameAsHash ? "--- key is already a hash ---" : row.keyHash}
</span>
</div>

<Button variant="ghost" onClick={() => handleRemoveRow(index)}>
<Button variant="ghost" onClick={() => handleRemoveRow(index)} disabled={isDisabled}>
<XIcon className="w-4 h-4" />
</Button>
</div>
Expand All @@ -66,13 +70,16 @@ const StorageItem = ({ row, index, handleRemoveRow, handleKeyChange, handleValue
value={row.value}
onChange={(e) => handleValueChange(index, e.target.value)}
className="flex-1 mt-1"
disabled={isDisabled}
/>
</div>
</div>
</div>
);
};
export const TrieInput = ({ onChange, initialRows }: TrieInputProps) => {
const isDisabled = useAppSelector(hasPVMGeneratedStorage);

const [rows, setRows] = useState<StorageRow[]>([
{
key: "",
Expand Down Expand Up @@ -153,7 +160,7 @@ export const TrieInput = ({ onChange, initialRows }: TrieInputProps) => {
/>
);
})}
<Button className="mt-3 ml-3" variant="secondary" onClick={() => handleInsertRow()}>
<Button className="mt-3 ml-3" variant="secondary" onClick={() => handleInsertRow()} disabled={isDisabled}>
<PlusIcon className="w-4 h-4" /> Add new
</Button>
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useDebuggerActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "@/store/debugger/debuggerSlice";
import { useAppDispatch, useAppSelector } from "@/store/hooks";
import {
clearAllWorkersStorage,
createWorker,
destroyWorker,
initAllWorkers,
Expand All @@ -24,7 +25,6 @@ import {
setAllWorkersCurrentInstruction,
setAllWorkersCurrentState,
setAllWorkersPreviousState,
setAllWorkersStorage,
WorkerState,
} from "@/store/workers/workersSlice";
import { AvailablePvms, ExpectedState, Status } from "@/types/pvm";
Expand Down Expand Up @@ -52,7 +52,7 @@ export const useDebuggerActions = () => {
dispatch(setClickedInstruction(null));

if (storage) {
await dispatch(setAllWorkersStorage()).unwrap();
await dispatch(await clearAllWorkersStorage()).unwrap();
}
},
[dispatch, programPreviewResult, storage],
Expand Down
2 changes: 1 addition & 1 deletion src/packages/web-worker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type CommandWorkerRequestParams =
| { command: Commands.RUN }
| { command: Commands.STOP }
| { command: Commands.MEMORY; payload: { startAddress: number; stopAddress: number } }
| { command: Commands.SET_STORAGE; payload: { storage: Storage } }
| { command: Commands.SET_STORAGE; payload: { storage: Storage | null } }
| { command: Commands.HOST_CALL; payload: { hostCallIdentifier: HostCallIdentifiers } }
| { command: Commands.SET_SERVICE_ID; payload: { serviceId: number } };

Expand Down
11 changes: 10 additions & 1 deletion src/store/debugger/debuggerSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createSlice } from "@reduxjs/toolkit";
import { CurrentInstruction, DebuggerEcalliStorage, ExpectedState } from "@/types/pvm.ts";
import { InstructionMode } from "@/components/Instructions/types.ts";
import { RootState } from "@/store";
import { isEqual } from "lodash";

export interface DebuggerState {
isProgramInvalid: boolean;
Expand All @@ -19,6 +20,7 @@ export interface DebuggerState {
pvmInitialized: boolean;
stepsToPerform: number;
storage: DebuggerEcalliStorage | null;
userProvidedStorage: DebuggerEcalliStorage | null;
serviceId: number | null;
}

Expand All @@ -44,6 +46,7 @@ const initialState: DebuggerState = {
pvmInitialized: false,
stepsToPerform: 1,
storage: null,
userProvidedStorage: null,
serviceId: parseInt("0x30303030", 16),
};

Expand Down Expand Up @@ -94,7 +97,11 @@ const debuggerSlice = createSlice({
state.hasHostCallOpen = action.payload;
},
setStorage(state, action) {
state.storage = action.payload;
state.storage = action.payload.storage;

if (action.payload.isUserProvided) {
state.userProvidedStorage = action.payload.storage;
}
},
setServiceId(state, action) {
state.serviceId = action.payload;
Expand Down Expand Up @@ -132,5 +139,7 @@ export const selectClickedInstruction = (state: RootState) => state.debugger.cli
export const selectInstructionMode = (state: RootState) => state.debugger.instructionMode;
export const selectIsDebugFinished = (state: RootState) => state.debugger.isDebugFinished;
export const selectPvmInitialized = (state: RootState) => state.debugger.pvmInitialized;
export const hasPVMGeneratedStorage = (state: RootState) =>
state.debugger.storage !== null && !isEqual(state.debugger.storage, state.debugger.userProvidedStorage);

export default debuggerSlice.reducer;
43 changes: 23 additions & 20 deletions src/store/workers/workersSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSlice, createAsyncThunk, isRejected } from "@reduxjs/toolkit";
import { RootState } from "@/store";
import { CurrentInstruction, ExpectedState, HostCallIdentifiers, Status } from "@/types/pvm.ts";
import { CurrentInstruction, DebuggerEcalliStorage, ExpectedState, HostCallIdentifiers, Status } from "@/types/pvm.ts";
import {
selectIsDebugFinished,
setHasHostCallOpen,
Expand Down Expand Up @@ -112,28 +112,31 @@ export const loadWorker = createAsyncThunk(
}
},
);

export const setAllWorkersStorage = createAsyncThunk("workers/setAllStorage", async (_, { getState }) => {
export const clearAllWorkersStorage = createAsyncThunk("workers/clearAllStorage", async (_, { getState, dispatch }) => {
const state = getState() as RootState;
const debuggerState = state.debugger;
const storage = debuggerState.storage;

if (storage === null) {
throw new Error("Storage is not set");
}

return Promise.all(
await state.workers.map(async (worker) => {
await asyncWorkerPostMessage(worker.id, worker.worker, {
command: Commands.SET_STORAGE,
payload: {
storage: toPvmStorage(storage),
},
});
}),
);
dispatch(setStorage({ storage: state.debugger.userProvidedStorage, isUserProvided: true }));
await dispatch(setAllWorkersStorage({ storage: state.debugger.userProvidedStorage })).unwrap();
});

export const setAllWorkersStorage = createAsyncThunk(
"workers/setAllStorage",
async ({ storage }: { storage: DebuggerEcalliStorage | null }, { getState }) => {
const state = getState() as RootState;

return Promise.all(
await state.workers.map(async (worker) => {
await asyncWorkerPostMessage(worker.id, worker.worker, {
command: Commands.SET_STORAGE,
payload: {
storage: storage && toPvmStorage(storage),
},
});
}),
);
},
);

export const setAllWorkersServiceId = createAsyncThunk("workers/setAllStorage", async (_, { getState }) => {
const state = getState() as RootState;
const debuggerState = state.debugger;
Expand Down Expand Up @@ -307,7 +310,7 @@ export const handleHostCall = createAsyncThunk(
state.debugger.storage
) {
const newStorage = mergePVMAndDebuggerEcalliStorage(resp.payload.storage, state.debugger.storage);
dispatch(setStorage(newStorage));
dispatch(setStorage({ storage: newStorage, isUserProvided: false }));
}

if ((getState() as RootState).debugger.isRunMode) {
Expand Down
Loading