From 11cb516546e4576fc378f7f3dd90e84000510762 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 15 Dec 2022 10:55:32 +0200 Subject: [PATCH] Display warning/error on preimages (#8545) * Display warning/error on preimages * Display image depositor info * Bytes length warning * Adjust error message * Comment flow * Error adjust * media classes --- .../page-preimages/src/Preimages/Call.tsx | 51 ++++++++++++ .../page-preimages/src/Preimages/Preimage.tsx | 13 +--- .../page-preimages/src/Preimages/index.tsx | 1 + packages/page-preimages/src/types.ts | 6 ++ packages/page-preimages/src/usePreimage.ts | 77 ++++++++++++++----- .../src/Hashes/{Call.tsx => Details.tsx} | 18 +---- packages/page-whitelist/src/Hashes/index.tsx | 7 +- packages/react-components/src/Table/index.tsx | 4 + 8 files changed, 129 insertions(+), 48 deletions(-) create mode 100644 packages/page-preimages/src/Preimages/Call.tsx rename packages/page-whitelist/src/Hashes/{Call.tsx => Details.tsx} (53%) diff --git a/packages/page-preimages/src/Preimages/Call.tsx b/packages/page-preimages/src/Preimages/Call.tsx new file mode 100644 index 0000000000..84463404d6 --- /dev/null +++ b/packages/page-preimages/src/Preimages/Call.tsx @@ -0,0 +1,51 @@ +// Copyright 2017-2022 @polkadot/app-preimages authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { Preimage } from '../types'; + +import React from 'react'; + +import { AddressMini, CallExpander, MarkError, MarkWarning } from '@polkadot/react-components'; + +import { useTranslation } from '../translate'; + +interface Props { + className?: string; + value?: Preimage; +} + +function PreimageCall ({ className = '', value }: Props): React.ReactElement { + const { t } = useTranslation(); + + return ( + <> + + {value && ( + <> + {value.proposal && ( + ('call')} + value={value.proposal} + /> + )} + { + (value.proposalError && ) || + (value.proposalWarning && ) + } + + )} + + + {value?.deposit && ( + + )} + + + ); +} + +export default React.memo(PreimageCall); diff --git a/packages/page-preimages/src/Preimages/Preimage.tsx b/packages/page-preimages/src/Preimages/Preimage.tsx index fac31a4789..ef3d62ccc7 100644 --- a/packages/page-preimages/src/Preimages/Preimage.tsx +++ b/packages/page-preimages/src/Preimages/Preimage.tsx @@ -5,11 +5,10 @@ import type { HexString } from '@polkadot/util/types'; import React from 'react'; -import { CallExpander } from '@polkadot/react-components'; import { formatNumber } from '@polkadot/util'; -import { useTranslation } from '../translate'; import usePreimage from '../usePreimage'; +import Call from './Call'; import Hash from './Hash'; interface Props { @@ -18,20 +17,12 @@ interface Props { } function Preimage ({ className, value }: Props): React.ReactElement { - const { t } = useTranslation(); const info = usePreimage(value); return ( - - {info?.proposal && ( - ('proposal')} - value={info.proposal} - /> - )} - + {info?.bytes && formatNumber(info.bytes.length)} diff --git a/packages/page-preimages/src/Preimages/index.tsx b/packages/page-preimages/src/Preimages/index.tsx index f0a91d4cb5..9dc4cc7a78 100644 --- a/packages/page-preimages/src/Preimages/index.tsx +++ b/packages/page-preimages/src/Preimages/index.tsx @@ -25,6 +25,7 @@ function Hashes ({ className }: Props): React.ReactElement { const headerRef = useRef([ [t('preimages'), 'start', 2], + [undefined, 'media--1300'], [t('size')], [t('status')], [t('count')] diff --git a/packages/page-preimages/src/types.ts b/packages/page-preimages/src/types.ts index b21e65f3c6..78eb97ec3c 100644 --- a/packages/page-preimages/src/types.ts +++ b/packages/page-preimages/src/types.ts @@ -9,11 +9,17 @@ import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; export interface Preimage { + deposit?: { + amount: BN; + who: string; + }; count: number; bytes: Bytes | null; proposal: Call | null; + proposalError: string | null; proposalHash: HexString; proposalLength: BN; + proposalWarning: string | null; registry: Registry; status: PalletPreimageRequestStatus | null; } diff --git a/packages/page-preimages/src/usePreimage.ts b/packages/page-preimages/src/usePreimage.ts index 6f3f2439f2..33597287c1 100644 --- a/packages/page-preimages/src/usePreimage.ts +++ b/packages/page-preimages/src/usePreimage.ts @@ -3,7 +3,7 @@ import type { ApiPromise } from '@polkadot/api'; import type { Bytes, Option } from '@polkadot/types'; -import type { Call, Hash } from '@polkadot/types/interfaces'; +import type { AccountId32, Call, Hash } from '@polkadot/types/interfaces'; import type { FrameSupportPreimagesBounded, PalletPreimageRequestStatus } from '@polkadot/types/lookup'; import type { BN } from '@polkadot/util'; import type { HexString } from '@polkadot/util/types'; @@ -14,18 +14,33 @@ import { useMemo } from 'react'; import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks'; import { BN_ZERO, isBn, isString } from '@polkadot/util'; -function createResult (api: ApiPromise, optStatus: Option, optBytes: Option, [proposalHash, proposalLength]: [HexString, BN]): Preimage { +interface BytesParams { + deposit: [AccountId32, BN] | null; + params: [[hexHash: HexString, length: BN]] +} + +function createResult (api: ApiPromise, optStatus: Option, optBytes: Option, { deposit, params: [[proposalHash, proposalLength]] }: BytesParams): Preimage { const status = optStatus.unwrapOr(null); const bytes = optBytes.unwrapOr(null); let count = 0; let proposal: Call | null = null; + let proposalError: string | null = null; + let proposalWarning: string | null = null; if (bytes) { try { proposal = api.registry.createType('Call', bytes.toU8a(true)); + + if (proposal.encodedLength !== proposalLength.toNumber()) { + proposalWarning = 'Call length does not match on-chain stored preimage length'; + } } catch (error) { console.error(error); + + proposalError = 'Unable to decode preimage bytes into a valid Call'; } + } else { + proposalWarning = 'No preimage bytes found'; } if (status && status.isRequested) { @@ -41,25 +56,45 @@ function createResult (api: ApiPromise, optStatus: Option): [HexString, BN] { +function getBytesParams (hash: Hash | HexString, optStatus: Option): BytesParams { const status = optStatus.unwrapOr(null); const hexHash = isString(hash) ? hash : hash.toHex(); - return status - ? status.isRequested - ? [hexHash, status.asRequested.len.unwrapOr(BN_ZERO)] - : [hexHash, status.asUnrequested.len] - : [hexHash, BN_ZERO]; + if (!status) { + return { + deposit: null, + params: [[hexHash, BN_ZERO]] + }; + } else if (status.isRequested) { + const { deposit, len } = status.asRequested; + + return { + deposit: deposit.unwrapOr(null), + params: [[hexHash, len.unwrapOr(BN_ZERO)]] + }; + } + + const { deposit, len } = status.asUnrequested; + + return { + deposit, + params: [[hexHash, len]] + }; } export function getPreimageHash (hashOrBounded: Hash | HexString | FrameSupportPreimagesBounded): HexString { @@ -81,28 +116,30 @@ export function getPreimageHash (hashOrBounded: Hash | HexString | FrameSupportP function usePreimageImpl (hashOrBounded?: Hash | HexString | FrameSupportPreimagesBounded | null): Preimage | undefined { const { api } = useApi(); - const hash = useMemo( + // retrieve the status using only the hash of the image + const paramsStatus = useMemo( // we need a hash _and_ be on the newest supported version of the pallet // (after the application of bounded calls) - () => api.query.preimage?.preimageFor?.creator.meta.type.isMap && api.registry.lookup.getTypeDef(api.query.preimage.preimageFor.creator.meta.type.asMap.key).type === '(H256,u32)' && hashOrBounded && - getPreimageHash(hashOrBounded), + () => api.query.preimage?.preimageFor?.creator.meta.type.isMap && api.registry.lookup.getTypeDef(api.query.preimage.preimageFor.creator.meta.type.asMap.key).type === '(H256,u32)' && hashOrBounded + ? [getPreimageHash(hashOrBounded)] + : undefined, [api, hashOrBounded] ); - const optStatus = useCall>(hash && api.query.preimage?.statusFor, [hash]); + const optStatus = useCall>(paramsStatus && api.query.preimage?.statusFor, paramsStatus); - const params = useMemo( - () => hash && optStatus && getParams(hash, optStatus), - [hash, optStatus] + // from the retrieved status (if any), get the on-chain stored bytes + const paramsBytes = useMemo( + () => paramsStatus && optStatus && getBytesParams(paramsStatus[0], optStatus), + [optStatus, paramsStatus] ); - const optBytes = useCall>(params && api.query.preimage?.preimageFor, [params]); + const optBytes = useCall>(paramsBytes && api.query.preimage?.preimageFor, paramsBytes && paramsBytes.params); + // extract all the preimage info we have retrieved return useMemo( - () => optBytes && optStatus && params - ? createResult(api, optStatus, optBytes, params) - : undefined, - [api, optBytes, optStatus, params] + () => optBytes && optStatus && paramsBytes && createResult(api, optStatus, optBytes, paramsBytes), + [api, optBytes, optStatus, paramsBytes] ); } diff --git a/packages/page-whitelist/src/Hashes/Call.tsx b/packages/page-whitelist/src/Hashes/Details.tsx similarity index 53% rename from packages/page-whitelist/src/Hashes/Call.tsx rename to packages/page-whitelist/src/Hashes/Details.tsx index 5eb49b73ed..ee4dd45753 100644 --- a/packages/page-whitelist/src/Hashes/Call.tsx +++ b/packages/page-whitelist/src/Hashes/Details.tsx @@ -5,34 +5,24 @@ import type { HexString } from '@polkadot/util/types'; import React from 'react'; +import Call from '@polkadot/app-preimages/Preimages/Call'; import Hash from '@polkadot/app-preimages/Preimages/Hash'; import usePreimage from '@polkadot/app-preimages/usePreimage'; -import { CallExpander } from '@polkadot/react-components'; - -import { useTranslation } from '../translate'; interface Props { className?: string; value: HexString; } -function Call ({ className, value }: Props): React.ReactElement { - const { t } = useTranslation(); +function Details ({ className, value }: Props): React.ReactElement { const info = usePreimage(value); return ( - - {info?.proposal && ( - ('call')} - value={info.proposal} - /> - )} - + ); } -export default React.memo(Call); +export default React.memo(Details); diff --git a/packages/page-whitelist/src/Hashes/index.tsx b/packages/page-whitelist/src/Hashes/index.tsx index 8e16641154..944bce7110 100644 --- a/packages/page-whitelist/src/Hashes/index.tsx +++ b/packages/page-whitelist/src/Hashes/index.tsx @@ -9,7 +9,7 @@ import { Table } from '@polkadot/react-components'; import { useTranslation } from '../translate'; import useHashes from '../useHashes'; -import Call from './Call'; +import Details from './Details'; import Summary from './Summary'; interface Props { @@ -24,7 +24,8 @@ function Hashes ({ className }: Props): React.ReactElement { const headerRef = useRef([ [t('calls'), 'start'], - [undefined, 'all'] + [undefined, 'all'], + [undefined, 'media--1300'] ]); return ( @@ -36,7 +37,7 @@ function Hashes ({ className }: Props): React.ReactElement { header={headerRef.current} > {hashes && hashes.map((h) => ( - diff --git a/packages/react-components/src/Table/index.tsx b/packages/react-components/src/Table/index.tsx index 43034fc706..a3b083ad53 100644 --- a/packages/react-components/src/Table/index.tsx +++ b/packages/react-components/src/Table/index.tsx @@ -152,6 +152,10 @@ export default React.memo(styled(Table)` text-align: left; vertical-align: middle; + > article.mark { + margin-left: 0rem; + } + &:first-child { border-left: 1px solid var(--border-table); }