# 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`](#snapshot) | `anvil_snapshot` | `Hex` | Saves the state of the chain and returns an id. |
| [`revert`](#revert) | `anvil_revert` | `boolean` | Brings the chain back to a snapshot. |
| [`dumpState`](#dumpstate) | `anvil_dumpState` | `Hex` | The state of the chain as one blob. |
| [`loadState`](#loadstate) | `anvil_loadState` | `boolean` | Merges a dumped state into the chain. |
| [`rollback`](#rollback) | `anvil_rollback` | `null` | Removes the latest blocks, one by default. |

Each returns an [`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method). To
replace the latest blocks with other ones, use [`Anvil.reorg`](https://ethect.dev/rpc/anvil/reorg/).

## snapshot

Saves the state of the chain and returns the id to [`revert`](#revert) to.

```ts
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`](https://ethect.dev/reference/primitives/hex/), the id of the snapshot. anvil counts
  them from `"0x0"`.
- The snapshots a node holds are listed by
  [`Anvil.metadata`](https://ethect.dev/rpc/anvil/node/#metadata), under `snapshots`.

## revert

Brings the chain back to a snapshot: its blocks, its state and its clock.

```ts
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`](https://ethect.dev/reference/primitives/hex/)), as returned by
  [`snapshot`](#snapshot).
- **Returns:** `boolean`. `true` when the chain was brought back, `false` when the node does
  not know the snapshot.
- Reverting spends the snapshot and every snapshot taken after it: a second `revert` to any of
  them returns `false`. Take a new snapshot to revert to the same point again.

## dumpState

Returns the state of the chain as one blob, to give to [`loadState`](#loadstate).

```ts
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`). With `true` the blob holds
  the state at every block, not only at the latest.
- **Returns:** [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/). The blob is anvil's own format: keep it
  as it is.

## loadState

Merges a state from [`dumpState`](#dumpstate) into the chain.

```ts
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`](https://ethect.dev/reference/primitives/hex/)), a blob from
  [`dumpState`](#dumpstate).
- **Returns:** `boolean`. anvil answers `true`.
- 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 `RpcError` of reason `InvalidParams` and the message
  `Failed to decode state dump`.

## rollback

Removes the latest blocks, one by default.

```ts
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 `RpcError` of reason `InvalidParams`.

## 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.setBalance`](https://ethect.dev/rpc/anvil/state/#setbalance) and the other state setters wrote.
- **A fresh node per test** is the other way to isolate tests: see
  [`AnvilNode`](https://ethect.dev/reference/testing/anvil-node/).
