Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(integration): Reuse snapshots #1682

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/util/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ const connect = async (params: computeArgvType | serverParamsType) => {

/* Track node snapshot ids for easy snapshot/revert */
let lastSnapshotId: string;
let snapshotBlockNumber: number;
let lastSnapshotBlockNumber: number;

return {
...spawnInfo,
Expand All @@ -298,15 +298,18 @@ const connect = async (params: computeArgvType | serverParamsType) => {
deploy: params.deploy ? undefined : deployFn,
snapshot: async () => {
lastSnapshotId = await params.provider.send("evm_snapshot", []);
snapshotBlockNumber = await params.provider.getBlockNumber();
lastSnapshotBlockNumber = await params.provider.getBlockNumber();
return lastSnapshotId;
},
revert: async (snapshotId = lastSnapshotId) => {
await params.provider.send("evm_revert", [snapshotId]);
const blockNumberAfterRevert = await params.provider.getBlockNumber();
if (blockNumberAfterRevert != snapshotBlockNumber) {
if (
snapshotId == lastSnapshotId &&
blockNumberAfterRevert != lastSnapshotBlockNumber
) {
throw Error(
`evm_revert did not revert to expected block number ${snapshotBlockNumber} but to ${blockNumberAfterRevert}. Snapshots are deleted when reverting - did you take a new snapshot after the last revert?`,
`evm_revert did not revert to expected block number ${lastSnapshotBlockNumber} but to ${blockNumberAfterRevert}`,
);
}
},
Expand Down
7 changes: 3 additions & 4 deletions src/util/test/mochaHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type hookInfo = {
arbitrager: account;
};
server?: serverType;
snapshotId?: string;
closeCurrentProxy?: () => Promise<void>;
};

Expand Down Expand Up @@ -106,7 +107,7 @@ export const mochaHooks = {
// making sure that last one is mined before snapshot is taken, anvil may snapshot too early otherwise
await tx.wait();
mgv.disconnect();
await hook.server.snapshot();
hook.snapshotId = await hook.server.snapshot();
},

async beforeEachImpl(hook: hookInfo) {
Expand Down Expand Up @@ -194,9 +195,7 @@ export const mochaHooks = {
// Note, this is updated on this global instance, so a test should never read it inside an non-awaited async request
hook.server.url = `http://${serverParams.host}:${currentProxyPort}`;

await hook.server.revert();
// revert removes the old snapshot, a new snapshot is therefore needed. https://github.com/foundry-rs/foundry/blob/6262fbec64021463fd403204039201983effa00d/evm/src/executor/fork/database.rs#L117
await hook.server.snapshot();
await hook.server.revert(hook.snapshotId);
}
},

Expand Down
Loading