Skip to content

Commit 8f96c44

Browse files
author
adamczykm
committed
Trying provers in webworkers efforts #1
1 parent 34c5ac8 commit 8f96c44

File tree

18 files changed

+387
-133
lines changed

18 files changed

+387
-133
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/// <reference types="next" />
22
/// <reference types="next/image-types/global" />
3-
/// <reference types="next/navigation-types/compat/navigation" />
43

54
// NOTE: This file should not be edited
65
// see https://nextjs.org/docs/basic-features/typescript for more information.

minauth-demo/minauth-demo-client/next.config.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
/** @type {import('next').NextConfig} */
22
module.exports = {
33
// ... your other next.js configurations ...
4-
4+
async headers() {
5+
return [
6+
{
7+
// Apply these headers to all routes in your application.
8+
source: '/(.*)',
9+
headers: [
10+
{ key: 'Cross-Origin-Opener-Policy', value: 'same-origin' },
11+
{ key: 'Cross-Origin-Embedder-Policy', value: 'require-corp' }
12+
]
13+
}
14+
];
15+
},
516
compiler: {
617
styledComponents: true
718
}

minauth-demo/minauth-demo-client/package-lock.json

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

minauth-demo/minauth-demo-client/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"@rjsf/validator-ajv8": "^5.13.1",
1515
"@types/styled-components": "^5.1.28",
1616
"babel-plugin-styled-components": "^2.1.4",
17+
"comlink": "^4.4.1",
18+
"encoding": "^0.1.13",
19+
"fp-ts": "^2.16.1",
1720
"minauth": "file:../../minauth/dist",
1821
"minauth-merkle-membership-plugin": "file:../../minauth-plugins/minauth-merkle-membership-plugin/dist",
1922
"minauth-simple-preimage-plugin": "file:../../minauth-plugins/minauth-simple-preimage-plugin/dist",

minauth-demo/minauth-demo-client/src/app/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ function LandingPage() {
2323
interact with different proving schemas.
2424
</p>
2525

26-
<Link href="/simplePreimage">Go to Schema 1</Link>
27-
<Link href="/schema2">Go to Schema 2</Link>
26+
<Link href="./provers/simplePreimage">Go to Schema 1</Link>
27+
{/* <Link href="/schema2">Go to Schema 2</Link> */}
2828
{/* Add more links as necessary */}
2929
</div>
3030
);
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use client';
2+
import { TypedWorker } from '@/app/typedworker';
3+
import WorkerComponent, { WorkerState } from '@/app/workercomponent';
4+
import {
5+
ProverCompilationResult,
6+
ProverZkCircuit,
7+
mkProverCompilerWorker
8+
} from '@/workers/provercompilertypes';
9+
import { useState } from 'react';
10+
11+
const mkWorker: () => TypedWorker<
12+
ProverZkCircuit,
13+
ProverCompilationResult
14+
> = () => {
15+
return mkProverCompilerWorker();
16+
};
17+
18+
const workerInput: ProverZkCircuit = { type: 'SimplePreimage' };
19+
20+
const SimplePreimage: React.FC = () => {
21+
const [proverCompilationState, setProverCompilationState] =
22+
useState<WorkerState>({
23+
status: 'idle'
24+
});
25+
26+
function onProverCompilationStateChange(state: WorkerState): void {
27+
setProverCompilationState(state);
28+
}
29+
30+
function verificationKey(): string | undefined {
31+
return proverCompilationState.result;
32+
}
33+
34+
/* const schema: RJSFSchema = {
35+
* title: 'A simple form',
36+
* type: 'object',
37+
* required: ['firstName', 'lastName'],
38+
* properties: {
39+
* firstName: {
40+
* type: 'string',
41+
* title: 'First name'
42+
* },
43+
* lastName: {
44+
* type: 'string',
45+
* title: 'Last name'
46+
* }
47+
* }
48+
* }; */
49+
50+
/* const uiSchema: UiSchema = {
51+
* firstName: {
52+
* 'ui:autofocus': true
53+
* },
54+
* lastName: {
55+
* 'ui:autofocus': true
56+
* }
57+
* };
58+
59+
* const onSubmit = ({ formData }) => {
60+
* console.log('Data submitted:', formData);
61+
* }; */
62+
63+
return (
64+
<div>
65+
<p>Hello World! </p>
66+
<WorkerComponent
67+
mkWorker={mkWorker}
68+
workerInput={workerInput}
69+
onWorkerStateChange={onProverCompilationStateChange}
70+
/>
71+
<p>{verificationKey() ? <div> verificationKey() </div> : <></>} </p>
72+
</div>
73+
);
74+
};
75+
76+
export default SimplePreimage;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
export interface WorkerStatus<R> {
3+
status: 'success' | 'error' | 'processing';
4+
result?: R;
5+
error?: {
6+
message: string;
7+
error?: unknown;
8+
};
9+
}
10+
11+
export interface TypedWorker<Input, Output> {
12+
worker: Worker;
13+
setOnMessage: (
14+
onmessage: (ev: MessageEvent<WorkerStatus<Output>>) => any
15+
) => void;
16+
workerPostMessage: (
17+
message: Input,
18+
options?: StructuredSerializeOptions
19+
) => void;
20+
}
21+
22+
export const mkTypedWorker = <Input, Output>(
23+
url: URL
24+
): TypedWorker<Input, Output> => {
25+
const worker = new Worker(url);
26+
27+
const setOnMessage = (
28+
onmessage: (ev: MessageEvent<WorkerStatus<Output>>) => any
29+
) => {
30+
worker.onmessage = onmessage;
31+
};
32+
console.log('typeof setOnMessage', typeof setOnMessage);
33+
34+
const workerPostMessage = (
35+
message: Input,
36+
options?: StructuredSerializeOptions
37+
): void => {
38+
console.log('posting message to worker', message);
39+
worker.postMessage(JSON.stringify(message), options);
40+
};
41+
return {
42+
worker,
43+
setOnMessage,
44+
workerPostMessage
45+
};
46+
};
47+
48+
export const mkTypedWorker_workaround = <Input, Output>(
49+
worker: Worker
50+
): TypedWorker<Input, Output> => {
51+
const setOnMessage = (
52+
onmessage: (ev: MessageEvent<WorkerStatus<Output>>) => any
53+
) => {
54+
worker.onmessage = onmessage;
55+
};
56+
console.log('typeof setOnMessage', typeof setOnMessage);
57+
58+
const workerPostMessage = (
59+
message: Input,
60+
options?: StructuredSerializeOptions
61+
): void => {
62+
console.log('posting message to worker', message);
63+
worker.postMessage(JSON.stringify(message), options);
64+
};
65+
return {
66+
worker,
67+
setOnMessage,
68+
workerPostMessage
69+
};
70+
};
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React, { useState, useEffect, useRef } from 'react';
2+
import { TypedWorker, WorkerStatus } from './typedworker';
3+
4+
export interface WorkerComponentProps<WorkerInput, WorkerOutput> {
5+
mkWorker: () => TypedWorker<WorkerInput, WorkerOutput>;
6+
workerInput: WorkerInput;
7+
onWorkerStateChange?: (state: WorkerState) => void;
8+
}
9+
10+
export interface WorkerState {
11+
status: 'idle' | 'processing' | 'success' | 'error';
12+
result?: string;
13+
error?: string;
14+
}
15+
export const WorkerComponent = <WorkerInput, WorkerOutput>({
16+
mkWorker,
17+
workerInput,
18+
onWorkerStateChange
19+
}: WorkerComponentProps<WorkerInput, WorkerOutput>) => {
20+
const [workerState, setWorkerState] = useState<WorkerState>({
21+
status: 'idle'
22+
});
23+
const workerInitialized = useRef(false);
24+
25+
useEffect(() => {
26+
const { worker, setOnMessage, workerPostMessage } = mkWorker();
27+
console.log('wtf', typeof workerPostMessage);
28+
29+
// Start the worker immediately with the provided proverType
30+
if (!workerInitialized.current) {
31+
// start the work
32+
console.log('posting worker input', workerInput);
33+
workerPostMessage(workerInput);
34+
workerInitialized.current = true;
35+
}
36+
37+
setOnMessage((event: MessageEvent<WorkerStatus<WorkerOutput>>) => {
38+
console.log('Message received from worker', event.data);
39+
const { status, result, error } = event.data;
40+
41+
if (status === 'success') {
42+
setWorkerStateAndUpdateParent({
43+
status,
44+
result: JSON.stringify(result)
45+
});
46+
} else if (status === 'error') {
47+
setWorkerStateAndUpdateParent({ status, error: error?.message });
48+
} else if (status === 'processing') {
49+
setWorkerStateAndUpdateParent({ status });
50+
}
51+
});
52+
53+
return () => {
54+
worker.terminate();
55+
};
56+
}, []); // Dependency array includes proverType to re-run effect if it changes
57+
// Function to update component state and notify parent
58+
const setWorkerStateAndUpdateParent = (newState: WorkerState) => {
59+
setWorkerState(newState);
60+
onWorkerStateChange?.(newState);
61+
};
62+
const jebieto = JSON.stringify(workerState);
63+
64+
return (
65+
<div>
66+
{workerState.status === 'idle' && <p>Idle...</p>}
67+
{workerState.status === 'processing' && <p>Processing...</p>}
68+
{workerState.status === 'success' && <p>Result: {workerState.result}</p>}
69+
{workerState.status === 'error' && <p>Error: {workerState.error}</p>}
70+
<p>{jebieto}</p>
71+
</div>
72+
);
73+
};
74+
75+
export default WorkerComponent;

minauth-demo/minauth-demo-client/src/pages/_app.tsx

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)