# Raw data

The seven methods that return chain data in its encoded form: a header, a block, transactions,
receipts and a block access list as the bytes a node stores and hashes.

| Method | JSON-RPC | Returns | Description |
| --- | --- | --- | --- |
| [`getRawHeader`](#getrawheader) | `debug_getRawHeader` | `Hex` | A block header, RLP-encoded. |
| [`getRawBlock`](#getrawblock) | `debug_getRawBlock` | `Hex` | A block, RLP-encoded. |
| [`getRawTransaction`](#getrawtransaction) | `debug_getRawTransaction` | `Hex \| null` | A transaction in its signed, encoded form, by hash. |
| [`getRawTransactions`](#getrawtransactions) | `debug_getRawTransactions` | `Hex[]` | The transactions of a block, each in its signed, encoded form. |
| [`getRawReceipts`](#getrawreceipts) | `debug_getRawReceipts` | `Hex[]` | The receipts of a block, each in its encoded form. |
| [`getRawBlockAccessList`](#getrawblockaccesslist) | `debug_getRawBlockAccessList` | `Hex` | What a block read and wrote, RLP-encoded (EIP-7928). |
| [`getBadBlocks`](#getbadblocks) | `debug_getBadBlocks` | bad blocks | The invalid blocks the node saw on the network lately. |

Each returns an [`Effect<…, Method.Error, Transport>`](https://ethect.dev/rpc/method/#calling-a-method).
The methods about a block take a [`BlockId`](https://ethect.dev/rpc/types/block-id/): a number, a tag or an
object that names the block by hash. The param is required.

ethect has no RLP decoder: the bytes are returned as [`Hex`](https://ethect.dev/reference/primitives/hex/), for
you to hash, compare or hand to another tool.

## getRawHeader

Returns the header of a block, RLP-encoded. Its Keccak-256 hash is the hash of the block.

```ts
import { Effect } from "effect";
import { Hash, Hex } from "@ethect/core";
import { Debug } from "@ethect/core/rpc";

const blockHash = Effect.gen(function* () {
  const header = yield* Debug.getRawHeader("latest");
  const bytes = yield* Hex.toBytes(header);
  return Hash.keccak256(bytes); // the `hash` of the latest block
});
```

- **JSON-RPC:** `debug_getRawHeader`
- **Parameters:** `block` ([`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/).
- For a block it does not have, anvil 1.9 fails the call with an `RpcError` of reason
  `ResourceNotFound`.

## getRawBlock

Returns a block, RLP-encoded: its header, its transactions and the rest of its body.

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

const sizes = Effect.gen(function* () {
  const raw = yield* Debug.getRawBlock("latest");
  const block = yield* Eth.getBlockByNumber("latest", false);
  return { bytes: Hex.size(raw), size: block?.size }; // the same number
});
```

- **JSON-RPC:** `debug_getRawBlock`
- **Parameters:** `block` ([`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/). It is what
  [`Debug.traceBlock`](https://ethect.dev/rpc/debug/trace-block/#traceblock) takes.
- For a block it does not have, anvil 1.9 fails the call with an `RpcError` of reason
  `ResourceNotFound`.

## getRawTransaction

Returns a transaction in its signed, encoded form, by hash.

```ts
import { Debug } from "@ethect/core/rpc";
import type { Hash } from "@ethect/core";

// "0x02f8…" for an EIP-1559 transaction, or null
const raw = (hash: Hash.Hash) => Debug.getRawTransaction(hash);
```

- **JSON-RPC:** `debug_getRawTransaction`
- **Parameters:** `hash` ([`Hash`](https://ethect.dev/reference/primitives/hash/)).
- **Returns:** `Hex | null`. `null` when the node knows no transaction with that hash. A
  transaction still in the pool is returned too.
- It is the value
  [`Eth.getRawTransactionByHash`](https://ethect.dev/rpc/eth/transaction-lookups/#getrawtransactionbyhash)
  returns, and what
  [`Eth.sendRawTransaction`](https://ethect.dev/rpc/eth/send-raw-transaction/) takes.

## getRawTransactions

Returns the transactions of a block, each in its signed, encoded form.

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

const count = Debug.getRawTransactions("latest").pipe(
  Effect.map((transactions) => transactions.length),
);
```

- **JSON-RPC:** `debug_getRawTransactions`
- **Parameters:** `block` ([`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** `ReadonlyArray<Hex>`, in the order of the block. Empty for a block without
  transactions.
- Not served by every client.
- anvil 1.9 also answers an empty array for a block it does not have.

## getRawReceipts

Returns the receipts of a block, each in its encoded form.

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

const types = Effect.gen(function* () {
  const receipts = yield* Debug.getRawReceipts("latest");
  // a typed receipt starts with its type byte: "0x02" for EIP-1559
  return receipts.map((receipt) => receipt.slice(0, 4));
});
```

- **JSON-RPC:** `debug_getRawReceipts`
- **Parameters:** `block` ([`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** `ReadonlyArray<Hex>`, one per transaction of the block.
- For a block number above the head, anvil 1.9 fails the call with an `RpcError` of reason
  `InvalidParams`.

## getRawBlockAccessList

Returns what a block read and wrote, RLP-encoded (EIP-7928). The decoded form is
[`Eth.getBlockAccessList`](https://ethect.dev/rpc/eth/block-details/#getblockaccesslist).

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

const accessList = Debug.getRawBlockAccessList("latest").pipe(
  Effect.catchReason("RpcError", "MethodNotFound", () => Effect.succeed(null)),
);
```

- **JSON-RPC:** `debug_getRawBlockAccessList`
- **Parameters:** `block` ([`BlockId.BlockId`](https://ethect.dev/rpc/types/block-id/)).
- **Returns:** [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/).
- anvil 1.9 does not serve it: it answers `Method not found`. The method was checked against a
  [mock](https://ethect.dev/reference/testing/mock-provider/), not against a node.

## getBadBlocks

Returns the invalid blocks the node saw on the network lately, each decoded and RLP-encoded.

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

const hashes = Debug.getBadBlocks().pipe(
  Effect.map((bad) => bad.map((entry) => entry.hash)),
);
```

- **JSON-RPC:** `debug_getBadBlocks`
- **Parameters:** none.
- **Returns:** an array of objects with three fields, described below.
- anvil 1.9 does not serve it: it answers `Method not found`. The method was checked against a
  [mock](https://ethect.dev/reference/testing/mock-provider/), not against a node.

| Field | Type | Description |
| --- | --- | --- |
| `block` | [`Block.Full`](https://ethect.dev/rpc/types/block/) | the block, with its transactions in full |
| `hash` | [`Hash`](https://ethect.dev/reference/primitives/hash/) | the hash of the block |
| `rlp` | [`Hex.Hex`](https://ethect.dev/reference/primitives/hex/) | the block, RLP-encoded |

## Behavior

- **The block param is required.** Unlike the state methods of `Eth`, these methods do not
  leave the block to the node. Pass `"latest"` for the head.
- **By hash too.** A block can be named as `{ blockHash }`. The header and the block anvil
  returns are then the ones the number gives.
- **A missing block is not reported the same way everywhere.** On anvil 1.9, `getRawHeader`
  and `getRawBlock` fail with `ResourceNotFound`, `getRawReceipts` fails with `InvalidParams`,
  and `getRawTransactions` succeeds with an empty array.
- **A method the node does not serve** fails with an `RpcError` of reason `MethodNotFound`.
