feat: block explorer routes, addressed by hash
`#/:chain/:height` opens a block and then rewrites the address to
`#/:chain/:hash`. The redirect is the point, not tidiness: `block` is keyed on
(chain, height) and a reorg overwrites, so a height names a *position* and a
link to one comes to mean a different block the moment the chain forks there. A
hash names one block for good — including after it has lost, which is the only
way an orphan is linkable at all.
`GET /v1/chains/{chain}/blocks/{ref}` takes either spelling; a decimal number
and 32 bytes of hex cannot be confused, so a caller holding a number is not made
to guess which one this API wanted. The same reasoning puts block refs in the
second path segment beside the window: no window name is all digits or 0x plus
64 hex characters.
The answer is assembled from two sources because neither is sufficient, and
they fail at different times:
- The node holds the block and forgets it. Non-canonical bodies go once finality
passes them (blocks-pruning defaults to archive-canonical, and Planck finalises
~100 blocks back), and difficulty is a state read behind a 256-block default.
- The observer holds what the node never had — when the block was seen, whether
that sighting was at the tip — and what it has since forgotten: the difficulty
read while the state behind it still existed.
So resolution degrades in a stated order rather than failing. Node first; a
header it cannot serve falls back to `block_displacement`, which is what makes
an orphan describable at all; a node that is away falls back to the recorded
row. `from_node` tells the reader which they are looking at, and every value
neither source has is spelled out in words — "the body is pruned", "this
observer never saw this block" — because a blank on a block page reads as zero.
Verified against the live chain: a tip block carries difficulty, one extrinsic,
a 1.9 s gap and 473 ms of propagation; height 1000000 keeps its body and loses
difficulty to state pruning; genesis has no author digest and does not panic; a
seeded displacement serves as an orphan with the winner linked, and the winner
lists it in `also_seen`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jp6a8EDar9ueEhAxzep4V5
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
|
||||
import type { AttributionSource } from "./AttributionSource";
|
||||
import type { BigUintDec } from "./BigUintDec";
|
||||
import type { ChainId } from "./ChainId";
|
||||
import type { MinerId } from "./MinerId";
|
||||
|
||||
/**
|
||||
* One block, as completely as it can still be described.
|
||||
*
|
||||
* Assembled from two sources that know different things, because neither is
|
||||
* sufficient on its own:
|
||||
*
|
||||
* - The **node** holds the block itself — parent, body, and whether this hash
|
||||
* still holds its height. It forgets: `blocks-pruning` defaults to
|
||||
* `archive-canonical`, so a non-canonical body goes once finality passes it,
|
||||
* and difficulty is a state read behind a 256-block window.
|
||||
* - The **observer** holds what the node never had — when we first saw the
|
||||
* block, whether that sighting was at the tip — and what it has since
|
||||
* forgotten, the difficulty recorded while the state still existed.
|
||||
*
|
||||
* Every field the pair cannot supply is `None` rather than a substituted
|
||||
* plausible value. A block page that quietly filled in the current difficulty
|
||||
* for one it could not look up would be the same lie the ingest path was fixed
|
||||
* to stop telling.
|
||||
*/
|
||||
export type BlockDetail = {
|
||||
/**
|
||||
* Which chain.
|
||||
*/
|
||||
chain: ChainId,
|
||||
/**
|
||||
* Block number.
|
||||
*/
|
||||
height: number,
|
||||
/**
|
||||
* This block's hash — the identity the page is addressed by, so that a
|
||||
* link keeps meaning the same block after the height has moved on.
|
||||
*/
|
||||
hash: string,
|
||||
/**
|
||||
* Parent hash, from the header.
|
||||
*/
|
||||
parent_hash: string | null,
|
||||
/**
|
||||
* Author's reward preimage from the `pow_` PreRuntime digest.
|
||||
*/
|
||||
miner: MinerId | null,
|
||||
/**
|
||||
* What to call the author: a telemetry node name, or the abbreviated
|
||||
* preimage. `None` where the author could not be decoded at all.
|
||||
*/
|
||||
display: string | null,
|
||||
/**
|
||||
* Whether `display` is an inferred name or the preimage.
|
||||
*/
|
||||
attribution: AttributionSource | null,
|
||||
/**
|
||||
* How much of the attribution vote window agrees, 0.0–1.0.
|
||||
*/
|
||||
confidence: number | null,
|
||||
/**
|
||||
* Expected hashes to win this block: the difficulty in force at its
|
||||
* parent, which is the figure the miner actually had to meet.
|
||||
*/
|
||||
difficulty: BigUintDec | null,
|
||||
/**
|
||||
* The author's own timestamp, from the block's `Timestamp::set` inherent.
|
||||
*/
|
||||
authored_at: string | null,
|
||||
/**
|
||||
* When this observer first saw the block. `None` for a block it never saw
|
||||
* at all — an orphan on a fork it missed, or history before it started.
|
||||
*/
|
||||
observed_at: string | null,
|
||||
/**
|
||||
* Whether `observed_at` was a tip sighting. When false, the difference
|
||||
* between the timestamps measures when the observer got round to looking
|
||||
* and nothing about the network, so the UI must not read it as propagation.
|
||||
*/
|
||||
at_tip: boolean | null,
|
||||
/**
|
||||
* Seconds between this block's timestamp and its parent's — how long this
|
||||
* block took to find, against the chain's target.
|
||||
*/
|
||||
seconds_since_parent: number | null,
|
||||
/**
|
||||
* Extrinsics in the body, inherents included. `None` once the body has
|
||||
* been pruned.
|
||||
*/
|
||||
extrinsics: number | null,
|
||||
/**
|
||||
* Whether this hash is the one that currently holds this height.
|
||||
*/
|
||||
canonical: boolean,
|
||||
/**
|
||||
* The hash that currently holds this height. Equal to `hash` when
|
||||
* canonical; on an orphan page it is the link to the block that won.
|
||||
*/
|
||||
canonical_hash: string | null,
|
||||
/**
|
||||
* Other hashes this observer has recorded at this height — the losing
|
||||
* sides of past reorgs. Navigable, which is the whole reason the
|
||||
* displacement is written down.
|
||||
*/
|
||||
also_seen: Array<string>,
|
||||
/**
|
||||
* False when the node could not be reached or no longer holds the block,
|
||||
* so everything here came from what the observer had already recorded.
|
||||
* The UI owes the reader that distinction rather than showing gaps.
|
||||
*/
|
||||
from_node: boolean, };
|
||||
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
import type { ApiError } from './generated/ApiError'
|
||||
import type { BlockDetail } from './generated/BlockDetail'
|
||||
import type { ChainSeries } from './generated/ChainSeries'
|
||||
import type { MinerDetail } from './generated/MinerDetail'
|
||||
import type { Window as WindowName } from './generated/Window'
|
||||
@@ -59,6 +60,24 @@ export function fetchSeries(
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* One block, by height or by hash.
|
||||
*
|
||||
* A height names whatever currently holds it and a hash names one block for
|
||||
* good, so the caller redirects to the hash once the height resolves — see
|
||||
* `BlockPanel`. The API accepts either spelling at the same path.
|
||||
*/
|
||||
export function fetchBlock(
|
||||
chain: string,
|
||||
block: string,
|
||||
signal?: AbortSignal,
|
||||
): Promise<BlockDetail> {
|
||||
return get<BlockDetail>(
|
||||
`/chains/${encodeURIComponent(chain)}/blocks/${encodeURIComponent(block)}`,
|
||||
signal,
|
||||
)
|
||||
}
|
||||
|
||||
/** One miner's standing and history. */
|
||||
export function fetchMiner(
|
||||
chain: string,
|
||||
|
||||
Reference in New Issue
Block a user