From 53faefd1a8cd50420a31b920bb63b2be9f1ee979 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 3 Oct 2022 10:46:54 +0300 Subject: [PATCH] Adjust useWeights results (prepare for conv.) (#8192) --- .../src/modals/MultisigApprove.tsx | 10 ++--- packages/page-contracts/src/useWeight.ts | 6 +-- packages/page-council/src/Motions/Close.tsx | 6 +-- .../page-explorer/src/BlockInfo/ByHash.tsx | 2 +- .../page-explorer/src/BlockInfo/Extrinsic.tsx | 4 +- .../page-explorer/src/BlockInfo/Summary.tsx | 2 +- .../page-tech-comm/src/Proposals/Close.tsx | 6 +-- packages/react-hooks/src/useTxBatch.ts | 5 ++- packages/react-hooks/src/useWeight.ts | 44 +++++++++++++------ 9 files changed, 52 insertions(+), 33 deletions(-) diff --git a/packages/page-accounts/src/modals/MultisigApprove.tsx b/packages/page-accounts/src/modals/MultisigApprove.tsx index 6aa4c831af..06ef79702d 100644 --- a/packages/page-accounts/src/modals/MultisigApprove.tsx +++ b/packages/page-accounts/src/modals/MultisigApprove.tsx @@ -51,7 +51,7 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }: const { allAccounts } = useAccounts(); const [callHex, setCallHex] = useState(''); const [{ callData, callError, callInfo }, setCallData] = useState(EMPTY_CALL); - const [callWeight, callLength] = useWeight(callData); + const { encodedCallLength, v1Weight } = useWeight(callData); const [hash, setHash] = useState(() => ongoing[0][0].toHex()); const [{ isMultiCall, multisig }, setMultisig] = useState(() => ({ isMultiCall: false, multisig: null })); const [isCallOverride, setCallOverride] = useState(true); @@ -139,20 +139,20 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }: ? isMultiCall && isCallOverride ? callData ? multiMod.asMulti.meta.args.length === 6 - ? multiMod.asMulti(threshold, others, multisig.when, callData.toHex(), false, callWeight) + ? multiMod.asMulti(threshold, others, multisig.when, callData.toHex(), false, v1Weight) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore (We are doing toHex here since we have a Vec input) : multiMod.asMulti(threshold, others, multisig.when, callData) : null : multiMod.approveAsMulti.meta.args.length === 5 - ? multiMod.approveAsMulti(threshold, others, multisig.when, hash, callWeight) + ? multiMod.approveAsMulti(threshold, others, multisig.when, hash, v1Weight) // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore : multiMod.approveAsMulti(threshold, others, multisig.when, hash) : multiMod.cancelAsMulti(threshold, others, multisig.when, hash) : null ); - }, [api, callData, callWeight, hash, isCallOverride, isMultiCall, others, multisig, threshold, type]); + }, [api, callData, hash, isCallOverride, isMultiCall, others, multisig, threshold, type, v1Weight]); const isAye = type === 'aye'; @@ -277,7 +277,7 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }: accountId={signatory} extrinsic={tx} icon={isAye ? 'check' : 'times'} - isDisabled={!tx || (isAye && (!whoFilter.length || (!!callData && !callLength)))} + isDisabled={!tx || (isAye && (!whoFilter.length || (!!callData && !encodedCallLength)))} label={isAye ? 'Approve' : 'Reject'} onStart={onClose} /> diff --git a/packages/page-contracts/src/useWeight.ts b/packages/page-contracts/src/useWeight.ts index 61a3d956ff..5b60f35da0 100644 --- a/packages/page-contracts/src/useWeight.ts +++ b/packages/page-contracts/src/useWeight.ts @@ -19,7 +19,7 @@ function useWeightImpl (): UseWeight { api.consts.system.blockWeights ? api.consts.system.blockWeights.maxBlock : api.consts.system.maximumBlockWeight as Weight - ).div(BN_MILLION).div(BN_TEN) + ).v1Weight.div(BN_MILLION).div(BN_TEN) ); const [isEmpty, setIsEmpty] = useState(false); @@ -29,7 +29,7 @@ function useWeightImpl (): UseWeight { api.consts.system.blockWeights ? api.consts.system.blockWeights.maxBlock : api.consts.system.maximumBlockWeight as Weight - ).div(BN_MILLION).div(BN_TEN)), + ).v1Weight.div(BN_MILLION).div(BN_TEN)), [api] ); @@ -45,7 +45,7 @@ function useWeightImpl (): UseWeight { api.consts.system.blockWeights ? api.consts.system.blockWeights.maxBlock : api.consts.system.maximumBlockWeight as Weight - )).toNumber(); + ).v1Weight).toNumber(); percentage = (executionTime / blockTime.toNumber()) * 100; // execution is 2s of 6s blocks, i.e. 1/3 diff --git a/packages/page-council/src/Motions/Close.tsx b/packages/page-council/src/Motions/Close.tsx index 8efb82cfed..a48645851c 100644 --- a/packages/page-council/src/Motions/Close.tsx +++ b/packages/page-council/src/Motions/Close.tsx @@ -22,7 +22,7 @@ function Close ({ hasFailed, hash, idNumber, proposal }: Props): React.ReactElem const { api } = useApi(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); - const [proposalWeight, proposalLength] = useWeight(proposal); + const { encodedCallLength, v1Weight } = useWeight(proposal); const modLocation = useCollectiveInstance('council'); // protect against older versions @@ -57,13 +57,13 @@ function Close ({ hasFailed, hash, idNumber, proposal }: Props): React.ReactElem [ !!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.consts.system.blockWeights && api.consts.system.blockWeights.maxBlock && convertWeight(api.consts.system.blockWeights.maxBlock).v1Weight ], [api, runtimeVersion] ); diff --git a/packages/page-explorer/src/BlockInfo/Extrinsic.tsx b/packages/page-explorer/src/BlockInfo/Extrinsic.tsx index e1c1882633..dbba94334b 100644 --- a/packages/page-explorer/src/BlockInfo/Extrinsic.tsx +++ b/packages/page-explorer/src/BlockInfo/Extrinsic.tsx @@ -57,9 +57,9 @@ function filterEvents (index: number, events?: KeyedEvent[] | null, maxBlockWeig return [ dispatchInfo, - weight, + weight && weight.v1Weight, weight && maxBlockWeight - ? weight.mul(BN_TEN_THOUSAND).div(maxBlockWeight).toNumber() / 100 + ? weight.v1Weight.mul(BN_TEN_THOUSAND).div(maxBlockWeight).toNumber() / 100 : 0, filtered ]; diff --git a/packages/page-explorer/src/BlockInfo/Summary.tsx b/packages/page-explorer/src/BlockInfo/Summary.tsx index a57844cbf3..f4560e41e1 100644 --- a/packages/page-explorer/src/BlockInfo/Summary.tsx +++ b/packages/page-explorer/src/BlockInfo/Summary.tsx @@ -30,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(convertWeight(((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight)) + ? weight.iadd(convertWeight(((method === 'ExtrinsicSuccess' ? data[0] : data[1]) as DispatchInfo).weight).v1Weight) : weight ], [new BN(0), new BN(0), new BN(0)]) : []; diff --git a/packages/page-tech-comm/src/Proposals/Close.tsx b/packages/page-tech-comm/src/Proposals/Close.tsx index 8fe45598a6..9f5df83c1b 100644 --- a/packages/page-tech-comm/src/Proposals/Close.tsx +++ b/packages/page-tech-comm/src/Proposals/Close.tsx @@ -24,7 +24,7 @@ function Close ({ hasFailed, hash, idNumber, proposal, type }: Props): React.Rea const { api } = useApi(); const [isOpen, toggleOpen] = useToggle(); const [accountId, setAccountId] = useState(null); - const [proposalWeight, proposalLength] = useWeight(proposal); + const { encodedCallLength, v1Weight } = useWeight(proposal); const modLocation = useCollectiveInstance(type); if (!modLocation) { @@ -58,13 +58,13 @@ function Close ({ hasFailed, hash, idNumber, proposal, type }: Props): React.Rea [] | null | false, ); setBatchSize((prev) => - weight.isZero() + weight.v1Weight.isZero() ? prev : Math.floor( maxBlock + .v1Weight .muln(64) // 65% of the block weight on a single extrinsic (64 for safety) - .div(weight) + .div(weight.v1Weight) .toNumber() / 100 ) ); diff --git a/packages/react-hooks/src/useWeight.ts b/packages/react-hooks/src/useWeight.ts index 4315d7807f..47f197a52d 100644 --- a/packages/react-hooks/src/useWeight.ts +++ b/packages/react-hooks/src/useWeight.ts @@ -20,19 +20,39 @@ interface V2Weight { proofSize: ICompact; } +interface V2WeightConstruct { + refTime: BN | ICompact; +} + +interface Result { + encodedCallLength: number; + v1Weight: BN; + v2Weight: V2WeightConstruct; +} + // a random address that we are using for our queries const ZERO_ACCOUNT = '5CAUdnwecHGxxyr5vABevAfZ34Fi4AaraDRMwfDQXQ52PXqg'; -const EMPTY_STATE: [BN, number] = [BN_ZERO, 0]; +const EMPTY_STATE: Result = { + encodedCallLength: 0, + v1Weight: BN_ZERO, + v2Weight: { refTime: BN_ZERO } +}; -// 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(); +// return both v1 & v2 weight structures (would depend on actual use) +export function convertWeight (weight: V1Weight | V2Weight): { v1Weight: BN, v2Weight: V2WeightConstruct } { + if ((weight as V2Weight).proofSize) { + const refTime = (weight as V2Weight).refTime.toBn(); + + return { v1Weight: refTime, v2Weight: weight as V2Weight }; + } + + const refTime = (weight as V1Weight).toBn(); + + return { v1Weight: refTime, v2Weight: { refTime } }; } // for a given call, calculate the weight -function useWeightImpl (call?: Call | null): [BN, number] { +function useWeightImpl (call?: Call | null): Result { const { api } = useApi(); const mountedRef = useIsMountedRef(); const [state, setState] = useState(EMPTY_STATE); @@ -41,13 +61,11 @@ function useWeightImpl (call?: Call | null): [BN, number] { if (call && api.call.transactionPaymentApi) { nextTick(async (): Promise => { try { - const extrinsic = api.tx(call); - const { weight } = await extrinsic.paymentInfo(ZERO_ACCOUNT); + const { v1Weight, v2Weight } = convertWeight( + (await api.tx(call).paymentInfo(ZERO_ACCOUNT)).weight + ); - mountedRef.current && setState([ - convertWeight(weight), - call.encodedLength - ]); + mountedRef.current && setState({ encodedCallLength: call.encodedLength, v1Weight, v2Weight }); } catch (error) { console.error(error); }