# AnvilNode.reorg

Replaces the last blocks of an anvil chain by empty ones, as a reorg does. What they held is
gone, neither mined nor pending.

## Usage

```ts
import { Effect } from "effect";
import { type Address, Call, Tx } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
import { AnvilNode } from "@ethect/core/testing";

/** A payment, then a reorg that takes it back. Gives what the node says of it afterwards. */
const takenBack = (to: Address.Address) =>
  Effect.gen(function* () {
    const receipt = yield* Tx.send(Call.make({ to, value: 1n }));
    yield* AnvilNode.reorg(1); // the last block is replaced by an empty one
    return yield* Eth.getTransactionReceipt(receipt.transactionHash); // null
  });
```

It acts on the node of the [`Transport`](https://ethect.dev/reference/transport/) in context: an anvil started
with [`AnvilNode.layer`](https://ethect.dev/reference/testing/anvil-node/#layer), or any other. Use it to test
what follows the chain: an [indexer](https://ethect.dev/guides/indexer/), a wait for several confirmations, a
page that shows a value per block.

### A transaction mined again

A transaction that was rolled back is not pending either. Read its bytes before the reorg,
and send them again after it: the same transaction, in another block.

```ts
import { Effect } from "effect";
import type { Hash } from "@ethect/core";
import { Eth } from "@ethect/core/rpc";
import { AnvilNode } from "@ethect/core/testing";

const minedAgain = (hash: Hash.Hash) =>
  Effect.gen(function* () {
    const raw = yield* Eth.getRawTransactionByHash(hash);
    yield* AnvilNode.reorg(1);
    return yield* Eth.sendRawTransaction(raw!); // the same hash
  });
```

## Returns

`Effect<void, Transport.Error, Transport>`

Nothing. When it ends, the chain is as long as it was, and its last `depth` blocks are others.

## Parameters

### depth

- **Type:** `number`

How many blocks to replace, counted from the head.

## Behavior

- **The new blocks are empty.** To choose what they hold, use
  [`Anvil.reorg`](https://ethect.dev/rpc/anvil/reorg/), which is one request and takes transactions.
- **The new blocks have other hashes.** From the same parent, time and transactions, anvil
  mines the very same block again. The first new block is one second later than the head
  was, so that an empty block replaced by an empty one is still another block.
- **The heights are the same.** A watcher of [`Block.watch`](https://ethect.dev/reference/block/#what-a-reorg-looks-like)
  gets a block whose parent it was given `depth` blocks ago.
- **What the replaced blocks held is gone.** Its receipt is `null`, and it is not in the pool.
- **It is not one step for the node.** Between the rollback and the new blocks, the chain is
  shorter: do not read it from another fiber meanwhile.
- **A batch of [`TestWallet`](https://ethect.dev/reference/testing/test-wallet/) runs once.** Mined again in
  another block, it does nothing: test a reorg with plain transactions.

## Errors

| Error | When |
| --- | --- |
| [`RpcError`](https://ethect.dev/reference/errors/rpc-error/) | the node refused: more blocks than the chain has, or a node that is not anvil |
| [`TransportError`](https://ethect.dev/reference/errors/transport-error/) | the node could not be reached |

## Under the hood

Four requests to the node of the `Transport` in context, in this order.

- **your node** `eth_getBlockByNumber("latest", false)`: the time of the head
- **your node** `anvil_rollback(depth)`: the chain, without its last blocks
- **your node** `anvil_setNextBlockTimestamp(that time + 1)`: one second later than the head was
- **your node** `anvil_mine(depth)`: as many blocks, empty
