Skip to content

Commit

Permalink
remove concept of pop. complex, inhibits pure mem mode. just use sepa…
Browse files Browse the repository at this point in the history
…rate test files. now requires explicit setting of E2E_DATA_PATH.
  • Loading branch information
charlielye committed Apr 9, 2024
1 parent 4862405 commit 8865c79
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
16 changes: 6 additions & 10 deletions yarn-project/end-to-end/src/e2e_token_contract/burn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
11 changes: 4 additions & 7 deletions yarn-project/end-to-end/src/e2e_token_contract/minting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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...');
Expand All @@ -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();
Expand Down
16 changes: 6 additions & 10 deletions yarn-project/end-to-end/src/e2e_token_contract/sheilding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
Expand Down Expand Up @@ -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 = <T>(
name: string,
apply: (context: SubsystemsContext) => Promise<T>,
restore: (snapshotData: T, context: SubsystemsContext) => Promise<void> = () => Promise.resolve(),
): Promise<void> => 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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
16 changes: 6 additions & 10 deletions yarn-project/end-to-end/src/e2e_token_contract/unsheilding.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
35 changes: 20 additions & 15 deletions yarn-project/end-to-end/src/fixtures/snapshot_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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;
Expand All @@ -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<T>(
name: string,
apply: (context: SubsystemsContext) => Promise<T>,
restore: (snapshotData: T, context: SubsystemsContext) => Promise<void> = () =>
Promise.resolve(),
restore: (snapshotData: T, context: SubsystemsContext) => Promise<void> = () => 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)) {
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8865c79

Please sign in to comment.