Skip to content

Commit f28fcdb

Browse files
authored
chore: avm-proving and avm-integration tests do not require simulator to export function with jest mocks (#10228)
Subclass ContractDataSource instead of mocking. Use real WorldStateDB. Helper function exported by simulator no longer requires jest-mock-extended.
1 parent eeea0aa commit f28fcdb

File tree

5 files changed

+103
-62
lines changed

5 files changed

+103
-62
lines changed

noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr

+1-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ contract AvmTest {
619619
dep::aztec::oracle::debug_log::debug_log("pedersen_hash_with_index");
620620
let _ = pedersen_hash_with_index(args_field);
621621
dep::aztec::oracle::debug_log::debug_log("test_get_contract_instance");
622-
test_get_contract_instance(context.this_address());
622+
test_get_contract_instance(AztecAddress::from_field(args_field[0]));
623623
dep::aztec::oracle::debug_log::debug_log("get_address");
624624
let _ = get_address();
625625
dep::aztec::oracle::debug_log::debug_log("get_sender");

yarn-project/bb-prover/src/avm_proving.test.ts

+6-12
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@ import path from 'path';
1010
import { type BBSuccess, BB_RESULT, generateAvmProof, verifyAvmProof } from './bb/execute.js';
1111
import { extractAvmVkData } from './verification_key/verification_key_data.js';
1212

13-
const TIMEOUT = 180_000;
14-
1513
describe('AVM WitGen, proof generation and verification', () => {
16-
it(
17-
'Should prove and verify bulk_testing',
18-
async () => {
19-
await proveAndVerifyAvmTestContract(
20-
'bulk_testing',
21-
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x)),
22-
);
23-
},
24-
TIMEOUT,
25-
);
14+
it('Should prove and verify bulk_testing', async () => {
15+
await proveAndVerifyAvmTestContract(
16+
'bulk_testing',
17+
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10].map(x => new Fr(x)),
18+
);
19+
}, 180_000);
2620
});
2721

2822
async function proveAndVerifyAvmTestContract(functionName: string, calldata: Fr[] = []) {

yarn-project/ivc-integration/src/avm_integration.test.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { BufferReader } from '@aztec/foundation/serialize';
1212
import { type FixedLengthArray } from '@aztec/noir-protocol-circuits-types/types';
1313
import { simulateAvmTestContractGenerateCircuitInputs } from '@aztec/simulator/public/fixtures';
1414

15-
import { jest } from '@jest/globals';
1615
import fs from 'fs/promises';
1716
import { tmpdir } from 'node:os';
1817
import os from 'os';
@@ -23,9 +22,6 @@ import { MockPublicBaseCircuit, witnessGenMockPublicBaseCircuit } from './index.
2322

2423
// Auto-generated types from noir are not in camel case.
2524
/* eslint-disable camelcase */
26-
27-
jest.setTimeout(240_000);
28-
2925
const logger = createDebugLogger('aztec:avm-integration');
3026

3127
describe('AVM Integration', () => {
@@ -120,7 +116,7 @@ describe('AVM Integration', () => {
120116
);
121117

122118
expect(verifyResult.status).toBe(BB_RESULT.SUCCESS);
123-
});
119+
}, 240_000);
124120
});
125121

