Link from parachains to block events (#4409)

This commit is contained in:
Jaco Greeff 2021-01-14 11:58:51 +01:00 committed by GitHub
parent 177e637f54
commit a898eb6db7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 17 deletions

View File

@ -3,7 +3,7 @@
import type { LinkOption } from '@polkadot/apps-config/settings/types';
import type { Option } from '@polkadot/types';
import type { Balance, BlockNumber, Hash, HeadData, Header, ParaId } from '@polkadot/types/interfaces';
import type { Balance, BlockNumber, HeadData, Header, ParaId } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import React, { useMemo } from 'react';
@ -18,7 +18,7 @@ interface Props {
bestNumber?: BN;
className?: string;
id: ParaId;
lastRelayParent?: Hash;
lastInclusion?: [string, string];
}
const transformHead = {
@ -49,14 +49,14 @@ function getChainLink (endpoints: LinkOption[]): React.ReactNode {
return <a href={`${window.location.origin}${window.location.pathname}?rpc=${encodeURIComponent(value)}`}>{text}</a>;
}
function Parachain ({ bestNumber, className = '', id, lastRelayParent }: Props): React.ReactElement<Props> {
function Parachain ({ bestNumber, className = '', id, lastInclusion }: Props): React.ReactElement<Props> {
const { api } = useApi();
const { api: paraApi, endpoints } = useParaApi(id);
const headHex = useCall<string | null>(api.query.paras.heads, [id], transformHead);
// const watermark = useCall<BlockNumber | null>(api.query.hrmp?.hrmpWatermarks, [id], transformMark);
const paraBest = useCall<BlockNumber>(paraApi?.derive.chain.bestNumber);
const paraIssu = useCall<Balance>(paraApi?.query.balances?.totalIssuance);
const lastRelayNumber = useCall<BN>(lastRelayParent && api.rpc.chain.getHeader, [lastRelayParent], transformLast);
const lastRelayNumber = useCall<BN>(lastInclusion && api.rpc.chain.getHeader, [lastInclusion && lastInclusion[1]], transformLast);
const blockDelay = useMemo(
() => lastRelayNumber && bestNumber && bestNumber.sub(lastRelayNumber).subn(1),
@ -74,7 +74,12 @@ function Parachain ({ bestNumber, className = '', id, lastRelayParent }: Props):
<td className='together'>{chainLink}</td>
<td className='all start together hash'>{headHex}</td>
<td className='number'>{blockDelay && <BlockToTime blocks={blockDelay} />}</td>
<td className='number'>{lastRelayNumber && formatNumber(lastRelayNumber)}</td>
<td className='number'>
{lastInclusion && lastRelayNumber && (
<a href={`#/explorer/query/${lastInclusion[0]}`}>{formatNumber(lastRelayNumber)}</a>
)
}
</td>
<td className='number'>{paraBest && formatNumber(paraBest)}</td>
<td className='number'>{paraIssu && <FormatBalance valueFormatted={paraIssu.toHuman()} />}</td>
</tr>

View File

@ -1,7 +1,8 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CandidateReceipt, EventRecord, Hash, ParaId } from '@polkadot/types/interfaces';
import type { SignedBlockExtended } from '@polkadot/api-derive/type';
import type { CandidateReceipt, ParaId } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import React, { useEffect, useRef, useState } from 'react';
@ -20,34 +21,38 @@ function ParachainList ({ ids }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const { api } = useApi();
const bestNumber = useCall<BN>(api.derive.chain.bestNumber);
const eventRecords = useCall<EventRecord[]>(api.query.system.events);
const [lastIncluded, setLastIncluded] = useState<Record<string, Hash>>({});
const lastBlock = useCall<SignedBlockExtended>(api.derive.chain.subscribeNewBlocks);
const [lastIncluded, setLastIncluded] = useState<Record<string, [string, string]>>({});
useEffect((): void => {
eventRecords && setLastIncluded((prev) => {
const updated: Record<string, Hash> = {};
lastBlock && setLastIncluded((prev) => {
const updated: Record<string, [string, string]> = {};
const blockHash = lastBlock.block.header.hash.toHex();
let wasUpdated = false;
eventRecords.forEach(({ event, phase }) => {
if (phase.isApplyExtrinsic && api.events.inclusion?.CandidateBacked.is(event)) {
lastBlock.events.forEach(({ event, phase }) => {
if (phase.isApplyExtrinsic && api.events.inclusion.CandidateBacked.is(event)) {
const { descriptor } = event.data[0] as CandidateReceipt;
updated[descriptor.paraId.toString()] = descriptor.relayParent;
updated[descriptor.paraId.toString()] = [
blockHash,
descriptor.relayParent.toHex()
];
wasUpdated = true;
}
});
return wasUpdated
? Object.entries(prev).reduce((updated: Record<string, Hash>, [id, hash]): Record<string, Hash> => {
? Object.entries(prev).reduce((updated: Record<string, [string, string]>, [id, hashes]): Record<string, [string, string]> => {
if (!updated[id]) {
updated[id] = hash;
updated[id] = hashes;
}
return updated;
}, updated)
: prev;
});
}, [api, eventRecords]);
}, [api, lastBlock]);
const headerRef = useRef([
[t('parachains'), 'start', 2],
@ -67,7 +72,7 @@ function ParachainList ({ ids }: Props): React.ReactElement<Props> {
bestNumber={bestNumber}
id={id}
key={id.toString()}
lastRelayParent={lastIncluded[id.toString()]}
lastInclusion={lastIncluded[id.toString()]}
/>
))}
</Table>