Drop Metadata V0-V8 (#2634)

* Drop MetadatV0

* Apply suggestions from code review

* Restore asV1

* Remove V0 comment

* Handle DoNotConstruct in enum

* No imports from DoNotConstruct

* Merge & re-generate

* Attempt empty construction

* Drop Metadata from 0-4

* CHANGELOG

* assert(5)

* BE vs LE

* Metadataall to no-construction

* Empty U8a create

* Remove old comment

* Santize handling

* Update CHANGELOG.md

* Drop support for Metadata < 9

* Remove old pre-9 definitions

* Update CHANGELOG.md

* CHANGELOG

* Remove v8-era tests

* v8 tests
This commit is contained in:
Jaco Greeff
2020-09-25 16:32:48 +02:00
committed by GitHub
parent dff7243aad
commit 5ce3524ccd
56 changed files with 179 additions and 35167 deletions
@@ -2,150 +2,13 @@
// SPDX-License-Identifier: Apache-2.0
import { Text, TypeRegistry } from '@polkadot/types';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { assert, stringToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import { u8aToHex } from '@polkadot/util';
import createFunction from './createFunction';
describe('createFunction', (): void => {
const registry = new TypeRegistry();
it('should create timestamp.now correctly', (): void => {
expect(
createFunction(registry, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
meta: { type: {} } as any,
method: 'Now',
prefix: 'Timestamp',
section: 'timestamp'
}, { metaVersion: 8 })()
).toEqual(
Uint8Array.from([64, 14, 73, 68, 207, 217, 141, 111, 76, 195, 116, 209, 111, 90, 78, 63, 156]) // Length-prefixed
);
});
it('allows overrides on key (keeping name)', (): void => {
expect(
createFunction(
registry,
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
meta: { type: {} } as any,
method: 'authorityCount',
prefix: 'Substrate',
section: 'substrate'
},
{
key: ':auth:len',
metaVersion: 8,
skipHashing: true
}
).method
).toEqual('authorityCount');
});
it('allows overrides on key (unhashed)', (): void => {
const key = ':auth:len';
expect(
createFunction(
registry,
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
meta: { type: {} } as any,
method: 'authorityCount',
prefix: 'Substrate',
section: 'substrate'
},
{
key,
metaVersion: 8,
skipHashing: true
}
)()
).toEqual(
u8aConcat(
Uint8Array.from([key.length << 2]),
stringToU8a(':auth:len')
)
);
});
describe('the created double map function', (): void => {
let storageFn: StorageEntry;
beforeAll((): void => {
storageFn = createFunction(registry, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
meta: {
name: 'metaName',
type: {
asDoubleMap: {
hasher: registry.createType('StorageHasher', 'Blake2_256'),
key1: new Text(registry, 'AccountId'),
key2: new Text(registry, 'AccountId'),
key2Hasher: registry.createType('StorageHasher', 'Twox128'),
value: new Text(registry, 'Balance')
},
isDoubleMap: true
}
} as any,
method: 'FreeBalance',
prefix: 'GenericAsset',
section: 'genericAsset'
}, { metaVersion: 8 });
});
it('should return correct key', (): void => {
const result = storageFn(['5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP', '5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP']);
expect(u8aToHex(result)).toEqual('0xc000fa40e72d7173e69ee54b980345ea01cb81e64258502e0247af4303dee91ec0aec2ecd3a60ab080cff7b52a8f6d543b');
});
it('needs two arguments', (): void => {
expect(
(): Uint8Array => storageFn(['5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP'])
).toThrow(/requires two arguments/);
});
it('accepts an optional parameter for key construction', (): void => {
const iterKey = storageFn.iterKey;
assert(iterKey, 'storageFn.iterKey is undefined');
const result = iterKey('5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP');
expect(u8aToHex(result)).toEqual('0x223416315e3dddca3b5a47fd0ac8e4916482b9ade7bc6657aaca787ba1add3b4c4303117deb55aad9858c8a873273280f78d172b398d5e77e43a2db5e42163e9');
expect(u8aToHex(iterKey())).toEqual('0x223416315e3dddca3b5a47fd0ac8e4916482b9ade7bc6657aaca787ba1add3b4');
});
});
it('allows creates double map function with a Null type key', (): void => {
const storageFn = createFunction(registry, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
meta: {
type: {
asDoubleMap: {
hasher: registry.createType('StorageHasher', 'Blake2_256'),
key1: new Text(registry, 'Null'),
key2: new Text(registry, 'Hash'),
key2Hasher: registry.createType('StorageHasher', 'Blake2_256'),
value: new Text(registry, 'Vec<(BlockNumber,EventIndex)>')
},
isDoubleMap: true
}
} as any,
method: 'EventTopics',
prefix: 'System',
section: 'system'
}, { metaVersion: 8 });
// the value of the Null type key does not effect the result
expect(u8aToHex(storageFn(['any', [1, 2, 3]]))).toEqual(u8aToHex(storageFn([[1, 2, 3], [1, 2, 3]])));
// the value of the not Null type key does effect the result
expect(u8aToHex(storageFn(['any', [1, 2, 3]]))).not.toEqual(u8aToHex(storageFn(['any', [1, 2, 3, 4]])));
});
it('allows creating of known DoubleMap keys (with Bytes)', (): void => {
const storageFn = createFunction(registry, {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
@@ -8,7 +8,7 @@ import BN from 'bn.js';
import { Compact, Raw } from '@polkadot/types/codec';
import { createTypeUnsafe } from '@polkadot/types/create';
import StorageKey, { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { assert, compactStripLength, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util';
import { assert, compactStripLength, isNull, isUndefined, stringLowerFirst, u8aConcat } from '@polkadot/util';
import { xxhashAsU8a } from '@polkadot/util-crypto';
import getHasher, { HasherFunction } from './getHasher';
@@ -68,7 +68,7 @@ function createPrefixedKey ({ method, prefix }: CreateItemFn): Uint8Array {
// create a key for a DoubleMap type
/** @internal */
function createKeyDoubleMap (registry: Registry, itemFn: CreateItemFn, stringKey: string, args: [CreateArgType, CreateArgType], [hasher1, hasher2]: [HasherFunction, HasherFunction?], metaVersion: number): Uint8Array {
function createKeyDoubleMap (registry: Registry, itemFn: CreateItemFn, stringKey: string, args: [CreateArgType, CreateArgType], [hasher1, hasher2]: [HasherFunction, HasherFunction?]): Uint8Array {
const { meta: { name, type } } = itemFn;
// since we are passing an almost-unknown through, trust, but verify
@@ -83,23 +83,16 @@ function createKeyDoubleMap (registry: Registry, itemFn: CreateItemFn, stringKey
const val2 = createTypeUnsafe(registry, map.key2.toString(), [key2]).toU8a();
// as per createKey, always add the length prefix (underlying it is Bytes)
return Compact.addLengthPrefix(
metaVersion <= 8
? u8aConcat(
hasher1(u8aConcat(stringToU8a(stringKey), val1)),
hasher2(val2)
)
: u8aConcat(
createPrefixedKey(itemFn),
hasher1(val1),
hasher2(val2)
)
);
return Compact.addLengthPrefix(u8aConcat(
createPrefixedKey(itemFn),
hasher1(val1),
hasher2(val2)
));
}
// create a key for either a map or a plain value
/** @internal */
function createKey (registry: Registry, itemFn: CreateItemFn, stringKey: string, arg: CreateArgType, hasher: (value: Uint8Array) => Uint8Array, metaVersion: number): Uint8Array {
function createKey (registry: Registry, itemFn: CreateItemFn, stringKey: string, arg: CreateArgType, hasher: (value: Uint8Array) => Uint8Array): Uint8Array {
const { meta: { name, type } } = itemFn;
let param: Uint8Array = EMPTY_U8A;
@@ -112,11 +105,12 @@ function createKey (registry: Registry, itemFn: CreateItemFn, stringKey: string,
}
// StorageKey is a Bytes, so is length-prefixed
return Compact.addLengthPrefix(
metaVersion <= 8
? hasher(u8aConcat(stringToU8a(stringKey), param))
: u8aConcat(createPrefixedKey(itemFn), param.length ? hasher(param) : EMPTY_U8A)
);
return Compact.addLengthPrefix(u8aConcat(
createPrefixedKey(itemFn),
param.length
? hasher(param)
: EMPTY_U8A
));
}
// attach the metadata to expand to a StorageFunction
@@ -150,7 +144,7 @@ function extendHeadMeta (registry: Registry, { meta: { documentation, name, type
fallback: registry.createType('Bytes', createTypeUnsafe(registry, outputType).toHex()),
modifier: registry.createType('StorageEntryModifierLatest', 1), // required
name,
type: registry.createType('StorageEntryTypeLatest', registry.createType('PlainTypeLatest', type.isMap ? type.asMap.key : type.asDoubleMap.key1), 0)
type: registry.createType('StorageEntryTypeLatest', registry.createType('Type', type.isMap ? type.asMap.key : type.asDoubleMap.key1), 0)
});
const prefixKey = registry.createType('StorageKey', iterFn, { method, section });
@@ -195,8 +189,8 @@ export default function createFunction (registry: Registry, itemFn: CreateItemFn
// For doublemap queries the params is passed in as an tuple, [key1, key2]
const _storageFn = (arg?: CreateArgType | [CreateArgType?, CreateArgType?]): Uint8Array =>
type.isDoubleMap
? createKeyDoubleMap(registry, itemFn, stringKey, arg as [CreateArgType, CreateArgType], [hasher, key2Hasher], options.metaVersion)
: createKey(registry, itemFn, stringKey, arg as CreateArgType, options.skipHashing ? NULL_HASHER : hasher, options.metaVersion);
? createKeyDoubleMap(registry, itemFn, stringKey, arg as [CreateArgType, CreateArgType], [hasher, key2Hasher])
: createKey(registry, itemFn, stringKey, arg as CreateArgType, options.skipHashing ? NULL_HASHER : hasher);
const storageFn = expandWithMeta(itemFn, _storageFn as StorageEntry);
@@ -7,7 +7,6 @@ import { u8aToHex } from '@polkadot/util';
import Metadata from '../../../Metadata';
import rpcMetadata from '../../../Metadata/static';
import rpcMetadataV8 from '../../../Metadata/v8/static';
import Decorated from '../../Decorated';
const keyring = testingPairs({ type: 'ed25519' });
@@ -39,24 +38,4 @@ describe('fromMetadata', (): void => {
);
});
});
describe('V8', (): void => {
const registry = new TypeRegistry();
const metadata = new Metadata(registry, rpcMetadataV8);
registry.setMetadata(metadata);
const decorated = new Decorated(registry, metadata);
it('should return the correct length-prefixed storage key', (): void => {
expect(
u8aToHex(
decorated.query.balances.freeBalance(keyring.alice.address)
)
).toEqual(
// old storage key format
'0x807f864e18e3dd8b58386310d2fe0919eef27c6e558564b7f67f22d99d20f587bb'
);
});
});
});
@@ -1,32 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import { changesTrieConfig, childStorageKeyPrefix, code, extrinsicIndex, heapPages } from './substrate';
describe('substrate', (): void => {
const registry = new TypeRegistry();
it('changesTrieConfig should return the correct storage key', (): void => {
expect(changesTrieConfig(registry, 8)()).toEqual(Uint8Array.from([52, 58, 99, 104, 97, 110, 103, 101, 115, 95, 116, 114, 105, 101])); // Length-prefixed
});
it('childStorageKeyPrefix should return the correct storage key', (): void => {
expect(childStorageKeyPrefix(registry, 8)()).toEqual(Uint8Array.from([60, 58, 99, 104, 105, 108, 100, 95, 115, 116, 111, 114, 97, 103, 101, 58])); // Length-prefixed
});
it('code should return the correct storage key', (): void => {
expect(code(registry, 8)()).toEqual(Uint8Array.from([20, 58, 99, 111, 100, 101])); // Length-prefixed
});
it('extrinsicIndex should return the correct storage key', (): void => {
expect(extrinsicIndex(registry, 8)()).toEqual(
Uint8Array.from([64, 58, 101, 120, 116, 114, 105, 110, 115, 105, 99, 95, 105, 110, 100, 101, 120]) // Length-prefixed
);
});
it('heapPages should return the correct storage key', (): void => {
expect(heapPages(registry, 8)()).toEqual(Uint8Array.from([40, 58, 104, 101, 97, 112, 112, 97, 103, 101, 115])); // Length-prefixed
});
});
+18 -8
View File
@@ -3,19 +3,29 @@
import { Registry } from '@polkadot/types/types';
import { isHex, hexToU8a } from '@polkadot/util';
import { isString, u8aConcat, u8aToU8a } from '@polkadot/util';
import MetadataVersioned from './MetadataVersioned';
const VERSION_IDX = 4; // magic u32 preceding
// magic u32 preceding the version id
const VERSION_IDX = 4;
// first we try and parse using the versioned structure, if this does fail,
// we adjust with the magic number and a manual version and re-try. As soon as
// we remove support for V0, we will just do a new here
function decodeMetadata (registry: Registry, _value: Uint8Array | string = new Uint8Array()): MetadataVersioned {
const value = isHex(_value)
? hexToU8a(_value)
// magic + lowest supported version
const EMPTY_METADATA = u8aConcat(new Uint8Array([0x6d, 0x65, 0x74, 0x61, 9]));
const EMPTY_U8A = new Uint8Array();
function sanitizeInput (_value: Uint8Array | string = EMPTY_U8A): Uint8Array {
if (isString(_value)) {
return sanitizeInput(u8aToU8a(_value));
}
return _value.length === 0
? EMPTY_METADATA
: _value;
}
function decodeMetadata (registry: Registry, _value?: Uint8Array | string): MetadataVersioned {
const value = sanitizeInput(_value);
const version = value[VERSION_IDX];
try {
@@ -1,31 +1,22 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataAll, MetadataLatest, MetadataV0, MetadataV1, MetadataV2, MetadataV3, MetadataV4, MetadataV5, MetadataV6, MetadataV7, MetadataV8, MetadataV9, MetadataV10, MetadataV11 } from '@polkadot/types/interfaces/metadata';
import { MetadataAll, MetadataLatest, MetadataV9, MetadataV10, MetadataV11, MetadataV12 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import Struct from '@polkadot/types/codec/Struct';
import { assert } from '@polkadot/util';
import MagicNumber from './MagicNumber';
import v0ToV1 from './v0/toV1';
import v1ToV2 from './v1/toV2';
import v2ToV3 from './v2/toV3';
import v3ToV4 from './v3/toV4';
import v4ToV5 from './v4/toV5';
import v5ToV6 from './v5/toV6';
import v6ToV7 from './v6/toV7';
import v7ToV8 from './v7/toV8';
import v8ToV9 from './v8/toV9';
import v9ToV10 from './v9/toV10';
import v10ToV11 from './v10/toV11';
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 | 13;
type MetaAsX = 'asV0' | 'asV1' | 'asV2' | 'asV3' | 'asV4' | 'asV5' | 'asV6' | 'asV7' | 'asV8' | 'asV9' | 'asV10' | 'asV11' | 'asV12';
type MetaMapped = MetadataV9 | MetadataV10 | MetadataV11 | MetadataV12;
type MetaVersions = 9 | 10 | 11 | 12 | 13;
type MetaAsX = 'asV9' | 'asV10' | 'asV11' | 'asV12';
/**
* @name MetadataVersioned
@@ -74,75 +65,12 @@ export default class MetadataVersioned extends Struct {
}
/**
* @description Returns the wrapped metadata as a V0 object
*/
public get asV0 (): MetadataV0 {
this._assertVersion(0);
return this._metadata.asV0;
}
/**
* @description Returns the wrapped values as a V1 object
*/
public get asV1 (): MetadataV1 {
return this._getVersion(1, v0ToV1);
}
/**
* @description Returns the wrapped values as a V2 object
*/
public get asV2 (): MetadataV2 {
return this._getVersion(2, v1ToV2);
}
/**
* @description Returns the wrapped values as a V3 object
*/
public get asV3 (): MetadataV3 {
return this._getVersion(3, v2ToV3);
}
/**
* @description Returns the wrapped values as a V4 object
*/
public get asV4 (): MetadataV4 {
return this._getVersion(4, v3ToV4);
}
/**
* @description Returns the wrapped values as a V5 object
*/
public get asV5 (): MetadataV5 {
return this._getVersion(5, v4ToV5);
}
/**
* @description Returns the wrapped values as a V6 object
*/
public get asV6 (): MetadataV6 {
return this._getVersion(6, v5ToV6);
}
/**
* @description Returns the wrapped values as a V7 object
*/
public get asV7 (): MetadataV7 {
return this._getVersion(7, v6ToV7);
}
/**
* @description Returns the wrapped values as a V8 object
*/
public get asV8 (): MetadataV8 {
return this._getVersion(8, v7ToV8);
}
/**
* @description Returns the wrapped values as a V9 object
* @description Returns the wrapped metadata as a V1 object
*/
public get asV9 (): MetadataV9 {
return this._getVersion(9, v8ToV9);
this._assertVersion(9);
return this._metadata.asV9;
}
/**
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-50
View File
@@ -1,50 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { EventMetadataV1, FunctionMetadataV1, MetadataV0, MetadataV1, ModuleMetadataV1, RuntimeModuleMetadataV0, StorageFunctionMetadataV1 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { Option } from '@polkadot/types/codec';
import { Text } from '@polkadot/types/primitive';
import { stringUpperFirst } from '@polkadot/util';
/** @internal */
function toV1Calls (registry: Registry, { module: { call: { functions } } }: RuntimeModuleMetadataV0): Option<FunctionMetadataV1> {
return functions.length
? new Option(registry, 'Vec<FunctionMetadataV1>', functions)
: new Option(registry, 'Vec<FunctionMetadataV1>');
}
/** @internal */
function toV1Events (registry: Registry, metadataV0: MetadataV0, prefix: Text): Option<EventMetadataV1> {
const events = metadataV0.outerEvent.events.find((event): boolean => event[0].eq(prefix));
return events
? new Option(registry, 'Vec<EventMetadataV1>', events[1])
: new Option(registry, 'Vec<EventMetadataV1>');
}
/** @internal */
function toV1Storage (registry: Registry, { storage }: RuntimeModuleMetadataV0): Option<StorageFunctionMetadataV1> {
return new Option(registry, 'Vec<StorageFunctionMetadataV1>', storage.unwrapOr(undefined)?.functions);
}
/** @internal */
export default function toV1 (registry: Registry, metadataV0: MetadataV0): MetadataV1 {
return registry.createType('MetadataV1', {
modules: metadataV0.modules.map((mod): ModuleMetadataV1 => {
// The prefix of this module (capitalized)
const prefix = mod.storage.isSome
? mod.storage.unwrap().prefix.toString()
: stringUpperFirst(mod.prefix.toString()); // If this module doesn't have storage, we just assume the prefix is the name capitalized
return registry.createType('ModuleMetadataV1', {
calls: toV1Calls(registry, mod),
events: toV1Events(registry, metadataV0, mod.prefix),
name: mod.prefix, // Not capitalized
prefix, // Capitalized
storage: toV1Storage(registry, mod)
});
})
});
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV1', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 1, rpcData, staticSubstrate);
toLatest(registry, 1, rpcData, false);
defaultValues(registry, rpcData, false);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV1, MetadataV2 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
/** @internal */
export default function toV2 (registry: Registry, metadataV1: MetadataV1): MetadataV2 {
return registry.createType('MetadataV2', metadataV1);
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV2', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 2, rpcData, staticSubstrate);
toLatest(registry, 2, rpcData, false);
defaultValues(registry, rpcData, false);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV2, MetadataV3 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
/** @internal */
export default function toV3 (registry: Registry, metadataV2: MetadataV2): MetadataV3 {
return registry.createType('MetadataV3', metadataV2);
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV3', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 3, rpcData, staticSubstrate);
toLatest(registry, 3, rpcData, false);
defaultValues(registry, rpcData, false);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-61
View File
@@ -1,61 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV3, MetadataV4, ModuleMetadataV4, StorageFunctionMetadataV3, StorageFunctionMetadataV4 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { Option } from '@polkadot/types/codec';
/** @internal */
function toV4StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV3): StorageFunctionMetadataV4 {
const { documentation, fallback, modifier, name, type } = storageFn;
// Convert the old type to the new type: there is one new field
// called `hasher`, which we initialize to xxHash (the default in
// v3).
const [newType, index] = type.isPlain
? [type.asPlain, 0]
: type.isMap
? [registry.createType('MapTypeV4', {
hasher: registry.createType('StorageHasherV4', 'Twox128'),
key: type.asMap.key,
linked: type.asMap.linked,
value: type.asMap.value
}), 1]
: [registry.createType('DoubleMapTypeV4', {
hasher: registry.createType('StorageHasherV4', 'Twox128'),
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
key2Hasher: type.asDoubleMap.key2Hasher,
value: type.asDoubleMap.value
}), 2];
return registry.createType('StorageFunctionMetadataV4', {
documentation,
fallback,
modifier,
name,
type: registry.createType('StorageFunctionTypeV4', newType, index)
});
}
/** @internal */
export default function toV4 (registry: Registry, { modules }: MetadataV3): MetadataV4 {
return registry.createType('MetadataV4', {
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV4 =>
registry.createType('ModuleMetadataV4', {
calls,
events,
name,
prefix,
storage: storage.isSome
? new Option(
registry,
'Vec<StorageFunctionMetadataV4>',
storage.unwrap().map((v): StorageFunctionMetadataV4 => toV4StorageFunction(registry, v))
)
: undefined
})
)
});
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import staticSubstrate from './static-substrate.json';
import rpcData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV4', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 4, rpcData, staticSubstrate);
toLatest(registry, 4, rpcData, false);
defaultValues(registry, rpcData, false);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-71
View File
@@ -1,71 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV4, MetadataV5, ModuleMetadataV5, StorageFunctionMetadataV4, StorageFunctionMetadataV5, StorageHasherV5 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
import { Option } from '@polkadot/types/codec';
import Text from '@polkadot/types/primitive/Text';
import { assert } from '@polkadot/util';
const hasherMap: Map<string, string> = new Map([
['blake2_128', 'Blake2_128'],
['blake2_256', 'Blake2_256'],
['twox_128', 'Twox128'],
['twox_256', 'Twox256'],
['twox_64_concat', 'Twox64Concat']
]);
/** @internal */
function toStorageHasher (registry: Registry, text: Text): StorageHasherV5 {
const mapped = hasherMap.get(text.toString());
assert(mapped, `Invalid Storage hasher: ${text.toString()}`);
return registry.createType('StorageHasherV5', mapped);
}
/** @internal */
function toV5StorageFunction (registry: Registry, storageFn: StorageFunctionMetadataV4): StorageFunctionMetadataV5 {
const { documentation, fallback, modifier, name, type } = storageFn;
const [newType, index] = type.isPlain
? [type.asPlain, 0]
: type.isMap
? [type.asMap, 1]
: [registry.createType('DoubleMapTypeV5', {
hasher: type.asDoubleMap.hasher,
key1: type.asDoubleMap.key1,
key2: type.asDoubleMap.key2,
key2Hasher: toStorageHasher(registry, type.asDoubleMap.key2Hasher),
value: type.asDoubleMap.value
}), 2];
return registry.createType('StorageFunctionMetadataV5', {
documentation,
fallback,
modifier,
name,
type: registry.createType('StorageFunctionTypeV5', newType, index)
});
}
/** @internal */
export default function toV5 (registry: Registry, { modules }: MetadataV4): MetadataV5 {
return registry.createType('MetadataV5', {
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV5 =>
registry.createType('ModuleMetadataV5', {
calls,
events,
name,
prefix,
storage: storage.isSome
? new Option(
registry,
'Vec<StorageFunctionMetadataV5>',
storage.unwrap().map((v): StorageFunctionMetadataV5 => toV5StorageFunction(registry, v))
)
: undefined
})
)
});
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import substrateJson from './static-substrate.json';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV5 (substrate)', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 5, substrateData, substrateJson);
toLatest(registry, 5, substrateData, false);
defaultValues(registry, substrateData, false);
});
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
-21
View File
@@ -1,21 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV5, MetadataV6, ModuleMetadataV6 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
/** @internal */
export default function toV6 (registry: Registry, { modules }: MetadataV5): MetadataV6 {
return registry.createType('MetadataV6', {
modules: modules.map(({ calls, events, name, prefix, storage }): ModuleMetadataV6 =>
registry.createType('ModuleMetadataV6', {
calls,
constants: [],
events,
name,
prefix,
storage
})
)
});
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import substrateJson from './static-substrate.json';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV6 (substrate)', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 6, substrateData, substrateJson);
toLatest(registry, 6, 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
-22
View File
@@ -1,22 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV6, MetadataV7, ModuleMetadataV7 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
/** @internal */
export default function toV7 (registry: Registry, { modules }: MetadataV6): MetadataV7 {
return registry.createType('MetadataV7', {
modules: modules.map(({ calls, constants, events, name, prefix, storage }): ModuleMetadataV7 =>
registry.createType('ModuleMetadataV7', {
calls,
constants,
events,
name,
storage: storage.isSome
? registry.createType('StorageMetadataV7', { items: storage.unwrap(), prefix })
: null
})
)
});
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import substrateJson from './static-substrate.json';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV7 (substrate)', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 7, substrateData, substrateJson);
toLatest(registry, 7, 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
-21
View File
@@ -1,21 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV7, MetadataV8, ModuleMetadataV8 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
/** @internal */
export default function toV8 (registry: Registry, { modules }: MetadataV7): MetadataV8 {
return registry.createType('MetadataV8', {
modules: modules.map(({ calls, constants, events, name, storage }): ModuleMetadataV8 =>
registry.createType('ModuleMetadataV8', {
calls,
constants,
errors: [],
events,
name,
storage
})
)
});
}
@@ -1,18 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { TypeRegistry } from '@polkadot/types';
import substrateJson from './static-substrate.json';
import substrateData from './static';
import { decodeLatestSubstrate, defaultValues, toLatest } from '../util/testUtil';
describe('MetadataV8 (substrate)', (): void => {
const registry = new TypeRegistry();
decodeLatestSubstrate(registry, 8, substrateData, substrateJson);
toLatest(registry, 8, 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
-10
View File
@@ -1,10 +0,0 @@
// Copyright 2017-2020 @polkadot/metadata authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { MetadataV8, MetadataV9 } from '@polkadot/types/interfaces/metadata';
import { Registry } from '@polkadot/types/types';
/** @internal */
export default function toV9 (registry: Registry, { modules }: MetadataV8): MetadataV9 {
return registry.createType('MetadataV9', { modules });
}