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
This commit is contained in:
Jaco 2022-12-15 10:55:32 +02:00 committed by GitHub
parent a4f448ae68
commit 11cb516546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 129 additions and 48 deletions

View File

@ -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<Props> {
const { t } = useTranslation();
return (
<>
<td className={`${className} all`}>
{value && (
<>
{value.proposal && (
<CallExpander
labelHash={t<string>('call')}
value={value.proposal}
/>
)}
{
(value.proposalError && <MarkError content={value.proposalError} />) ||
(value.proposalWarning && <MarkWarning content={value.proposalWarning} />)
}
</>
)}
</td>
<td className='media--1300'>
{value?.deposit && (
<AddressMini
balance={value.deposit.amount}
value={value.deposit.who}
withBalance
/>
)}
</td>
</>
);
}
export default React.memo(PreimageCall);

View File

@ -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<Props> {
const { t } = useTranslation();
const info = usePreimage(value);
return (
<tr className={ className }>
<Hash value={value} />
<td className='all'>
{info?.proposal && (
<CallExpander
labelHash={t<string>('proposal')}
value={info.proposal}
/>
)}
</td>
<Call value={info} />
<td className='number'>
{info?.bytes && formatNumber(info.bytes.length)}
</td>

View File

@ -25,6 +25,7 @@ function Hashes ({ className }: Props): React.ReactElement<Props> {
const headerRef = useRef([
[t('preimages'), 'start', 2],
[undefined, 'media--1300'],
[t('size')],
[t('status')],
[t('count')]

View File

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

View File

@ -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<PalletPreimageRequestStatus>, optBytes: Option<Bytes>, [proposalHash, proposalLength]: [HexString, BN]): Preimage {
interface BytesParams {
deposit: [AccountId32, BN] | null;
params: [[hexHash: HexString, length: BN]]
}
function createResult (api: ApiPromise, optStatus: Option<PalletPreimageRequestStatus>, optBytes: Option<Bytes>, { 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<PalletPreimageRequestS
return {
bytes,
count,
deposit: deposit
? { amount: deposit[1], who: deposit[0].toString() }
: undefined,
proposal,
proposalError,
proposalHash,
proposalLength,
proposalWarning,
registry: api.registry,
status
};
}
function getParams (hash: Hash | HexString, optStatus: Option<PalletPreimageRequestStatus>): [HexString, BN] {
function getBytesParams (hash: Hash | HexString, optStatus: Option<PalletPreimageRequestStatus>): 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<Option<PalletPreimageRequestStatus>>(hash && api.query.preimage?.statusFor, [hash]);
const optStatus = useCall<Option<PalletPreimageRequestStatus>>(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<Option<Bytes>>(params && api.query.preimage?.preimageFor, [params]);
const optBytes = useCall<Option<Bytes>>(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]
);
}

View File

@ -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<Props> {
const { t } = useTranslation();
function Details ({ className, value }: Props): React.ReactElement<Props> {
const info = usePreimage(value);
return (
<tr className={ className }>
<Hash value={value} />
<td className='all'>
{info?.proposal && (
<CallExpander
labelHash={t<string>('call')}
value={info.proposal}
/>
)}
</td>
<Call value={info} />
</tr>
);
}
export default React.memo(Call);
export default React.memo(Details);

View File

@ -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<Props> {
const headerRef = useRef([
[t('calls'), 'start'],
[undefined, 'all']
[undefined, 'all'],
[undefined, 'media--1300']
]);
return (
@ -36,7 +37,7 @@ function Hashes ({ className }: Props): React.ReactElement<Props> {
header={headerRef.current}
>
{hashes && hashes.map((h) => (
<Call
<Details
key={h}
value={h}
/>

View File

@ -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);
}