EEVM.BlockEnv (eevm v0.1.0)

Copy Markdown View Source

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.

FieldOpcodeDescription
numberNUMBER (0x43)Current block number
timestampTIMESTAMP (0x42)Block timestamp (seconds since epoch)
coinbaseCOINBASE (0x41)Address of the block producer
gas_limitGASLIMIT (0x45)Block gas limit; defaults to 30,000,000, a mainnet-sized block, so transactions fit
prev_randaoPREVRANDAO (0x44)RANDAO mix from the beacon chain (DIFFICULTY before the Merge)
base_feeBASEFEE (0x48)Block base fee (EIP-1559)
blob_base_feeBLOBBASEFEE (0x4A)Block blob base fee (EIP-4844, EIP-7516)
block_hashesBLOCKHASH (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

t()

@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

from_header(header, opts \\ [])

@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}

hash(block_env, block_number)

@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).

new()

@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

new(opts)

@spec new(keyword()) :: t()

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