Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed broken Block Details for blocks/chains with V1 Weight #11225

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/page-explorer/src/BlockInfo/ByHash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}>
Expand Down
12 changes: 7 additions & 5 deletions packages/page-explorer/src/BlockInfo/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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)
Expand All @@ -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)])
Expand Down
8 changes: 8 additions & 0 deletions packages/react-hooks/src/useWeight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading