# Headers, counts & uncles

The methods that read one part of a block: its header, how many transactions and uncles it
has, one of its uncles, and its access list.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getHeaderByNumber`](#getheaderbynumber) | `eth_getHeaderByNumber` | `Block.Header \| null` | A block header by number or tag. |
| [`getHeaderByHash`](#getheaderbyhash) | `eth_getHeaderByHash` | `Block.Header \| null` | A block header by hash. |
| [`getBlockTransactionCountByNumber`](#getblocktransactioncountbynumber) | `eth_getBlockTransactionCountByNumber` | `number \| null` | How many transactions a block has, by number or tag. |
| [`getBlockTransactionCountByHash`](#getblocktransactioncountbyhash) | `eth_getBlockTransactionCountByHash` | `number \| null` | How many transactions a block has, by hash. |
| [`getUncleCountByBlockNumber`](#getunclecountbyblocknumber) | `eth_getUncleCountByBlockNumber` | `number \| null` | How many uncles a block has, by number or tag. |
| [`getUncleCountByBlockHash`](#getunclecountbyblockhash) | `eth_getUncleCountByBlockHash` | `number \| null` | How many uncles a block has, by hash. |
| [`getUncleByBlockNumberAndIndex`](#getunclebyblocknumberandindex) | `eth_getUncleByBlockNumberAndIndex` | `Block.Block \| null` | An uncle of a block, by block number or tag and position. |
| [`getUncleByBlockHashAndIndex`](#getunclebyblockhashandindex) | `eth_getUncleByBlockHashAndIndex` | `Block.Block \| null` | An uncle of a block, by block hash and position. |
| [`getBlockAccessList`](#getblockaccesslist) | `eth_getBlockAccessList` | `BlockAccessList \| null` | What a block read and wrote, per account. |

Each method names its block with `block`, a number or a tag, or with `blockHash`. Each returns
an [`Effect`](https://ethect.dev/rpc/method/#calling-a-method) of the type in the table, which fails with a
`Method.Error` and needs a `Transport`. For the whole block, use
[`Eth.getBlockByNumber`](https://ethect.dev/rpc/eth/get-block-by-number/) or
[`Eth.getBlockByHash`](https://ethect.dev/rpc/eth/get-block-by-hash/).

## getHeaderByNumber

Returns the header of a block by number or tag: the block without its transactions, uncles and
withdrawals.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const baseFee = Effect.gen(function* () {
  const header = yield* Eth.getHeaderByNumber("latest");
  if (header === null) return null;
  return header.baseFeePerGas; // 1000000000n on a fresh anvil
});
```

- **JSON-RPC:** `eth_getHeaderByNumber`
- **Parameters:** `block` ([`BlockId.NumberOrTag`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** [`Block.Header`](https://ethect.dev/rpc/types/block/), or `null` when the node does not have
  the block.

## getHeaderByHash

Returns the header of a block by its hash.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const parent = Effect.gen(function* () {
  const head = yield* Eth.getHeaderByNumber("latest");
  if (head === null) return null;
  return yield* Eth.getHeaderByHash(head.parentHash);
});
```

- **JSON-RPC:** `eth_getHeaderByHash`
- **Parameters:** `blockHash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)).
- **Returns:** [`Block.Header`](https://ethect.dev/rpc/types/block/), or `null` when no block has the hash.

## getBlockTransactionCountByNumber

Returns how many transactions a block has, by number or tag.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const count = Eth.getBlockTransactionCountByNumber("latest").pipe(
  Effect.map((count) => (count === null ? "no such block" : `${count} transaction(s)`)),
);
```

- **JSON-RPC:** `eth_getBlockTransactionCountByNumber`
- **Parameters:** `block` ([`BlockId.NumberOrTag`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** `number`, or `null` when the node does not have the block. With `"pending"`,
  anvil counts the transactions waiting in its pool.

## getBlockTransactionCountByHash

Returns how many transactions a block has, by block hash.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const count = Eth.getBlockTransactionCountByHash(
  "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
);
```

- **JSON-RPC:** `eth_getBlockTransactionCountByHash`
- **Parameters:** `blockHash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)).
- **Returns:** `number`, or `null` when no block has the hash.

## getUncleCountByBlockNumber

Returns how many uncles a block has, by number or tag. It is always `0` for a block mined
since the merge.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const uncles = Eth.getUncleCountByBlockNumber(15_000_000n);
```

- **JSON-RPC:** `eth_getUncleCountByBlockNumber`
- **Parameters:** `block` ([`BlockId.NumberOrTag`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** `number`, or `null`.
- For a block it does not have, anvil 1.9 does not answer `null`: the call fails with an
  `RpcError` whose reason is `ResourceNotFound`.

## getUncleCountByBlockHash

Returns how many uncles a block has, by block hash.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const uncles = Eth.getUncleCountByBlockHash(
  "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
);
```

- **JSON-RPC:** `eth_getUncleCountByBlockHash`
- **Parameters:** `blockHash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)).
- **Returns:** `number`, or `null`.
- For a hash no block has, anvil 1.9 does not answer `null`: the call fails with an `RpcError`
  whose reason is `ResourceNotFound`.

## getUncleByBlockNumberAndIndex

Returns one uncle of a block, by the number or tag of the block and the position of the uncle.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const uncle = Eth.getUncleByBlockNumberAndIndex(15_000_000n, 0).pipe(
  Effect.map((uncle) => uncle?.miner),
);
```

- **JSON-RPC:** `eth_getUncleByBlockNumberAndIndex`
- **Parameters:** `block` ([`BlockId.NumberOrTag`](https://ethect.dev/rpc/types/block-id/)), then `index`
  (`number`).
- **Returns:** [`Block.Block`](https://ethect.dev/rpc/types/block/), or `null` when the block has no uncle
  at that position.

## getUncleByBlockHashAndIndex

Returns one uncle of a block, by the hash of the block and the position of the uncle.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const uncle = Eth.getUncleByBlockHashAndIndex(
  "0xe5178f52e018186949d9fe9168c6011820f9c6bd449a93dbb07c09dfc0af3dfa",
  0,
);
// null: the block has no uncle
```

- **JSON-RPC:** `eth_getUncleByBlockHashAndIndex`
- **Parameters:** `blockHash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)), then `index` (`number`).
- **Returns:** [`Block.Block`](https://ethect.dev/rpc/types/block/), or `null` when the block has no uncle
  at that position.

## getBlockAccessList

Returns what a block read and wrote, account by account, as defined by EIP-7928.

```ts
import { Effect } from "effect";
import { Eth } from "@ethect/core/rpc";

const touched = Effect.gen(function* () {
  const accessList = yield* Eth.getBlockAccessList("latest");
  if (accessList === null) return [];
  return accessList.map((account) => account.address);
});
```

- **JSON-RPC:** `eth_getBlockAccessList`
- **Parameters:** `block` ([`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)): a number, a tag,
  or an object that names the block by number or by hash.
- **Returns:** [`BlockAccessList.BlockAccessList`](https://ethect.dev/rpc/types/block-access-list/), or
  `null` for a block from before the Amsterdam fork, which has no access list.
- For a block above its head, anvil 1.9 fails with an `RpcError` whose reason is
  `InvalidParams`.
- anvil 1.9 started with `--hardfork amsterdam` sends storage slots and values in a short form
  (`"0x7"`). They are read as 32-byte words.

## Behavior

- **`null` means the node has nothing to give.** The block does not exist, or it has no such
  uncle or access list. The two uncle counts on anvil are the exception noted above.
- **Counts and positions are `number`.** A transaction count, an uncle count and an `index` are
  small: they are not `bigint`. See [values on the wire](https://ethect.dev/rpc/guides/values/).
- **Hashes are checked before sending.** A `blockHash` that is not 32 bytes of hex fails with a
  `CodecError` on the params, and nothing is sent.
