Skip to content

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.

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 in context: an anvil started with AnvilNode.layer, or any other. Use it to test what follows the chain: an indexer, a wait for several confirmations, a page that shows a value per block.

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.

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
});

Effect<void, Transport.Error, Transport>

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

  • Type: number

How many blocks to replace, counted from the head.

  • The new blocks are empty. To choose what they hold, use 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 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 runs once. Mined again in another block, it does nothing: test a reorg with plain transactions.
Error When
RpcError the node refused: more blocks than the chain has, or a node that is not anvil
TransportError the node could not be reached

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

  1. your nodeeth_getBlockByNumber("latest", false)the time of the head
  2. your nodeanvil_rollback(depth)the chain, without its last blocks
  3. your nodeanvil_setNextBlockTimestamp(that time + 1)one second later than the head was
  4. your nodeanvil_mine(depth)as many blocks, empty