From 26d698db91a1a91c2d7dd6188cc55e9aeba14d5f Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 3 Oct 2022 09:44:50 +0300 Subject: [PATCH] Adjust hooks for V2 weight usage (#8191) * Adjust for V2 weight usage * contracts weight conversion * Explorer with weight conversion * Adjust --- packages/page-contracts/src/useWeight.ts | 24 ++++++++++--------- .../page-explorer/src/BlockInfo/ByHash.tsx | 11 ++++++--- .../page-explorer/src/BlockInfo/Extrinsic.tsx | 19 ++++++++------- .../src/BlockInfo/Extrinsics.tsx | 10 ++++---- .../page-explorer/src/BlockInfo/Summary.tsx | 7 +++--- packages/react-hooks/src/useTxBatch.ts | 15 ++++++++---- packages/react-hooks/src/useWeight.ts | 20 +++++++++++++++- 7 files changed, 71 insertions(+), 35 deletions(-) diff --git a/packages/page-contracts/src/useWeight.ts b/packages/page-contracts/src/useWeight.ts index 66db7febd5..61a3d956ff 100644 --- a/packages/page-contracts/src/useWeight.ts +++ b/packages/page-contracts/src/useWeight.ts @@ -8,26 +8,28 @@ import type { UseWeight } from './types'; import { useCallback, useMemo, useState } from 'react'; import { createNamedHook, useApi, useBlockInterval } from '@polkadot/react-hooks'; +import { convertWeight } from '@polkadot/react-hooks/useWeight'; import { BN_MILLION, BN_TEN, BN_ZERO } from '@polkadot/util'; function useWeightImpl (): UseWeight { const { api } = useApi(); const blockTime = useBlockInterval(); const [megaGas, _setMegaGas] = useState( - (api.consts.system.blockWeights - ? api.consts.system.blockWeights.maxBlock - : api.consts.system.maximumBlockWeight as Weight + convertWeight( + api.consts.system.blockWeights + ? api.consts.system.blockWeights.maxBlock + : api.consts.system.maximumBlockWeight as Weight ).div(BN_MILLION).div(BN_TEN) ); const [isEmpty, setIsEmpty] = useState(false); const setMegaGas = useCallback( - (value?: BN | undefined) => _setMegaGas(value || ( - (api.consts.system.blockWeights - ? api.consts.system.blockWeights.maxBlock - : api.consts.system.maximumBlockWeight as Weight - ).div(BN_MILLION).div(BN_TEN) - )), + (value?: BN | undefined) => + _setMegaGas(value || convertWeight( + api.consts.system.blockWeights + ? api.consts.system.blockWeights.maxBlock + : api.consts.system.maximumBlockWeight as Weight + ).div(BN_MILLION).div(BN_TEN)), [api] ); @@ -39,11 +41,11 @@ function useWeightImpl (): UseWeight { if (megaGas) { weight = megaGas.mul(BN_MILLION); - executionTime = weight.mul(blockTime).div( + executionTime = weight.mul(blockTime).div(convertWeight( api.consts.system.blockWeights ? api.consts.system.blockWeights.maxBlock : api.consts.system.maximumBlockWeight as Weight - ).toNumber(); + )).toNumber(); percentage = (executionTime / blockTime.toNumber()) * 100; // execution is 2s of 6s blocks, i.e. 1/3 diff --git a/packages/page-explorer/src/BlockInfo/ByHash.tsx b/packages/page-explorer/src/BlockInfo/ByHash.tsx index ba02040ccf..33f5b81a77 100644 --- a/packages/page-explorer/src/BlockInfo/ByHash.tsx +++ b/packages/page-explorer/src/BlockInfo/ByHash.tsx @@ -10,6 +10,7 @@ import { Link } from 'react-router-dom'; import { AddressSmall, Columar, LinkExternal, 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 Events from '../Events'; @@ -55,8 +56,11 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme const [blkError, setBlkError] = useState(error); const [evtError, setEvtError] = useState(); - const isVersionCurrent = useMemo( - () => !!runtimeVersion && api.runtimeVersion.specName.eq(runtimeVersion.specName) && api.runtimeVersion.specVersion.eq(runtimeVersion.specVersion), + 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) + ], [api, runtimeVersion] ); @@ -110,7 +114,7 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme
diff --git a/packages/page-explorer/src/BlockInfo/Extrinsic.tsx b/packages/page-explorer/src/BlockInfo/Extrinsic.tsx index dd8696d11b..e1c1882633 100644 --- a/packages/page-explorer/src/BlockInfo/Extrinsic.tsx +++ b/packages/page-explorer/src/BlockInfo/Extrinsic.tsx @@ -2,13 +2,14 @@ // SPDX-License-Identifier: Apache-2.0 import type { KeyedEvent } from '@polkadot/react-query/types'; -import type { BlockNumber, DispatchInfo, Extrinsic, Weight } from '@polkadot/types/interfaces'; +import type { BlockNumber, DispatchInfo, Extrinsic } from '@polkadot/types/interfaces'; import type { ICompact, INumber } from '@polkadot/types/types'; import React, { useMemo } from 'react'; import styled from 'styled-components'; import { AddressMini, Call, Expander, LinkExternal } from '@polkadot/react-components'; +import { convertWeight } from '@polkadot/react-hooks/useWeight'; import { BN, formatNumber } from '@polkadot/util'; import Event from '../Event'; @@ -19,7 +20,7 @@ interface Props { className?: string; events?: KeyedEvent[] | null; index: number; - maxBlockWeight?: Weight; + maxBlockWeight?: BN; value: Extrinsic; withLink: boolean; } @@ -36,7 +37,7 @@ function getEra ({ era }: Extrinsic, blockNumber?: BlockNumber): [number, number return null; } -function filterEvents (index: number, events?: KeyedEvent[] | null, maxBlockWeight?: Weight): [DispatchInfo | undefined, number, KeyedEvent[]] { +function filterEvents (index: number, events?: KeyedEvent[] | null, maxBlockWeight?: BN): [DispatchInfo | undefined, BN | undefined, number, KeyedEvent[]] { const filtered = events ? events.filter(({ record: { phase } }) => phase.isApplyExtrinsic && @@ -52,11 +53,13 @@ function filterEvents (index: number, events?: KeyedEvent[] | null, maxBlockWeig ? infoRecord.record.event.data[0] as DispatchInfo : infoRecord.record.event.data[1] as DispatchInfo : undefined; + const weight = dispatchInfo && convertWeight(dispatchInfo.weight); return [ dispatchInfo, - dispatchInfo && maxBlockWeight - ? dispatchInfo.weight.mul(BN_TEN_THOUSAND).div(maxBlockWeight).toNumber() / 100 + weight, + weight && maxBlockWeight + ? weight.mul(BN_TEN_THOUSAND).div(maxBlockWeight).toNumber() / 100 : 0, filtered ]; @@ -104,7 +107,7 @@ function ExtrinsicDisplay ({ blockNumber, className = '', events, index, maxBloc [blockNumber, t, value] ); - const [dispatchInfo, weightPercentage, thisEvents] = useMemo( + const [, weight, weightPercentage, thisEvents] = useMemo( () => filterEvents(index, events, maxBlockWeight), [index, events, maxBlockWeight] ); @@ -152,9 +155,9 @@ function ExtrinsicDisplay ({ blockNumber, className = '', events, index, maxBloc )}
- {dispatchInfo && ( + {weight && ( <> - <>{formatNumber(dispatchInfo.weight)} + <>{formatNumber(weight)}
{weightPercentage.toFixed(2)}%
)} diff --git a/packages/page-explorer/src/BlockInfo/Extrinsics.tsx b/packages/page-explorer/src/BlockInfo/Extrinsics.tsx index 8d8e79651d..11af95c311 100644 --- a/packages/page-explorer/src/BlockInfo/Extrinsics.tsx +++ b/packages/page-explorer/src/BlockInfo/Extrinsics.tsx @@ -3,11 +3,11 @@ import type { KeyedEvent } from '@polkadot/react-query/types'; import type { BlockNumber, Extrinsic } from '@polkadot/types/interfaces'; +import type { BN } from '@polkadot/util'; import React, { useMemo } from 'react'; import { Table } from '@polkadot/react-components'; -import { useApi } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; import ExtrinsicDisplay from './Extrinsic'; @@ -17,13 +17,13 @@ interface Props { className?: string; events?: KeyedEvent[] | null; label?: React.ReactNode; + maxBlockWeight?: BN; value?: Extrinsic[] | null; withLink: boolean; } -function Extrinsics ({ blockNumber, className = '', events, label, value, withLink }: Props): React.ReactElement { +function Extrinsics ({ blockNumber, className = '', events, label, maxBlockWeight, value, withLink }: Props): React.ReactElement { const { t } = useTranslation(); - const { api } = useApi(); const header = useMemo(() => [ [label || t('extrinsics'), 'start', 2], @@ -39,13 +39,13 @@ function Extrinsics ({ blockNumber, className = '', events, label, value, withLi header={header} isFixed > - {value?.map((extrinsic, index): React.ReactNode => + {value && value.map((extrinsic, index): React.ReactNode => diff --git a/packages/page-explorer/src/BlockInfo/Summary.tsx b/packages/page-explorer/src/BlockInfo/Summary.tsx index 789d26cfbf..a57844cbf3 100644 --- a/packages/page-explorer/src/BlockInfo/Summary.tsx +++ b/packages/page-explorer/src/BlockInfo/Summary.tsx @@ -2,12 +2,13 @@ // SPDX-License-Identifier: Apache-2.0 import type { KeyedEvent } from '@polkadot/react-query/types'; -import type { Balance, DispatchInfo, SignedBlock, Weight } from '@polkadot/types/interfaces'; +import type { Balance, DispatchInfo, SignedBlock } from '@polkadot/types/interfaces'; import React, { useMemo } from 'react'; 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, formatNumber } from '@polkadot/util'; @@ -15,7 +16,7 @@ import { useTranslation } from '../translate'; interface Props { events?: KeyedEvent[] | null; - maxBlockWeight?: Weight; + maxBlockWeight?: BN; signedBlock?: SignedBlock; } @@ -29,7 +30,7 @@ function extractEventDetails (events?: KeyedEvent[] | null): [BN?, BN?, BN?] { ? transfers.iadd(data[2] as Balance) : transfers, section === 'system' && ['ExtrinsicFailed', 'ExtrinsicSuccess'].includes(method) - ? weight.iadd(((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight) + ? weight.iadd(convertWeight(((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight)) : weight ], [new BN(0), new BN(0), new BN(0)]) : []; diff --git a/packages/react-hooks/src/useTxBatch.ts b/packages/react-hooks/src/useTxBatch.ts index a9de26ef68..d314997de7 100644 --- a/packages/react-hooks/src/useTxBatch.ts +++ b/packages/react-hooks/src/useTxBatch.ts @@ -13,6 +13,7 @@ import { isFunction, nextTick } from '@polkadot/util'; import { createNamedHook } from './createNamedHook'; import { useAccounts } from './useAccounts'; import { useApi } from './useApi'; +import { convertWeight } from './useWeight'; function createBatches (api: ApiPromise, txs: SubmittableExtrinsic<'promise'>[], batchSize: number, type: BatchType = 'default'): SubmittableExtrinsic<'promise'>[] { if (batchSize === 1 || !isFunction(api.tx.utility?.batch)) { @@ -49,10 +50,16 @@ function useTxBatchImpl (txs?: SubmittableExtrinsic<'promise'>[] | null | false, txs && txs.length && allAccounts[0] && api.call.transactionPaymentApi && nextTick(async (): Promise => { try { - const { weight } = await txs[0].paymentInfo(allAccounts[0]); - const maxBlock = api.consts.system.blockWeights - ? api.consts.system.blockWeights.maxBlock - : api.consts.system.maximumBlockWeight as Weight; + const weight = convertWeight( + ( + await txs[0].paymentInfo(allAccounts[0]) + ).weight + ); + const maxBlock = convertWeight( + api.consts.system.blockWeights + ? api.consts.system.blockWeights.maxBlock + : api.consts.system.maximumBlockWeight as Weight + ); setBatchSize((prev) => weight.isZero() diff --git a/packages/react-hooks/src/useWeight.ts b/packages/react-hooks/src/useWeight.ts index 0626496f90..4315d7807f 100644 --- a/packages/react-hooks/src/useWeight.ts +++ b/packages/react-hooks/src/useWeight.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { Call } from '@polkadot/types/interfaces'; +import type { ICompact, INumber } from '@polkadot/types/types'; import type { BN } from '@polkadot/util'; import { useEffect, useState } from 'react'; @@ -12,10 +13,24 @@ import { createNamedHook } from './createNamedHook'; import { useApi } from './useApi'; import { useIsMountedRef } from './useIsMountedRef'; +type V1Weight = INumber; + +interface V2Weight { + refTime: ICompact; + proofSize: ICompact; +} + // a random address that we are using for our queries const ZERO_ACCOUNT = '5CAUdnwecHGxxyr5vABevAfZ34Fi4AaraDRMwfDQXQ52PXqg'; const EMPTY_STATE: [BN, number] = [BN_ZERO, 0]; +// is this a new V2 weight +export function convertWeight (weight: V1Weight | V2Weight): BN { + return (weight as V2Weight).proofSize + ? (weight as V2Weight).refTime.toBn() + : (weight as V1Weight).toBn(); +} + // for a given call, calculate the weight function useWeightImpl (call?: Call | null): [BN, number] { const { api } = useApi(); @@ -29,7 +44,10 @@ function useWeightImpl (call?: Call | null): [BN, number] { const extrinsic = api.tx(call); const { weight } = await extrinsic.paymentInfo(ZERO_ACCOUNT); - mountedRef.current && setState([weight, call.encodedLength]); + mountedRef.current && setState([ + convertWeight(weight), + call.encodedLength + ]); } catch (error) { console.error(error); }