From e7fdb7048f5bb9f656977f45c526b8facad4da1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Molina?= Date: Tue, 4 Jun 2024 15:05:56 +0200 Subject: [PATCH] Add proof size summary --- .../page-explorer/src/BlockInfo/ByHash.tsx | 8 ++- .../page-explorer/src/BlockInfo/Summary.tsx | 55 +++++++++++++------ packages/react-hooks/src/useWeight.ts | 6 +- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/packages/page-explorer/src/BlockInfo/ByHash.tsx b/packages/page-explorer/src/BlockInfo/ByHash.tsx index f61fd2ebef41..aee6bf0e018f 100644 --- a/packages/page-explorer/src/BlockInfo/ByHash.tsx +++ b/packages/page-explorer/src/BlockInfo/ByHash.tsx @@ -3,6 +3,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, RuntimeVersionPartial, SignedBlock } from '@polkadot/types/interfaces'; import React, { useEffect, useMemo, useState } from 'react'; @@ -59,7 +60,7 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme const [isVersionCurrent, maxBlockWeight] = useMemo( () => [ !!runtimeVersion && api.runtimeVersion.specName.eq(runtimeVersion.specName) && api.runtimeVersion.specVersion.eq(runtimeVersion.specVersion), - api.consts.system.blockWeights && api.consts.system.blockWeights.maxBlock && convertWeight(api.consts.system.blockWeights.maxBlock).v1Weight + api.consts.system.blockWeights && api.consts.system.blockWeights.maxBlock && convertWeight(api.consts.system.blockWeights.maxBlock).v2Weight ], [api, runtimeVersion] ); @@ -123,7 +124,8 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme
@@ -167,7 +169,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 9ecd6ca2c94a..1b1a6a40fc5e 100644 --- a/packages/page-explorer/src/BlockInfo/Summary.tsx +++ b/packages/page-explorer/src/BlockInfo/Summary.tsx @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 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 React, { useMemo } from 'react'; @@ -10,41 +11,47 @@ 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, formatNumber } from '@polkadot/util'; +import { BN, BN_ONE, BN_THREE, BN_TWO, BN_ZERO, formatNumber } from '@polkadot/util'; import { useTranslation } from '../translate.js'; interface Props { events?: KeyedEvent[] | null; maxBlockWeight?: BN; + maxProofSize?: BN; signedBlock?: SignedBlock; } -function extractEventDetails (events?: KeyedEvent[] | null): [BN?, BN?, BN?] { +function extractEventDetails (events?: KeyedEvent[] | null): [BN?, BN?, BN?, BN?] { return events - ? events.reduce(([deposits, transfers, weight], { record: { event: { data, method, section } } }) => [ - section === 'balances' && method === 'Deposit' - ? deposits.iadd(data[1] as Balance) - : deposits, - section === 'balances' && method === 'Transfer' - ? transfers.iadd(data[2] as Balance) - : transfers, - section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method) - ? weight.iadd( - convertWeight( + ? events.reduce(([deposits, transfers, weight], { record: { event: { data, method, section } } }) => { + return [ + section === 'balances' && method === 'Deposit' + ? deposits.iadd(data[1] as Balance) + : deposits, + section === 'balances' && method === 'Transfer' + ? transfers.iadd(data[2] as Balance) + : transfers, + section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method) + ? weight.iadd(convertWeight( ((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight - ).v1Weight - ) - : weight - ], [new BN(0), new BN(0), new BN(0)]) + ).v1Weight) + : weight, + section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method) + ? (convertWeight( + ((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight + ).v2Weight as V2Weight).proofSize.toBn() + : BN_ZERO + ]; + }, [new BN(0), new BN(0), new BN(0), new BN(0)]) : []; } -function Summary ({ events, maxBlockWeight, signedBlock }: Props): React.ReactElement | null { +function Summary ({ events, maxBlockWeight, maxProofSize, signedBlock }: Props): React.ReactElement | null { const { t } = useTranslation(); const { api } = useApi(); - const [deposits, transfers, weight] = useMemo( + const [deposits, transfers, weight, size] = useMemo( () => extractEventDetails(events), [events] ); @@ -86,6 +93,18 @@ function Summary ({ events, maxBlockWeight, signedBlock }: Props): React.ReactEl ? formatNumber(weight) : 999,999,999} + {maxProofSize && size && + + {formatNumber(size)} + }
diff --git a/packages/react-hooks/src/useWeight.ts b/packages/react-hooks/src/useWeight.ts index 6c8549aa455c..a08f0472a241 100644 --- a/packages/react-hooks/src/useWeight.ts +++ b/packages/react-hooks/src/useWeight.ts @@ -16,7 +16,7 @@ import { useIsMountedRef } from './useIsMountedRef.js'; type V1Weight = INumber; -interface V2Weight { +export interface V2Weight { refTime: ICompact; proofSize: ICompact; } @@ -48,13 +48,13 @@ export function convertWeight (weight: V1Weight | V2Weight): WeightResult { // V1.5 weight (when not converted) const refTime = (weight as V2Weight).refTime.toBn(); - return { v1Weight: refTime, v2Weight: { refTime } }; + return { v1Weight: refTime, v2Weight: { proofSize: BN_ZERO, refTime } }; } // V1 weight const refTime = (weight as V1Weight).toBn(); - return { v1Weight: refTime, v2Weight: { refTime } }; + return { v1Weight: refTime, v2Weight: { proofSize: BN_ZERO, refTime } }; } // for a given call, calculate the weight