Skip to main content

Save and load chain state

Dump the state to a file once, then boot or load other nodes from it.

Save a fixture to a file​

const builder = await new AnvilContainer()
.withTestClient((client) => client.withAccount(0))
.start();
try {
token = await deployToken(builder.testClient);
holders = await builder.addresses();
for (const holder of holders) {
await mint(builder.testClient, token, holder, parseEther("1000"));
}
await builder.dumpStateToFile(fixtureFile); // the `--load-state` format
oracleCode = (await builder.testClient.getCode({
address: await deployFixedPriceOracle(builder.testClient),
}))!;
} finally {
await builder.stop();
}

Tested in examples/test/e2e/state-fixtures.e2e.test.ts:38

Boot a node from it​

const anvil = await new AnvilContainer(
new AnvilOptions().state.withLoadState(fixtureFile),
).start();
try {
expect(await balanceOf(anvil.testClient, token, holders[9])).toBe(
parseEther("1000"),
);
} finally {
await anvil.stop();
}

Tested in examples/test/e2e/state-fixtures.e2e.test.ts:63

Load it into a running node​

await anvil.loadStateFromFile(fixtureFile);

Tested in examples/test/e2e/state-fixtures.e2e.test.ts:84

Loading merges into the current state and discards snapshots.

In memory​

await anvil.setBalanceEther(alice, 42);
const state = await anvil.dumpState(); // hex blob, e.g. to cache between suites

await anvil.reset(); // back to genesis: the balance is gone…
expect(await anvil.getBalance(alice)).toBe(parseEther("10000"));

await anvil.loadState(state); // …and restored
expect(await anvil.getBalance(alice)).toBe(parseEther("42"));

Tested in examples/test/e2e/state-fixtures.e2e.test.ts:99

Let anvil dump on stop​

withDumpState() / withState() take host paths, copied in and out by stop():

const configuration = new AnvilOptions().state
.withDumpState("./out/state.json") // copied to the host by stop()
.withStateInterval(30) // also dump every 30 s while running
.withPreserveHistoricalStates() // include historical states in dumps
.build();

expect(configuration.fileExports).toEqual([
{
flag: "--dump-state",
hostPath: path.resolve("out/state.json"),
containerPath: "/tmp/anvil-dump-state.json",
when: "stop",
},
]);

// --state loads the file when it exists and dumps to it on stop.
const persistent = new AnvilOptions().state
.withState("./.anvil/state.json")
.build();
expect(persistent.fileImports[0].required).toBe(false);
expect(persistent.fileExports[0].when).toBe("stop");

Tested in examples/test/unit/options.test.ts:139

anvil's resolved configuration, copied out after start:

// anvil's resolved config (incl. dev keys), copied out right after start.
const configuration = new AnvilOptions().state
.withConfigOut("./out/anvil-config.json")
.build();
expect(configuration.fileExports[0]).toMatchObject({
flag: "--config-out",
when: "start",
});

Tested in examples/test/unit/options.test.ts:165

Files in your image: containerPath():

const baked = containerPath("/data/fixture.json"); // not copied
const configuration = new AnvilOptions().state.withLoadState(baked).build();

expect(isContainerPath(baked)).toBe(true);
expect(configuration.args).toContain("/data/fixture.json");
expect(configuration.fileImports).toEqual([]);

Tested in examples/test/unit/options.test.ts:178

Concepts​