# `EEVM.BlockEnv`
[🔗](https://github.com/mw2000/eevm/blob/v0.1.0/lib/eevm/block_env.ex#L1)

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 |

# `t`

```elixir
@type t() :: %EEVM.BlockEnv{
  base_fee: non_neg_integer(),
  blob_base_fee: non_neg_integer(),
  block_hashes: %{required(non_neg_integer()) =&gt; &lt;&lt;_::256&gt;&gt;},
  coinbase: non_neg_integer(),
  gas_limit: non_neg_integer(),
  number: non_neg_integer(),
  parent_beacon_block_root: &lt;&lt;_::256&gt;&gt;,
  prev_randao: non_neg_integer(),
  timestamp: non_neg_integer()
}
```

# `from_header`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
