|
| 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); |
0 commit comments