fix: handle potential undefined weight in convertWeight function and improve proofSize handling (#11225)

This commit is contained in:
Arjun Porwal 2025-01-22 18:07:37 +05:30 committed by GitHub
parent 1fb07f37ab
commit ce336a6916
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 7 deletions

View File

@ -12,7 +12,7 @@ import { Link } from 'react-router-dom';
import { AddressSmall, Columar, LinkExternal, MarkError, Table } from '@polkadot/react-components';
import { useApi, useIsMountedRef } from '@polkadot/react-hooks';
import { convertWeight } from '@polkadot/react-hooks/useWeight';
import { formatNumber } from '@polkadot/util';
import { formatNumber, isBn } from '@polkadot/util';
import Events from '../Events.js';
import { useTranslation } from '../translate.js';
@ -125,7 +125,7 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme
<Summary
events={events}
maxBlockWeight={(maxBlockWeight as V2Weight).refTime.toBn()}
maxProofSize={(maxBlockWeight as V2Weight).proofSize.toBn()}
maxProofSize={isBn(maxBlockWeight.proofSize) ? maxBlockWeight.proofSize : (maxBlockWeight as V2Weight).proofSize.toBn()}
signedBlock={getBlock}
/>
<Table header={header}>

View File

@ -11,7 +11,7 @@ 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 } from '@polkadot/util';
import { BN, BN_ONE, BN_THREE, BN_TWO, BN_ZERO, formatNumber, isBn } from '@polkadot/util';
import { useTranslation } from '../translate.js';
@ -25,6 +25,10 @@ interface Props {
function extractEventDetails (events?: KeyedEvent[] | null): [BN?, BN?, BN?, BN?] {
return events
? events.reduce(([deposits, transfers, weight], { record: { event: { data, method, section } } }) => {
const size = (convertWeight(
((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo)?.weight
).v2Weight as V2Weight).proofSize;
return [
section === 'balances' && method === 'Deposit'
? deposits.iadd(data[1] as Balance)
@ -34,13 +38,11 @@ function extractEventDetails (events?: KeyedEvent[] | null): [BN?, BN?, BN?, BN?
: transfers,
section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method)
? weight.iadd(convertWeight(
((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight
((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo)?.weight
).v1Weight)
: weight,
section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method)
? (convertWeight(
((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight
).v2Weight as V2Weight).proofSize.toBn()
? (isBn(size) ? size : size.toBn())
: BN_ZERO
];
}, [new BN(0), new BN(0), new BN(0), new BN(0)])

View File

@ -39,6 +39,14 @@ const EMPTY_STATE: Partial<Result> = {
// return both v1 & v2 weight structures (would depend on actual use)
export function convertWeight (weight: V1Weight | V2Weight): WeightResult {
// We need to handle it because sometimes input parameters are passed with type casting,
// which can result in them being undefined or null under certain conditions.
if (!weight) {
const refTime = BN_ZERO;
return { v1Weight: refTime, v2Weight: { proofSize: BN_ZERO, refTime } };
}
if ((weight as V2Weight).proofSize) {
// V2 weight
const refTime = (weight as V2Weight).refTime.toBn();