# Anvil.reorg

Replaces the latest blocks of a test node with new ones, and puts the transactions you give in
the new blocks you name.

## Usage

```ts
import { Effect } from "effect";
import { FetchHttpClient } from "effect/http";
import { Address, Transport } from "@ethect/core";
import { Anvil, Eth } from "@ethect/core/rpc";

const FROM = Address.makeUnsafe("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266");
const TO = Address.makeUnsafe("0x70997970C51812dc3A010C7d01b50e0d17dc79C8");

const program = Effect.gen(function* () {
  yield* Anvil.mine(4);
  // replace blocks 3 and 4, with one transfer in the new block 3
  yield* Anvil.reorg(2, [[{ from: FROM, to: TO, value: 1n }, 0]]);
  const block = yield* Eth.getBlockByNumber(3n, false);
  return { head: yield* Eth.blockNumber(), transactions: block?.transactions.length };
});

const result = await program.pipe(
  Effect.provide(Transport.http("http://127.0.0.1:8545")),
  Effect.provide(FetchHttpClient.layer),
  Effect.runPromise,
);
// { head: 4n, transactions: 1 }
```

The chain keeps its height: the two latest blocks are removed and two new ones take their
numbers. The examples below leave the transport out.

### A signed transaction

A transaction can also be given signed and encoded, for instance one that
[`Eth.signTransaction`](https://ethect.dev/rpc/eth/sign-transaction/) returned.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const replaced = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.mine(2);
  const signed = yield* Eth.signTransaction({ from: to, to: from, value: 1n });
  yield* Anvil.reorg(2, [
    [{ from, to, value: 1n }, 0],
    [signed, 1],
  ]);
});
```

The request goes in the first new block and the signed transaction in the second.

### Detecting the reorg

After the call, a block number that was read before names another block.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const changed = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.mine(2);
  const before = yield* Eth.getBlockByNumber(2n, false);
  yield* Anvil.reorg(1, [[{ from, to, value: 1n }, 0]]);
  const after = yield* Eth.getBlockByNumber(2n, false);
  return before?.hash !== after?.hash; // true
});
```

## Returns

[`Effect<null, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method)

`null` once the blocks are replaced.

## Parameters

### depth

- **Type:** `number`

How many of the latest blocks to replace. It is at least `1` and at most the height of the
chain.

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const replaced = Effect.gen(function* () {
  const [from, to] = yield* Eth.accounts();
  yield* Anvil.mine(5);
  // blocks 3, 4 and 5 are replaced
  yield* Anvil.reorg(3, [[{ from, to, value: 1n }, 0]]);
  return yield* Eth.blockNumber(); // 5n
});
```

### transactions

- **Type:** `ReadonlyArray<readonly [TransactionRequest | Hex, number]>`

The transactions of the new blocks, each as a pair: the transaction, then the position of the
new block to put it in, `0` for the first.

| Part of the pair | Type | Description |
| --- | --- | --- |
| transaction | [`TransactionRequest`](https://ethect.dev/rpc/types/transaction-request/) | a request, sent from its `from` |
| transaction | [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/) | or a signed, encoded transaction |
| position | `number` | the new block it goes in, from `0` to `depth - 1` |

```ts
import { Effect } from "effect";
import { Anvil, Eth } from "@ethect/core/rpc";

const reordered = Effect.gen(function* () {
  const [first, second] = yield* Eth.accounts();
  yield* Anvil.mine(2);
  yield* Anvil.reorg(2, [
    [{ from: first, to: second, value: 1n }, 1],
    [{ from: second, to: first, value: 2n }, 1],
  ]);
});
```

Several transactions can name the same block. An empty array gives empty blocks.

## Behavior

- **Same numbers, same timestamps.** A new block has the number and the `timestamp` of the
  block it replaces. Its hash differs when its transactions do.
- **Empty for empty changes nothing.** Replacing blocks that held no transaction with empty
  ones gives the same blocks, hashes included. Give a transaction, or replace a block that had
  one, to get a different chain.
- **The old transactions are gone.** The transactions of the replaced blocks are not put in the
  new ones and do not return to the pool.
  [`Eth.getTransactionByHash`](https://ethect.dev/rpc/eth/get-transaction-by-hash/) returns `null` for
  them.
- **The pool is left alone.** A transaction waiting in the pool stays there. Only the
  transactions given go into the new blocks.
- **State goes back first.** The new blocks run on the state of the last block kept. A balance
  set with [`Anvil.setBalance`](https://ethect.dev/rpc/anvil/state/#setbalance) after that block is lost.
- **A transaction that cannot run is left out.** A request whose sender cannot pay does not
  fail the call: the new block is mined without it.
- **Who sends a request.** `from` has to be an account the node holds or impersonates. Without
  `from`, the node's first account sends.
- **JSON numbers on the wire.** `depth` and the block positions are sent as JSON numbers, not
  as hex quantities: anvil refuses a quantity there.

## Errors

The three [errors](https://ethect.dev/reference/errors/) every method can fail with. The cases specific to this
method, as anvil 1.9 reports them:

| Error | Reason | When |
| --- | --- | --- |
| `RpcError` | `InvalidParams` | `depth` is above the height of the chain |
| `RpcError` | `InvalidParams` | a position is `depth` or more |
| `RpcError` | `InvalidParams` | the `from` of a request is not held or impersonated. The message is `No Signer available`. |
| `RpcError` | `InternalError` | `depth` is `0` |

## JSON-RPC method

`anvil_reorg`
