Skip to content

Commit 012e9f9

Browse files
Pauaniamalwaysuncomfortable
authored andcommitted
Adding in dynamic loadNetwork function
1 parent e210162 commit 012e9f9

File tree

6 files changed

+14666
-10001
lines changed

6 files changed

+14666
-10001
lines changed

.circleci/config.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ jobs:
9393
command: |
9494
yarn start
9595
96+
e2e-dynamic:
97+
executor: rust-node
98+
steps:
99+
- setup-sdk
100+
- run:
101+
working_directory: e2e/dynamic
102+
command: |
103+
yarn start
104+
96105
create-leo-app-cli:
97106
executor: rust-node
98107
steps:
@@ -181,6 +190,9 @@ workflows:
181190
- e2e-mainnet:
182191
requires:
183192
- sdk
193+
- e2e-dynamic:
194+
requires:
195+
- sdk
184196
- create-leo-app-cli:
185197
requires:
186198
- sdk

e2e/dynamic/index.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { loadNetwork } from "@provablehq/sdk/dynamic.js";
2+
3+
const mainnet = await loadNetwork("mainnet");
4+
5+
await mainnet.initThreadPool();
6+
7+
const programName = "hello_hello.aleo"
8+
9+
const hello_hello_program =`
10+
program ${programName};
11+
12+
function hello:
13+
input r0 as u32.public;
14+
input r1 as u32.private;
15+
add r0 r1 into r2;
16+
output r2 as u32.private;`
17+
18+
async function localProgramExecution(program, programName, aleoFunction, inputs) {
19+
const programManager = new mainnet.ProgramManager();
20+
21+
// Create a temporary account for the execution of the program
22+
const account = new mainnet.Account();
23+
programManager.setAccount(account);
24+
25+
// Create a key provider in order to re-use the same key for each execution
26+
const keyProvider = new mainnet.AleoKeyProvider();
27+
keyProvider.useCache(true);
28+
programManager.setKeyProvider(keyProvider);
29+
30+
// Pre-synthesize the program keys and then cache them in memory using key provider
31+
const keyPair = await programManager.synthesizeKeys(hello_hello_program, aleoFunction, inputs);
32+
programManager.keyProvider.cacheKeys(`${programName}:${aleoFunction}`, keyPair);
33+
34+
// Specify parameters for the key provider to use search for program keys. In particular specify the cache key
35+
// that was used to cache the keys in the previous step.
36+
const keyProviderParams = new mainnet.AleoKeyProviderParams({cacheKey: `${programName}:${aleoFunction}`});
37+
38+
// Execute once using the key provider params defined above. This will use the cached proving keys and make
39+
// execution significantly faster.
40+
let executionResponse = await programManager.run(
41+
program,
42+
aleoFunction,
43+
inputs,
44+
true,
45+
undefined,
46+
keyProviderParams,
47+
);
48+
console.log("hello_hello/hello executed - result:", executionResponse.getOutputs());
49+
50+
// Verify the execution using the verifying key that was generated earlier.
51+
const blockHeight = 9_000_000;
52+
if (programManager.verifyExecution(executionResponse, blockHeight)) {
53+
console.log("hello_hello/hello execution verified!");
54+
} else {
55+
throw("Execution failed verification!");
56+
}
57+
}
58+
59+
const start = Date.now();
60+
console.log("Starting execute!");
61+
await localProgramExecution(hello_hello_program, programName, "hello", ["5u32", "5u32"]);
62+
console.log("Execute finished!", Date.now() - start);

e2e/dynamic/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "e2e-dynamic",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"dependencies": {
10+
"@provablehq/sdk": "^0.9.10-testnet-rc"
11+
}
12+
}

sdk/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
"./mainnet.js": {
2222
"node": "./dist/mainnet/node.js",
2323
"default": "./dist/mainnet/browser.js"
24+
},
25+
"./dynamic.js": {
26+
"node": "./dist/dynamic/node.js",
27+
"default": "./dist/dynamic/browser.js"
2428
}
2529
},
2630
"files": [

sdk/rollup.config.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as $fs from "node:fs/promises";
2+
import * as $path from "node:path";
13
import typescript from "rollup-plugin-typescript2";
24
import replace from "@rollup/plugin-replace";
35
import $package from "./package.json" with { type: "json" };
@@ -7,7 +9,12 @@ const networks = [
79
"mainnet",
810
];
911

10-
export default networks.map((network) => {
12+
const runtimes = [
13+
"browser",
14+
"node",
15+
];
16+
17+
function buildNetwork(network) {
1118
return {
1219
input: {
1320
"node-polyfill": "./src/node-polyfill.ts",
@@ -49,4 +56,36 @@ export default networks.map((network) => {
4956
}),
5057
],
5158
};
52-
});
59+
}
60+
61+
async function buildRuntimes() {
62+
await Promise.all(runtimes.map(async (runtime) => {
63+
await $fs.mkdir(`dist/dynamic`, { recursive: true });
64+
65+
const loading = networks.map((network) => `"${network}": () => import("../${network}/${runtime}.js"),`).join("\n ");
66+
67+
const typings = networks.map((network) => `"${network}": typeof import("../${network}/${runtime}"),`).join("\n ");
68+
69+
await $fs.writeFile(`dist/dynamic/${runtime}.js`, `const networks = {
70+
${loading}
71+
};
72+
73+
export function loadNetwork(name) {
74+
return networks[name]();
75+
}
76+
`);
77+
78+
await $fs.writeFile(`dist/dynamic/${runtime}.d.ts`, `interface Networks {
79+
${typings}
80+
}
81+
82+
export { Networks };
83+
84+
export declare function loadNetwork<Key extends keyof Networks>(name: Key): Promise<Networks[Key]>;
85+
`);
86+
}));
87+
}
88+
89+
await buildRuntimes();
90+
91+
export default networks.map(buildNetwork);

0 commit comments

Comments
 (0)