From 33a80cb458ab2e400b962c8cdc1e926b936d485b Mon Sep 17 00:00:00 2001 From: Victor Oliva Date: Sat, 15 Nov 2025 18:57:44 -0300 Subject: [PATCH] move block author to a separate file --- src/pages/Explorer/Detail/BlockAuthor.tsx | 154 +++++++++++++++++++++ src/pages/Explorer/Detail/BlockInfo.tsx | 161 +--------------------- 2 files changed, 158 insertions(+), 157 deletions(-) create mode 100644 src/pages/Explorer/Detail/BlockAuthor.tsx diff --git a/src/pages/Explorer/Detail/BlockAuthor.tsx b/src/pages/Explorer/Detail/BlockAuthor.tsx new file mode 100644 index 0000000..a6db4b3 --- /dev/null +++ b/src/pages/Explorer/Detail/BlockAuthor.tsx @@ -0,0 +1,154 @@ +import { client$ } from "@/state/chains/chain.state" +import { Polkadot_people } from "@polkadot-api/descriptors" +import { + _void, + Bytes, + HexString, + Struct, + u32, + u64, + Variant, +} from "@polkadot-api/substrate-bindings" +import { state, useStateObservable } from "@react-rxjs/core" +import { BlockHeader, PolkadotClient } from "polkadot-api" +import { AddressIdentity } from "polkahub" +import { FC, useEffect } from "react" +import { + catchError, + combineLatest, + from, + map, + Observable, + switchMap, + take, +} from "rxjs" +import { blockInfo$, BlockState, finalized$ } from "../block.state" + +const validatorCache: WeakMap< + PolkadotClient, + Record +> = new WeakMap() + +const fetchValidators = ( + client: PolkadotClient, + at: HexString, +): Promise => + client.getUnsafeApi().query.Session.Validators.getValue({ + at, + }) +const fetchCachedValidators$ = (client: PolkadotClient, at: HexString) => { + const api = client.getUnsafeApi() + return from(api.query.Session.CurrentIndex.getValue()).pipe( + switchMap(async (idx) => { + const cache = validatorCache.get(client) ?? {} + validatorCache.set(client, cache) + + const cached = cache[idx] + if (cached) return { idx, validators: cached } + const validators = await fetchValidators(client, at) + return { idx, validators } + }), + ) +} +const validators$ = state( + (block: HexString) => + client$.pipe( + switchMap((client) => + combineLatest([ + fetchCachedValidators$(client, block), + blockInfo$(block).pipe(map((v) => v.status)), + ]).pipe( + map(([{ idx, validators }, blockStatus]) => { + if ( + blockStatus === BlockState.Finalized || + blockStatus === BlockState.Unknown + ) { + const cache = validatorCache.get(client) ?? {} + validatorCache.set(client, cache) + cache[idx] = validators + } + return validators + }), + withHodl(() => client.hodlBlock(block)), + take(1), + catchError(() => [null]), + ), + ), + ), + null, +) + +// Subscribe to the validators of the finalized block so that best blocks author's of the same session will load faster +const finalizedValidators$ = finalized$.pipe( + switchMap((block) => validators$(block.hash)), +) + +export const BlockAuthor: FC<{ hash: HexString; header: BlockHeader }> = ({ + hash, + header, +}) => { + const validators = useStateObservable(validators$(hash)) + const idx = getAuthorityIdx(header) + + useEffect(() => { + const sub = finalizedValidators$.subscribe() + return () => sub.unsubscribe() + }, []) + + if (idx == null || validators == null) return
+ + return ( + + ) +} + +const DigestWithVRF = Struct({ + authority_index: u32, + slot: u64, + vrf_signature: Struct({ + pre_output: Bytes(32), + proof: Bytes(64), + }), +}) +const BabePreDigest = Variant({ + Uknown: _void, + Primary: DigestWithVRF, + SecondaryPlain: Struct({ + authority_index: u32, + slot: u64, + }), + SecondaryVRF: DigestWithVRF, +}) + +const getAuthorityIdx = (header: BlockHeader) => { + const preRuntime = header.digests.find((d) => d.type === "preRuntime")?.value + + switch (preRuntime?.engine) { + case "BABE": { + const babe = BabePreDigest.dec(preRuntime.payload) + if (!babe.value) return null + return BigInt(babe.value.authority_index) + } + case "aura": { + return u64.dec(preRuntime.payload) + } + } + + return null +} + +const withHodl = + (hodl: () => () => void) => + (source$: Observable) => + new Observable((obs) => { + obs.add(source$.subscribe(obs)) + try { + const hodled = hodl() + obs.add(hodled) + } catch (_) { + // Nothing, it might happen if the block is not pinned + } + return obs + }) diff --git a/src/pages/Explorer/Detail/BlockInfo.tsx b/src/pages/Explorer/Detail/BlockInfo.tsx index 5ef8bc3..9d23a87 100644 --- a/src/pages/Explorer/Detail/BlockInfo.tsx +++ b/src/pages/Explorer/Detail/BlockInfo.tsx @@ -1,40 +1,16 @@ import { CopyText } from "@/components/Copy" import { Link } from "@/hashParams" -import { client$ } from "@/state/chains/chain.state" -import { Polkadot_people } from "@polkadot-api/descriptors" -import { - _void, - Bytes, - HexString, - Struct, - u32, - u64, - Variant, -} from "@polkadot-api/substrate-bindings" import { state, useStateObservable } from "@react-rxjs/core" import { combineKeys } from "@react-rxjs/utils" -import { BlockHeader, PolkadotClient } from "polkadot-api" -import { AddressIdentity } from "polkahub" -import { FC, ReactNode, useEffect } from "react" -import { - catchError, - combineLatest, - filter, - from, - map, - Observable, - startWith, - switchMap, - take, -} from "rxjs" +import { FC, ReactNode } from "react" +import { filter, map, startWith, switchMap, take } from "rxjs" import { BlockInfo, - blockInfo$, blockInfoState$, blocksByHeight$, BlockState, - finalized$, } from "../block.state" +import { BlockAuthor } from "./BlockAuthor" import { BlockStatusIcon, statusText } from "./BlockState" export const BlockInfoView: FC<{ @@ -72,7 +48,7 @@ export const BlockInfoView: FC<{ - +
)} @@ -108,135 +84,6 @@ const DetailTile: FC<{ label: string; children: ReactNode }> = ({ ) -const withHodl = - (hodl: () => () => void) => - (source$: Observable) => - new Observable((obs) => { - obs.add(source$.subscribe(obs)) - try { - const hodled = hodl() - obs.add(hodled) - } catch (_) { - // Nothing, it might happen if the block is not pinned - } - return obs - }) - -const validatorCache: WeakMap< - PolkadotClient, - Record -> = new WeakMap() - -const fetchValidators = ( - client: PolkadotClient, - at: HexString, -): Promise => - client.getUnsafeApi().query.Session.Validators.getValue({ - at, - }) -const fetchCachedValidators$ = (client: PolkadotClient, at: HexString) => { - const api = client.getUnsafeApi() - return from(api.query.Session.CurrentIndex.getValue()).pipe( - switchMap(async (idx) => { - const cache = validatorCache.get(client) ?? {} - validatorCache.set(client, cache) - - const cached = cache[idx] - if (cached) return { idx, validators: cached } - const validators = await fetchValidators(client, at) - return { idx, validators } - }), - ) -} -const validators$ = state( - (block: HexString) => - client$.pipe( - switchMap((client) => - combineLatest([ - fetchCachedValidators$(client, block), - blockInfo$(block).pipe(map((v) => v.status)), - ]).pipe( - map(([{ idx, validators }, blockStatus]) => { - if ( - blockStatus === BlockState.Finalized || - blockStatus === BlockState.Unknown - ) { - const cache = validatorCache.get(client) ?? {} - validatorCache.set(client, cache) - cache[idx] = validators - } - return validators - }), - withHodl(() => client.hodlBlock(block)), - take(1), - catchError(() => [null]), - ), - ), - ), - null, -) - -// Subscribe to the validators of the finalized block so that best blocks author's of the same session will load faster -const finalizedValidators$ = finalized$.pipe( - switchMap((block) => validators$(block.hash)), -) - -const Author: FC<{ hash: HexString; header: BlockHeader }> = ({ - hash, - header, -}) => { - const validators = useStateObservable(validators$(hash)) - const idx = getAuthorityIdx(header) - - useEffect(() => { - const sub = finalizedValidators$.subscribe() - return () => sub.unsubscribe() - }, []) - - if (idx == null || validators == null) return
- - return ( - - ) -} - -const DigestWithVRF = Struct({ - authority_index: u32, - slot: u64, - vrf_signature: Struct({ - pre_output: Bytes(32), - proof: Bytes(64), - }), -}) -const BabePreDigest = Variant({ - Uknown: _void, - Primary: DigestWithVRF, - SecondaryPlain: Struct({ - authority_index: u32, - slot: u64, - }), - SecondaryVRF: DigestWithVRF, -}) - -const getAuthorityIdx = (header: BlockHeader) => { - const preRuntime = header.digests.find((d) => d.type === "preRuntime")?.value - - switch (preRuntime?.engine) { - case "BABE": { - const babe = BabePreDigest.dec(preRuntime.payload) - if (!babe.value) return null - return BigInt(babe.value.authority_index) - } - case "aura": { - return u64.dec(preRuntime.payload) - } - } - - return null -} - const childBlocks$ = state( (hash: string) => blockInfoState$(hash).pipe(