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
This commit is contained in:
parent
dccecc2a90
commit
de400ac7a9
@ -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<State>({});
|
||||
const [{ blockWeight, events, getBlock, getHeader, nextBlockHash, runtimeVersion }, setState] = useState<State>({});
|
||||
const [blkError, setBlkError] = useState<Error | null | undefined>(error);
|
||||
const [evtError, setEvtError] = useState<Error | null | undefined>();
|
||||
|
||||
@ -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 (
|
||||
<div className={className}>
|
||||
<Summary
|
||||
blockWeight={blockWeight}
|
||||
events={events}
|
||||
maxBlockWeight={(maxBlockWeight as V2Weight).refTime.toBn()}
|
||||
maxProofSize={isBn(maxBlockWeight.proofSize) ? maxBlockWeight.proofSize : (maxBlockWeight as V2Weight).proofSize.toBn()}
|
||||
|
||||
@ -4,6 +4,7 @@
|
||||
import type { KeyedEvent } from '@polkadot/react-hooks/ctx/types';
|
||||
import type { V2Weight } from '@polkadot/react-hooks/useWeight';
|
||||
import type { Balance, DispatchInfo, SignedBlock } from '@polkadot/types/interfaces';
|
||||
import type { FrameSupportDispatchPerDispatchClassWeight } from '@polkadot/types/lookup';
|
||||
|
||||
import React, { useMemo } from 'react';
|
||||
|
||||
@ -11,20 +12,35 @@ import { CardSummary, SummaryBox } from '@polkadot/react-components';
|
||||
import { useApi } from '@polkadot/react-hooks';
|
||||
import { convertWeight } from '@polkadot/react-hooks/useWeight';
|
||||
import { FormatBalance } from '@polkadot/react-query';
|
||||
import { BN, BN_ONE, BN_THREE, BN_TWO, BN_ZERO, formatNumber, isBn } from '@polkadot/util';
|
||||
import { BN, BN_ONE, BN_THREE, BN_TWO, formatNumber, isBn } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate.js';
|
||||
|
||||
interface Props {
|
||||
events?: KeyedEvent[] | null;
|
||||
blockWeight?: FrameSupportDispatchPerDispatchClassWeight | null;
|
||||
maxBlockWeight?: BN;
|
||||
maxProofSize?: BN;
|
||||
signedBlock?: SignedBlock;
|
||||
}
|
||||
|
||||
function accumulateWeights (
|
||||
weight?: FrameSupportDispatchPerDispatchClassWeight | null
|
||||
): { totalRefTime: BN; totalProofSize: BN } {
|
||||
const totalRefTime = new BN(0);
|
||||
const totalProofSize = new BN(0);
|
||||
|
||||
(['normal', 'operational', 'mandatory'] as const).forEach((cls) => {
|
||||
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<Props> | null {
|
||||
function Summary ({ blockWeight, events, maxBlockWeight, maxProofSize, signedBlock }: Props): React.ReactElement<Props> | 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 (
|
||||
|
||||
Loading…
Reference in New Issue
Block a user