Skip to content

Commit

Permalink
chore: cleanup/speedup an AVM test
Browse files Browse the repository at this point in the history
  • Loading branch information
dbanks12 committed Feb 9, 2025
1 parent 4a8136c commit 8c2bb0b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
18 changes: 15 additions & 3 deletions yarn-project/bb-prover/src/avm_proving_tests/avm_proving_tester.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type MerkleTreeWriteOperations } from '@aztec/circuit-types';
import { type AvmCircuitInputs, AztecAddress, VerificationKeyData } from '@aztec/circuits.js';
import { PublicTxSimulationTester, type TestEnqueuedCall } from '@aztec/simulator/public/fixtures';
import { WorldStateDB } from '@aztec/simulator/server';
import { NativeWorldStateService } from '@aztec/world-state';

import fs from 'node:fs/promises';
Expand All @@ -25,21 +26,24 @@ export class AvmProvingTester extends PublicTxSimulationTester {
constructor(
private bbWorkingDirectory: string,
private checkCircuitOnly: boolean,
worldStateDB: WorldStateDB,
contractDataSource: SimpleContractDataSource,
merkleTrees: MerkleTreeWriteOperations,
skipContractDeployments: boolean,
) {
super(contractDataSource, merkleTrees, skipContractDeployments);
super(worldStateDB, contractDataSource, merkleTrees, skipContractDeployments);
}

static override async create(checkCircuitOnly: boolean = false, skipContractDeployments: boolean = false) {
const bbWorkingDirectory = await fs.mkdtemp(path.join(tmpdir(), 'bb-'));

const contractDataSource = new SimpleContractDataSource();
const merkleTrees = await (await NativeWorldStateService.tmp()).fork();
const worldStateDB = new WorldStateDB(merkleTrees, contractDataSource);
return new AvmProvingTester(
bbWorkingDirectory,
checkCircuitOnly,
worldStateDB,
contractDataSource,
merkleTrees,
skipContractDeployments,
Expand Down Expand Up @@ -110,19 +114,27 @@ export class AvmProvingTester extends PublicTxSimulationTester {
export class AvmProvingTesterV2 extends PublicTxSimulationTester {
constructor(
private bbWorkingDirectory: string,
worldStateDB: WorldStateDB,
contractDataSource: SimpleContractDataSource,
merkleTrees: MerkleTreeWriteOperations,
skipContractDeployments: boolean,
) {
super(contractDataSource, merkleTrees, skipContractDeployments);
super(worldStateDB, contractDataSource, merkleTrees, skipContractDeployments);
}

static override async create(skipContractDeployments: boolean = false) {
const bbWorkingDirectory = await fs.mkdtemp(path.join(tmpdir(), 'bb-'));

const contractDataSource = new SimpleContractDataSource();
const merkleTrees = await (await NativeWorldStateService.tmp()).fork();
return new AvmProvingTesterV2(bbWorkingDirectory, contractDataSource, merkleTrees, skipContractDeployments);
const worldStateDB = new WorldStateDB(merkleTrees, contractDataSource);
return new AvmProvingTesterV2(
bbWorkingDirectory,
worldStateDB,
contractDataSource,
merkleTrees,
skipContractDeployments,
);
}

async proveV2(avmCircuitInputs: AvmCircuitInputs): Promise<BBResult> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MerkleTreeId, PublicExecutionRequest, type Tx } from '@aztec/circuit-types';
import { MerkleTreeId, type MerkleTreeWriteOperations, PublicExecutionRequest, type Tx } from '@aztec/circuit-types';
import {
type AvmCircuitPublicInputs,
CallContext,
Expand Down Expand Up @@ -43,10 +43,20 @@ export type TestEnqueuedCall = {
export class PublicTxSimulationTester extends BaseAvmSimulationTester {
private txCount = 0;

constructor(
private worldStateDB: WorldStateDB,
contractDataSource: SimpleContractDataSource,
merkleTrees: MerkleTreeWriteOperations,
skipContractDeployments: boolean,
) {
super(contractDataSource, merkleTrees, skipContractDeployments);
}

public static async create(skipContractDeployments = false): Promise<PublicTxSimulationTester> {
const contractDataSource = new SimpleContractDataSource();
const merkleTrees = await (await NativeWorldStateService.tmp()).fork();
return new PublicTxSimulationTester(contractDataSource, merkleTrees, skipContractDeployments);
const worldStateDB = new WorldStateDB(merkleTrees, contractDataSource);
return new PublicTxSimulationTester(worldStateDB, contractDataSource, merkleTrees, skipContractDeployments);
}

public async simulateTx(
Expand All @@ -60,8 +70,7 @@ export class PublicTxSimulationTester extends BaseAvmSimulationTester {
globals.timestamp = TIMESTAMP;
globals.gasFees = DEFAULT_GAS_FEES;

const worldStateDB = new WorldStateDB(this.merkleTrees, this.contractDataSource);
const simulator = new PublicTxSimulator(this.merkleTrees, worldStateDB, globals, /*doMerkleOperations=*/ true);
const simulator = new PublicTxSimulator(this.merkleTrees, this.worldStateDB, globals, /*doMerkleOperations=*/ true);

const setupExecutionRequests: PublicExecutionRequest[] = [];
for (let i = 0; i < setupCalls.length; i++) {
Expand Down Expand Up @@ -118,7 +127,10 @@ export class PublicTxSimulationTester extends BaseAvmSimulationTester {
feePayer,
);

const startTime = performance.now();
const avmResult = await simulator.simulate(tx);
const endTime = performance.now();
this.logger.debug(`Public transaction simulation took ${endTime - startTime}ms`);

if (avmResult.revertCode.isOK()) {
await this.commitTxStateUpdates(avmResult.avmProvingRequest.inputs.publicInputs);
Expand Down

0 comments on commit 8c2bb0b

Please sign in to comment.