Skip to main content

Simulate reorgs and rollbacks

reorg() replaces the last blocks, rollback() removes them.

Reorg: a confirmed transaction disappears​

const receipt = await anvil.confirm(
anvil.testClient.sendTransaction({ to: alice, value: parseEther("1") }),
);
await anvil.mine(2); // 3 confirmations

// Replace the last 3 blocks with empty ones: the transaction disappears.
await anvil.testClient.reorg({ depth: 3 });

expect(await anvil.testClient.getBlockNumber()).toBe(
receipt.blockNumber + 2n, // same height, different blocks
);
await expect(
anvil.testClient.getTransactionReceipt({ hash: receipt.transactionHash }),
).rejects.toThrow(/could not be found/);

Tested in examples/test/e2e/chain-config-and-reorgs.e2e.test.ts:70

Reorg with different transactions​

// The new last block contains a payment to bob that never happened before.
await anvil.testClient.reorg({
depth: 1,
transactions: [
{
transaction: { from: deployer, to: bob, value: parseEther("5") },
block: 0,
},
],
});
expect(await anvil.getBalance(bob)).toBe(before + parseEther("5"));

Tested in examples/test/e2e/chain-config-and-reorgs.e2e.test.ts:92

Each transactions entry is a request (from an unlocked or impersonated account) or a signed raw transaction, placed in the block-th new block.

Roll back the tip​

const height = await anvil.testClient.getBlockNumber();
await anvil.mine(5);

await anvil.cheats.rollback(5); // remove the last 5 blocks (default 1)
expect(await anvil.testClient.getBlockNumber()).toBe(height);

await anvil.mine();
await anvil.testClient.rollback(); // the same as a test client action
expect(await anvil.testClient.getBlockNumber()).toBe(height);

Tested in examples/test/e2e/chain-config-and-reorgs.e2e.test.ts:108

Concepts​