Add definitions type check (#5153)

* Add definitions type check

* RPC tests, types tests & adjustments

* Rebuild

* Run runtime & rpc checks
This commit is contained in:
Jaco
2022-08-12 15:20:05 +03:00
committed by GitHub
parent f5600afa31
commit 69afe1a203
6 changed files with 190 additions and 6 deletions
@@ -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;
@@ -12,6 +12,12 @@ export default {
rpc: {},
runtime,
types: {
BenchmarkBatch: {
pallet: 'Text',
instance: 'Text',
benchmark: 'Text',
results: 'Vec<BenchmarkResult>'
},
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<u8>, u32, u32, bool)>'
}
}
} as Definitions;
@@ -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<BenchmarkResult>;
}
/** @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<ITuple<[BenchmarkParameter, u32]>>;
readonly extrinsicTime: u128;
readonly storageRootTime: u128;
readonly reads: u32;
readonly repeatReads: u32;
readonly writes: u32;
readonly repeatWrites: u32;
readonly proofSize: u32;
readonly benchKeys: Vec<ITuple<[Bytes, u32, u32, bool]>>;
}
export type PHANTOM_BENCHMARK = 'benchmark';
@@ -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<Record<string, unknown>>((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<u8>'].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();
});
});
}
});
});
});
});
});
+3 -3
View File
@@ -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',
+1 -1
View File
@@ -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<BlockV2>, Option<Vec<ReceiptV0>>, Option<Vec<TransactionStatus>>)'
type: '(Option<BlockV2>, Option<Vec<EthReceiptV3>>, Option<Vec<EthTransactionStatus>>)'
},
current_block: {
description: 'Return the current block.',