-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services use DOM lib, make keys pathable, remove 'internal message' f…
…rom types
- Loading branch information
1 parent
ad64968
commit 3027e44
Showing
14 changed files
with
261 additions
and
374 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const keyPaths = { | ||
delegatorVote: new URL('../keys/delegator_vote_pk.bin', import.meta.url), | ||
output: new URL('../keys/output_pk.bin', import.meta.url), | ||
spend: new URL('../keys/spend_pk.bin', import.meta.url), | ||
swap: new URL('../keys/swap_pk.bin', import.meta.url), | ||
swapClaim: new URL('../keys/swapclaim_pk.bin', import.meta.url), | ||
undelegateClaim: new URL('../keys/convert_pk.bin', import.meta.url), | ||
} as const; | ||
|
||
export default keyPaths; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"exactOptionalPropertyTypes": false, | ||
"composite": true, | ||
"module": "Node16", | ||
"outDir": "dist", | ||
"preserveWatchOutput": true, | ||
"rootDir": "src", | ||
"target": "ESNext" | ||
}, | ||
"extends": "@tsconfig/strictest/tsconfig.json", | ||
"include": ["src"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
91 changes: 91 additions & 0 deletions
91
packages/services/src/view-service/util/build-action-worker.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { | ||
ActionPlan, | ||
TransactionPlan, | ||
WitnessData, | ||
} from '@penumbra-zone/protobuf/penumbra/core/transaction/v1/transaction_pb'; | ||
import type { JsonObject, JsonValue } from '@bufbuild/protobuf'; | ||
import { FullViewingKey } from '@penumbra-zone/protobuf/penumbra/core/keys/v1/keys_pb'; | ||
import keyPaths from '@penumbra-zone/keys'; | ||
|
||
export interface WorkerBuildAction { | ||
transactionPlan: JsonObject; | ||
witness: JsonObject; | ||
fullViewingKey: JsonObject; | ||
actionPlanIndex: number; | ||
} | ||
|
||
interface ExecuteWorkerParams { | ||
transactionPlan: TransactionPlan; | ||
witness: WitnessData; | ||
fullViewingKey: FullViewingKey; | ||
actionPlanIndex: number; | ||
} | ||
|
||
// necessary to propagate errors that occur in promises | ||
// see: https://stackoverflow.com/questions/39992417/how-to-bubble-a-web-worker-error-in-a-promise-via-worker-onerror | ||
globalThis.addEventListener( | ||
'unhandledrejection', | ||
event => { | ||
// the event object has two special properties: | ||
// event.promise - the promise that generated the error | ||
// event.reason - the unhandled error object | ||
throw event.reason; | ||
}, | ||
{ once: true }, | ||
); | ||
|
||
const workerListener = ({ data }: MessageEvent<WorkerBuildAction>) => { | ||
console.debug('workerListener', data); | ||
const { | ||
transactionPlan: transactionPlanJson, | ||
witness: witnessJson, | ||
fullViewingKey: fullViewingKeyJson, | ||
actionPlanIndex, | ||
} = data; | ||
|
||
// Deserialize payload | ||
const transactionPlan = TransactionPlan.fromJson(transactionPlanJson); | ||
const witness = WitnessData.fromJson(witnessJson); | ||
const fullViewingKey = FullViewingKey.fromJson(fullViewingKeyJson); | ||
|
||
void executeWorker({ transactionPlan, witness, fullViewingKey, actionPlanIndex }).then( | ||
jsonAction => { | ||
console.debug('built action', jsonAction); | ||
globalThis.postMessage(jsonAction); | ||
}, | ||
); | ||
}; | ||
|
||
globalThis.addEventListener('message', workerListener, { once: true }); | ||
|
||
const executeWorker = async ({ | ||
transactionPlan, | ||
witness, | ||
fullViewingKey, | ||
actionPlanIndex, | ||
}: ExecuteWorkerParams): Promise<JsonValue> => { | ||
console.debug('executeWorker', transactionPlan, witness, fullViewingKey, actionPlanIndex); | ||
// Dynamically load wasm module | ||
const penumbraWasmModule = await import('@penumbra-zone/wasm/build'); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- asdf | ||
const actionType = transactionPlan.actions[actionPlanIndex]!.action.case!; | ||
|
||
type KeyPaths = Partial<Record<NonNullable<ActionPlan['action']['case']>, URL>>; | ||
const keyPath = (keyPaths as KeyPaths)[actionType]; | ||
|
||
console.debug('build-action-worker using keyPath', keyPath); | ||
|
||
// Build action according to specification in `TransactionPlan` | ||
const action = await penumbraWasmModule.buildActionParallel( | ||
transactionPlan, | ||
witness, | ||
fullViewingKey, | ||
actionPlanIndex, | ||
keyPath?.href, | ||
); | ||
|
||
console.debug('built action', action.toJson()); | ||
|
||
return action.toJson(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.