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 mined again
Section titled “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.
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
Section titled “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
Section titled “Parameters”- Type:
number
How many blocks to replace, counted from the head.
Behavior
Section titled “Behavior”- 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.watchgets a block whose parent it was givendepthblocks 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
TestWalletruns once. Mined again in another block, it does nothing: test a reorg with plain transactions.
Errors
Section titled “Errors”| 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 |
Under the hood
Section titled “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