Skip to content

Commit

Permalink
test(integration): Reuse snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
espendk committed Dec 16, 2023
1 parent 9a190a4 commit 77cc98b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
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

0 comments on commit 77cc98b

Please sign in to comment.