-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLoader.tsx
More file actions
337 lines (308 loc) · 12.1 KB
/
Copy pathLoader.tsx
File metadata and controls
337 lines (308 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { Button } from "../ui/button";
import { Examples } from "./Examples";
import { useState, useCallback, useEffect } from "react";
import { ProgramUploadFileOutput } from "./types";
import { useDebuggerActions } from "@/hooks/useDebuggerActions";
import { useAppDispatch, useAppSelector } from "@/store/hooks.ts";
import { setIsProgramEditMode, setSpiArgs, setServiceId, setHostCallsTrace } from "@/store/debugger/debuggerSlice.ts";
import { selectIsAnyWorkerLoading } from "@/store/workers/workersSlice";
import { isSerializedError } from "@/store/utils";
import { ProgramFileUpload } from "@/components/ProgramLoader/ProgramFileUpload.tsx";
import { useNavigate } from "react-router";
import { Links } from "./Links";
import { Separator } from "../ui/separator";
import { TriangleAlert, FileText } from "lucide-react";
import * as bytes from "@typeberry/lib/bytes";
import {
EntrypointSelector,
Entrypoint,
RefineParams,
AccumulateParams,
IsAuthorizedParams,
} from "./EntrypointSelector";
import { loadSpiConfig, saveSpiConfig } from "./spiConfig";
import { encodeRefineParams, encodeAccumulateParams, encodeIsAuthorizedParams } from "./spiEncoding";
import { getTraceSummary, parseTrace } from "@/lib/host-call-trace";
type LoaderStep = "upload" | "entrypoint";
export const Loader = ({ setIsDialogOpen }: { setIsDialogOpen?: (val: boolean) => void }) => {
const dispatch = useAppDispatch();
const [programLoad, setProgramLoad] = useState<ProgramUploadFileOutput>();
const [error, setError] = useState<string>();
const [currentStep, setCurrentStep] = useState<LoaderStep>("upload");
const [traceContent, setTraceContent] = useState<string | null>(null);
const [traceSummary, setTraceSummary] = useState<ReturnType<typeof getTraceSummary> | null>(null);
// Load saved config once on mount
const savedConfig = loadSpiConfig();
const [selectedEntrypoint, setSelectedEntrypoint] = useState<Entrypoint>(savedConfig.entrypoint);
const [refineParams, setRefineParams] = useState<RefineParams>(savedConfig.refineParams);
const [accumulateParams, setAccumulateParams] = useState<AccumulateParams>(savedConfig.accumulateParams);
const [isAuthorizedParams, setIsAuthorizedParams] = useState<IsAuthorizedParams>(savedConfig.isAuthorizedParams);
const [isManualMode, setIsManualMode] = useState<boolean>(savedConfig.isManualMode);
const [encodedSpiArgs, setEncodedSpiArgs] = useState<string>(savedConfig.encodedArgs);
const [encodingError, setEncodingError] = useState<string | null>(null);
const [manualPc, setManualPc] = useState<string>(savedConfig.manualPc);
const debuggerActions = useDebuggerActions();
const isLoading = useAppSelector(selectIsAnyWorkerLoading);
const navigate = useNavigate();
const isProgramLoaded = programLoad !== undefined;
const isSpiProgram = programLoad?.spiProgram !== null && programLoad?.spiProgram !== undefined;
useEffect(() => {
setError("");
}, [isLoading]);
// Reset step when program changes
useEffect(() => {
setCurrentStep("upload");
}, [programLoad]);
// Auto-switch to RAW mode when a trace is loaded
useEffect(() => {
if (traceContent !== null) {
setIsManualMode(true);
}
}, [traceContent]);
const handleFileUpload = useCallback((output: ProgramUploadFileOutput, trace?: string) => {
setProgramLoad(output);
if (trace) {
try {
setTraceContent(trace);
const parsed = parseTrace(trace);
setTraceSummary(getTraceSummary(parsed));
} catch (e) {
console.error("Failed to parse trace:", e);
setTraceContent(null);
setTraceSummary(null);
}
} else {
setTraceContent(null);
setTraceSummary(null);
}
}, []);
// Auto-encode parameters when they change and update PC
useEffect(() => {
if (!isSpiProgram) return;
try {
let pc: number;
let encoded: Uint8Array;
switch (selectedEntrypoint) {
case "refine":
pc = 0;
encoded = encodeRefineParams(refineParams);
break;
case "accumulate":
pc = 5;
encoded = encodeAccumulateParams(accumulateParams);
break;
case "is_authorized":
pc = 0;
encoded = encodeIsAuthorizedParams(isAuthorizedParams);
break;
}
setManualPc(pc.toString());
setEncodedSpiArgs(bytes.BytesBlob.blobFrom(encoded).toString());
setEncodingError(null);
} catch (error) {
setEncodingError(error instanceof Error ? error.message : "Encoding error");
}
}, [selectedEntrypoint, refineParams, accumulateParams, isAuthorizedParams, isSpiProgram]);
const handleLoad = useCallback(
async (program?: ProgramUploadFileOutput) => {
if (!programLoad && !program) return;
dispatch(setIsProgramEditMode(false));
try {
const loadedProgram = program || programLoad;
let modifiedProgram = loadedProgram;
// For SPI programs, update PC and encode parameters
if (loadedProgram?.spiProgram) {
// Use SPI args from trace if available, otherwise use auto-generated/manual ones
const spiArgsToUse = loadedProgram.spiArgs
? loadedProgram.spiArgs
: bytes.BytesBlob.parseBlob(encodedSpiArgs).raw;
// Set the SPI arguments
dispatch(setSpiArgs(spiArgsToUse));
// Extract and set service ID from entrypoint parameters
let serviceId: number;
switch (selectedEntrypoint) {
case "refine":
serviceId = parseInt(refineParams.id, 10) || 0;
break;
case "accumulate":
serviceId = parseInt(accumulateParams.id, 10) || 0;
break;
case "is_authorized":
serviceId = 0; // Use default for is_authorized
break;
}
dispatch(setServiceId(serviceId));
// Update the initial state with the correct PC for the entrypoint
const pc = parseInt(manualPc, 10) || 0;
modifiedProgram = {
...loadedProgram,
initial: {
...loadedProgram.initial,
pc,
},
};
}
await debuggerActions.handleProgramLoad(modifiedProgram);
if (traceContent) {
dispatch(setHostCallsTrace(traceContent));
}
// Save SPI configuration to localStorage after successful load
if (loadedProgram?.spiProgram) {
saveSpiConfig({
entrypoint: selectedEntrypoint,
refineParams,
accumulateParams,
isAuthorizedParams,
isManualMode,
manualPc,
encodedArgs: encodedSpiArgs,
});
}
setIsDialogOpen?.(false);
navigate("/", { replace: true });
} catch (error) {
if (error instanceof Error || isSerializedError(error)) {
setError(error.message);
} else {
setError("Unknown error occurred");
}
}
},
[
dispatch,
programLoad,
debuggerActions,
setIsDialogOpen,
navigate,
encodedSpiArgs,
manualPc,
selectedEntrypoint,
refineParams,
accumulateParams,
isAuthorizedParams,
isManualMode,
traceContent,
],
);
const hasTraceWithEntrypoint = traceContent !== null;
const handleNextStep = () => {
if (currentStep === "upload" && isSpiProgram && !hasTraceWithEntrypoint) {
setCurrentStep("entrypoint");
} else {
handleLoad();
}
};
const handleBackStep = () => {
setCurrentStep("upload");
};
return (
<div className="flex flex-col w-full h-full bg-card pb-3 min-w-[50vw]">
<p className="sm:mb-4 bg-brand-dark dark:bg-brand/65 text-white text-xs font-light px-3 pt-3 pb-2">
{currentStep === "upload"
? "Start with an example program or upload your file"
: "Select entrypoint for SPI program"}
</p>
<div className="flex flex-col px-7 pt-[30px] h-full overflow-auto">
{currentStep === "upload" && (
<>
<Examples
disabled={isLoading}
onProgramLoad={(val) => {
handleFileUpload(val);
if (val.spiProgram === null) {
handleLoad(val);
}
}}
/>
<div className="my-10">
<ProgramFileUpload
onFileUpload={handleFileUpload}
isError={error !== undefined}
setError={setError}
disabled={isLoading}
/>
</div>
{error && (
<p className="flex items-top text-destructive-foreground text-[11px] whitespace-pre-line">
<TriangleAlert className="mr-2" height="18px" /> {error}
</p>
)}
{!error && programLoad && (
<div className="text-xs">
<div className="mt-2 flex justify-between items-center">
<span className="block text-xs font-bold min-w-[150px]">Detected:</span>
<code className="flex-1 ml-2"> {programLoad.kind}</code>
</div>
<div className="mt-2 flex justify-between items-center">
<span className="block text-xs font-bold min-w-[150px]">Name:</span>
<code className="flex-1 ml-2">{programLoad.name}</code>
</div>
<div className="mt-2 flex items-center">
<span className="block text-xs font-bold min-w-[150px]">Initial state:</span>
<details open={false} className="flex-1 ml-2">
<summary>view</summary>
<pre className="max-h-[300px] overflow-auto">{JSON.stringify(programLoad.initial, null, 2)}</pre>
</details>
</div>
{traceSummary && (
<div className="mt-4 pt-4 border-t">
<div className="flex justify-between items-center">
<span className="block text-xs font-bold min-w-[150px]">Host Call Trace:</span>
<span className="flex items-center gap-1 text-green-600 dark:text-green-400">
<FileText className="w-3 h-3" />
{traceSummary.hostCallCount} host call{traceSummary.hostCallCount !== 1 ? "s" : ""} included
</span>
</div>
</div>
)}
</div>
)}
{!error && !programLoad && <Links />}
</>
)}
{currentStep === "entrypoint" && (
<div className="min-h-[400px]">
<EntrypointSelector
selectedEntrypoint={selectedEntrypoint}
onEntrypointChange={setSelectedEntrypoint}
refineParams={refineParams}
onRefineParamsChange={setRefineParams}
accumulateParams={accumulateParams}
manualPc={manualPc}
onManualPcChange={setManualPc}
onAccumulateParamsChange={setAccumulateParams}
isAuthorizedParams={isAuthorizedParams}
onIsAuthorizedParamsChange={setIsAuthorizedParams}
encodedSpiArgs={encodedSpiArgs}
onEncodedSpiArgsChange={setEncodedSpiArgs}
encodingError={encodingError}
isManualMode={isManualMode}
onIsManualModeChange={setIsManualMode}
/>
</div>
)}
</div>
<div className="px-5 mt-[30px]">
<Separator />
</div>
<div className="m-6 mb-7 flex justify-between">
{currentStep === "entrypoint" && (
<Button className="mt-3 min-w-[92px]" type="button" variant="outline" onClick={handleBackStep}>
Back
</Button>
)}
<div className="flex-1" />
<Button
className="mt-3 min-w-[92px]"
id="load-button"
data-testid="load-button"
type="button"
disabled={!isProgramLoaded || isLoading}
onClick={currentStep === "entrypoint" ? () => handleLoad() : handleNextStep}
>
{currentStep === "entrypoint" || !isSpiProgram || hasTraceWithEntrypoint ? "Load" : "Next"}
</Button>
</div>
</div>
);
};