Support for Metadata v12 (#2599)

* Support for Metadata v12

* v12 static data

* Treat 255 as reserved

* Point static to v12
This commit is contained in:
Jaco Greeff
2020-09-14 12:35:30 +02:00
committed by GitHub
parent 28355424bd
commit dac3261a16
13 changed files with 12001 additions and 47 deletions
@@ -20,12 +20,13 @@ import v7ToV8 from './v7/toV8';
import v8ToV9 from './v8/toV9';
import v9ToV10 from './v9/toV10';
import v10ToV11 from './v10/toV11';
import v11ToLatest from './v11/toLatest';
import v11ToV12 from './v11/toV12';
import v12ToLatest from './v12/toLatest';
import { getUniqTypes, toCallsOnly } from './util';
type MetaMapped = MetadataV0 | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4 | MetadataV5 | MetadataV6 | MetadataV7 | MetadataV8 | MetadataV9 | MetadataV10 | MetadataV11;
type MetaVersions = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
type MetaAsX = 'asV0' | 'asV1' | 'asV2' | 'asV3' | 'asV4' | 'asV5' | 'asV6' | 'asV7' | 'asV8' | 'asV9' | 'asV10' | 'asV11';
type MetaVersions = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13;
type MetaAsX = 'asV0' | 'asV1' | 'asV2' | 'asV3' | 'asV4' | 'asV5' | 'asV6' | 'asV7' | 'asV8' | 'asV9' | 'asV10' | 'asV11' | 'asV12';
/**
* @name MetadataVersioned
@@ -161,12 +162,19 @@ export default class MetadataVersioned extends Struct {
return this._getVersion(11, v10ToV11);
}
/**
* @description Returns the wrapped values as a V10 object
*/
public get asV12 (): MetadataV11 {
return this._getVersion(12, v11ToV12);
}
/**
* @description Returns the wrapped values as a latest version object
*/
public get asLatest (): MetadataLatest {
// This is non-existent & latest - applied here to do the module-specific type conversions
return this._getVersion(12, v11ToLatest);
return this._getVersion(13, v12ToLatest);
}
/**
+1 -1
View File
@@ -2,6 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import metadata from './v11/static';
import metadata from './v12/static';
export default metadata;
@@ -0,0 +1,21 @@
// Copyright 2017-2020 @polkadot/metadata 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 { MetadataV11, MetadataV12, ModuleMetadataV12 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
/**
* @internal
**/
export default function toV12 (registry: Registry, { extrinsic, modules }: MetadataV11): MetadataV12 {
return registry.createType('MetadataLatest', {
extrinsic,
modules: modules.map((mod): ModuleMetadataV12 =>
registry.createType('ModuleMetadataV12', {
...mod,
index: 255
})
)
});
}
@@ -0,0 +1,19 @@
// Copyright 2017-2020 @polkadot/metadata 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 { TypeRegistry } from '@polkadot/types';
import substrateJson from './static-substrate.json';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV12 (substrate)', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 12, substrateData, substrateJson);
toLatest(registry, 12, substrateData);
defaultValues(registry, substrateData);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
@@ -2,12 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { EventMetadataV11, EventMetadataLatest, FunctionMetadataV11, FunctionMetadataLatest, MetadataV11, MetadataLatest, ModuleMetadataLatest, StorageMetadataV11, StorageMetadataLatest, StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata';
import { EventMetadataV12, EventMetadataLatest, FunctionMetadataV12, FunctionMetadataLatest, MetadataV12, MetadataLatest, ModuleMetadataLatest, StorageMetadataV12, StorageMetadataLatest, StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata';
import { Registry, OverrideModuleType } from '@polkadot/types/types';
import { getModuleTypes } from '@polkadot/types-known';
import { Type } from '@polkadot/types/primitive';
import { stringCamelCase } from '@polkadot/util';
import { assert, stringCamelCase } from '@polkadot/util';
// TODO Handle consts as well
@@ -37,7 +37,7 @@ function setTypeOverride (sectionTypes: OverrideModuleType, type: Type): void {
* Apply module-specific type overrides (always be done as part of toLatest)
* @internal
**/
function convertCalls (registry: Registry, calls: FunctionMetadataV11[], sectionTypes: OverrideModuleType): FunctionMetadataLatest[] {
function convertCalls (registry: Registry, calls: FunctionMetadataV12[], sectionTypes: OverrideModuleType): FunctionMetadataLatest[] {
return calls.map(({ args, documentation, name }): FunctionMetadataLatest => {
args.forEach(({ type }) => setTypeOverride(sectionTypes, type));
@@ -49,7 +49,7 @@ function convertCalls (registry: Registry, calls: FunctionMetadataV11[], section
* Apply module-specific type overrides (always be done as part of toLatest)
* @internal
**/
function convertEvents (registry: Registry, events: EventMetadataV11[], sectionTypes: OverrideModuleType): EventMetadataLatest[] {
function convertEvents (registry: Registry, events: EventMetadataV12[], sectionTypes: OverrideModuleType): EventMetadataLatest[] {
return events.map(({ args, documentation, name }): EventMetadataLatest => {
args.forEach((type) => setTypeOverride(sectionTypes, type));
@@ -61,7 +61,7 @@ function convertEvents (registry: Registry, events: EventMetadataV11[], sectionT
* Apply module-specific storage type overrides (always part of toLatest)
* @internal
**/
function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV11, sectionTypes: OverrideModuleType): StorageMetadataLatest {
function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV12, sectionTypes: OverrideModuleType): StorageMetadataLatest {
return registry.createType('StorageMetadataLatest', {
items: items.map(({ documentation, fallback, modifier, name, type }): StorageEntryMetadataLatest => {
let resultType: Type;
@@ -87,7 +87,12 @@ function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV
* most-recent metadata, since it allows us a chance to actually apply call and storage specific type aliasses
* @internal
**/
export default function toLatest (registry: Registry, { extrinsic, modules }: MetadataV11): MetadataLatest {
export default function toLatest (registry: Registry, { extrinsic, modules }: MetadataV12): MetadataLatest {
const allIndexes = modules.map(({ index }) => index.toNumber());
const hasReserved = allIndexes.some((index) => index !== 255) && allIndexes.some((index) => index === 255);
assert(!hasReserved, 'This API implementation only supports module indexes 0-254, index 255 is marked as reserved');
return registry.createType('MetadataLatest', {
extrinsic,
modules: modules.map((mod): ModuleMetadataLatest => {