From de400ac7a9f4f6a444a749204b7bf8195266ca7f Mon Sep 17 00:00:00 2001 From: Arjun Porwal <65214523+ap211unitech@users.noreply.github.com> Date: Wed, 6 Aug 2025 09:03:51 +0530 Subject: [PATCH] Accumulate proof size for block details page (#11748) * refactor: event details extraction to include proof size in calculations * feat: fetch block weight from storage for source of truth --- .../page-explorer/src/BlockInfo/ByHash.tsx | 13 +++++- .../page-explorer/src/BlockInfo/Summary.tsx | 41 +++++++++++++++---- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/packages/page-explorer/src/BlockInfo/ByHash.tsx b/packages/page-explorer/src/BlockInfo/ByHash.tsx index 9bb800a55e..ae355f4ef2 100644 --- a/packages/page-explorer/src/BlockInfo/ByHash.tsx +++ b/packages/page-explorer/src/BlockInfo/ByHash.tsx @@ -5,6 +5,7 @@ import type { HeaderExtended } from '@polkadot/api-derive/types'; import type { KeyedEvent } from '@polkadot/react-hooks/ctx/types'; import type { V2Weight } from '@polkadot/react-hooks/useWeight'; import type { EventRecord, Hash, RuntimeVersionPartial, SignedBlock } from '@polkadot/types/interfaces'; +import type { FrameSupportDispatchPerDispatchClassWeight } from '@polkadot/types/lookup'; import React, { useEffect, useMemo, useState } from 'react'; import { Link } from 'react-router-dom'; @@ -29,6 +30,7 @@ interface Props { interface State { events?: KeyedEvent[] | null; + blockWeight?: FrameSupportDispatchPerDispatchClassWeight | null; getBlock?: SignedBlock; getHeader?: HeaderExtended; nextBlockHash?: Hash | null; @@ -37,8 +39,9 @@ interface State { const EMPTY_HEADER: [React.ReactNode?, string?, number?][] = [['...', 'start', 6]]; -function transformResult ([[runtimeVersion, events], getBlock, getHeader]: [[RuntimeVersionPartial, EventRecord[] | null], SignedBlock, HeaderExtended?]): State { +function transformResult ([[runtimeVersion, events, blockWeight], getBlock, getHeader]: [[RuntimeVersionPartial, EventRecord[] | null, FrameSupportDispatchPerDispatchClassWeight|null], SignedBlock, HeaderExtended?]): State { return { + blockWeight, events: events?.map((record, index) => ({ indexes: [index], key: `${Date.now()}-${index}-${record.hash.toHex()}`, @@ -54,7 +57,7 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme const { t } = useTranslation(); const { api } = useApi(); const mountedRef = useIsMountedRef(); - const [{ events, getBlock, getHeader, nextBlockHash, runtimeVersion }, setState] = useState({}); + const [{ blockWeight, events, getBlock, getHeader, nextBlockHash, runtimeVersion }, setState] = useState({}); const [blkError, setBlkError] = useState(error); const [evtError, setEvtError] = useState(); @@ -88,6 +91,11 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme .catch((error: Error) => { mountedRef.current && setEvtError(error); + return null; + }), + apiAt.query.system + .blockWeight() + .catch(() => { return null; }) ]) @@ -166,6 +174,7 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme return (
{ + totalRefTime.iadd(weight?.[cls].refTime.toBn() ?? new BN(0)); + totalProofSize.iadd(weight?.[cls].proofSize.toBn() ?? new BN(0)); + }); + + return { totalProofSize, totalRefTime }; +} + function extractEventDetails (events?: KeyedEvent[] | null): [BN?, BN?, BN?, BN?] { return events - ? events.reduce(([deposits, transfers, weight], { record: { event: { data, method, section } } }) => { + ? events.reduce(([deposits, transfers, weight, proofSize], { record: { event: { data, method, section } } }) => { const size = (convertWeight( ((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo)?.weight ).v2Weight as V2Weight).proofSize; @@ -42,20 +58,31 @@ function extractEventDetails (events?: KeyedEvent[] | null): [BN?, BN?, BN?, BN? ).v1Weight) : weight, section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method) - ? (isBn(size) ? size : size.toBn()) - : BN_ZERO + ? proofSize.iadd(isBn(size) ? size : size.toBn()) + : proofSize ]; }, [new BN(0), new BN(0), new BN(0), new BN(0)]) : []; } -function Summary ({ events, maxBlockWeight, maxProofSize, signedBlock }: Props): React.ReactElement | null { +function Summary ({ blockWeight, events, maxBlockWeight, maxProofSize, signedBlock }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); const [deposits, transfers, weight, size] = useMemo( - () => extractEventDetails(events), - [events] + () => { + const eventDetails = extractEventDetails(events); + const { totalProofSize, totalRefTime } = accumulateWeights(blockWeight); + + // Block weight is the source of truth; using events data as fallback only + if (blockWeight) { + eventDetails[2] = totalRefTime; + eventDetails[3] = totalProofSize; + } + + return eventDetails; + }, + [blockWeight, events] ); return (