The block environment a transaction executes in: the fields the EVM's
block opcodes read. EEVM.Handler.execute/4 takes one; EEVM.Block
derives it from each header with from_header/2.
The chain id isn't here: it lives in EEVM.Config with the rest of the
chain's rules.
| Field | Opcode | Description |
|---|---|---|
number | NUMBER (0x43) | Current block number |
timestamp | TIMESTAMP (0x42) | Block timestamp (seconds since epoch) |
coinbase | COINBASE (0x41) | Address of the block producer |
gas_limit | GASLIMIT (0x45) | Block gas limit; defaults to 30,000,000, a mainnet-sized block, so transactions fit |
prev_randao | PREVRANDAO (0x44) | RANDAO mix from the beacon chain (DIFFICULTY before the Merge) |
base_fee | BASEFEE (0x48) | Block base fee (EIP-1559) |
blob_base_fee | BLOBBASEFEE (0x4A) | Block blob base fee (EIP-4844, EIP-7516) |
block_hashes | BLOCKHASH (0x40) | Recent block numbers mapped to their 32-byte hashes |
parent_beacon_block_root | — | Parent beacon block root, stored by the EIP-4788 system call |
Summary
Functions
Returns the environment of the block header describes.
Returns the hash of block block_number as the word BLOCKHASH pushes.
Creates a new block environment with default values: block 0 with no base fee, and a 30,000,000 gas limit so a transaction can run in it.
Creates a new block environment with the given overrides.
Types
@type t() :: %EEVM.BlockEnv{ base_fee: non_neg_integer(), blob_base_fee: non_neg_integer(), block_hashes: %{required(non_neg_integer()) => <<_::256>>}, coinbase: non_neg_integer(), gas_limit: non_neg_integer(), number: non_neg_integer(), parent_beacon_block_root: <<_::256>>, prev_randao: non_neg_integer(), timestamp: non_neg_integer() }
Functions
@spec from_header( EEVM.Block.Header.t(), keyword() ) :: t()
Returns the environment of the block header describes.
The blob base fee follows from header.excess_blob_gas under the
hardfork's blob schedule, and prev_randao holds the header's difficulty
before Paris (EIP-4399). Options: :config or :hardfork (default
:cancun), and :block_hashes, recent block numbers mapped to their
hashes for BLOCKHASH.
Example
iex> header = EEVM.Block.Header.new(number: 5, gas_limit: 30_000_000, base_fee_per_gas: 7)
iex> env = EEVM.BlockEnv.from_header(header)
iex> {env.number, env.gas_limit, env.base_fee}
{5, 30000000, 7}
@spec hash(t(), non_neg_integer()) :: non_neg_integer()
Returns the hash of block block_number as the word BLOCKHASH pushes.
Returns 0 if the block number is not in the last 256 blocks or is the current/future block. This matches the EVM spec — BLOCKHASH can only access the most recent 256 ancestors. A hash given as an integer is still accepted, for one minor version (deprecated).
@spec new() :: t()
Creates a new block environment with default values: block 0 with no base fee, and a 30,000,000 gas limit so a transaction can run in it.
iex> EEVM.BlockEnv.new().gas_limit
30000000
Creates a new block environment with the given overrides.
Example
iex> block = EEVM.BlockEnv.new(number: 18_000_000, base_fee: 7)
iex> block.number
18000000