Adjust useWeights results (prepare for conv.) (#8192)

This commit is contained in:
Jaco 2022-10-03 10:46:54 +03:00 committed by GitHub
parent 273ad24071
commit 53faefd1a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 52 additions and 33 deletions

View File

@ -51,7 +51,7 @@ function MultisigApprove ({ className = '', onClose, ongoing, threshold, who }:
const { allAccounts } = useAccounts();
const [callHex, setCallHex] = useState<string>('');
const [{ callData, callError, callInfo }, setCallData] = useState<CallData>(EMPTY_CALL);
const [callWeight, callLength] = useWeight(callData);
const { encodedCallLength, v1Weight } = useWeight(callData);
const [hash, setHash] = useState<string | null>(() => ongoing[0][0].toHex());
const [{ isMultiCall, multisig }, setMultisig] = useState<MultiInfo>(() => ({ 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<u8> 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}
/>

View File

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

View File

@ -22,7 +22,7 @@ function Close ({ hasFailed, hash, idNumber, proposal }: Props): React.ReactElem
const { api } = useApi();
const [isOpen, toggleOpen] = useToggle();
const [accountId, setAccountId] = useState<string | null>(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
<Modal.Actions>
<TxButton
accountId={accountId}
isDisabled={!hasFailed && !proposalLength}
isDisabled={!hasFailed && !encodedCallLength}
onStart={toggleOpen}
params={
api.tx[modLocation].close.meta.args.length === 4
? hasFailed
? [hash, idNumber, 0, 0]
: [hash, idNumber, proposalWeight, proposalLength]
: [hash, idNumber, v1Weight, encodedCallLength]
: [hash, idNumber]
}
tx={api.tx[modLocation].closeOperational || api.tx[modLocation].close}

View File

@ -59,7 +59,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)
api.consts.system.blockWeights && api.consts.system.blockWeights.maxBlock && convertWeight(api.consts.system.blockWeights.maxBlock).v1Weight
],
[api, runtimeVersion]
);

View File

@ -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
];

View File

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

View File

@ -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<string | null>(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
<Modal.Actions>
<TxButton
accountId={accountId}
isDisabled={!hasFailed && !proposalLength}
isDisabled={!hasFailed && !encodedCallLength}
onStart={toggleOpen}
params={
api.tx[modLocation].close.meta.args.length === 4
? hasFailed
? [hash, idNumber, 0, 0]
: [hash, idNumber, proposalWeight, proposalLength]
: [hash, idNumber, v1Weight, encodedCallLength]
: [hash, idNumber]
}
tx={api.tx[modLocation].closeOperational || api.tx[modLocation].close}

View File

@ -62,12 +62,13 @@ function useTxBatchImpl (txs?: SubmittableExtrinsic<'promise'>[] | 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
)
);

View File

@ -20,19 +20,39 @@ interface V2Weight {
proofSize: ICompact<INumber>;
}
interface V2WeightConstruct {
refTime: BN | ICompact<INumber>;
}
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<void> => {
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);
}