Skip to content

Commit

Permalink
Fixing bad merge with worker.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Pauan committed Sep 24, 2024
1 parent f351159 commit db0100e
Showing 1 changed file with 85 additions and 80 deletions.
165 changes: 85 additions & 80 deletions sdk/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ keyProvider.useCache(true);
let lastLocalProgram: string = "";

export interface WorkerAPI {
executeOffline: (
run: (
localProgram: string,
aleoFunction: string,
inputs: string[],
Expand All @@ -27,7 +27,7 @@ export interface WorkerAPI {

getPrivateKey: () => Promise<PrivateKey>;
}
async function executeOffline(
async function run(
localProgram: string,
aleoFunction: string,
inputs: string[],
Expand All @@ -37,100 +37,105 @@ async function executeOffline(
console.log("Web worker: Executing function locally...");
const startTime = performance.now();

// Ensure the program is valid and that it contains the function specified
let program;

try {
// Ensure the program is valid and that it contains the function specified
const program = programManager.createProgramFromSource(localProgram);
if (program instanceof Error) {
throw new Error("Error creating program from source");
}
const program_id = program.id();
if (!program.hasFunction(aleoFunction)) {
throw new Error(`Program ${program_id} does not contain function ${aleoFunction}`);
}
const cacheKey = `${program_id}:${aleoFunction}`;

// Get the program imports
const imports = await programManager.networkClient.getProgramImports(
program = programManager.createProgramFromSource(localProgram);
} catch (e) {
throw new Error("Error creating program from source");
}

const program_id = program.id();
if (!program.hasFunction(aleoFunction)) {
throw new Error(`Program ${program_id} does not contain function ${aleoFunction}`);
}
const cacheKey = `${program_id}:${aleoFunction}`;


// Get the program imports
let imports;

try {
imports = await programManager.networkClient.getProgramImports(
localProgram
);
} catch (e) {
throw new Error("Error getting program imports");
}

if (imports instanceof Error) {
throw new Error("Error getting program imports");
}
// Get the proving and verifying keys for the function
if (lastLocalProgram !== localProgram) {
const keys = <FunctionKeyPair>await programManager.synthesizeKeys(
localProgram,
aleoFunction,
inputs,
PrivateKey.from_string(privateKey)
);
programManager.keyProvider.cacheKeys(cacheKey, keys);
lastLocalProgram = localProgram;
}

// Pass the cache key to the execute function
const keyParams = new AleoKeyProviderParams({
cacheKey: cacheKey,
});

// Execute the function locally
const response = await programManager.run(
// Get the proving and verifying keys for the function
if (lastLocalProgram !== localProgram) {
const keys = <FunctionKeyPair>await programManager.synthesizeKeys(
localProgram,
aleoFunction,
inputs,
proveExecution,
imports,
keyParams,
undefined,
undefined,
PrivateKey.from_string(privateKey),
PrivateKey.from_string(privateKey)
);
programManager.keyProvider.cacheKeys(cacheKey, keys);
lastLocalProgram = localProgram;
}

// Pass the cache key to the execute function
const keyParams = new AleoKeyProviderParams({
cacheKey: cacheKey,
});

// Execute the function locally
const response = await programManager.run(
localProgram,
aleoFunction,
inputs,
proveExecution,
imports,
keyParams,
undefined,
undefined,
PrivateKey.from_string(privateKey),
);

// Return the outputs to the main thread
console.log(
`Web worker: Local execution completed in ${
performance.now() - startTime
} ms`
);
const outputs = response.getOutputs();
const execution = response.getExecution();
let executionString = "";

let keys;

try {
keys = keyProvider.getKeys(cacheKey);
} catch (e) {
throw new Error("Could not get verifying key");
}

const verifyingKey = keys[1];

// Return the outputs to the main thread
console.log(
`Web worker: Local execution completed in ${
performance.now() - startTime
} ms`
if (execution) {
verifyFunctionExecution(
execution,
verifyingKey,
program,
"hello"
);
const outputs = response.getOutputs();
const execution = response.getExecution();
let executionString = "";

const keys = keyProvider.getKeys(cacheKey);

if (keys instanceof Error) {
throw new Error("Could not get verifying key");
}

const verifyingKey = keys[1];

if (execution) {
verifyFunctionExecution(
execution,
verifyingKey,
program,
"hello"
);
executionString = execution.toString();
console.log("Execution verified successfully: " + execution);
} else {
executionString = "";
}

console.log(`Function execution response: ${outputs}`);

return { outputs: outputs, execution: executionString };
} catch (error) {
console.error(error);
return error ? error.toString() : "Unknown error";
executionString = execution.toString();
console.log("Execution verified successfully: " + execution);
} else {
executionString = "";
}

console.log(`Function execution response: ${outputs}`);

return { outputs: outputs, execution: executionString };
}

async function getPrivateKey() {
const privateKey = new PrivateKey();
return privateKey.to_string();
}

const workerAPI = { executeOffline, getPrivateKey };
const workerAPI = { run, getPrivateKey };
expose(workerAPI);

0 comments on commit db0100e

Please sign in to comment.