Set static metadata to v15 and update typegen to generate runtime apis and use v15 metadata (#5987)

* Set static metadata to v15

* Fix testUtil

* Fix metadata spec

* support metadata files

* lint and fix commented tests

* Fix test

* Add augment static runtime types by metadata

* Fix alias typing for runtime def types

* Cleanup and add remaining generated files

* lint

* Fix typegen for edgecase

* Update polkadot to 1003003

* Fix tests

* Set correct rpc
This commit is contained in:
Tarik Gul
2024-10-18 05:01:08 +03:00
committed by GitHub
parent 1772458304
commit bc199956f3
42 changed files with 160378 additions and 12515 deletions
+18 -6
View File
@@ -5,9 +5,12 @@
import type { HexString } from '@polkadot/util/types';
import kusama from '@polkadot/types-support/metadata/static-kusama';
import polkadot from '@polkadot/types-support/metadata/static-polkadot';
import substrate from '@polkadot/types-support/metadata/static-substrate';
import kusamaV14 from '@polkadot/types-support/metadata/v14/kusama-hex';
import polkadotV14 from '@polkadot/types-support/metadata/v14/polkadot-hex';
import substrateV14 from '@polkadot/types-support/metadata/v14/substrate-hex';
import kusama from '@polkadot/types-support/metadata/v15/kusama-hex';
import polkadot from '@polkadot/types-support/metadata/v15/polkadot-hex';
import substrate from '@polkadot/types-support/metadata/v15/substrate-hex';
import { TypeRegistry } from '../create/index.js';
import { Metadata } from './Metadata.js';
@@ -18,9 +21,17 @@ const allData: Record<string, HexString> = {
substrate
};
const allDataV14: Record<string, HexString> = {
kusama: kusamaV14,
polkadot: polkadotV14,
substrate: substrateV14
};
for (const type of ['kusama', 'polkadot', 'substrate'] as const) {
describe(`${type}metadata`, (): void => {
const metadata = new Metadata(new TypeRegistry(), allData[type]);
const registry = new TypeRegistry();
const opaqueMetadata = registry.createType('Option<OpaqueMetadata>', registry.createType('Raw', allData[type]).toU8a()).unwrap();
const metadata = new Metadata(registry, opaqueMetadata.toHex());
it('allows creation from hex', (): void => {
expect(
@@ -28,8 +39,9 @@ for (const type of ['kusama', 'polkadot', 'substrate'] as const) {
).toEqual(metadata.toJSON());
});
it('has a sane toCallsOnly', (): void => {
const test = metadata.asCallsOnly;
it('has a sane toCallsOnly V14 only', (): void => {
const metadatav14 = new Metadata(new TypeRegistry(), allDataV14[type]);
const test = metadatav14.asCallsOnly;
// it has a useful length
expect(
+48 -25
View File
@@ -6,6 +6,7 @@
/* global describe, it, expect */
import type { Registry } from '@polkadot/types-codec/types';
import type { HexString } from '@polkadot/util/types';
import type { MetaVersionAll } from '../versions.js';
import type { Check } from './types.js';
@@ -50,9 +51,29 @@ function readJson <T = unknown> (version: number, type: string, sub: 'json' | 't
) as unknown as T;
}
function handleMetadata (registry: Registry, version: MetaVersionAll, data: HexString): Metadata {
let metadata: Metadata;
if (version > 15) {
const opaqueMetadata = registry.createType('Option<OpaqueMetadata>', registry.createType('Raw', data).toU8a()).unwrap();
metadata = new Metadata(registry, opaqueMetadata.toHex());
} else {
try {
metadata = new Metadata(registry, data);
} catch {
const opaqueMetadata = registry.createType('Option<OpaqueMetadata>', registry.createType('Raw', data).toU8a()).unwrap();
metadata = new Metadata(registry, opaqueMetadata.toHex());
}
}
return metadata;
}
/** @internal */
export function decodeLatestMeta (registry: Registry, type: string, version: MetaVersionAll, { data }: Check): void {
const metadata = new Metadata(registry, data);
const metadata = handleMetadata(registry, version, data);
registry.setMetadata(metadata);
@@ -96,7 +117,7 @@ export function decodeLatestMeta (registry: Registry, type: string, version: Met
/** @internal */
export function toLatest (registry: Registry, version: MetaVersionAll, { data }: Check, withThrow = true): void {
it(`converts v${version} to latest`, (): void => {
const metadata = new Metadata(registry, data);
const metadata = handleMetadata(registry, version, data);
registry.setMetadata(metadata);
@@ -109,9 +130,9 @@ export function toLatest (registry: Registry, version: MetaVersionAll, { data }:
}
/** @internal */
export function defaultValues (registry: Registry, { data, fails = [] }: Check, withThrow = true, withFallbackCheck = false): void {
export function defaultValues (registry: Registry, { data, fails = [] }: Check, withThrow = true, withFallbackCheck = false, version: MetaVersionAll): void {
describe('storage with default values', (): void => {
const metadata = new Metadata(registry, data);
const metadata = handleMetadata(registry, version, data);
const { pallets } = metadata.asLatest;
pallets.filter(({ storage }) => storage.isSome).forEach(({ name, storage }): void => {
@@ -153,28 +174,30 @@ export function defaultValues (registry: Registry, { data, fails = [] }: Check,
});
}
function serialize (registry: Registry, { data }: Check): void {
const metadata = new Metadata(registry, data);
function serialize (registry: Registry, { data }: Check, version: MetaVersionAll): void {
const metadata = handleMetadata(registry, version, data);
it('serializes to hex in the same form as retrieved', (): void => {
expect(metadata.toHex()).toEqual(data);
});
if (version < 15) {
it('serializes to hex in the same form as retrieved', (): void => {
expect(metadata.toHex()).toEqual(data);
});
// NOTE Assuming the first passes this is actually something that doesn't test
// anything new. If the first line in this function passed and the above values
// are equivalent, this would be as well.
it.skip('can construct from a re-serialized form', (): void => {
expect(
() => new Metadata(registry, metadata.toHex())
).not.toThrow();
});
// NOTE Assuming the first passes this is actually something that doesn't test
// anything new. If the first line in this function passed and the above values
// are equivalent, this would be as well.
it.skip('can construct from a re-serialized form', (): void => {
expect(
() => new Metadata(registry, metadata.toHex())
).not.toThrow();
});
// as used in the extension
it('can construct from asCallsOnly.toHex()', (): void => {
expect(
() => new Metadata(registry, metadata.asCallsOnly.toHex())
).not.toThrow();
});
// as used in the extension
it('can construct from asCallsOnly.toHex()', (): void => {
expect(
() => new Metadata(registry, metadata.asCallsOnly.toHex())
).not.toThrow();
});
}
}
export function testMeta (version: MetaVersionAll, matchers: Record<string, Check>, withFallback = true): void {
@@ -183,10 +206,10 @@ export function testMeta (version: MetaVersionAll, matchers: Record<string, Chec
const registry = new TypeRegistry();
describe(type, (): void => {
serialize(registry, matcher);
serialize(registry, matcher, version);
decodeLatestMeta(registry, type, version, matcher);
toLatest(registry, version, matcher);
defaultValues(registry, matcher, true, withFallback);
defaultValues(registry, matcher, true, withFallback, version);
});
}
});
@@ -1,11 +1,19 @@
// Copyright 2017-2024 @polkadot/types authors & contributors
// SPDX-License-Identifier: Apache-2.0
import kusamaData from '@polkadot/types-support/metadata/v15/kusama-hex';
import polkadotData from '@polkadot/types-support/metadata/v15/polkadot-hex';
import substrateData from '@polkadot/types-support/metadata/v15/substrate-hex';
import { testMeta } from '../util/testUtil.js';
testMeta(15, {
kusama: {
data: kusamaData
},
polkadot: {
data: polkadotData
},
substrate: {
data: substrateData
}