From ce336a6916ac1e3ef8755b515504db6d77bb0ce4 Mon Sep 17 00:00:00 2001 From: Arjun Porwal <65214523+ap211unitech@users.noreply.github.com> Date: Wed, 22 Jan 2025 18:07:37 +0530 Subject: [PATCH] fix: handle potential undefined weight in convertWeight function and improve proofSize handling (#11225) --- packages/page-explorer/src/BlockInfo/ByHash.tsx | 4 ++-- packages/page-explorer/src/BlockInfo/Summary.tsx | 12 +++++++----- packages/react-hooks/src/useWeight.ts | 8 ++++++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/page-explorer/src/BlockInfo/ByHash.tsx b/packages/page-explorer/src/BlockInfo/ByHash.tsx index 02f8891486..2243a92a5f 100644 --- a/packages/page-explorer/src/BlockInfo/ByHash.tsx +++ b/packages/page-explorer/src/BlockInfo/ByHash.tsx @@ -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 diff --git a/packages/page-explorer/src/BlockInfo/Summary.tsx b/packages/page-explorer/src/BlockInfo/Summary.tsx index fcd4acd905..dd78551fd9 100644 --- a/packages/page-explorer/src/BlockInfo/Summary.tsx +++ b/packages/page-explorer/src/BlockInfo/Summary.tsx @@ -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)]) diff --git a/packages/react-hooks/src/useWeight.ts b/packages/react-hooks/src/useWeight.ts index 27641a74a9..46ad675833 100644 --- a/packages/react-hooks/src/useWeight.ts +++ b/packages/react-hooks/src/useWeight.ts @@ -39,6 +39,14 @@ const EMPTY_STATE: Partial = { // 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();