diff --git a/yarn-project/end-to-end/src/e2e_token_contract/access_control.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/access_control.test.ts index 1cccd8813a7..8efc7d88277 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/access_control.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/access_control.test.ts @@ -4,15 +4,12 @@ describe('e2e_token_contract access control', () => { const t = new TokenContractTest('access_control'); beforeAll(async () => { - await t.pushBaseSnapshots(); + await t.applyBaseSnapshots(); + await t.setup(); }); afterAll(async () => { - await t.popBaseSnapshots(); - }); - - beforeEach(async () => { - await t.snapshotManager.setup(); + await t.teardown(); }); afterEach(async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/burn.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/burn.test.ts index c40740ee763..ff7aed370b5 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/burn.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/burn.test.ts @@ -8,19 +8,15 @@ describe('e2e_token_contract burn', () => { let { asset, accounts, tokenSim, wallets } = t; beforeAll(async () => { - await t.pushBaseSnapshots(); - await t.pushMintSnapshot(); + await t.applyBaseSnapshots(); + await t.applyMintSnapshot(); + await t.setup(); + // Have to destructure again to ensure we have latest refs. + ({ asset, accounts, tokenSim, wallets } = t); }); afterAll(async () => { - await t.snapshotManager.pop(); // mint - await t.popBaseSnapshots(); - }); - - beforeEach(async () => { - await t.snapshotManager.setup(); - // Have to destructure again to ensure we have latest refs. - ({ asset, accounts, tokenSim, wallets } = t); + await t.teardown(); }); afterEach(async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts index 9bff43deee2..2fa48998dcb 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts @@ -8,16 +8,13 @@ describe('e2e_token_contract minting', () => { let { asset, accounts, tokenSim, wallets } = t; beforeAll(async () => { - await t.pushBaseSnapshots(); + await t.applyBaseSnapshots(); + await t.setup(); + ({ asset, accounts, tokenSim, wallets } = t); }); afterAll(async () => { - await t.popBaseSnapshots(); - }); - - beforeEach(async () => { - await t.snapshotManager.setup(); - ({ asset, accounts, tokenSim, wallets } = t); + await t.teardown(); }); afterEach(async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts index 9bb2d1084f3..43762d88e26 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/reading_constants.test.ts @@ -20,9 +20,9 @@ describe('e2e_token_contract reading constants', () => { let reader: ReaderContract; beforeAll(async () => { - await t.pushBaseSnapshots(); + await t.applyBaseSnapshots(); - await t.snapshotManager.snapshot( + await t.snapshot( 'reading_constants', async () => { logger('Deploying ReaderContract...'); @@ -35,16 +35,15 @@ describe('e2e_token_contract reading constants', () => { logger(`Reader contract restored to ${readerAddress}.`); }, ); + + await t.setup(); }); afterAll(async () => { - await t.snapshotManager.pop(); // reading_constants - await t.popBaseSnapshots(); + await t.teardown(); }); - beforeEach(async () => { - await t.snapshotManager.setup(); - }); + beforeEach(async () => {}); afterEach(async () => { await t.tokenSim.check(); diff --git a/yarn-project/end-to-end/src/e2e_token_contract/sheilding.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/sheilding.test.ts index 14da530347a..99fcd3c1336 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/sheilding.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/sheilding.test.ts @@ -10,20 +10,16 @@ describe('e2e_token_contract shield + redeem shield', () => { let secretHash: Fr; beforeAll(async () => { - await t.pushBaseSnapshots(); - await t.pushMintSnapshot(); + await t.applyBaseSnapshots(); + await t.applyMintSnapshot(); + await t.setup(); + // Have to destructure again to ensure we have latest refs. + ({ asset, accounts, tokenSim, wallets } = t); secretHash = computeMessageSecretHash(secret); }); afterAll(async () => { - await t.snapshotManager.pop(); // mint - await t.popBaseSnapshots(); - }); - - beforeEach(async () => { - await t.snapshotManager.setup(); - // Have to destructure again to ensure we have latest refs. - ({ asset, accounts, tokenSim, wallets } = t); + await t.teardown(); }); afterEach(async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts b/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts index 72896148d07..f32e3b4536f 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/token_contract_test.ts @@ -12,17 +12,22 @@ import { } from '@aztec/aztec.js'; import { DocsExampleContract, TokenContract } from '@aztec/noir-contracts.js'; -import { SnapshotManager, addAccounts, publicDeployAccounts } from '../fixtures/snapshot_manager.js'; +import { + SnapshotManager, + type SubsystemsContext, + addAccounts, + publicDeployAccounts, +} from '../fixtures/snapshot_manager.js'; import { TokenSimulator } from '../simulators/token_simulator.js'; -const { E2E_DATA_PATH: dataPath = './data' } = process.env; +const { E2E_DATA_PATH: dataPath } = process.env; export class TokenContractTest { static TOKEN_NAME = 'Aztec Token'; static TOKEN_SYMBOL = 'AZT'; static TOKEN_DECIMALS = 18n; + private snapshotManager: SnapshotManager; logger: DebugLogger; - snapshotManager: SnapshotManager; wallets: AccountWallet[] = []; accounts: CompleteAddress[] = []; asset!: TokenContract; @@ -39,7 +44,7 @@ export class TokenContractTest { * 1. Add 3 accounts. * 2. Publicly deploy accounts, deploy token contract and a "bad account". */ - async pushBaseSnapshots() { + async applyBaseSnapshots() { await this.snapshotManager.snapshot('3_accounts', addAccounts(3, this.logger), async ({ accountKeys }, { pxe }) => { const accountManagers = accountKeys.map(ak => getSchnorrAccount(pxe, ak[0], ak[1], 1)); this.wallets = await Promise.all(accountManagers.map(a => a.getWallet())); @@ -97,28 +102,34 @@ export class TokenContractTest { // }); } - async popBaseSnapshots() { - await this.snapshotManager.pop(); // e2e_token_contract - await this.snapshotManager.pop(); // 3_accounts + async setup() { + await this.snapshotManager.setup(); } - async addPendingShieldNoteToPXE(accountIndex: number, amount: bigint, secretHash: Fr, txHash: TxHash) { - const storageSlot = new Fr(5); // The storage slot of `pending_shields` is 5. - const noteTypeId = new Fr(84114971101151129711410111011678111116101n); // TransparentNote + snapshot = ( + name: string, + apply: (context: SubsystemsContext) => Promise, + restore: (snapshotData: T, context: SubsystemsContext) => Promise = () => Promise.resolve(), + ): Promise => this.snapshotManager.snapshot(name, apply, restore); + async teardown() { + await this.snapshotManager.teardown(); + } + + async addPendingShieldNoteToPXE(accountIndex: number, amount: bigint, secretHash: Fr, txHash: TxHash) { const note = new Note([new Fr(amount), secretHash]); const extendedNote = new ExtendedNote( note, this.accounts[accountIndex].address, this.asset.address, - storageSlot, - noteTypeId, + TokenContract.storage.pending_shields.slot, + TokenContract.notes.TransparentNote.id, txHash, ); await this.wallets[accountIndex].addNote(extendedNote); } - async pushMintSnapshot() { + async applyMintSnapshot() { await this.snapshotManager.snapshot( 'mint', async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/transfer_private.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/transfer_private.test.ts index 4df010374a9..3251c7422a9 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/transfer_private.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/transfer_private.test.ts @@ -7,18 +7,14 @@ describe('e2e_token_contract transfer private', () => { let { asset, accounts, tokenSim, wallets, badAccount } = t; beforeAll(async () => { - await t.pushBaseSnapshots(); - await t.pushMintSnapshot(); + await t.applyBaseSnapshots(); + await t.applyMintSnapshot(); + await t.setup(); + ({ asset, accounts, tokenSim, wallets, badAccount } = t); }); afterAll(async () => { - await t.snapshotManager.pop(); // mint - await t.popBaseSnapshots(); - }); - - beforeEach(async () => { - await t.snapshotManager.setup(); - ({ asset, accounts, tokenSim, wallets, badAccount } = t); + await t.teardown(); }); afterEach(async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/transfer_public.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/transfer_public.test.ts index f29b1c91afd..13430c1916a 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/transfer_public.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/transfer_public.test.ts @@ -8,19 +8,15 @@ describe('e2e_token_contract transfer public', () => { let { asset, accounts, tokenSim, wallets, badAccount } = t; beforeAll(async () => { - await t.pushBaseSnapshots(); - await t.pushMintSnapshot(); + await t.applyBaseSnapshots(); + await t.applyMintSnapshot(); + await t.setup(); + // Have to destructure again to ensure we have latest refs. + ({ asset, accounts, tokenSim, wallets, badAccount } = t); }); afterAll(async () => { - await t.snapshotManager.pop(); // mint - await t.popBaseSnapshots(); - }); - - beforeEach(async () => { - await t.snapshotManager.setup(); - // Have to destructure again to ensure we have latest refs. - ({ asset, accounts, tokenSim, wallets, badAccount } = t); + await t.teardown(); }); afterEach(async () => { diff --git a/yarn-project/end-to-end/src/e2e_token_contract/unsheilding.test.ts b/yarn-project/end-to-end/src/e2e_token_contract/unsheilding.test.ts index 38d7a627deb..998b978e081 100644 --- a/yarn-project/end-to-end/src/e2e_token_contract/unsheilding.test.ts +++ b/yarn-project/end-to-end/src/e2e_token_contract/unsheilding.test.ts @@ -7,19 +7,15 @@ describe('e2e_token_contract unshielding', () => { let { asset, accounts, tokenSim, wallets } = t; beforeAll(async () => { - await t.pushBaseSnapshots(); - await t.pushMintSnapshot(); + await t.applyBaseSnapshots(); + await t.applyMintSnapshot(); + await t.setup(); + // Have to destructure again to ensure we have latest refs. + ({ asset, accounts, tokenSim, wallets } = t); }); afterAll(async () => { - await t.snapshotManager.pop(); // mint - await t.popBaseSnapshots(); - }); - - beforeEach(async () => { - await t.snapshotManager.setup(); - // Have to destructure again to ensure we have latest refs. - ({ asset, accounts, tokenSim, wallets } = t); + await t.teardown(); }); afterEach(async () => { diff --git a/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts b/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts index f08725fa497..d28e0396667 100644 --- a/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts +++ b/yarn-project/end-to-end/src/fixtures/snapshot_manager.ts @@ -11,8 +11,8 @@ import { } from '@aztec/aztec.js'; import { deployInstance, registerContractClass } from '@aztec/aztec.js/deployment'; import { asyncMap } from '@aztec/foundation/async-map'; -import { resolver, reviver } from '@aztec/foundation/serialize'; import { createDebugLogger } from '@aztec/foundation/log'; +import { resolver, reviver } from '@aztec/foundation/serialize'; import { type PXEService, createPXEService, getPXEServiceConfig } from '@aztec/pxe'; import { type Anvil, createAnvil } from '@viem/anvil'; @@ -26,7 +26,7 @@ import { MNEMONIC } from './fixtures.js'; import { getACVMConfig } from './get_acvm_config.js'; import { setupL1Contracts } from './setup_l1_contracts.js'; -type SubsystemsContext = { +export type SubsystemsContext = { anvil: Anvil; acvmConfig: any; aztecNode: AztecNodeService; @@ -47,17 +47,28 @@ export class SnapshotManager { private livePath: string; private logger: DebugLogger; - constructor(testName: string, private dataPath: string) { - this.livePath = join(this.dataPath, 'live', testName); + constructor(testName: string, private dataPath?: string) { + this.livePath = this.dataPath ? join(this.dataPath, 'live', testName) : ''; this.logger = createDebugLogger(`aztec:snapshot_manager:${testName}`); } public async snapshot( name: string, apply: (context: SubsystemsContext) => Promise, - restore: (snapshotData: T, context: SubsystemsContext) => Promise = () => - Promise.resolve(), + restore: (snapshotData: T, context: SubsystemsContext) => Promise = () => Promise.resolve(), ) { + if (!this.dataPath) { + // We are running in disabled mode. Just apply the state. + this.logger(`No data path given, will not persist any snapshots.`); + this.context = await this.setupFromFresh(); + this.logger(`Applying state transition for ${name}...`); + const snapshotData = await apply(this.context); + this.logger(`State transition for ${name} complete.`); + // Execute the restoration function. + await restore(snapshotData, this.context); + return; + } + const snapshotPath = join(this.dataPath, 'snapshots', ...this.snapshotStack.map(e => e.name), name, 'snapshot'); if (existsSync(snapshotPath)) { @@ -134,14 +145,6 @@ export class SnapshotManager { return this.context; } - /** - * Destroy the current subsystem context and pop off the top of the snapshot stack. - */ - public async pop() { - this.snapshotStack.pop(); - await this.teardown(); - } - /** * Destroys the current subsystem context. */ @@ -201,7 +204,9 @@ export class SnapshotManager { pxeConfig.dataDirectory = statePath; const pxe = await createPXEService(aztecNode, pxeConfig); - writeFileSync(`${statePath}/aztec_node_config.json`, JSON.stringify(aztecNodeConfig)); + if (statePath) { + writeFileSync(`${statePath}/aztec_node_config.json`, JSON.stringify(aztecNodeConfig)); + } return { aztecNodeConfig,