EEVM.Context.Block (eevm v0.1.0)

Copy Markdown View Source

Block-level context — information about the block being executed, exposed to the EVM via environment opcodes.

Fields

FieldOpcodeDescription
numberNUMBER (0x43)Current block number
timestampTIMESTAMP (0x42)Block timestamp (seconds since epoch)
coinbaseCOINBASE (0x41)Address of the block producer
gaslimitGASLIMIT (0x45)Block gas limit
prevrandaoPREVRANDAO (0x44)Previous block's RANDAO mix (post-Merge)
basefeeBASEFEE (0x48)Block base fee from EIP-1559
blob_base_feeBLOBBASEFEE (0x4A)Block blob base fee from EIP-7516
chain_idCHAINID (0x46)Network ID (1=mainnet, 137=Polygon, etc.)
hashesBLOCKHASH (0x40)Map of recent block numbers to their hashes
parent_beacon_block_root(no opcode)Parent beacon block root (EIP-4788)

parent_beacon_block_root

Added in Cancun (EIP-4788). Not exposed through an opcode — instead, the EL client performs a system call into the beacon-roots contract at block start to record this value keyed by the parent block's timestamp. User contracts then CALL that contract to read historical beacon roots.

PREVRANDAO

Before The Merge, opcode 0x44 was DIFFICULTY. Post-Merge, the same opcode returns the RANDAO mix from the Beacon Chain — a source of on-chain randomness.

Summary

Functions

Returns the block hash for a given block number.

Creates a new block context with default values.

Creates a new block context with the given overrides.

Types

t()

@type t() :: %EEVM.Context.Block{
  basefee: non_neg_integer(),
  blob_base_fee: non_neg_integer(),
  chain_id: non_neg_integer(),
  coinbase: non_neg_integer(),
  gaslimit: non_neg_integer(),
  hashes: %{required(non_neg_integer()) => non_neg_integer()},
  number: non_neg_integer(),
  parent_beacon_block_root: non_neg_integer(),
  prevrandao: non_neg_integer(),
  timestamp: non_neg_integer()
}

Functions

hash(block, block_number)

@spec hash(t(), non_neg_integer()) :: non_neg_integer()

Returns the block hash for a given block number.

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.

new()

@spec new() :: t()

Creates a new block context with default values.

new(opts)

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

Creates a new block context with the given overrides.

Example

iex> block = EEVM.Context.Block.new(number: 18_000_000, chain_id: 1)
iex> block.number
18000000