Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache verification key in CLI [Do Not Merge Until New Published Version of SnarkyJS is Added] #233

Merged
merged 24 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cf9b191
feat: parse contract digests from build.json
ymekuria Jul 7, 2022
ad44a6a
refactor: rename to cachedigests
ymekuria Jul 7, 2022
99deec2
feat: add current digest to cache and complie contract if not in dige…
ymekuria Jul 7, 2022
f154871
feat: add cache.json file to template
ymekuria Jul 12, 2022
e754e30
refactor: move cache.json to template root
ymekuria Jul 13, 2022
077c9e0
feat: intialize cache when empty
ymekuria Jul 13, 2022
ba49925
feat: use cached verification key if current digest is equal to cache…
ymekuria Jul 13, 2022
2ec530e
feat: compile contract and update cache if digest changes
ymekuria Jul 13, 2022
aea2207
refactor: remove cache.json file from template
ymekuria Jul 13, 2022
8e91346
feat: save cache.json when build dir is emptied
ymekuria Jul 13, 2022
2ccff4b
refactor: add comments
ymekuria Jul 13, 2022
f2807bd
refactor: remove redundant read and write from cach.json
ymekuria Jul 14, 2022
1a99003
refactor: replace let with const
ymekuria Jul 14, 2022
68ab811
refactor: add optional chaining to remove cache initialization block
ymekuria Jul 14, 2022
592946d
feat: move cache log feedback outside of callback
ymekuria Jul 14, 2022
359fc73
fix: ensure no error when cache.json or build folder does not exist
ymekuria Jul 18, 2022
f225c53
feat: update tsconfig
ymekuria Jul 7, 2022
a67f9e3
Update template and examples with preconditions and new CircuitValue …
ymekuria Aug 11, 2022
c92b159
Fix deploying to QANet with zk deploy command (#243)
ymekuria Aug 12, 2022
a69bec2
fix: intialize cache
ymekuria Aug 12, 2022
e51d7fe
remove non-fee-payer nonce increment
mitschabaude Aug 12, 2022
0876ed0
0.4.13
mitschabaude Aug 12, 2022
06bfe24
chore: bump version to 0.4.14
ymekuria Aug 12, 2022
ccce41d
Merge branch 'main' into feature/verification-key-cache
ymekuria Aug 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zkapp-cli",
"version": "0.4.13",
"version": "0.4.14",
"description": "CLI to create a zkApp (\"zero-knowledge app\") for Mina Protocol.",
"keywords": [
"cli",
Expand Down
50 changes: 47 additions & 3 deletions src/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,21 @@ async function deploy({ alias, yes }) {
}

await step('Build project', async () => {
// store cache to add after build directory is emptied
let cache;
try {
cache = fs.readJsonSync(`${DIR}/build/cache.json`);
} catch (err) {
if (err.code === 'ENOENT') {
cache = {};
} else {
console.error(err);
}
}

fs.emptyDirSync(`${DIR}/build`); // ensure old artifacts don't remain
fs.outputJsonSync(`${DIR}/build/cache.json`, cache, { spaces: 2 });

await sh('npm run build --silent');
});

Expand Down Expand Up @@ -240,14 +254,44 @@ async function deploy({ alias, yes }) {
let zkAppPrivateKey = PrivateKey.fromBase58(privateKey); // The private key of the zkApp
let zkAppAddress = zkAppPrivateKey.toPublicKey(); // The public key of the zkApp

const verificationKey = await step(
const { verificationKey, isCached } = await step(
'Generate verification key (takes 10-30 sec)',
async () => {
let { verificationKey } = await zkApp.compile(zkAppAddress);
return verificationKey;
let cache = fs.readJsonSync(`${DIR}/build/cache.json`);
// compute a hash of the contract's circuit to determine if 'zkapp.compile' should re-run or cached verfification key can be used
let currentDigest = await zkApp.digest(zkAppAddress);

// initialize cache if 'zk deploy' is run the first time on the contract
if (!cache[contractName]) {
cache[contractName] = { digest: '', verificationKey: '' };
}

if (cache[contractName]?.digest === currentDigest) {
return {
verificationKey: cache[contractName].verificationKey,
isCached: true,
};
} else {
const { verificationKey } = await zkApp.compile(zkAppAddress);
// update cache with new verification key and currrentDigest
cache[contractName].verificationKey = verificationKey;
cache[contractName].digest = currentDigest;

fs.writeJsonSync(`${DIR}/build/cache.json`, cache, {
spaces: 2,
});

return { verificationKey, isCached: false };
}
}
);

// Can't include the log message inside the callback b/c it will break
// the step formatting.
if (isCached) {
log(' Using the cached verification key');
}

let { fee } = config.networks[alias];
if (!fee) {
log(
Expand Down
1 change: 1 addition & 0 deletions templates/project-ts/build/cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}