Metadata V11 (#1774)

* Make a start ... V11 definitions

* V11 conversion

* Fix ErrorMetadataV11

* Extension point for signed extensions

* Add V11 to mapped

* Expand toLatest comments
This commit is contained in:
Jaco Greeff
2020-01-30 15:49:37 +01:00
committed by GitHub
parent de9f033d99
commit 1c327c60d9
14 changed files with 8340 additions and 107 deletions
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { MetadataAll, MetadataLatest, MetadataV0, MetadataV1, MetadataV2, MetadataV3, MetadataV4, MetadataV5, MetadataV6, MetadataV7, MetadataV8, MetadataV9, MetadataV10 } from '@polkadot/types/interfaces/metadata';
import { MetadataAll, MetadataLatest, MetadataV0, MetadataV1, MetadataV2, MetadataV3, MetadataV4, MetadataV5, MetadataV6, MetadataV7, MetadataV8, MetadataV9, MetadataV10, MetadataV11 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { createType } from '@polkadot/types/codec/create/createType';
@@ -20,12 +20,13 @@ import v6ToV7 from './v6/toV7';
import v7ToV8 from './v7/toV8';
import v8ToV9 from './v8/toV9';
import v9ToV10 from './v9/toV10';
import v10ToLatest from './v10/toLatest';
import v10ToV11 from './v10/toV11';
import v11ToLatest from './v11/toLatest';
import { getUniqTypes, toCallsOnly } from './util';
type MetaMapped = MetadataV0 | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4 | MetadataV5 | MetadataV6 | MetadataV7 | MetadataV8 | MetadataV9 | MetadataV10;
type MetaVersions = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11;
type MetaAsX = 'asV0' | 'asV1' | 'asV2' | 'asV3' | 'asV4' | 'asV5' | 'asV6' | 'asV7' | 'asV8' | 'asV9' | 'asV10';
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';
/**
* @name MetadataVersioned
@@ -154,12 +155,19 @@ export default class MetadataVersioned extends Struct {
return this.getVersion(10, v9ToV10);
}
/**
* @description Returns the wrapped values as a V10 object
*/
public get asV11 (): MetadataV11 {
return this.getVersion(11, v10ToV11);
}
/**
* @description Returns the wrapped values as a latest version object
*/
public get asLatest (): MetadataLatest {
// 11 is non-existent & latest - applied here to do to the type conversions
return this.getVersion(11, v10ToLatest);
// This is non-existent & latest - applied here to do the module-specific type conversions
return this.getVersion(12, v11ToLatest);
}
/**
+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 './v10/static';
import metadata from './v11/static';
export default metadata;
@@ -8,12 +8,13 @@ import { AnyJsonObject, Registry } from '@polkadot/types/types';
import { createType } from '@polkadot/types';
/** @internal */
export default function toCallsOnly (registry: Registry, { modules }: MetadataLatest): AnyJsonObject | string {
export default function toCallsOnly (registry: Registry, { extrinsic, modules }: MetadataLatest): AnyJsonObject | string {
return createType(registry, 'MetadataLatest', {
// FIXME, this needs typing, not any
modules: modules.map(({ calls, name }): any => ({
name,
calls
}))
})),
extrinsic
}).toJSON();
}
@@ -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 { MetadataV10, MetadataV11 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { createType } from '@polkadot/types';
/** @internal */
export default function toV11 (registry: Registry, { modules }: MetadataV10): MetadataV11 {
return createType(registry, 'MetadataV11', {
modules,
// This is new in V11, pass V0 here - something non-existing, telling the API to use
// the fallback for this information (on-chain detection)
extrinsic: {
version: 0,
signedExtensions: []
}
});
}
@@ -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('MetadataV10 (substrate)', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 11, substrateData, substrateJson);
toLatest(registry, 11, 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
@@ -3,13 +3,19 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { OverrideModuleType } from '@polkadot/types/known/types';
import { FunctionMetadataV10, FunctionMetadataLatest, MetadataV10, MetadataLatest, ModuleMetadataV10, ModuleMetadataLatest, StorageMetadataV10, StorageMetadataLatest, StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata';
import { FunctionMetadataV11, FunctionMetadataLatest, MetadataV11, MetadataLatest, ModuleMetadataLatest, StorageMetadataV11, StorageMetadataLatest, StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { getModuleTypes } from '@polkadot/types/known';
import { Type, createType } from '@polkadot/types';
import { stringCamelCase } from '@polkadot/util';
// TODO Handle consts as well
/**
* Find and apply the correct type override
* @internal
**/
function setTypeOverride (sectionTypes: OverrideModuleType, type: Type): void {
const override = Object.keys(sectionTypes).find((aliased): boolean => type.eq(aliased));
@@ -18,9 +24,11 @@ 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: FunctionMetadataV10[], sectionTypes: OverrideModuleType): FunctionMetadataLatest[] {
/**
* Apply module-specific type overrides (always be done as part of toLatest)
* @internal
**/
function convertCalls (registry: Registry, calls: FunctionMetadataV11[], sectionTypes: OverrideModuleType): FunctionMetadataLatest[] {
return calls.map(({ args, documentation, name }): FunctionMetadataLatest => {
args.forEach(({ type }): void => setTypeOverride(sectionTypes, type));
@@ -28,9 +36,11 @@ function convertCalls (registry: Registry, calls: FunctionMetadataV10[], section
});
}
// apply module-specific storage type overrides (always part of toLatest)
/** @internal */
function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV10, sectionTypes: OverrideModuleType): StorageMetadataLatest {
/**
* Apply module-specific storage type overrides (always part of toLatest)
* @internal
**/
function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV11, sectionTypes: OverrideModuleType): StorageMetadataLatest {
return createType(registry, 'StorageMetadataLatest', {
items: items.map(({ documentation, fallback, modifier, name, type }): StorageEntryMetadataLatest => {
let resultType: Type;
@@ -51,26 +61,28 @@ function convertStorage (registry: Registry, { items, prefix }: StorageMetadataV
});
}
/** @internal */
function convertModule (registry: Registry, mod: ModuleMetadataV10): ModuleMetadataLatest {
const calls = mod.calls.unwrapOr(null);
const storage = mod.storage.unwrapOr(null);
const sectionTypes = getModuleTypes(stringCamelCase(mod.name.toString()));
return createType(registry, 'ModuleMetadataLatest', {
...mod,
calls: calls
? convertCalls(registry, calls, sectionTypes)
: null,
storage: storage
? convertStorage(registry, storage, sectionTypes)
: null
});
}
/** @internal */
export default function toLatest (registry: Registry, { modules }: MetadataV10): MetadataLatest {
/**
* Convert the Metadata (which is an alias) to latest - effectively this _always_ get applied to the top-level &
* 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, { modules, extrinsic }: MetadataV11): MetadataLatest {
return createType(registry, 'MetadataLatest', {
modules: modules.map((mod): ModuleMetadataLatest => convertModule(registry, mod))
modules: modules.map((mod): ModuleMetadataLatest => {
const calls = mod.calls.unwrapOr(null);
const storage = mod.storage.unwrapOr(null);
const sectionTypes = getModuleTypes(stringCamelCase(mod.name.toString()));
return createType(registry, 'ModuleMetadataLatest', {
...mod,
calls: calls
? convertCalls(registry, calls, sectionTypes)
: null,
storage: storage
? convertStorage(registry, storage, sectionTypes)
: null
});
}),
extrinsic
});
}