diff --git a/src/lib/deploy.js b/src/lib/deploy.js index 5c6cc17ce80929826c37441284269a554c40cc04..49096abe4bd5e9c8e1462416a58a563317194891 100644 --- a/src/lib/deploy.js +++ b/src/lib/deploy.js @@ -556,22 +556,45 @@ async function generateVerificationKey( process.exit(1); } } - // import and compile ZkProgram if smart contract to deploy verifies it - if (zkProgramNameArg) { - zkProgram = await getZkProgram(projectRoot, zkProgramNameArg); - const currentZkProgramDigest = await zkProgram.digest(); - await zkProgram.compile(); - const result = await zkApp.compile(zkAppAddress); - verificationKey = result.verificationKey; + const MAX_ITERATIONS = 5; - // Add ZkProgram name to cache of the smart contract that verifies it - cache[contractName].zkProgram = zkProgramNameArg; - // Initialize zkprogram cache if not defined - cache[zkProgramNameArg] = cache[zkProgramNameArg] ?? {}; - cache[zkProgramNameArg].digest = currentZkProgramDigest; + // import and compile ZkProgram if smart contract to deploy verifies it + for (let i = 0; i < MAX_ITERATIONS; i++) { + if (zkProgramNameArg) { + zkProgram = await getZkProgram(projectRoot, zkProgramNameArg); + const currentZkProgramDigest = await zkProgram.digest(); + await zkProgram.compile(); + + // Add ZkProgram name to cache of the smart contract that verifies it + cache[contractName].zkProgram = zkProgramNameArg; + // Initialize zkprogram cache if not defined + cache[zkProgramNameArg] = cache[zkProgramNameArg] ?? {}; + cache[zkProgramNameArg].digest = currentZkProgramDigest; + + zkProgramNameArg = null; + + try { + // attempt to compile the zkApp + const result = await zkApp.compile(zkAppAddress); + + verificationKey = result.verificationKey; + } catch (error) { + // if the zkApp compilation fails because the ZkProgram compilation output that the smart contract verifies is not found, + // the error message is parsed to get the ZkProgram name argument. + if (error.message.includes(`but we cannot find compilation output for`)) { + zkProgramNameArg = getZkProgramNameArg(error.message); + } else { + console.error(error); + process.exit(1); + } + } + } else { + break; + } } + // update cache with new smart contract verification key and currrentDigest cache[contractName].verificationKey = verificationKey; cache[contractName].digest = currentDigest;