Skip to content

Commit cfefd1e

Browse files
committed
feat: contract class overrides for simulation
Adds StateOverrides.contractClasses for shadowing contract classes in the simulator's contract DB - the building block for simulating against mock implementations or unregistered classes.
1 parent 5afa477 commit cfefd1e

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

docs/docs-developers/docs/aztec-js/how_to_test.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ Use these to:
8888

8989
- Set up state preconditions without running a full setup transaction
9090
- Reproduce a bug from production by pinning storage to the values seen at a specific block
91-
- Simulate a contract instance as if it had been upgraded
91+
- Simulate a contract instance as if it had been upgraded (see `fastForwardContractUpdate` below)
9292
- Test branches that depend on rare values without orchestrating the contract calls that produce them
9393

9494
### Fast-forwarding a contract update

yarn-project/aztec-node/src/aztec-node/server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ export class AztecNodeService implements AztecNode, AztecNodeAdmin, AztecNodeDeb
15521552
}),
15531553
});
15541554
const contractsDB = new PublicContractsDB(this.contractDataSource, this.log.getBindings());
1555-
contractsDB.addContracts(contractOverrides);
1555+
contractsDB.addContracts(contractOverrides, stateOverrides?.contractClasses);
15561556
const processor = publicProcessorFactory.create(merkleTreeFork, newGlobalVariables, config, contractsDB);
15571557

15581558
// REFACTOR: Consider merging ProcessReturnValues into ProcessedTx

yarn-project/simulator/src/public/public_db_sources.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ export class PublicContractsDB implements PublicContractsDBInterface {
7676
this.addContractsFromLogs(contractDeploymentData.getRevertibleContractDeploymentData());
7777
}
7878

79-
/** Inserts typed contract instances directly into the current checkpoint. */
80-
public addContracts(contractInstances?: ContractInstanceWithAddress[]): void {
79+
/** Inserts typed contract instances and classes directly into the current checkpoint. */
80+
public addContracts(
81+
contractInstances?: ContractInstanceWithAddress[],
82+
contractClasses?: ContractClassPublic[],
83+
): void {
8184
const currentState = this.getCurrentState();
85+
for (const contractClass of contractClasses ?? []) {
86+
currentState.addClass(contractClass.id, contractClass);
87+
}
8288
for (const instance of contractInstances ?? []) {
8389
currentState.addInstance(instance.address, instance);
8490
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
import { z } from 'zod';
22

3+
import { type ContractClassPublic, ContractClassPublicSchema } from '../contract/interfaces/contract_class.js';
34
import { type PublicStorageOverride, PublicStorageOverrideSchema } from './public_storage_override.js';
45

56
/**
67
* Pre-simulation state-tree overrides applied to the ephemeral world-state fork before running the tx.
78
*
89
* - `publicStorage`: write specific (contract, slot, value) entries in the public-data tree.
10+
* - `contractClasses`: override contract classes in the contract DB.
911
*
10-
* Contract instance/artifact overrides live separately in `ContractOverrides` (see `@aztec/stdlib/tx`).
12+
* Per-address contract instance/artifact overrides live separately in `ContractOverrides`
13+
* (see `@aztec/stdlib/tx`).
1114
*/
1215
export type StateOverrides = {
1316
/** Public-storage writes to apply before simulation. */
1417
publicStorage?: PublicStorageOverride[];
18+
/** Contract classes to override in the contract DB for the duration of the simulation. */
19+
contractClasses?: ContractClassPublic[];
1520
};
1621

1722
export const StateOverridesSchema = z.object({
1823
publicStorage: z.array(PublicStorageOverrideSchema).optional(),
24+
contractClasses: z.array(ContractClassPublicSchema).optional(),
1925
});

0 commit comments

Comments
 (0)