126122
async function proveAvmTestContract(functionName: string, calldata: Fr[] = []): Promise<BBSuccess> {

yarn-project/simulator/src/public/fixtures/index.ts

+94-43
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { PublicExecutionRequest, Tx } from '@aztec/circuit-types';
22
import {
33
type AvmCircuitInputs,
44
CallContext,
5+
type ContractClassPublic,
6+
type ContractInstanceWithAddress,
57
DEFAULT_GAS_LIMIT,
8+
type FunctionSelector,
69
Gas,
710
GasFees,
811
GasSettings,
@@ -20,19 +23,16 @@ import {
2023
computePublicBytecodeCommitment,
2124
} from '@aztec/circuits.js';
2225
import { makeContractClassPublic, makeContractInstanceFromClassId } from '@aztec/circuits.js/testing';
26+
import { type ContractArtifact } from '@aztec/foundation/abi';
2327
import { AztecAddress } from '@aztec/foundation/aztec-address';
2428
import { Fr, Point } from '@aztec/foundation/fields';
2529
import { openTmpStore } from '@aztec/kv-store/utils';
26-
import { PublicTxSimulator, type WorldStateDB } from '@aztec/simulator';
30+
import { PublicTxSimulator, WorldStateDB } from '@aztec/simulator';
2731
import { NoopTelemetryClient } from '@aztec/telemetry-client/noop';
2832
import { MerkleTrees } from '@aztec/world-state';
2933

30-
import { mock } from 'jest-mock-extended';
31-
3234
import { getAvmTestContractBytecode, getAvmTestContractFunctionSelector } from '../../avm/fixtures/index.js';
3335

34-
const TIMESTAMP = new Fr(99833);
35-
3636
/**
3737
* If assertionErrString is set, we expect a (non exceptional halting) revert due to a failing assertion and
3838
* we check that the revert reason error contains this string. However, the circuit must correctly prove the
@@ -49,46 +49,14 @@ export async function simulateAvmTestContractGenerateCircuitInputs(
4949

5050
const globalVariables = GlobalVariables.empty();
5151
globalVariables.gasFees = GasFees.empty();
52-
globalVariables.timestamp = TIMESTAMP;
52+
globalVariables.timestamp = new Fr(99833);
5353

54-
const worldStateDB = mock<WorldStateDB>();
5554
const telemetry = new NoopTelemetryClient();
5655
const merkleTrees = await (await MerkleTrees.new(openTmpStore(), telemetry)).fork();
57-
worldStateDB.getMerkleInterface.mockReturnValue(merkleTrees);
58-
59-
// Top level contract call
60-
const bytecode = getAvmTestContractBytecode('public_dispatch');
61-
const dispatchSelector = getAvmTestContractFunctionSelector('public_dispatch');
62-
const publicFn: PublicFunction = { bytecode, selector: dispatchSelector };
63-
const contractClass = makeContractClassPublic(0, publicFn);
64-
const contractInstance = makeContractInstanceFromClassId(contractClass.id);
65-
66-
// The values here should match those in `avm_simulator.test.ts`
67-
const instanceGet = new SerializableContractInstance({
68-
version: 1,
69-
salt: new Fr(0x123),
70-
deployer: new AztecAddress(new Fr(0x456)),
71-
contractClassId: new Fr(0x789),
72-
initializationHash: new Fr(0x101112),
73-
publicKeys: new PublicKeys(
74-
new Point(new Fr(0x131415), new Fr(0x161718), false),
75-
new Point(new Fr(0x192021), new Fr(0x222324), false),
76-
new Point(new Fr(0x252627), new Fr(0x282930), false),
77-
new Point(new Fr(0x313233), new Fr(0x343536), false),
78-
),
79-
}).withAddress(contractInstance.address);
80-
worldStateDB.getContractInstance
81-
.mockResolvedValueOnce(contractInstance)
82-
.mockResolvedValueOnce(instanceGet) // test gets deployer
83-
.mockResolvedValueOnce(instanceGet) // test gets class id
84-
.mockResolvedValueOnce(instanceGet) // test gets init hash
85-
.mockResolvedValue(contractInstance);
86-
worldStateDB.getContractClass.mockResolvedValue(contractClass);
87-
worldStateDB.getBytecode.mockResolvedValue(bytecode);
88-
worldStateDB.getBytecodeCommitment.mockResolvedValue(computePublicBytecodeCommitment(bytecode));
89-
90-
const storageValue = new Fr(5);
91-
worldStateDB.storageRead.mockResolvedValue(Promise.resolve(storageValue));
56+
const contractDataSource = new MockedAvmTestContractDataSource();
57+
const worldStateDB = new WorldStateDB(merkleTrees, contractDataSource);
58+
59+
const contractInstance = contractDataSource.contractInstance;
9260

9361
const simulator = new PublicTxSimulator(
9462
merkleTrees,
@@ -99,7 +67,12 @@ export async function simulateAvmTestContractGenerateCircuitInputs(
9967
/*doMerkleOperations=*/ true,
10068
);
10169

102-
const callContext = new CallContext(sender, contractInstance.address, dispatchSelector, /*isStaticCall=*/ false);
70+
const callContext = new CallContext(
71+
sender,
72+
contractInstance.address,
73+
contractDataSource.fnSelector,
74+
/*isStaticCall=*/ false,
75+
);
10376
const executionRequest = new PublicExecutionRequest(callContext, calldata);
10477

10578
const tx: Tx = createTxForPublicCall(executionRequest);
@@ -159,3 +132,81 @@ export function createTxForPublicCall(
159132

160133
return tx;
161134
}
135+
136+
class MockedAvmTestContractDataSource {
137+
private fnName = 'public_dispatch';
138+
private bytecode: Buffer;
139+
public fnSelector: FunctionSelector;
140+
private publicFn: PublicFunction;
141+
private contractClass: ContractClassPublic;
142+
public contractInstance: ContractInstanceWithAddress;
143+
private bytecodeCommitment: Fr;
144+
private otherContractInstance: ContractInstanceWithAddress;
145+
146+
constructor() {
147+
this.bytecode = getAvmTestContractBytecode(this.fnName);
148+
this.fnSelector = getAvmTestContractFunctionSelector(this.fnName);
149+
this.publicFn = { bytecode: this.bytecode, selector: this.fnSelector };
150+
this.contractClass = makeContractClassPublic(0, this.publicFn);
151+
this.contractInstance = makeContractInstanceFromClassId(this.contractClass.id);
152+
this.bytecodeCommitment = computePublicBytecodeCommitment(this.bytecode);
153+
// The values here should match those in `avm_simulator.test.ts`
154+
this.otherContractInstance = new SerializableContractInstance({
155+
version: 1,
156+
salt: new Fr(0x123),
157+
deployer: new AztecAddress(new Fr(0x456)),
158+
contractClassId: new Fr(0x789),
159+
initializationHash: new Fr(0x101112),
160+
publicKeys: new PublicKeys(
161+
new Point(new Fr(0x131415), new Fr(0x161718), false),
162+
new Point(new Fr(0x192021), new Fr(0x222324), false),
163+
new Point(new Fr(0x252627), new Fr(0x282930), false),
164+
new Point(new Fr(0x313233), new Fr(0x343536), false),
165+
),
166+
}).withAddress(this.contractInstance.address);
167+
}
168+
169+
getPublicFunction(_address: AztecAddress, _selector: FunctionSelector): Promise<PublicFunction> {
170+
return Promise.resolve(this.publicFn);
171+
}
172+
173+
getBlockNumber(): Promise<number> {
174+
throw new Error('Method not implemented.');
175+
}
176+
177+
getContractClass(_id: Fr): Promise<ContractClassPublic> {
178+
return Promise.resolve(this.contractClass);
179+
}
180+
181+
getBytecodeCommitment(_id: Fr): Promise<Fr> {
182+
return Promise.resolve(this.bytecodeCommitment);
183+
}
184+
185+
addContractClass(_contractClass: ContractClassPublic): Promise<void> {
186+
return Promise.resolve();
187+
}
188+
189+
getContract(address: AztecAddress): Promise<ContractInstanceWithAddress> {
190+
if (address.equals(this.contractInstance.address)) {
191+
return Promise.resolve(this.contractInstance);
192+
} else {
193+
return Promise.resolve(this.otherContractInstance);
194+
}
195+
}
196+
197+
getContractClassIds(): Promise<Fr[]> {
198+
throw new Error('Method not implemented.');
199+
}
200+
201+
getContractArtifact(_address: AztecAddress): Promise<ContractArtifact | undefined> {
202+
throw new Error('Method not implemented.');
203+
}
204+
205+
getContractFunctionName(_address: AztecAddress, _selector: FunctionSelector): Promise<string> {
206+
return Promise.resolve(this.fnName);
207+
}
208+
209+
addContractArtifact(_address: AztecAddress, _contract: ContractArtifact): Promise<void> {
210+
return Promise.resolve();
211+
}
212+
}

yarn-project/simulator/src/public/public_tx_simulator.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ describe('public_tx_simulator', () => {
204204
);
205205
},
206206
);
207-
});
207+
}, 30_000);
208208

209209
afterEach(async () => {
210210
await treeStore.delete();

0 commit comments

Comments
 (0)