# Network difficulty by epoch

`GET /stats/mining/difficulty` · Lithos client API · Statistics

Source: https://lithosprotocol.org/docs/api/get-difficulty-epochs

The network difficulty curve, one point per difficulty epoch. Ergo only readjusts difficulty
on epoch boundaries, so every block inside an epoch carries the same value and one record
describes all 128 of them. That makes the table cheap enough to keep for years — it is not
bounded by the history retention window the other statistics use.

Each epoch carries its height range, its first and last block timestamps, and
`hashesPerSecond`: the work of the epoch's block intervals divided by the time those same
intervals took. Use it for network hashrate as well as difficulty; the two differ only by
how long the blocks took.

`current` is the epoch in progress. It is recomputed on every collection cycle rather than
stored, so it is the only entry that can still change, and its `complete` is false. Stored
epochs stay a full epoch behind the tip so a reorg cannot rewrite one.

Indices count epochs from height 1. With no range the newest `limit` epochs are returned.
`oldestIndex` and `newestIndex` report what the table actually holds, and `backfillComplete`
says whether it has finished walking back to its floor — on mainnet that floor is the EIP-37
activation, below which epochs were 1024 blocks long and are not described here.

## Parameters

| Name | In | Type | Required | Description |

|---|---|---|---|---|

| `from` | query | integer · int32 | no | First epoch index to include. Clamped to `oldestIndex`. |

| `to` | query | integer · int32 | no | Last epoch index to include; defaults to `newestIndex` and cannot exceed it. |

| `limit` | query | integer · int32 | no | How many epochs to return, counted back from `to`. |

## Responses

### 200: The requested epochs, oldest first, plus the epoch in progress.

`DifficultyEpochHistory`

- `source` (MiningCursor, required): The processed chain point a statistics result was read from.
  - `height` (integer, required)
  - `blockId` (string, required)
  - `parentId` (string, required)
  - `timestamp` (integer · int64, required): The block's own timestamp in Unix milliseconds, not the time the client read it.
- `epochLength` (integer, required): Blocks per epoch. 128 on mainnet since EIP-37, and on testnet throughout.
- `epochs` (DifficultyEpoch[], required): Stored epochs in the requested window, oldest first. A gap means that epoch is not held.
  - `index` (integer · int32, required): Epoch number, counting from height 1. Epoch `n` spans heights `128n + 1` to `128n + 128`.
  - `startHeight` (integer, required): First block of the epoch, and the one whose header supplied `difficulty`.
  - `endHeight` (integer, required): Last block of the epoch, or the current tip while the epoch is still in progress.
  - `startTimestamp` (integer · int64, required): Timestamp of `startHeight`, in Unix milliseconds.
  - `endTimestamp` (integer · int64, required): Timestamp of `endHeight`, in Unix milliseconds.
  - `difficulty` (string, required): Required difficulty for every block in the range, as an exact decimal integer.
  - `complete` (boolean, required): False only for the epoch in progress, whose `endHeight` still moves with the tip.
  - `hashesPerSecond` (string): Network hashrate across the epoch: `difficulty × (endHeight − startHeight)` divided by the seconds between the two timestamps, so both sides count the same block intervals. Omitted when the range covers no interval or no elapsed time.
- `current` (DifficultyEpoch): One difficulty epoch. Every block from `startHeight` to `endHeight` carries `difficulty`, because Ergo only readjusts on epoch boundaries.
- `previous` (DifficultyEpoch): One difficulty epoch. Every block from `startHeight` to `endHeight` carries `difficulty`, because Ergo only readjusts on epoch boundaries.
- `oldestIndex` (integer · int32): Oldest epoch the table holds; absent while it is empty.
- `newestIndex` (integer · int32): Newest stored epoch, which stays a full epoch behind the tip. Absent while the table is empty.
- `backfillComplete` (boolean, required): Whether the table has reached its floor. While false it is still walking backwards and older epochs will appear.
- `status` (string, required, one of disabled, loading, ready, stale, unavailable, catching-up, recovering): How far collection has got. `ready` means it reached the last checked chain tip. `catching-up` and `recovering` mean replay or reorg work is still running, `stale` means the last successful check is too old, and `unavailable` means collection failed. On `disabled` or `loading` the numbers are not evidence that the period was quiet.

### 400: A negative index, a reversed range, or a limit outside 1–512.

`ApiError`

- `error` (integer, required): Error code
- `reason` (string, required): Error message explaining the reason of the error
- `detail` (string, required): Detailed error description

### 503: History is loading, disabled, busy, or unreadable. Check `mining.status` in /stats before retrying.

`ApiError`

- `error` (integer, required): Error code
- `reason` (string, required): Error message explaining the reason of the error
- `detail` (string, required): Detailed error description

## Example request

```bash
curl -X GET "http://localhost:9000/stats/mining/difficulty"
```

## Example response

```json
{
  "source": {
    "height": 0,
    "blockId": "string",
    "parentId": "string",
    "timestamp": 0
  },
  "epochLength": 0,
  "epochs": [
    {
      "index": 0,
      "startHeight": 0,
      "endHeight": 0,
      "startTimestamp": 0,
      "endTimestamp": 0,
      "difficulty": "0",
      "complete": true,
      "hashesPerSecond": "0"
    }
  ],
  "current": {
    "index": 0,
    "startHeight": 0,
    "endHeight": 0,
    "startTimestamp": 0,
    "endTimestamp": 0,
    "difficulty": "0",
    "complete": true,
    "hashesPerSecond": "0"
  },
  "previous": {
    "index": 0,
    "startHeight": 0,
    "endHeight": 0,
    "startTimestamp": 0,
    "endTimestamp": 0,
    "difficulty": "0",
    "complete": true,
    "hashesPerSecond": "0"
  },
  "oldestIndex": 0,
  "newestIndex": 0,
  "backfillComplete": true,
  "status": "disabled"
}
```
