Move runtime ordering checks to test (#5328)

This commit is contained in:
Jaco
2022-11-11 09:49:59 +02:00
committed by GitHub
parent a34e14a89f
commit e23401092f
15 changed files with 67 additions and 52 deletions
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { ChainUpgradesGenerated } from '../types';
import type { ChainUpgradesExpanded } from '../types';
import fs from 'fs';
@@ -20,7 +20,7 @@ const urls = {
describe.each(keys)('generate %s', (chain): void => {
const avail = allGen[chain];
const final: ChainUpgradesGenerated = [];
const final: ChainUpgradesExpanded = [];
let api: ApiPromise;
beforeAll(async (): Promise<void> => {
@@ -34,9 +34,9 @@ describe.each(keys)('generate %s', (chain): void => {
// Auto-generated from on-chain data & manual definitions, do not edit
/* eslint-disable quotes, comma-spacing */
import type { ChainUpgradesGenerated } from '../types';
import type { ChainUpgradesExpanded } from '../types';
const upgrades: ChainUpgradesGenerated = ${JSON.stringify(final, null, 2)};
const upgrades: ChainUpgradesExpanded = ${JSON.stringify(final, null, 2)};
export default upgrades;
`);
@@ -4,9 +4,9 @@
// Auto-generated from on-chain data & manual definitions, do not edit
/* eslint-disable quotes, comma-spacing */
import type { ChainUpgradesGenerated } from '../types';
import type { ChainUpgradesExpanded } from '../types';
const upgrades: ChainUpgradesGenerated = [
const upgrades: ChainUpgradesExpanded = [
[
0,
1020,
@@ -4,9 +4,9 @@
// Auto-generated from on-chain data & manual definitions, do not edit
/* eslint-disable quotes, comma-spacing */
import type { ChainUpgradesGenerated } from '../types';
import type { ChainUpgradesExpanded } from '../types';
const upgrades: ChainUpgradesGenerated = [
const upgrades: ChainUpgradesExpanded = [
[
0,
0,
@@ -4,9 +4,9 @@
// Auto-generated from on-chain data & manual definitions, do not edit
/* eslint-disable quotes, comma-spacing */
import type { ChainUpgradesGenerated } from '../types';
import type { ChainUpgradesExpanded } from '../types';
const upgrades: ChainUpgradesGenerated = [
const upgrades: ChainUpgradesExpanded = [
[
214356,
4,
@@ -1,7 +1,9 @@
// Copyright 2017-2022 @polkadot/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { u8aEq } from '@polkadot/util';
import type { ChainUpgradesExpanded, ChainUpgradesRaw } from './types';
import { stringify, u8aEq } from '@polkadot/util';
import * as allGen from './e2e';
import * as allMan from './manual';
@@ -40,6 +42,20 @@ const TESTS: TestDef[] = [
}
];
function checkOrder (network: string, versions: [number, number, ...unknown[]][]): void {
const ooo = versions.filter((curr, index): boolean => {
const prev = versions[index - 1];
return index === 0
? false
: curr[0] <= prev[0] || curr[1] <= prev[1];
});
if (ooo.length) {
throw new Error(`${network}: Mismatched upgrade ordering: ${stringify(ooo)}`);
}
}
describe('generated', (): void => {
it('should have all the chains', (): void => {
expect(Object.keys(allMan).sort()).toEqual(Object.keys(allGen).sort());
@@ -58,6 +74,14 @@ describe('generated', (): void => {
throw new Error(`${chain}:: missing generated apis found, run yarn test:one packages/types-known/src/upgrades/e2e`);
}
});
it('manual should be correctly ordered', (): void => {
checkOrder(chain, (allGen as Record<string, ChainUpgradesExpanded>)[chain]);
});
it('generated should be correctly ordered', (): void => {
checkOrder(chain, (allMan as Record<string, ChainUpgradesRaw>)[chain]);
});
});
});
+7 -23
View File
@@ -2,39 +2,23 @@
// SPDX-License-Identifier: Apache-2.0
import type { ChainUpgrades } from '@polkadot/types/types';
import type { ChainUpgradesGenerated } from './types';
import type { HexString } from '@polkadot/util/types';
import type { ChainUpgradesExpanded } from './types';
import { selectableNetworks } from '@polkadot/networks';
import { BN, hexToU8a, stringify } from '@polkadot/util';
import { BN, hexToU8a } from '@polkadot/util';
import * as allKnown from './e2e';
// testnets are not available in the networks map
const NET_EXTRA: Record<string, { genesisHash: string[] }> = {
const NET_EXTRA: Record<string, { genesisHash: HexString[] }> = {
westend: {
genesisHash: ['0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e']
}
};
/** @internal */
function checkOrder (network: string, versions: ChainUpgradesGenerated): ChainUpgradesGenerated {
const ooo = versions.filter((curr, index): boolean => {
const prev = versions[index - 1];
return index === 0
? false
: curr[0] <= prev[0] || curr[1] <= prev[1];
});
if (ooo.length) {
throw new Error(`${network}: Mismatched upgrade ordering: ${stringify(ooo)}`);
}
return versions;
}
/** @internal */
function mapRaw ([network, versions]: [string, ChainUpgradesGenerated]): ChainUpgrades {
function mapRaw ([network, versions]: [string, ChainUpgradesExpanded]): ChainUpgrades {
const chain = selectableNetworks.find((n) => n.network === network) || NET_EXTRA[network];
if (!chain) {
@@ -44,7 +28,7 @@ function mapRaw ([network, versions]: [string, ChainUpgradesGenerated]): ChainUp
return {
genesisHash: hexToU8a(chain.genesisHash[0]),
network,
versions: checkOrder(network, versions).map(([blockNumber, specVersion, apis]) => ({
versions: versions.map(([blockNumber, specVersion, apis]) => ({
apis,
blockNumber: new BN(blockNumber),
specVersion: new BN(specVersion)
@@ -53,6 +37,6 @@ function mapRaw ([network, versions]: [string, ChainUpgradesGenerated]): ChainUp
}
// Type overrides for specific spec types & versions as given in runtimeVersion
const upgrades = Object.entries<ChainUpgradesGenerated>(allKnown).map(mapRaw);
const upgrades = Object.entries<ChainUpgradesExpanded>(allKnown).map(mapRaw);
export default upgrades;
@@ -1,9 +1,9 @@
// Copyright 2017-2022 @polkadot/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ChainUpgradesManual } from '../types';
import type { ChainUpgradesRaw } from '../types';
const upgrades: ChainUpgradesManual = [
const upgrades: ChainUpgradesRaw = [
[0, 1020], [26669, 1021], [38245, 1022], [54248, 1023], [59659, 1024],
[67651, 1025], [82191, 1027], [83238, 1028], [101503, 1029], [203466, 1030],
[295787, 1031], [461692, 1032], [504329, 1033], [569327, 1038], [587687, 1039],
@@ -1,9 +1,9 @@
// Copyright 2017-2022 @polkadot/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ChainUpgradesManual } from '../types';
import type { ChainUpgradesRaw } from '../types';
const upgrades: ChainUpgradesManual = [
const upgrades: ChainUpgradesRaw = [
[0, 0], [29231, 1], [188836, 5], [199405, 6], [214264, 7],
[244358, 8], [303079, 9], [314201, 10], [342400, 11], [443963, 12],
[528470, 13], [687751, 14], [746085, 15], [787923, 16], [799302, 17],
@@ -1,9 +1,9 @@
// Copyright 2017-2022 @polkadot/types-known authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ChainUpgradesManual } from '../types';
import type { ChainUpgradesRaw } from '../types';
const upgrades: ChainUpgradesManual = [
const upgrades: ChainUpgradesRaw = [
[214356, 4], [392764, 7], [409740, 8], [809976, 20], [877581, 24],
[879238, 25], [889472, 26], [902937, 27], [932751, 28], [991142, 29],
[1030162, 31], [1119657, 32], [1199282, 33], [1342534, 34], [1392263, 35],
+2 -4
View File
@@ -3,8 +3,6 @@
import type { HexString } from '@polkadot/util/types';
export type ChainUpgradesRaw = [number, number][];
export type ChainUpgradesRaw = [blockNumber: number, specVersion: number][];
export type ChainUpgradesGenerated = [number, number, [HexString, number][]][];
export type ChainUpgradesManual = [number, number][];
export type ChainUpgradesExpanded = [blockNumber: number, specVersion: number, runtimeApis: [apiHash: HexString, apiVersion: number][]][];
+13 -4
View File
@@ -13,20 +13,26 @@ import typesChain from './chain';
import typesSpec from './spec';
import upgrades from './upgrades';
/**
* @description Perform the callback function using the stringified spec/chain
* @internal
* */
function withNames <T> (chainName: Text | string, specName: Text | string, fn: (c: string, s: string) => T): T {
return fn(chainName.toString(), specName.toString());
}
// flatten a VersionedType[] into a Record<string, string>
/** @internal */
/**
* @descriptionFflatten a VersionedType[] into a Record<string, string>
* @internal
* */
function filterVersions (versions: OverrideVersionedType[] = [], specVersion: number): RegistryTypes {
return versions
.filter(({ minmax: [min, max] }) =>
(min === undefined || min === null || specVersion >= min) &&
(max === undefined || max === null || specVersion <= max)
)
.reduce((result: RegistryTypes, { types }): RegistryTypes =>
objectSpread(result, types), {}
.reduce((result: RegistryTypes, { types }) =>
objectSpread<RegistryTypes>(result, types), {}
);
}
@@ -65,6 +71,9 @@ export function getSpecTypes ({ knownTypes }: Registry, chainName: Text | string
);
}
/**
* @description Based on the chain or spec, return the hasher used
*/
export function getSpecHasher ({ knownTypes }: Registry, chainName: Text | string, specName: Text | string): CodecHasher | null {
return withNames(chainName, specName, (c, s) =>
knownTypes.hasher ||