Cleanup contracts weight handling (#3898)
This commit is contained in:
parent
d83eebb834
commit
97fb76c875
@ -163,7 +163,7 @@ function Call ({ className = '', contract, messageIndex, onCallResult, onChangeM
|
||||
<InputMegaGas
|
||||
estimatedWeight={estimatedWeight}
|
||||
help={t<string>('The maximum amount of gas to use for this contract call. If the call requires more, it will fail.')}
|
||||
{...weight}
|
||||
weight={weight}
|
||||
/>
|
||||
{message.isMutating && (
|
||||
<Toggle
|
||||
|
||||
@ -160,7 +160,7 @@ function Deploy ({ codeHash, constructorIndex = 0, onClose, setConstructorIndex
|
||||
/>
|
||||
<InputMegaGas
|
||||
help={t<string>('The maximum amount of gas that can be used by this deployment, if the code requires more, the deployment will fail.')}
|
||||
{...weight}
|
||||
weight={weight}
|
||||
/>
|
||||
</Modal.Content>
|
||||
<Modal.Actions onCancel={onClose}>
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2020 @polkadot/app-contracts authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { UseWeight } from '../types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import React, { useEffect, useMemo, useState } from 'react';
|
||||
import styled from 'styled-components';
|
||||
@ -10,16 +12,12 @@ import { useTranslation } from '../translate';
|
||||
|
||||
interface Props {
|
||||
className?: string;
|
||||
estimatedWeight?: BN | null;
|
||||
executionTime: number;
|
||||
estimatedWeight?: BN;
|
||||
help: React.ReactNode;
|
||||
isValid: boolean;
|
||||
megaGas: BN;
|
||||
percentage: number;
|
||||
setMegaGas: (value?: BN) => void;
|
||||
weight: UseWeight;
|
||||
}
|
||||
|
||||
function InputMegaGas ({ className, estimatedWeight, executionTime, help, isValid, megaGas, percentage, setMegaGas }: Props): React.ReactElement<Props> {
|
||||
function InputMegaGas ({ className, estimatedWeight, help, weight: { executionTime, isValid, megaGas, percentage, setMegaGas } }: Props): React.ReactElement<Props> {
|
||||
const { t } = useTranslation();
|
||||
const [withEstimate, setWithEstimate] = useState(false);
|
||||
|
||||
@ -30,15 +28,8 @@ function InputMegaGas ({ className, estimatedWeight, executionTime, help, isVali
|
||||
[estimatedWeight]
|
||||
);
|
||||
|
||||
const label = useMemo(
|
||||
() => estimatedMg
|
||||
? t<string>('max gas allowed (M, {{estimatedMg}} estimated)', { replace: { estimatedMg: estimatedMg.toString() } })
|
||||
: t<string>('max gas allowed (M)'),
|
||||
[estimatedMg, t]
|
||||
);
|
||||
|
||||
useEffect((): void => {
|
||||
estimatedMg && withEstimate && setMegaGas(estimatedMg);
|
||||
withEstimate && estimatedMg && setMegaGas(estimatedMg);
|
||||
}, [estimatedMg, setMegaGas, withEstimate]);
|
||||
|
||||
const isDisabled = !!estimatedMg && withEstimate;
|
||||
@ -50,7 +41,11 @@ function InputMegaGas ({ className, estimatedWeight, executionTime, help, isVali
|
||||
help={help}
|
||||
isDisabled={isDisabled}
|
||||
isError={!isValid}
|
||||
label={label}
|
||||
label={
|
||||
estimatedMg
|
||||
? t<string>('max gas allowed (M, {{estimatedMg}} estimated)', { replace: { estimatedMg: estimatedMg.toString() } })
|
||||
: t<string>('max gas allowed (M)')
|
||||
}
|
||||
onChange={isDisabled ? undefined : setMegaGas}
|
||||
value={isDisabled ? undefined : megaGas}
|
||||
>
|
||||
|
||||
@ -25,10 +25,10 @@ export interface ContractJsonOld {
|
||||
}
|
||||
|
||||
export interface UseWeight {
|
||||
isValid: boolean;
|
||||
weight: BN;
|
||||
executionTime: number;
|
||||
isValid: boolean;
|
||||
megaGas: BN;
|
||||
percentage: number;
|
||||
setMegaGas: React.Dispatch<BN | undefined>
|
||||
setMegaGas: React.Dispatch<BN | undefined>;
|
||||
weight: BN;
|
||||
}
|
||||
|
||||
@ -4,43 +4,45 @@
|
||||
import { UseWeight } from './types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { useApi, useBlockTime } from '@polkadot/react-hooks';
|
||||
import { BN_TEN, BN_ZERO } from '@polkadot/util';
|
||||
|
||||
const BN_MILLION = new BN(1e6);
|
||||
|
||||
export default function useWeight (initialValue?: BN): UseWeight {
|
||||
export default function useWeight (): UseWeight {
|
||||
const { api } = useApi();
|
||||
const [blockTime] = useBlockTime();
|
||||
const [megaGas, setMegaGas] = useState<BN | undefined>(
|
||||
initialValue || api.consts.system.maximumBlockWeight.div(BN_MILLION).div(BN_TEN)
|
||||
);
|
||||
const [executionTime, percentage, weight, isValid] = useMemo(
|
||||
(): [number, number, BN, boolean] => {
|
||||
if (!megaGas) {
|
||||
return [0, 0, BN_ZERO, false];
|
||||
}
|
||||
const [megaGas, _setMegaGas] = useState<BN>(api.consts.system.maximumBlockWeight.div(BN_MILLION).div(BN_TEN));
|
||||
|
||||
const weight = megaGas.mul(BN_MILLION);
|
||||
const executionTime = weight.muln(blockTime).div(api.consts.system.maximumBlockWeight).toNumber();
|
||||
const percentage = (executionTime / blockTime) * 100;
|
||||
const setMegaGas = useCallback(
|
||||
(value?: BN | undefined) => _setMegaGas(value || api.consts.system.maximumBlockWeight.div(BN_MILLION).div(BN_TEN)),
|
||||
[api]
|
||||
);
|
||||
|
||||
return useMemo((): UseWeight => {
|
||||
let executionTime = 0;
|
||||
let percentage = 0;
|
||||
let weight = BN_ZERO;
|
||||
let isValid = false;
|
||||
|
||||
if (megaGas) {
|
||||
weight = megaGas.mul(BN_MILLION);
|
||||
executionTime = weight.muln(blockTime).div(api.consts.system.maximumBlockWeight).toNumber();
|
||||
percentage = (executionTime / blockTime) * 100;
|
||||
|
||||
// execution is 2s of 6s blocks, i.e. 1/3
|
||||
return [executionTime / 3000, percentage, weight, !megaGas.isZero() && percentage < 100];
|
||||
},
|
||||
[api, blockTime, megaGas]
|
||||
);
|
||||
executionTime = executionTime / 3000;
|
||||
isValid = !megaGas.isZero() && percentage < 65;
|
||||
}
|
||||
|
||||
return useMemo(
|
||||
() => ({
|
||||
return {
|
||||
executionTime,
|
||||
isValid,
|
||||
megaGas: megaGas || BN_ZERO,
|
||||
percentage,
|
||||
setMegaGas,
|
||||
weight
|
||||
}),
|
||||
[executionTime, isValid, megaGas, percentage, setMegaGas, weight]
|
||||
);
|
||||
};
|
||||
}, [api, blockTime, megaGas, setMegaGas]);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user