History
The five methods that save the chain and bring it back: snapshots to revert to, the whole state as a blob to load again, and a rollback of the latest blocks.
| Method | JSON-RPC | Returns | Description |
|---|---|---|---|
snapshot |
anvil_snapshot |
Hex |
Saves the state of the chain and returns an id. |
revert |
anvil_revert |
boolean |
Brings the chain back to a snapshot. |
dumpState |
anvil_dumpState |
Hex |
The state of the chain as one blob. |
loadState |
anvil_loadState |
boolean |
Merges a dumped state into the chain. |
rollback |
anvil_rollback |
null |
Removes the latest blocks, one by default. |
Each returns an Effect<…, Method.Error, Transport>. To
replace the latest blocks with other ones, use Anvil.reorg.
snapshot
Section titled “snapshot”Saves the state of the chain and returns the id to revert to.
import { Effect } from "effect";import { Anvil } from "@ethect/core/rpc";
const saved = Effect.gen(function* () { const id = yield* Anvil.snapshot(); // "0x0" yield* Anvil.mine(3); return id;});- JSON-RPC:
anvil_snapshot - Parameters: none.
- Returns:
Hex.Hex, the id of the snapshot. anvil counts them from"0x0". - The snapshots a node holds are listed by
Anvil.metadata, undersnapshots.
revert
Section titled “revert”Brings the chain back to a snapshot: its blocks, its state and its clock.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Anvil, Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const undone = Effect.gen(function* () { const id = yield* Anvil.snapshot(); yield* Anvil.setBalance(ADDRESS, 10n ** 18n); yield* Anvil.mine(); const reverted = yield* Anvil.revert(id); // true const balance = yield* Eth.getBalance(ADDRESS, "latest"); // 0n const block = yield* Eth.blockNumber(); // 0n return { reverted, balance, block };});- JSON-RPC:
anvil_revert - Parameters:
id(Hex.Hex), as returned bysnapshot. - Returns:
boolean.truewhen the chain was brought back,falsewhen the node does not know the snapshot. - Reverting spends the snapshot and every snapshot taken after it: a second
revertto any of them returnsfalse. Take a new snapshot to revert to the same point again.
dumpState
Section titled “dumpState”Returns the state of the chain as one blob, to give to loadState.
import { Effect } from "effect";import { Anvil } from "@ethect/core/rpc";
const dumps = Effect.gen(function* () { const state = yield* Anvil.dumpState(); const history = yield* Anvil.dumpState(true); return { state, history };});- JSON-RPC:
anvil_dumpState - Parameters:
preserveHistoricalStates(optional,boolean). Withtruethe blob holds the state at every block, not only at the latest. - Returns:
Hex.Hex. The blob is anvil’s own format: keep it as it is.
loadState
Section titled “loadState”Merges a state from dumpState into the chain.
import { Effect } from "effect";import { Address } from "@ethect/core";import { Anvil, Eth } from "@ethect/core/rpc";
const ADDRESS = Address.makeUnsafe("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045");
const restored = Effect.gen(function* () { yield* Anvil.setBalance(ADDRESS, 5n); const state = yield* Anvil.dumpState(); yield* Anvil.setBalance(ADDRESS, 6n); yield* Anvil.loadState(state); return yield* Eth.getBalance(ADDRESS, "latest"); // 5n});- JSON-RPC:
anvil_loadState - Parameters:
state(Hex.Hex), a blob fromdumpState. - Returns:
boolean. anvil answerstrue. - The accounts of the blob replace those of the chain. An account the blob does not hold stays as it is.
- The head becomes the block the blob was dumped at, on the node it came from or on another.
- A blob anvil cannot read fails with an
RpcErrorof reasonInvalidParamsand the messageFailed to decode state dump.
rollback
Section titled “rollback”Removes the latest blocks, one by default.
import { Effect } from "effect";import { Anvil, Eth } from "@ethect/core/rpc";
const shorter = Effect.gen(function* () { yield* Anvil.mine(5); yield* Anvil.rollback(); yield* Anvil.rollback(2); return yield* Eth.blockNumber(); // 2n});- JSON-RPC:
anvil_rollback - Parameters:
depth(optional,number): how many blocks to remove. One when left out. - Returns:
null. - The state goes back to the last block kept. The transactions of the removed blocks are gone: they do not return to the pool.
- A depth above the height of the chain fails with an
RpcErrorof reasonInvalidParams.
Behavior
Section titled “Behavior”- Which one to use. A snapshot lives in the node: revert to it to undo the steps of a test. A dumped state is a value you can keep and load into another node.
- State set by hand is included. A snapshot and a dump both hold what
Anvil.setBalanceand the other state setters wrote. - A fresh node per test is the other way to isolate tests: see
AnvilNode.