Use CandidateIncluded events for last inclusion (#4406)

This commit is contained in:
Jaco Greeff 2021-01-14 10:15:24 +01:00 committed by GitHub
parent 6292b4e5d6
commit ef185f3c0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 15 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, HeadData, ParaId } from '@polkadot/types/interfaces';
import type { Balance, BlockNumber, Hash, HeadData, Header, ParaId } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import React, { useMemo } from 'react';
@ -18,6 +18,7 @@ interface Props {
bestNumber?: BN;
className?: string;
id: ParaId;
lastRelayParent?: Hash;
}
const transformHead = {
@ -27,11 +28,15 @@ const transformHead = {
: null
};
const transformMark = {
transform: (watermark: Option<BlockNumber>): BlockNumber | null =>
watermark.isSome
? watermark.unwrap()
: null
// const transformMark = {
// transform: (watermark: Option<BlockNumber>): BlockNumber | null =>
// watermark.isSome
// ? watermark.unwrap()
// : null
// };
const transformLast = {
transform: (header: Header) => header.number.unwrap()
};
function getChainLink (endpoints: LinkOption[]): React.ReactNode {
@ -44,17 +49,18 @@ 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 }: Props): React.ReactElement<Props> {
function Parachain ({ bestNumber, className = '', id, lastRelayParent }: 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 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 blockDelay = useMemo(
() => watermark && bestNumber && bestNumber.sub(watermark),
[bestNumber, watermark]
() => lastRelayNumber && bestNumber && bestNumber.sub(lastRelayNumber).subn(1),
[bestNumber, lastRelayNumber]
);
const chainLink = useMemo(
@ -68,7 +74,7 @@ function Parachain ({ bestNumber, className = '', id }: Props): React.ReactEleme
<td className='together'>{chainLink}</td>
<td className='all start together hash'>{headHex}</td>
<td className='number'>{blockDelay && <BlockToTime blocks={blockDelay} />}</td>
<td className='number'>{watermark && formatNumber(watermark)}</td>
<td className='number'>{lastRelayNumber && formatNumber(lastRelayNumber)}</td>
<td className='number'>{paraBest && formatNumber(paraBest)}</td>
<td className='number'>{paraIssu && <FormatBalance valueFormatted={paraIssu.toHuman()} />}</td>
</tr>

View File

@ -1,10 +1,10 @@
// Copyright 2017-2021 @polkadot/app-parachains authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ParaId } from '@polkadot/types/interfaces';
import type { CandidateReceipt, EventRecord, Hash, ParaId } from '@polkadot/types/interfaces';
import BN from 'bn.js';
import React, { useRef } from 'react';
import React, { useEffect, useRef, useState } from 'react';
import { Table } from '@polkadot/react-components';
import { useApi, useCall } from '@polkadot/react-hooks';
@ -20,11 +20,39 @@ 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>>({});
useEffect((): void => {
eventRecords && setLastIncluded((prev) => {
const updated: Record<string, Hash> = {};
let wasUpdated = false;
eventRecords.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;
wasUpdated = true;
}
});
return wasUpdated
? Object.entries(prev).reduce((updated: Record<string, Hash>, [id, hash]): Record<string, Hash> => {
if (!updated[id]) {
updated[id] = hash;
}
return updated;
}, updated)
: prev;
});
}, [api, eventRecords]);
const headerRef = useRef([
[t('parachains'), 'start', 2],
[t('heads'), 'start'],
[t('watermark'), 'undefined', 2],
[t('relay parent'), 'undefined', 2],
[t('chain best')],
[t('issuance')]
]);
@ -39,6 +67,7 @@ function ParachainList ({ ids }: Props): React.ReactElement<Props> {
bestNumber={bestNumber}
id={id}
key={id.toString()}
lastRelayParent={lastIncluded[id.toString()]}
/>
))}
</Table>

View File

@ -32,7 +32,11 @@ function EventsBase ({ children }: Props): React.ReactElement<Props> {
api.query.system.events((records): void => {
const newEvents: IndexedEvent[] = records
.map((record, index) => ({ indexes: [index], record }))
.filter(({ record: { event: { method, section } } }) => section !== 'system' && (method !== 'Deposit' || !['balances', 'treasury'].includes(section)))
.filter(({ record: { event: { method, section } } }) =>
section !== 'system' &&
(method !== 'Deposit' || !['balances', 'treasury'].includes(section)) &&
(section !== 'inclusion' || !['CandidateBacked', 'CandidateIncluded'].includes(method))
)
.reduce((combined: IndexedEvent[], e): IndexedEvent[] => {
const prev = combined.find(({ record: { event: { method, section } } }) => e.record.event.section === section && e.record.event.method === method);