feat: decode extrinsics, and show them
An event is the chain's account of what happened; an extrinsic is the request that caused it, and carries what no event does — who signed, what they called, the nonce and tip, and whether it worked. A history built from events alone omits every failed attempt and never names a submitter. The same oracle does both. The metadata's extrinsic type carries four parameters and the registry names all of them, including `qp_dilithium_crypto::types::DilithiumSignatureScheme` — the part that looked like it would need hand-written post-quantum knowledge and does not. The chain describes its own signature scheme, so `decode_extrinsic` reads a Quantus transaction without a line of code that knows Quantus exists. Two facts about this chain's extrinsics, both now in CLAUDE.md. The first byte is not the version: the top two bits are a type tag, and mainnet carries `0x84` (signed, v4) and `0x05` (bare, v5) in the same block while the metadata declares version 4 — check the byte against the metadata and you reject every timestamp inherent on the chain. And a signature is 5.3 KiB, two orders of magnitude larger than the call it authorises; it is decoded to find where the call begins and then discarded, keeping only the scheme's name and the byte count. Both halves of a block index in one pass, off calls already being made. Whether a dispatch succeeded comes from the `ExtrinsicSuccess`/`ExtrinsicFailed` events in that same decoded list — read, used, not stored. The block page grows a table below the facts: call, signer, arguments, and the signature's size. The account page merges extrinsics into the history it already had, ordered by block with the request above the results it caused. `source` is 1 for an extrinsic and 0 for an event so that every part of the sort key descends, which lets the paging cursor be a plain row comparison rather than a mixed-direction one Postgres cannot express. Nested calls are why this had to be generic. A `Utility::batch_all` carries whole calls in its arguments, so a transfer's recipient can be three levels down — and because the decoder collects account ids wherever they appear, that batched transfer shows on the recipient's page tagged *received* even though they signed nothing. Verified on mainnet 12,616: the batch renders both its transfers with destinations and values, above the two `Balances::Transfer` events they produced. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jp6a8EDar9ueEhAxzep4V5
This commit is contained in:
@@ -22,125 +22,22 @@ import type { AccountEvent } from '../api/generated/AccountEvent'
|
||||
import { RequestFailed, fetchAccount } from '../api/rest'
|
||||
import { exactTokens, height as fmtHeight, shortAddress, tokens, when } from '../lib/format'
|
||||
import { href } from '../lib/routes'
|
||||
import { Payload } from './Payload'
|
||||
|
||||
/** Field names that carry a balance, and so want the token treatment. */
|
||||
const AMOUNTS = new Set(['reward', 'amount', 'value', 'fee', 'actual_fee', 'tip'])
|
||||
|
||||
/** Human labels for the events the site sees most. Anything else uses its own
|
||||
* `Pallet::Variant`, which is already the runtime's answer and not a guess. */
|
||||
/** Human labels for what the site sees most. Anything else shows its own
|
||||
* `Pallet::name`, which is already the runtime's answer and not a guess. */
|
||||
const LABELS: Record<string, string> = {
|
||||
'MiningRewards::MinerRewarded': 'Mining reward',
|
||||
'MiningRewards::FeesCollected': 'Fees collected',
|
||||
'Balances::Transfer': 'Transfer',
|
||||
'Balances::transfer_allow_death': 'Transfer',
|
||||
'Balances::transfer_keep_alive': 'Transfer',
|
||||
'Utility::batch_all': 'Batch',
|
||||
'Timestamp::set': 'Timestamp',
|
||||
}
|
||||
|
||||
function label(event: AccountEvent): string {
|
||||
return LABELS[`${event.pallet}::${event.variant}`] ?? `${event.pallet}::${event.variant}`
|
||||
}
|
||||
|
||||
/** A 32-byte account id, which is what the decoder renders accounts as. */
|
||||
function isAccount(value: unknown): value is string {
|
||||
return typeof value === 'string' && /^0x[0-9a-f]{64}$/.test(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* One event's fields, in the runtime's own vocabulary.
|
||||
*
|
||||
* The account this page is about is folded away: every row here involves it by
|
||||
* definition, and repeating it in every row would bury the half that differs.
|
||||
*/
|
||||
function Fields({
|
||||
event,
|
||||
account,
|
||||
chain,
|
||||
decimals,
|
||||
symbol,
|
||||
}: {
|
||||
event: AccountEvent
|
||||
account: string
|
||||
chain: string
|
||||
decimals: number
|
||||
symbol: string
|
||||
}) {
|
||||
const shown = Object.entries(event.fields as Record<string, unknown>).filter(
|
||||
([, value]) => !(isAccount(value) && value === account),
|
||||
)
|
||||
return (
|
||||
<span className="event-fields">
|
||||
{shown.map(([key, value]) => (
|
||||
<span key={key} className="event-field">
|
||||
{/* The key only when there is something to tell apart. A reward has
|
||||
one field left once this account is folded out of it, and
|
||||
"REWARD 0.31 QTC" in a column headed Detail says the same thing
|
||||
twice. */}
|
||||
{shown.length > 1 && <span className="event-key">{key}</span>}
|
||||
<Value
|
||||
name={key}
|
||||
value={value}
|
||||
chain={chain}
|
||||
addresses={event.addresses}
|
||||
decimals={decimals}
|
||||
symbol={symbol}
|
||||
/>
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function Value({
|
||||
name,
|
||||
value,
|
||||
chain,
|
||||
addresses,
|
||||
decimals,
|
||||
symbol,
|
||||
}: {
|
||||
name: string
|
||||
value: unknown
|
||||
chain: string
|
||||
addresses: Record<string, string>
|
||||
decimals: number
|
||||
symbol: string
|
||||
}) {
|
||||
if (AMOUNTS.has(name) && (typeof value === 'string' || typeof value === 'number')) {
|
||||
const raw = String(value)
|
||||
return (
|
||||
<span className="numeral" title={`${exactTokens(raw, decimals)} ${symbol}`}>
|
||||
{tokens(raw, decimals)} {symbol}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
if (isAccount(value)) {
|
||||
// The counterparty, in the spelling they would recognise and as a way to
|
||||
// their own page. The raw id is kept in the title, because it is what the
|
||||
// chain actually stored and what anyone checking our working needs.
|
||||
const address = addresses[value]
|
||||
if (!address) {
|
||||
return (
|
||||
<span className="numeral" title={value}>
|
||||
{shortAddress(value)}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<Link
|
||||
className="numeral"
|
||||
to={href({ chain, account: address })}
|
||||
title={`${address}\n${value}`}
|
||||
>
|
||||
{shortAddress(address)}
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
// Anything the cases above do not know. Shown rather than dropped: the index
|
||||
// holds every field the runtime emitted, and a page that hides the ones it
|
||||
// has no styling for is lying by omission.
|
||||
return (
|
||||
<span className="numeral">
|
||||
{typeof value === 'object' ? JSON.stringify(value) : String(value)}
|
||||
</span>
|
||||
)
|
||||
function label(row: AccountEvent): string {
|
||||
return LABELS[`${row.pallet}::${row.name}`] ?? `${row.pallet}::${row.name}`
|
||||
}
|
||||
|
||||
export function AccountPanel({
|
||||
@@ -267,7 +164,7 @@ export function AccountPanel({
|
||||
<tr>
|
||||
<th scope="col">Block</th>
|
||||
<th scope="col" className="left">
|
||||
Event
|
||||
What
|
||||
</th>
|
||||
<th scope="col" className="left">
|
||||
Detail
|
||||
@@ -278,25 +175,59 @@ export function AccountPanel({
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{detail.activity.map((event) => (
|
||||
<tr key={`${event.height}-${event.event_index}`}>
|
||||
{detail.activity.map((row) => (
|
||||
<tr key={`${row.height}-${row.source}-${row.index}`}>
|
||||
<td className="numeral">
|
||||
<Link to={href({ chain, block: String(event.height) })}>
|
||||
#{fmtHeight(event.height)}
|
||||
<Link to={href({ chain, block: String(row.height) })}>
|
||||
#{fmtHeight(row.height)}
|
||||
</Link>
|
||||
</td>
|
||||
<td className="left">{label(event)}</td>
|
||||
<td className="left">
|
||||
<Fields
|
||||
event={event}
|
||||
account={detail.account}
|
||||
chain={chain}
|
||||
decimals={decimals}
|
||||
symbol={symbol}
|
||||
<span className="miner-cell">
|
||||
{label(row)}
|
||||
{/* A request and a result read differently and must
|
||||
not look the same: an extrinsic is what somebody
|
||||
asked for, and unlike an event it can have
|
||||
failed. */}
|
||||
{row.source === 'extrinsic' && (
|
||||
<span
|
||||
className="chip chip-submitted"
|
||||
title={
|
||||
row.signer === null
|
||||
? 'An inherent: inserted by the block author, not submitted by anyone.'
|
||||
: row.signer === detail.account
|
||||
? 'Submitted and signed by this account.'
|
||||
: 'Submitted by another account; this one is named in the call.'
|
||||
}
|
||||
>
|
||||
{row.signer === null
|
||||
? 'inherent'
|
||||
: row.signer === detail.account
|
||||
? 'sent'
|
||||
: 'received'}
|
||||
</span>
|
||||
)}
|
||||
{row.success === false && (
|
||||
<span className="chip chip-failed" title="The dispatch failed.">
|
||||
failed
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
</td>
|
||||
<td className="left">
|
||||
<Payload
|
||||
value={row.fields}
|
||||
ctx={{
|
||||
chain,
|
||||
addresses: row.addresses,
|
||||
decimals,
|
||||
symbol,
|
||||
focus: detail.account,
|
||||
}}
|
||||
/>
|
||||
</td>
|
||||
<td className="numeral event-when" style={{ color: 'var(--text-muted)' }}>
|
||||
{when(event.at)}
|
||||
{when(row.at)}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
@@ -310,7 +241,9 @@ export function AccountPanel({
|
||||
has not made that claim yet — saying so is the difference between
|
||||
"nothing before block 8,075" and "nothing, as far as we looked". */}
|
||||
<p className="panel-note">
|
||||
Read from the chain's own events, decoded against the runtime that produced each block.
|
||||
Requests and results together: an <strong>extrinsic</strong> is what somebody asked the
|
||||
chain to do, an event is what it then reported. Both decoded against the runtime that
|
||||
produced each block.
|
||||
{detail.indexed_from !== null && detail.indexed_to !== null && (
|
||||
<>
|
||||
{' '}
|
||||
|
||||
@@ -21,8 +21,16 @@ import { Link } from 'react-router-dom'
|
||||
|
||||
import type { BlockDetail } from '../api/generated/BlockDetail'
|
||||
import { RequestFailed, fetchBlock } from '../api/rest'
|
||||
import { ago, bigNumber, height as fmtHeight, seconds, shortMiner } from '../lib/format'
|
||||
import {
|
||||
ago,
|
||||
bigNumber,
|
||||
height as fmtHeight,
|
||||
seconds,
|
||||
shortAddress,
|
||||
shortMiner,
|
||||
} from '../lib/format'
|
||||
import { href } from '../lib/routes'
|
||||
import { Payload } from './Payload'
|
||||
|
||||
/** A label and its value, with the caveat that makes the value readable. */
|
||||
function Fact({
|
||||
@@ -55,6 +63,8 @@ export function BlockPanel({
|
||||
chain,
|
||||
block,
|
||||
targetSeconds,
|
||||
decimals,
|
||||
symbol,
|
||||
onResolved,
|
||||
}: {
|
||||
chain: string
|
||||
@@ -62,6 +72,10 @@ export function BlockPanel({
|
||||
block: string
|
||||
/** The chain's target block time, for reading the gap against. */
|
||||
targetSeconds: number
|
||||
/** The chain's token decimals, for rendering call arguments. */
|
||||
decimals: number
|
||||
/** Its symbol. */
|
||||
symbol: string
|
||||
/** Called with the canonical hash once a height has resolved to one. */
|
||||
onResolved: (hash: string) => void
|
||||
}) {
|
||||
@@ -217,6 +231,99 @@ export function BlockPanel({
|
||||
</Fact>
|
||||
</div>
|
||||
|
||||
{/* The body, decoded. The count in the facts above comes from the
|
||||
node and goes when the node forgets the block; these come from
|
||||
this observer's index and stay. An empty list is therefore not
|
||||
"no extrinsics" — a block always has at least a timestamp — it is
|
||||
"the index has not read this block yet", and it says so. */}
|
||||
<div className="runtime-group" style={{ padding: '14px 16px 4px' }}>
|
||||
<div className="eyebrow">
|
||||
Extrinsics{detail.body.length > 0 && ` (${detail.body.length})`}
|
||||
</div>
|
||||
{detail.body.length === 0 ? (
|
||||
<p className="panel-note" style={{ border: 0, padding: '8px 0 8px' }}>
|
||||
{detail.extrinsics === null
|
||||
? 'Not indexed, and the node cannot answer for this block either.'
|
||||
: `The node reports ${detail.extrinsics} in the body; the event index has not read this block yet. It works outward from the tip.`}
|
||||
</p>
|
||||
) : (
|
||||
<div className="scroll-x">
|
||||
<table className="board">
|
||||
<caption className="visually-hidden">
|
||||
This block's extrinsics, in body order
|
||||
</caption>
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">#</th>
|
||||
<th scope="col" className="left">
|
||||
Call
|
||||
</th>
|
||||
<th scope="col" className="left">
|
||||
Signer
|
||||
</th>
|
||||
<th scope="col" className="left">
|
||||
Arguments
|
||||
</th>
|
||||
<th scope="col">Signature</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{detail.body.map((x) => (
|
||||
<tr key={x.index}>
|
||||
<td className="numeral">{x.index}</td>
|
||||
<td className="left">
|
||||
<span className="miner-cell">
|
||||
<code>
|
||||
{x.pallet}::{x.call}
|
||||
</code>
|
||||
{x.success === false && (
|
||||
<span className="chip chip-failed" title="The dispatch failed.">
|
||||
failed
|
||||
</span>
|
||||
)}
|
||||
</span>
|
||||
</td>
|
||||
<td className="left">
|
||||
{x.signer_address ? (
|
||||
<Link
|
||||
className="numeral"
|
||||
to={href({ chain, account: x.signer_address })}
|
||||
title={`${x.signer_address}\n${x.signer}`}
|
||||
>
|
||||
{shortAddress(x.signer_address)}
|
||||
</Link>
|
||||
) : (
|
||||
/* Not anonymous: nobody signed it. An inherent is
|
||||
the block author's own, and the chain accepts it
|
||||
because of where it sits, not because of a
|
||||
signature. */
|
||||
<span className="runtime-modifier">inherent</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="left">
|
||||
<Payload
|
||||
value={x.args}
|
||||
ctx={{ chain, addresses: x.addresses, decimals, symbol }}
|
||||
/>
|
||||
</td>
|
||||
<td
|
||||
className="numeral"
|
||||
title={
|
||||
x.signature
|
||||
? `${x.signature}, ${x.signature_bytes.toLocaleString('en-US')} bytes`
|
||||
: undefined
|
||||
}
|
||||
>
|
||||
{x.signature ? `${Math.round(x.signature_bytes / 1024)} KiB` : '—'}
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="block-links">
|
||||
{detail.parent_hash && (
|
||||
<div>
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Rendering a decoded call's arguments or an event's fields.
|
||||
*
|
||||
* One component for both because they are the same thing: a JSON object whose
|
||||
* keys are names the runtime chose, whose account ids are `0x` hex, and whose
|
||||
* integers past 2^53 are strings. Neither the block page nor the account page
|
||||
* knows what a transfer is, and neither should — a pallet added next year
|
||||
* renders here without a line of code.
|
||||
*
|
||||
* `AMOUNTS` is presentation only. A field it does not recognise still appears,
|
||||
* as its key and its value; the list decides which of them get the token
|
||||
* treatment rather than which of them exist. That is the difference between a
|
||||
* page that shows what happened and one that shows the subset somebody
|
||||
* remembered to write a case for.
|
||||
*/
|
||||
|
||||
import { Link } from 'react-router-dom'
|
||||
|
||||
import { exactTokens, shortAddress, tokens } from '../lib/format'
|
||||
import { href } from '../lib/routes'
|
||||
|
||||
/** Field names that carry a balance, and so want the token treatment. */
|
||||
const AMOUNTS = new Set(['reward', 'amount', 'value', 'fee', 'actual_fee', 'tip'])
|
||||
|
||||
/** A 32-byte account id, which is what the decoder renders accounts as. */
|
||||
function isAccount(value: unknown): value is string {
|
||||
return typeof value === 'string' && /^0x[0-9a-f]{64}$/.test(value)
|
||||
}
|
||||
|
||||
export interface PayloadContext {
|
||||
chain: string
|
||||
/** `0x hex → SS58` for every account the payload mentions. */
|
||||
addresses: Record<string, string>
|
||||
decimals: number
|
||||
symbol: string
|
||||
/** Folded out of the rendering: the account whose page this is. Every row
|
||||
* involves it by definition, and repeating it buries the half that differs. */
|
||||
focus?: string
|
||||
}
|
||||
|
||||
export function Payload({ value, ctx }: { value: unknown; ctx: PayloadContext }) {
|
||||
const shown = Object.entries((value ?? {}) as Record<string, unknown>).filter(
|
||||
([, v]) => !(ctx.focus && isAccount(v) && v === ctx.focus),
|
||||
)
|
||||
if (shown.length === 0) return <span className="event-fields">—</span>
|
||||
return (
|
||||
<span className="event-fields">
|
||||
{shown.map(([key, v]) => (
|
||||
<span key={key} className="event-field">
|
||||
{/* The key only when there is something to tell apart. A reward has
|
||||
one field left once the page's own account is folded out, and
|
||||
"REWARD 0.31 QTC" in a column headed Detail says it twice. */}
|
||||
{shown.length > 1 && <span className="event-key">{key}</span>}
|
||||
<Value name={key} value={v} ctx={ctx} />
|
||||
</span>
|
||||
))}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
function Value({ name, value, ctx }: { name: string; value: unknown; ctx: PayloadContext }) {
|
||||
if (AMOUNTS.has(name) && (typeof value === 'string' || typeof value === 'number')) {
|
||||
const raw = String(value)
|
||||
return (
|
||||
<span className="numeral" title={`${exactTokens(raw, ctx.decimals)} ${ctx.symbol}`}>
|
||||
{tokens(raw, ctx.decimals)} {ctx.symbol}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
if (isAccount(value)) {
|
||||
// The counterparty in the spelling they would recognise, and as a way to
|
||||
// their own page. The raw id stays in the title, because it is what the
|
||||
// chain stored and what anyone checking our working needs.
|
||||
const address = ctx.addresses[value]
|
||||
if (!address) {
|
||||
return (
|
||||
<span className="numeral" title={value}>
|
||||
{shortAddress(value)}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<Link
|
||||
className="numeral"
|
||||
to={href({ chain: ctx.chain, account: address })}
|
||||
title={`${address}\n${value}`}
|
||||
>
|
||||
{shortAddress(address)}
|
||||
</Link>
|
||||
)
|
||||
}
|
||||
|
||||
// A nested structure: a `MultiAddress::Id` wrapper, or a whole call inside a
|
||||
// `Utility::batch_all`. Recursing rather than stringifying is what lets a
|
||||
// batched transfer show its recipient instead of a wall of JSON.
|
||||
if (value && typeof value === 'object') {
|
||||
const entries = Object.entries(value as Record<string, unknown>)
|
||||
// A single-key object is a variant wrapper — `{Id: "0x…"}`,
|
||||
// `{Balances: {…}}` — and its key is the name, not a field.
|
||||
if (entries.length === 1) {
|
||||
const [only, inner] = entries[0]!
|
||||
if (isAccount(inner) || typeof inner !== 'object') {
|
||||
return <Value name={name} value={inner} ctx={ctx} />
|
||||
}
|
||||
return (
|
||||
<span className="event-nested">
|
||||
<span className="event-key">{only}</span>
|
||||
<Payload value={inner} ctx={ctx} />
|
||||
</span>
|
||||
)
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
return (
|
||||
<span className="event-nested">
|
||||
{value.map((item, i) => (
|
||||
<Value key={i} name={name} value={item} ctx={ctx} />
|
||||
))}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<span className="event-nested">
|
||||
<Payload value={value} ctx={ctx} />
|
||||
</span>
|
||||
)
|
||||
}
|
||||
|
||||
// Anything the cases above do not know. Shown rather than dropped: the index
|
||||
// holds every field the runtime emitted, and a page that hides the ones it
|
||||
// has no styling for is lying by omission.
|
||||
return <span className="numeral">{String(value)}</span>
|
||||
}
|
||||
Reference in New Issue
Block a user