Skip to content

Commit 053e6a0

Browse files
committed
perf(world-state): compute genesis values on an ephemeral world state
1 parent 21a061c commit 053e6a0

2 files changed

Lines changed: 45 additions & 2 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { Fr } from '@aztec/foundation/curves/bn254';
2+
import { MerkleTreeId, PublicDataTreeLeaf } from '@aztec/stdlib/trees';
3+
import type { GenesisData } from '@aztec/stdlib/world-state';
4+
5+
import { jest } from '@jest/globals';
6+
7+
import { NativeWorldStateService } from './native/index.js';
8+
9+
jest.setTimeout(60_000);
10+
11+
describe('generateGenesisValues world state backend equivalence', () => {
12+
// A genesis with both non-empty prefilled public data and a non-zero timestamp, so the
13+
// fast-return branch in generateGenesisValues is not taken and the archive root is computed
14+
// from an actual world state.
15+
const genesis: GenesisData = {
16+
prefilledPublicData: [
17+
new PublicDataTreeLeaf(new Fr(1000), new Fr(2000)),
18+
new PublicDataTreeLeaf(new Fr(3000), new Fr(4000)),
19+
],
20+
genesisTimestamp: 1234567890n,
21+
};
22+
23+
const archiveRoot = async (ws: NativeWorldStateService) =>
24+
new Fr((await ws.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
25+
26+
// The consensus-critical guarantee behind computing genesis values on the fsync-off ephemeral
27+
// backend instead of tmp: both backends must derive the exact same on-chain genesis archive root.
28+
it('ephemeral and tmp produce identical genesis archive roots', async () => {
29+
const tmpWs = await NativeWorldStateService.tmp(undefined /* rollupAddress */, true /* cleanupTmpDir */, genesis);
30+
const ephemeralWs = await NativeWorldStateService.ephemeral(genesis);
31+
try {
32+
const tmpRoot = await archiveRoot(tmpWs);
33+
const ephemeralRoot = await archiveRoot(ephemeralWs);
34+
expect(ephemeralRoot).toEqual(tmpRoot);
35+
} finally {
36+
await tmpWs.close();
37+
await ephemeralWs.close();
38+
}
39+
});
40+
});

yarn-project/world-state/src/testing.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ async function generateGenesisValues(genesis: GenesisData) {
1414
};
1515
}
1616

17-
// Create a temporary world state to compute the genesis values.
18-
const ws = await NativeWorldStateService.tmp(undefined /* rollupAddress */, true /* cleanupTmpDir */, genesis);
17+
// Compute the genesis values on a throwaway world state. The archive root derives only from the
18+
// prefilled public data and the genesis timestamp, so the fsync-off ephemeral store (no version
19+
// manager, no crash-recoverability) produces an identical root while skipping the fsync overhead
20+
// that `tmp` pays. close() removes the tmpdir.
21+
const ws = await NativeWorldStateService.ephemeral(genesis);
1922
const genesisArchiveRoot = new Fr((await ws.getCommitted().getTreeInfo(MerkleTreeId.ARCHIVE)).root);
2023
await ws.close();
2124

0 commit comments

Comments
 (0)