Add versioned chain & spec types (#1510)
* Add versioned chain & spec types * getTypes, getVersionedTypes * Add min/max range * Extract specName & specVersion * Linting * Comment around helpers * Split types into seperate file * MIN, MAX comment * version -> minmax * Less code is always good * Remove unused import * Comment cleanup * Record<string, string> -> RegistryTypes * Cleanup linting, failing tests * Comments
This commit is contained in:
@@ -14,40 +14,10 @@ import { cryptoWaitReady, setSS58Format } from '@polkadot/util-crypto';
|
||||
import addressDefaults from '@polkadot/util-crypto/address/defaults';
|
||||
|
||||
import Decorate from './Decorate';
|
||||
import { getChainTypes, getMetadataTypes } from './typeInjector';
|
||||
|
||||
const KEEPALIVE_INTERVAL = 15000;
|
||||
|
||||
// these are override types for polkadot chains
|
||||
// NOTE The SessionKeys definition for Polkadot and Substrate (OpaqueKeys
|
||||
// implementation) are different. Detect Polkadot and inject the `Keys`
|
||||
// definition as applicable. (3 keys in substrate vs 4 in Polkadot).
|
||||
const TYPES_FOR_POLKADOT: Record<string, string> = {
|
||||
Keys: 'SessionKeysPolkadot'
|
||||
};
|
||||
|
||||
// NOTE this is for support of old, e.g. Alex, old metadata and BlockNumber/Index
|
||||
// This is detected based on metadata version, since this is what we have up-front
|
||||
const TYPES_SUBSTRATE_1 = {
|
||||
BlockNumber: 'u64',
|
||||
Index: 'u64',
|
||||
EventRecord: 'EventRecord0to76',
|
||||
ValidatorPrefs: 'ValidatorPrefs0to145'
|
||||
};
|
||||
|
||||
// Type overrides based on specific nodes
|
||||
const TYPES_CHAIN: Record<string, Record<string, string>> = {
|
||||
// TODO Remove this once it is not needed, i.e. upgraded
|
||||
'Kusama CC1': {
|
||||
RawBabePreDigest: 'RawBabePreDigest0to159'
|
||||
}
|
||||
};
|
||||
|
||||
// Type overrides for specific spec types as given in runtimeVersion
|
||||
const TYPES_SPEC: Record<string, Record<string, string>> = {
|
||||
kusama: TYPES_FOR_POLKADOT,
|
||||
polkadot: TYPES_FOR_POLKADOT
|
||||
};
|
||||
|
||||
const l = logger('api/decorator');
|
||||
|
||||
export default abstract class Init<ApiType extends ApiTypes> extends Decorate<ApiType> {
|
||||
@@ -116,23 +86,16 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
}
|
||||
|
||||
private async metaFromChain (optMetadata: Record<string, string>): Promise<Metadata> {
|
||||
const { typesChain = {}, typesSpec = {} } = this._options;
|
||||
const { typesChain, typesSpec } = this._options;
|
||||
const [genesisHash, runtimeVersion, chain, chainProps] = await Promise.all([
|
||||
this._rpcCore.chain.getBlockHash(0).toPromise(),
|
||||
this._rpcCore.state.getRuntimeVersion().toPromise(),
|
||||
this._rpcCore.system.chain().toPromise(),
|
||||
this._rpcCore.system.properties().toPromise()
|
||||
]);
|
||||
const specName = runtimeVersion.specName.toString();
|
||||
const chainName = chain.toString();
|
||||
|
||||
// based on the node spec & chain, inject specific type overrides
|
||||
this.registerTypes({
|
||||
...(TYPES_SPEC[specName] || {}),
|
||||
...(TYPES_CHAIN[chainName] || {}),
|
||||
...(typesSpec[specName] || {}),
|
||||
...(typesChain[chainName] || {})
|
||||
});
|
||||
this.registerTypes(getChainTypes(chain, runtimeVersion, typesChain, typesSpec));
|
||||
|
||||
// filter the RPC methods (this does an rpc-methods call)
|
||||
await this.filterRpc();
|
||||
@@ -157,12 +120,8 @@ export default abstract class Init<ApiType extends ApiTypes> extends Decorate<Ap
|
||||
}
|
||||
|
||||
private async initFromMeta (metadata: Metadata): Promise<boolean> {
|
||||
// HACK-ish Old EventRecord, BlockNumber & Indexes for e.g. Alex, based on metadata version
|
||||
// v3 = Alex
|
||||
// v4 = v1.0 branch
|
||||
if (metadata.version <= 4) {
|
||||
this.registerTypes(TYPES_SUBSTRATE_1);
|
||||
}
|
||||
// inject types based on metadata, if applicable
|
||||
this.registerTypes(getMetadataTypes(metadata.version));
|
||||
|
||||
const decoratedMeta = new DecoratedMeta(this.registry, metadata);
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// Copyright 2017-2019 @polkadot/api authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { RuntimeVersion } from '@polkadot/types/interfaces';
|
||||
import { RegistryTypes } from '@polkadot/types/types';
|
||||
|
||||
import { Text } from '@polkadot/types';
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
|
||||
interface VersionedType {
|
||||
minmax: [number?, number?]; // min (v >= min) and max (v <= max)
|
||||
types: RegistryTypes;
|
||||
}
|
||||
|
||||
// these are override types for polkadot chains
|
||||
// NOTE The SessionKeys definition for Polkadot and Substrate (OpaqueKeys
|
||||
// implementation) are different. Detect Polkadot and inject the `Keys`
|
||||
// definition as applicable. (4 keys in substrate vs 5 in Polkadot/CC3).
|
||||
const TYPES_POLKADOT_VERSIONED: VersionedType[] = [
|
||||
{
|
||||
minmax: [0, undefined],
|
||||
types: {
|
||||
Keys: 'SessionKeysPolkadot'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
// Type overrides based on specific nodes
|
||||
const TYPES_CHAIN: Record<string, VersionedType[]> = {};
|
||||
|
||||
// Type overrides based on metadata versions
|
||||
const TYPES_META: VersionedType[] = [
|
||||
{
|
||||
// NOTE this is for support of old, e.g. Alex, old metadata and BlockNumber/Index
|
||||
// This is detected based on metadata version, since this is what we have up-front
|
||||
// v3 = Alex
|
||||
// v4 = v1.0 branch
|
||||
minmax: [0, 4],
|
||||
types: {
|
||||
BlockNumber: 'u64',
|
||||
Index: 'u64',
|
||||
EventRecord: 'EventRecord0to76',
|
||||
ValidatorPrefs: 'ValidatorPrefs0to145'
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
// Type overrides for specific spec types & versions as given in runtimeVersion
|
||||
const TYPES_SPEC: Record<string, VersionedType[]> = {
|
||||
kusama: TYPES_POLKADOT_VERSIONED,
|
||||
polkadot: TYPES_POLKADOT_VERSIONED
|
||||
};
|
||||
|
||||
// flatten a VersionedType[] into a Record<string, string>
|
||||
function filterVersions (versions: VersionedType[] = [], version: number): RegistryTypes {
|
||||
return versions
|
||||
.filter(({ minmax: [min, max] }): boolean =>
|
||||
(isUndefined(min) || version >= min) &&
|
||||
(isUndefined(max) || version <= max)
|
||||
)
|
||||
.reduce((result: RegistryTypes, { types }): RegistryTypes => ({
|
||||
...result,
|
||||
...types
|
||||
}), {});
|
||||
}
|
||||
|
||||
// based on the metadata version, return the registry types
|
||||
export function getMetadataTypes (version: number): RegistryTypes {
|
||||
return filterVersions(TYPES_META, version);
|
||||
}
|
||||
|
||||
// based on the chain and runtimeVersion, get the applicable types (ready for registration)
|
||||
export function getChainTypes (chainName: Text, { specName, specVersion }: RuntimeVersion, typesChain: Record<string, RegistryTypes> = {}, typesSpec: Record<string, RegistryTypes> = {}): RegistryTypes {
|
||||
const _chainName = chainName.toString();
|
||||
const _specName = specName.toString();
|
||||
const _specVersion = specVersion.toNumber();
|
||||
|
||||
return {
|
||||
...filterVersions(TYPES_SPEC[_specName], _specVersion),
|
||||
...filterVersions(TYPES_CHAIN[_chainName], _specVersion),
|
||||
...(typesSpec[_specName] || {}),
|
||||
...(typesChain[_chainName] || {})
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user