From 69afe1a2033e935436992942704770657a397a04 Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 12 Aug 2022 14:20:05 +0200 Subject: [PATCH] Add definitions type check (#5153) * Add definitions type check * RPC tests, types tests & adjustments * Rebuild * Run runtime & rpc checks --- .../types-augment/src/registry/interfaces.ts | 4 +- .../src/interfaces/benchmark/definitions.ts | 17 +++ .../types/src/interfaces/benchmark/types.ts | 23 ++- .../types/src/interfaces/definitions.spec.ts | 144 ++++++++++++++++++ packages/types/src/interfaces/eth/rpc.ts | 6 +- packages/types/src/interfaces/eth/runtime.ts | 2 +- 6 files changed, 190 insertions(+), 6 deletions(-) create mode 100644 packages/types/src/interfaces/definitions.spec.ts diff --git a/packages/types-augment/src/registry/interfaces.ts b/packages/types-augment/src/registry/interfaces.ts index 909ea05408..530ac825b2 100644 --- a/packages/types-augment/src/registry/interfaces.ts +++ b/packages/types-augment/src/registry/interfaces.ts @@ -15,7 +15,7 @@ import type { UncleEntryItem } from '@polkadot/types/interfaces/authorship'; import type { AllowedSlots, BabeAuthorityWeight, BabeBlockWeight, BabeEpochConfiguration, BabeEquivocationProof, BabeGenesisConfiguration, BabeGenesisConfigurationV1, BabeWeight, Epoch, EpochAuthorship, MaybeRandomness, MaybeVrf, NextConfigDescriptor, NextConfigDescriptorV1, OpaqueKeyOwnershipProof, Randomness, RawBabePreDigest, RawBabePreDigestCompat, RawBabePreDigestPrimary, RawBabePreDigestPrimaryTo159, RawBabePreDigestSecondaryPlain, RawBabePreDigestSecondaryTo159, RawBabePreDigestSecondaryVRF, RawBabePreDigestTo159, SlotNumber, VrfData, VrfOutput, VrfProof } from '@polkadot/types/interfaces/babe'; import type { AccountData, BalanceLock, BalanceLockTo212, BalanceStatus, Reasons, ReserveData, ReserveIdentifier, VestingSchedule, WithdrawReasons } from '@polkadot/types/interfaces/balances'; import type { BeefyAuthoritySet, BeefyCommitment, BeefyId, BeefyNextAuthoritySet, BeefyPayload, BeefyPayloadId, BeefySignedCommitment, MmrRootHash, ValidatorSet, ValidatorSetId } from '@polkadot/types/interfaces/beefy'; -import type { BenchmarkConfig, BenchmarkList, BenchmarkMetadata, BenchmarkParameter } from '@polkadot/types/interfaces/benchmark'; +import type { BenchmarkBatch, BenchmarkConfig, BenchmarkList, BenchmarkMetadata, BenchmarkParameter, BenchmarkResult } from '@polkadot/types/interfaces/benchmark'; import type { CheckInherentsResult, InherentData, InherentIdentifier } from '@polkadot/types/interfaces/blockbuilder'; import type { BridgeMessageId, BridgedBlockHash, BridgedBlockNumber, BridgedHeader, CallOrigin, ChainId, DeliveredMessages, DispatchFeePayment, InboundLaneData, InboundRelayer, InitializationData, LaneId, MessageData, MessageKey, MessageNonce, MessagesDeliveryProofOf, MessagesProofOf, OperatingMode, OutboundLaneData, OutboundMessageFee, OutboundPayload, Parameter, RelayerId, UnrewardedRelayer, UnrewardedRelayersState } from '@polkadot/types/interfaces/bridges'; import type { BlockHash } from '@polkadot/types/interfaces/chain'; @@ -155,10 +155,12 @@ declare module '@polkadot/types/types/registry' { BeefyPayload: BeefyPayload; BeefyPayloadId: BeefyPayloadId; BeefySignedCommitment: BeefySignedCommitment; + BenchmarkBatch: BenchmarkBatch; BenchmarkConfig: BenchmarkConfig; BenchmarkList: BenchmarkList; BenchmarkMetadata: BenchmarkMetadata; BenchmarkParameter: BenchmarkParameter; + BenchmarkResult: BenchmarkResult; Bid: Bid; Bidder: Bidder; BidKind: BidKind; diff --git a/packages/types/src/interfaces/benchmark/definitions.ts b/packages/types/src/interfaces/benchmark/definitions.ts index 31a23d2205..d80f31d210 100644 --- a/packages/types/src/interfaces/benchmark/definitions.ts +++ b/packages/types/src/interfaces/benchmark/definitions.ts @@ -12,6 +12,12 @@ export default { rpc: {}, runtime, types: { + BenchmarkBatch: { + pallet: 'Text', + instance: 'Text', + benchmark: 'Text', + results: 'Vec' + }, BenchmarkConfig: { pallet: 'Bytes', benchmark: 'Bytes', @@ -30,6 +36,17 @@ export default { }, BenchmarkParameter: { _enum: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] + }, + BenchmarkResult: { + components: 'Vec<(BenchmarkParameter, u32)>', + extrinsicTime: 'u128', + storageRootTime: 'u128', + reads: 'u32', + repeatReads: 'u32', + writes: 'u32', + repeatWrites: 'u32', + proofSize: 'u32', + benchKeys: 'Vec<(Vec, u32, u32, bool)>' } } } as Definitions; diff --git a/packages/types/src/interfaces/benchmark/types.ts b/packages/types/src/interfaces/benchmark/types.ts index 976606ab33..5f6fc2e41b 100644 --- a/packages/types/src/interfaces/benchmark/types.ts +++ b/packages/types/src/interfaces/benchmark/types.ts @@ -1,9 +1,17 @@ // Auto-generated via `yarn polkadot-types-from-defs`, do not edit /* eslint-disable */ -import type { Bytes, Enum, Struct, Vec, bool, u32 } from '@polkadot/types-codec'; +import type { Bytes, Enum, Struct, Text, Vec, bool, u128, u32 } from '@polkadot/types-codec'; import type { ITuple } from '@polkadot/types-codec/types'; +/** @name BenchmarkBatch */ +export interface BenchmarkBatch extends Struct { + readonly pallet: Text; + readonly instance: Text; + readonly benchmark: Text; + readonly results: Vec; +} + /** @name BenchmarkConfig */ export interface BenchmarkConfig extends Struct { readonly pallet: Bytes; @@ -57,4 +65,17 @@ export interface BenchmarkParameter extends Enum { readonly type: 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z'; } +/** @name BenchmarkResult */ +export interface BenchmarkResult extends Struct { + readonly components: Vec>; + readonly extrinsicTime: u128; + readonly storageRootTime: u128; + readonly reads: u32; + readonly repeatReads: u32; + readonly writes: u32; + readonly repeatWrites: u32; + readonly proofSize: u32; + readonly benchKeys: Vec>; +} + export type PHANTOM_BENCHMARK = 'benchmark'; diff --git a/packages/types/src/interfaces/definitions.spec.ts b/packages/types/src/interfaces/definitions.spec.ts new file mode 100644 index 0000000000..147664d540 --- /dev/null +++ b/packages/types/src/interfaces/definitions.spec.ts @@ -0,0 +1,144 @@ +// Copyright 2017-2022 @polkadot/types authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { DefinitionsCall, DefinitionsRpc, DefinitionsTypes, RegistryTypes } from '../types'; + +import rpcMetadata from '@polkadot/types-support/metadata/static-substrate'; + +import { getTypeDef, TypeRegistry } from '../create'; +import { Metadata } from '../metadata'; +import * as all from './definitions'; + +interface CheckDef { + rpc: DefinitionsRpc; + runtime: DefinitionsCall; + types: DefinitionsTypes; +} + +const registry = new TypeRegistry(); +const types = Object.values(all).reduce>((r, { types = {} }) => { + Object.entries(types).forEach(([k, v]) => { + r[k] = v; + }); + + return r; +}, {}); + +registry.register(types as RegistryTypes); +registry.setMetadata(new Metadata(registry, rpcMetadata)); + +function inspectType (type: string): void { + console.error(type); + + // get the definition + const td = registry.isLookupType(type) + ? registry.lookup.getTypeDef(type) + : getTypeDef(type); + + console.error(td); + + // inspect the subs + if (Array.isArray(td.sub)) { + for (let i = 0; i < td.sub.length; i++) { + inspectType(td.sub[i].type); + } + } else if (td.sub) { + inspectType(td.sub.type); + } else if (!['Vec'].includes(type)) { + // see if we have it + registry.getOrThrow(type); + } + + // create a class + const Clazz = registry.createClass(type); + + // construct the class + // eslint-disable-next-line no-new + new Clazz(registry); + + // create it + registry.createType(type); +} + +describe('type definitions', (): void => { + const types = Object.entries(all).filter((v): v is [string, CheckDef] => + !!v[1].types && + Object.keys(v[1].types).length !== 0 && + v[0] === 'benchmark' + ); + + describe.each(types)('%s', (_, { types }): void => { + const typesKeys = Object.keys(types).filter((type) => + // meant to fail + type !== 'ExtrinsicUnknown' && + type !== 'ExtrinsicPayloadUnknown' && + // injected at runtime + type !== 'Origin' && + // it will fail of MetadataV0 + type !== 'MetadataAll' + ); + + it.each(typesKeys)('%s is known', (type): void => { + expect(() => inspectType(type)).not.toThrow(); + }); + }); +}); + +describe('rpc definitions', (): void => { + const rpcs = Object.entries(all).filter((v): v is [string, CheckDef] => + !!v[1].rpc && + Object.keys(v[1].rpc).length !== 0 + ); + + describe.each(rpcs)('%s', (section, { rpc }): void => { + const methodsEntries = Object.entries(rpc); + + describe.each(methodsEntries)('%s', (method, { params, type }): void => { + // We cannot constuct V0, so just ignore + if (section !== 'state' || method !== 'getMetadata') { + it(`output ${type} is known`, (): void => { + expect(() => inspectType(type)).not.toThrow(); + }); + } + + if (params.length) { + describe('params', (): void => { + it.each(params)('$name: $type is known', ({ type }): void => { + expect(() => inspectType(type)).not.toThrow(); + }); + }); + } + }); + }); +}); + +describe('runtime definitions', (): void => { + const runtimes = Object.entries(all).filter((v): v is [string, CheckDef] => + !!v[1].runtime && + Object.keys(v[1].runtime).length !== 0 + ); + + describe.each(runtimes)('%s', (_, { runtime }): void => { + const versionsEntries = Object.entries(runtime); + + describe.each(versionsEntries)('%s', (_, versions): void => { + describe.each(versions)('version $version', ({ methods }): void => { + const methodsEntries = Object.entries(methods); + + describe.each(methodsEntries)('%s', (_, { params, type }): void => { + it(`output ${type} is known`, (): void => { + expect(() => inspectType(type)).not.toThrow(); + }); + + if (params.length) { + describe('params', (): void => { + it.each(params)('$name: $type is known', ({ type }): void => { + expect(() => inspectType(type)).not.toThrow(); + }); + }); + } + }); + }); + }); + }); +}); diff --git a/packages/types/src/interfaces/eth/rpc.ts b/packages/types/src/interfaces/eth/rpc.ts index e3a4c78146..c132c601aa 100644 --- a/packages/types/src/interfaces/eth/rpc.ts +++ b/packages/types/src/interfaces/eth/rpc.ts @@ -19,13 +19,13 @@ const netRpc: DefinitionsRpc = { aliasSection: 'net', description: 'Returns number of peers connected to node.', params: [], - type: 'String' + type: 'Text' }, version: { aliasSection: 'net', description: 'Returns protocol version.', params: [], - type: 'String' + type: 'Text' } }; const web3Rpc: DefinitionsRpc = { @@ -33,7 +33,7 @@ const web3Rpc: DefinitionsRpc = { aliasSection: 'web3', description: 'Returns current client version.', params: [], - type: 'String' + type: 'Text' }, sha3: { aliasSection: 'web3', diff --git a/packages/types/src/interfaces/eth/runtime.ts b/packages/types/src/interfaces/eth/runtime.ts index fbc030fea6..c207749970 100644 --- a/packages/types/src/interfaces/eth/runtime.ts +++ b/packages/types/src/interfaces/eth/runtime.ts @@ -180,7 +180,7 @@ export const runtime: DefinitionsCall = { current_all: { description: 'Return all the current data for a block in a single runtime call.', params: [], - type: '(Option, Option>, Option>)' + type: '(Option, Option>, Option>)' }, current_block: { description: 'Return the current block.',