@nori-zk/proof-conversion
Version:
Verifying zkVM proofs inside o1js circuits, to generate Mina compatible proof
218 lines • 12.5 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _ComputationalPlanExecutor_instances, _ComputationalPlanExecutor_processPool, _ComputationalPlanExecutor_logger, _ComputationalPlanExecutor_planExecutionId, _ComputationalPlanExecutor_activePlans, _ComputationalPlanExecutor_performMainThreadStage, _ComputationalPlanExecutor_performSerialStage, _ComputationalPlanExecutor_performParallelStage, _ComputationalPlanExecutor_stagePrerequisiteIndicatesSkip, _ComputationalPlanExecutor_executeComputationalPlanInner;
import { Logger } from 'esm-iso-logger';
import { PlatformFeatureDetectionComputationalPlan, } from './plans/platform/index.js';
import { ProcessPool } from './processPool.js';
function applyNumaOptimization(stageProcessCommands, state) {
return stageProcessCommands.map((processCmd, idx) => {
const numaNode = idx % state.numaNodes;
const { cmd, args } = processCmd;
// replace the cmd with numactl and move the args one level down
const newCmd = 'numactl';
const newArgs = [
`--cpunodebind=${numaNode}`,
`--membind=${numaNode}`,
cmd,
...args,
];
const newProcessCommand = { ...processCmd, cmd: newCmd, args: newArgs };
if (newProcessCommand.printableArgs) {
newProcessCommand.printableArgs = [
0,
1,
2,
...newProcessCommand.printableArgs.map((idx) => idx + 3),
];
}
return newProcessCommand;
});
}
let executorId = 0;
export class ComputationalPlanExecutor {
async execute(plan, input) {
// Define platform features
const plaformPlan = new PlatformFeatureDetectionComputationalPlan();
// Execute platform plan
const platformFeatures = await __classPrivateFieldGet(this, _ComputationalPlanExecutor_instances, "m", _ComputationalPlanExecutor_executeComputationalPlanInner).call(this, {}, plaformPlan, input);
// Execute the given plan
return await __classPrivateFieldGet(this, _ComputationalPlanExecutor_instances, "m", _ComputationalPlanExecutor_executeComputationalPlanInner).call(this, platformFeatures, plan, input);
}
async terminate() {
let resolver = Promise.resolve();
__classPrivateFieldGet(this, _ComputationalPlanExecutor_activePlans, "f").forEach((activePlan) => {
resolver = resolver.then(async () => {
if (activePlan.plan.finally)
await activePlan.plan.finally(activePlan.state);
});
});
return resolver;
}
workerFreeStatus() {
return __classPrivateFieldGet(this, _ComputationalPlanExecutor_processPool, "f").workerFreeStatus();
}
constructor(poolSize) {
_ComputationalPlanExecutor_instances.add(this);
_ComputationalPlanExecutor_processPool.set(this, void 0);
_ComputationalPlanExecutor_logger.set(this, void 0);
_ComputationalPlanExecutor_planExecutionId.set(this, 0);
_ComputationalPlanExecutor_activePlans.set(this, new Map());
executorId++;
__classPrivateFieldSet(this, _ComputationalPlanExecutor_processPool, new ProcessPool(poolSize), "f");
__classPrivateFieldSet(this, _ComputationalPlanExecutor_logger, new Logger(`ComputationalPlanExecutor${executorId}`), "f");
}
}
_ComputationalPlanExecutor_processPool = new WeakMap(), _ComputationalPlanExecutor_logger = new WeakMap(), _ComputationalPlanExecutor_planExecutionId = new WeakMap(), _ComputationalPlanExecutor_activePlans = new WeakMap(), _ComputationalPlanExecutor_instances = new WeakSet(), _ComputationalPlanExecutor_performMainThreadStage = async function _ComputationalPlanExecutor_performMainThreadStage(stage, state) {
let result = stage.execute(state);
if (result instanceof Promise) {
await result;
}
}, _ComputationalPlanExecutor_performSerialStage = async function _ComputationalPlanExecutor_performSerialStage(stage, state) {
let processCmd = stage.processCmd;
if (processCmd instanceof Function) {
processCmd = processCmd(state);
}
if (stage.callback) {
const cmdResult = await __classPrivateFieldGet(this, _ComputationalPlanExecutor_processPool, "f")
.runCommand(processCmd)
.catch((err) => err);
const stageCallback = stage.callback(state, cmdResult);
if (stageCallback instanceof Promise) {
await stageCallback;
}
}
else
await __classPrivateFieldGet(this, _ComputationalPlanExecutor_processPool, "f")
.runCommand(processCmd)
.catch((err) => {
throw err.error;
});
}, _ComputationalPlanExecutor_performParallelStage = async function _ComputationalPlanExecutor_performParallelStage(stage, state) {
let processCmds = stage.processCmds;
if (processCmds instanceof Function) {
processCmds = processCmds(state);
}
// If this is numa optimised then we should modify our commands
let modifiedCommands = processCmds;
if (stage.numaOptimized && state.numaNodes) {
modifiedCommands = applyNumaOptimization(modifiedCommands, state);
}
if (stage.callback) {
const cmdResults = await Promise.all(modifiedCommands.map((cmd) => __classPrivateFieldGet(this, _ComputationalPlanExecutor_processPool, "f")
.runCommand(cmd)
.catch((err) => err)));
const stageCallback = stage.callback(state, cmdResults);
if (stageCallback instanceof Promise) {
await stageCallback;
}
}
else
await Promise.all(modifiedCommands.map((cmd) => __classPrivateFieldGet(this, _ComputationalPlanExecutor_processPool, "f").runCommand(cmd).catch((err) => {
throw err.error;
})));
}, _ComputationalPlanExecutor_stagePrerequisiteIndicatesSkip = async function _ComputationalPlanExecutor_stagePrerequisiteIndicatesSkip(stage, state) {
const prerequisiteCheck = stage.prerequisite;
if (prerequisiteCheck) {
let prerequisiteResult;
prerequisiteResult = prerequisiteCheck(state);
if (prerequisiteResult instanceof Promise) {
prerequisiteResult = await prerequisiteResult;
}
if (!prerequisiteResult) {
return true;
}
}
return false;
}, _ComputationalPlanExecutor_executeComputationalPlanInner = async function _ComputationalPlanExecutor_executeComputationalPlanInner(state = {}, plan, input) {
var _a;
__classPrivateFieldSet(this, _ComputationalPlanExecutor_planExecutionId, (_a = __classPrivateFieldGet(this, _ComputationalPlanExecutor_planExecutionId, "f"), _a++, _a), "f");
const planId = __classPrivateFieldGet(this, _ComputationalPlanExecutor_planExecutionId, "f");
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").log(`Executing computational plan '${plan.name}'.`);
let error = undefined;
const startTime = Date.now();
try {
__classPrivateFieldGet(this, _ComputationalPlanExecutor_activePlans, "f").set(planId, {
plan: plan,
state: state,
});
if (plan.init) {
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").log(`Calling the 'init' function of the '${plan.name}' computational plan.`);
await plan.init(state, input);
}
let stageStartTime;
for (let stage_idx = 0; stage_idx < plan.stages.length; stage_idx++) {
const stage = plan.stages[stage_idx];
stageStartTime = Date.now();
const prerequisiteCheck = stage.prerequisite;
if (prerequisiteCheck) {
let prerequisiteResult;
prerequisiteResult = prerequisiteCheck(state);
if (prerequisiteResult instanceof Promise) {
prerequisiteResult = await prerequisiteResult;
}
if (!prerequisiteResult) {
continue;
}
}
if (await __classPrivateFieldGet(this, _ComputationalPlanExecutor_instances, "m", _ComputationalPlanExecutor_stagePrerequisiteIndicatesSkip).call(this, stage, state))
continue;
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").log(`[${stage.type}] Executing stage ${stage_idx} '${stage.name}' of the '${plan.name}' computational plan.`);
switch (stage.type) {
case 'main-thread': {
await __classPrivateFieldGet(this, _ComputationalPlanExecutor_instances, "m", _ComputationalPlanExecutor_performMainThreadStage).call(this, stage, state);
break;
}
case 'serial-cmd': {
await __classPrivateFieldGet(this, _ComputationalPlanExecutor_instances, "m", _ComputationalPlanExecutor_performSerialStage).call(this, stage, state);
break;
}
case 'parallel-cmd': {
await __classPrivateFieldGet(this, _ComputationalPlanExecutor_instances, "m", _ComputationalPlanExecutor_performParallelStage).call(this, stage, state);
break;
}
default: {
throw new Error(`Unknown stage type '${stage.type}' aborting.`);
}
}
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").log(`[${stage.type}] Stage ${stage_idx} '${stage.name}' of the '${plan.name}' computational plan completed in ${(Date.now() - stageStartTime) / 1000} seconds.`);
}
// Run then
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").log(`Calling the 'then' function of the '${plan.name}' computational plan.`);
const result = await plan.then(state);
return result;
}
catch (err) {
error = err;
throw error;
}
finally {
const elapsed = (Date.now() - startTime) / 1000;
let finallyWorked = true;
__classPrivateFieldGet(this, _ComputationalPlanExecutor_activePlans, "f").delete(planId);
if (error)
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").error(`Execution of the '${plan.name}' computational plan failed after ${elapsed} seconds: ${error}`);
if (plan.finally) {
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").info(`Calling the 'finally' function of the '${plan.name}' computational plan.`);
await plan.finally(state).catch((err) => {
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").error(`An error occured when calling the 'finally' function of the '${plan.name}' computational plan. ${err}`);
finallyWorked = false;
});
}
if (!error) {
if (finallyWorked)
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").log(`Execution of the computational plan '${plan.name}' completed successfully in ${elapsed} seconds.`);
else
__classPrivateFieldGet(this, _ComputationalPlanExecutor_logger, "f").warn(`Computational plan '${plan.name}' completed successfully in ${elapsed} seconds. However the 'finally' function had an error. Some cache cleanup logic may not have completed and left stray files on your system.'`);
}
}
};
//# sourceMappingURL=executor.js.map