Skip to content

Commit 345ac23

Browse files
authored
Merge pull request #2599 from o1-labs/florian/cache-framework-no-write
Only write during `dump` mode
2 parents d413e06 + 3fe4851 commit 345ac23

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ This project adheres to
1818

1919
## [Unreleased](https://github.com/o1-labs/o1js/compare/3453d1e53...HEAD)
2020

21+
### Internal
22+
23+
- Change cache harness to only allow writes when `dump` mode is active.
24+
https://github.com/o1-labs/o1js/pull/2599
25+
2126
## [2.10.0](https://github.com/o1-labs/o1js/compare/114acff...3453d1e53) - 2025-09-27
2227

2328
### Internal

src/tests/cache/harness.ts

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,81 +5,94 @@ import { Cache, ProofBase, VerificationKey, verify as verifyProof } from 'o1js';
55
import path from 'path';
66
import util from 'util';
77

8-
const exec = util.promisify(execCallback)
8+
const exec = util.promisify(execCallback);
99

1010
export async function tmpdir(): Promise<string> {
11-
const { stdout } = await exec(`mktemp -d`)
12-
return stdout.trim()
11+
const { stdout } = await exec(`mktemp -d`);
12+
return stdout.trim();
1313
}
1414

1515
export async function untar(tarball: string): Promise<string> {
16-
const d = await tmpdir()
17-
await exec(`tar -xzf ${tarball} -C ${d}`)
16+
const d = await tmpdir();
17+
await exec(`tar -xzf ${tarball} -C ${d}`);
1818

19-
return d
19+
return d;
2020
}
2121

22-
export const allowedModes = ["check", "dump"] as const;
23-
export type AllowedMode = typeof allowedModes[number]
22+
export const allowedModes = ['check', 'dump'] as const;
23+
export type AllowedMode = (typeof allowedModes)[number];
2424
export function isAllowedMode(val: string): val is AllowedMode {
25-
return (allowedModes as readonly string[]).includes(val)
25+
return (allowedModes as readonly string[]).includes(val);
2626
}
2727

2828
export async function tar(directory: string, out: string) {
29-
const dirPath = path.resolve(directory)
29+
const dirPath = path.resolve(directory);
3030
const outPath = path.resolve(out);
3131
const dirName = path.dirname(outPath);
32-
fs.mkdirSync(dirName, { recursive: true })
32+
fs.mkdirSync(dirName, { recursive: true });
3333
await exec(`tar -czf ${outPath} .`, {
3434
cwd: dirPath,
35-
})
35+
});
3636
}
3737

38-
39-
export async function CacheHarness({ mode, tarball }: { mode: string, tarball: string }) {
40-
if (!isAllowedMode(mode)) { throw new Error(`mode should be one of ${allowedModes.join(' | ')}`) }
41-
38+
export async function CacheHarness({
39+
mode,
40+
tarball,
41+
debug = true,
42+
}: {
43+
mode: string;
44+
tarball: string;
45+
debug?: boolean;
46+
}) {
47+
if (!isAllowedMode(mode)) {
48+
throw new Error(`mode should be one of ${allowedModes.join(' | ')}`);
49+
}
4250

4351
const workingDir = await (async () => {
44-
if (mode == "check") {
52+
if (mode === 'check') {
4553
return await untar(tarball);
4654
} else {
4755
return tmpdir();
4856
}
49-
})()
57+
})();
5058

51-
const cacheDir = path.join(workingDir, "cache")
52-
const cache = Cache.FileSystem(cacheDir)
59+
const cacheDir = path.join(workingDir, 'cache');
60+
const cache = { ...Cache.FileSystem(cacheDir, debug), canWrite: mode === 'dump' };
5361

5462
const check = (verificationKey: VerificationKey, label: string) => {
55-
if (mode == "check") {
56-
const expectedVk = JSON.parse(fs.readFileSync(path.join(workingDir, `${label}.json`), "utf8"));
57-
assert.deepEqual(expectedVk.data, verificationKey.data, `expected (${label}) verification keys to remain the same`);
58-
59-
return
63+
if (mode === 'check') {
64+
const expectedVk = JSON.parse(
65+
fs.readFileSync(path.join(workingDir, `${label}.json`), 'utf8')
66+
);
67+
assert.deepEqual(
68+
expectedVk.data,
69+
verificationKey.data,
70+
`expected (${label}) verification keys to remain the same`
71+
);
72+
73+
return;
6074
}
6175

62-
if (mode == "dump") {
63-
fs.writeFileSync(path.join(workingDir, `${label}.json`), JSON.stringify(verificationKey))
76+
if (mode === 'dump') {
77+
fs.writeFileSync(path.join(workingDir, `${label}.json`), JSON.stringify(verificationKey));
6478
}
65-
}
79+
};
6680

6781
const verify = async <T, U>(proof: ProofBase<T, U>, label: string): Promise<boolean> => {
68-
const expectedVk = JSON.parse(fs.readFileSync(path.join(workingDir, `${label}.json`), "utf8"));
82+
const expectedVk = JSON.parse(fs.readFileSync(path.join(workingDir, `${label}.json`), 'utf8'));
6983

7084
const expectedOk = await verifyProof(proof, expectedVk);
71-
return expectedOk
72-
}
85+
return expectedOk;
86+
};
7387

7488
const finish = async () => {
75-
await tar(workingDir, tarball)
76-
}
77-
89+
await tar(workingDir, tarball);
90+
};
7891

7992
return {
8093
cache,
8194
check,
8295
verify,
8396
finish,
84-
}
97+
};
8598
}

0 commit comments

Comments
 (0)