Mining
The eight methods that decide when blocks are mined: per transaction, on a timer or on request, with mining paused or not.
| Method | JSON-RPC | Returns | Description |
|---|---|---|---|
getAutomine |
anvil_getAutomine |
boolean |
Whether a block is mined as soon as a transaction arrives. |
setAutomine |
anvil_setAutomine |
null |
Turns mining a block per transaction on or off. |
getIntervalMining |
anvil_getIntervalMining |
number | null |
The seconds between two blocks, when blocks are mined on a timer. |
setIntervalMining |
anvil_setIntervalMining |
null |
Mines a block every given number of seconds. |
getMiningPaused |
anvil_getMiningPaused |
boolean |
Whether mining is paused. |
setMiningPaused |
anvil_setMiningPaused |
null |
Pauses or resumes mining. |
mine |
anvil_mine |
null |
Mines blocks, one by default. |
mineDetailed |
anvil_mine_detailed |
MinedBlock[] |
Mines blocks and returns them, with what each transaction did. |
Each returns an Effect<…, Method.Error, Transport>. A
node started with plain anvil mines a block per transaction: getAutomine is true,
getIntervalMining is null and getMiningPaused is false.
getAutomine
Section titled “getAutomine”Returns whether a block is mined as soon as a transaction arrives.
import { Effect } from "effect";import { Anvil } from "@ethect/core/rpc";
const automine = Anvil.getAutomine().pipe( Effect.map((on) => (on ? "a block per transaction" : "on a timer or on request")),);- JSON-RPC:
anvil_getAutomine - Parameters: none.
- Returns:
boolean.
setAutomine
Section titled “setAutomine”Turns mining a block per transaction on or off.
import { Effect } from "effect";import { Anvil, Eth } from "@ethect/core/rpc";
const batched = Effect.gen(function* () { const [from, to] = yield* Eth.accounts(); yield* Anvil.setAutomine(false); yield* Eth.sendTransaction({ from, to, value: 1n }); yield* Eth.sendTransaction({ from, to, value: 2n }); yield* Anvil.mine(); const block = yield* Eth.getBlockByNumber("latest", false); return block?.transactions.length; // 2});- JSON-RPC:
anvil_setAutomine - Parameters:
enabled(boolean). - Returns:
null. truealso stops the timer ofsetIntervalMining.
getIntervalMining
Section titled “getIntervalMining”Returns the seconds between two blocks when blocks are mined on a timer.
import { Effect } from "effect";import { Anvil } from "@ethect/core/rpc";
const interval = Anvil.getIntervalMining().pipe( Effect.map((seconds) => (seconds === null ? "no timer" : `every ${seconds} s`)),);- JSON-RPC:
anvil_getIntervalMining - Parameters: none.
- Returns:
number | null. The interval in seconds, ornullwhen blocks are not mined on a timer.
setIntervalMining
Section titled “setIntervalMining”Mines a block every given number of seconds, and no longer one per transaction.
import { Effect } from "effect";import { Anvil } from "@ethect/core/rpc";
const timed = Effect.gen(function* () { yield* Anvil.setIntervalMining(60); const interval = yield* Anvil.getIntervalMining(); // 60 const automine = yield* Anvil.getAutomine(); // false return { interval, automine };});- JSON-RPC:
anvil_setIntervalMining - Parameters:
interval(number, in seconds).0stops the timer. - Returns:
null. - Stopping the timer with
0does not turn automine back on. Nothing is mined untilmineis called orsetAutomine(true)is.
getMiningPaused
Section titled “getMiningPaused”Returns whether mining is paused.
import { Effect } from "effect";import { Anvil } from "@ethect/core/rpc";
const paused = Anvil.getMiningPaused().pipe( Effect.map((on) => (on ? "paused" : "mining")),);- JSON-RPC:
anvil_getMiningPaused - Parameters: none.
- Returns:
boolean.
setMiningPaused
Section titled “setMiningPaused”Pauses or resumes mining, and leaves the mode as it is.
import { Effect } from "effect";import { Anvil, Eth } from "@ethect/core/rpc";
const held = Effect.gen(function* () { const [from, to] = yield* Eth.accounts(); yield* Anvil.setMiningPaused(true); const hash = yield* Eth.sendTransaction({ from, to, value: 1n }); // the transaction waits in the pool yield* Anvil.setMiningPaused(false); return hash;});- JSON-RPC:
anvil_setMiningPaused - Parameters:
paused(boolean). - Returns:
null. - While mining is paused,
getAutominestill returnstrueon a node that mines per transaction, andminestill mines. - Resuming mines what waited in the pool.
Mines blocks: one by default.
import { Effect } from "effect";import { Anvil, Eth } from "@ethect/core/rpc";
const mined = Effect.gen(function* () { yield* Anvil.mine(); // three blocks, their timestamps 12 seconds apart yield* Anvil.mine(3, 12n); return yield* Eth.blockNumber(); // 4n});| Parameter | Type | Description |
|---|---|---|
blocks (optional) |
number |
how many blocks to mine. One when left out. |
interval (optional) |
bigint |
the seconds between the timestamps of two blocks, the first one included |
- JSON-RPC:
anvil_mine - Returns:
null. - The transactions waiting in the pool go into the first block mined.
mineDetailed
Section titled “mineDetailed”Mines blocks and returns them in full, each transaction with what its execution returned.
import { Effect } from "effect";import { Anvil, Eth } from "@ethect/core/rpc";
const outputs = Effect.gen(function* () { const [from, to] = yield* Eth.accounts(); yield* Anvil.setAutomine(false); yield* Eth.sendTransaction({ from, to, value: 1n }); const [block] = yield* Anvil.mineDetailed(); return block?.transactions.map((transaction) => transaction.output); // ["0x"]});- JSON-RPC:
anvil_mine_detailed - Parameters:
options(optional,MineOptions): how many blocks to mine and the timestamp of the first. - Returns:
ReadonlyArray<MinedBlock>, oneMinedBlockper block mined. A transaction that reverted carries arevertReason. - A
timestamplower than the latest block’s fails with anRpcErrorof reasonInvalidParams.
Behavior
Section titled “Behavior”- Three modes. A node mines per transaction (automine), on a timer (interval mining), or
only on request. Turning one of the first two on turns the other off, and turning both off
leaves
mineas the only way to get a block. - A transaction is answered before its block. With any mode,
Eth.sendTransactionreturns the hash as soon as the transaction is in the pool.Eth.sendTransactionSyncwaits for the receipt. - Empty blocks.
minemines a block even when the pool is empty.mine(0)mines nothing.