Adjust hooks for V2 weight usage (#8191)

* Adjust for V2 weight usage

* contracts weight conversion

* Explorer with weight conversion

* Adjust
This commit is contained in:
Jaco 2022-10-03 09:44:50 +03:00 committed by GitHub
parent aed0d2fcb2
commit 26d698db91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 35 deletions

View File

@ -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<BN>(
(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

View File

@ -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 | null | undefined>(error);
const [evtError, setEvtError] = useState<Error | null | undefined>();
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
<div className={className}>
<Summary
events={events}
maxBlockWeight={api.consts.system.blockWeights?.maxBlock}
maxBlockWeight={maxBlockWeight}
signedBlock={getBlock}
/>
<Table
@ -149,6 +153,7 @@ function BlockByHash ({ className = '', error, value }: Props): React.ReactEleme
<Extrinsics
blockNumber={blockNumber}
events={events}
maxBlockWeight={maxBlockWeight}
value={getBlock.block.extrinsics}
withLink={isVersionCurrent}
/>

View File

@ -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
)}
</td>
<td className='top number media--1400'>
{dispatchInfo && (
{weight && (
<>
<>{formatNumber(dispatchInfo.weight)}</>
<>{formatNumber(weight)}</>
<div>{weightPercentage.toFixed(2)}%</div>
</>
)}

View File

@ -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<Props> {
function Extrinsics ({ blockNumber, className = '', events, label, maxBlockWeight, value, withLink }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const header = useMemo(() => [
[label || t<string>('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 =>
<ExtrinsicDisplay
blockNumber={blockNumber}
events={events}
index={index}
key={`extrinsic:${index}`}
maxBlockWeight={api.consts.system.blockWeights?.maxBlock}
maxBlockWeight={maxBlockWeight}
value={extrinsic}
withLink={withLink}
/>

View File

@ -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)])
: [];

View File

@ -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<void> => {
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()

View File

@ -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<INumber>;
proofSize: ICompact<INumber>;
}
// 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);
}