Fix generation of well-known keys (#3452)
* Fix generation of well-known keys * CHANGELOG * Linting, unfiy decorate params * Adjust
This commit is contained in:
@@ -17,7 +17,7 @@ function init (meta: string): [Constants, TypeRegistry] {
|
||||
|
||||
registry.setMetadata(metadata);
|
||||
|
||||
return [decorateConstants(registry, metadata.asLatest), registry];
|
||||
return [decorateConstants(registry, metadata.asLatest, 12), registry];
|
||||
}
|
||||
|
||||
describe('decorateConstants', (): void => {
|
||||
|
||||
@@ -8,7 +8,8 @@ import type { ConstantCodec, Constants, ModuleConstants } from '../types';
|
||||
import { hexToU8a, stringCamelCase } from '@polkadot/util';
|
||||
|
||||
/** @internal */
|
||||
export function decorateConstants (registry: Registry, { modules }: MetadataLatest): Constants {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export function decorateConstants (registry: Registry, { modules }: MetadataLatest, _metaVersion: number): Constants {
|
||||
return modules.reduce((result: Constants, { constants, name }): Constants => {
|
||||
if (constants.isEmpty) {
|
||||
return result;
|
||||
|
||||
@@ -46,7 +46,7 @@ describe('Decorated', () => {
|
||||
});
|
||||
|
||||
it('should return constants with the correct type and value', (): void => {
|
||||
const consts = decorateConstants(registry, metadata.asLatest);
|
||||
const consts = decorateConstants(registry, metadata.asLatest, metadata.version);
|
||||
|
||||
expect(consts.democracy.cooloffPeriod).toBeInstanceOf(registry.createClass('BlockNumber'));
|
||||
expect(consts.democracy.cooloffPeriod.toHex()).toEqual('0x000c4e00');
|
||||
|
||||
@@ -20,13 +20,14 @@ export function expandMetadata (registry: Registry, metadata: Metadata): Decorat
|
||||
assert(metadata instanceof Metadata, 'You need to pass a valid Metadata instance to Decorated');
|
||||
|
||||
const latest = metadata.asLatest;
|
||||
const version = metadata.version;
|
||||
|
||||
return {
|
||||
consts: decorateConstants(registry, latest),
|
||||
errors: decorateErrors(registry, latest, metadata.version),
|
||||
events: decorateEvents(registry, latest, metadata.version),
|
||||
query: decorateStorage(registry, latest, metadata.version),
|
||||
tx: decorateExtrinsics(registry, latest, metadata.version)
|
||||
consts: decorateConstants(registry, latest, version),
|
||||
errors: decorateErrors(registry, latest, version),
|
||||
events: decorateEvents(registry, latest, version),
|
||||
query: decorateStorage(registry, latest, version),
|
||||
tx: decorateExtrinsics(registry, latest, version)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ describe('createFunction', (): void => {
|
||||
method: 'NextKeys',
|
||||
prefix: 'Session',
|
||||
section: 'session'
|
||||
}, { metaVersion: 9 });
|
||||
}, {});
|
||||
|
||||
expect(
|
||||
u8aToHex(
|
||||
|
||||
@@ -9,15 +9,14 @@ import BN from 'bn.js';
|
||||
|
||||
import { Raw } from '@polkadot/types/codec';
|
||||
import { StorageKey } from '@polkadot/types/primitive';
|
||||
import { assert, compactAddLength, compactStripLength, isNull, isUndefined, stringLowerFirst, u8aConcat } from '@polkadot/util';
|
||||
import { assert, compactAddLength, compactStripLength, isNull, isUndefined, stringLowerFirst, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
import { xxhashAsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
import { getHasher, HasherFunction } from './getHasher';
|
||||
|
||||
export interface CreateItemOptions {
|
||||
key?: string;
|
||||
metaVersion: number;
|
||||
skipHashing?: boolean; // We don't hash the keys defined in ./substrate.ts
|
||||
skipHashing?: boolean;
|
||||
}
|
||||
|
||||
export interface CreateItemFn {
|
||||
@@ -35,7 +34,6 @@ interface IterFn {
|
||||
type CreateArgType = boolean | string | number | null | BN | BigInt | Uint8Array | Codec;
|
||||
|
||||
const EMPTY_U8A = new Uint8Array([]);
|
||||
const NULL_HASHER = (value: Uint8Array): Uint8Array => value;
|
||||
|
||||
// get the hashers, the base (and in the case of DoubleMap), the second key
|
||||
/** @internal */
|
||||
@@ -186,7 +184,9 @@ export function createFunction (registry: Registry, itemFn: CreateItemFn, option
|
||||
const storageFn = expandWithMeta(itemFn, (arg?: CreateArgType | [CreateArgType?, CreateArgType?]): Uint8Array =>
|
||||
type.isDoubleMap
|
||||
? createKeyDoubleMap(registry, itemFn, arg as [CreateArgType, CreateArgType], [hasher, key2Hasher])
|
||||
: createKey(registry, itemFn, arg as CreateArgType, options.skipHashing ? NULL_HASHER : hasher)
|
||||
: options.skipHashing
|
||||
? compactAddLength(u8aToU8a(options.key))
|
||||
: createKey(registry, itemFn, arg as CreateArgType, hasher)
|
||||
);
|
||||
|
||||
if (type.isMap || type.isDoubleMap) {
|
||||
|
||||
@@ -2,19 +2,21 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types/create';
|
||||
import { compactAddLength, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { getStorage } from './getStorage';
|
||||
|
||||
describe('getSorage', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
const storage = getStorage(registry, 8);
|
||||
const storage = getStorage(registry);
|
||||
|
||||
it('should return well known keys', (): void => {
|
||||
expect(typeof storage.substrate).toBe('object');
|
||||
|
||||
expect(storage.substrate.code()).toEqual(compactAddLength(u8aToU8a(':code')));
|
||||
|
||||
expect(storage.substrate.changesTrieConfig).toBeTruthy();
|
||||
expect(storage.substrate.childStorageKeyPrefix).toBeTruthy();
|
||||
expect(storage.substrate.code).toBeTruthy();
|
||||
expect(storage.substrate.extrinsicIndex).toBeTruthy();
|
||||
expect(storage.substrate.heapPages).toBeTruthy();
|
||||
});
|
||||
|
||||
@@ -8,12 +8,12 @@ import type { Storage } from '../types';
|
||||
import { substrate } from './substrate';
|
||||
|
||||
/** @internal */
|
||||
export function getStorage (registry: Registry, metaVersion: number): Storage {
|
||||
export function getStorage (registry: Registry): Storage {
|
||||
return {
|
||||
substrate: Object
|
||||
.entries(substrate)
|
||||
.reduce((storage: Record<string, StorageEntry>, [key, fn]): Record<string, StorageEntry> => {
|
||||
(storage as Record<string, unknown>)[key] = fn(registry, metaVersion);
|
||||
(storage as Record<string, unknown>)[key] = fn(registry);
|
||||
|
||||
return storage;
|
||||
}, {})
|
||||
|
||||
@@ -11,7 +11,8 @@ import { createFunction } from './createFunction';
|
||||
import { getStorage } from './getStorage';
|
||||
|
||||
/** @internal */
|
||||
export function decorateStorage (registry: Registry, { modules }: MetadataLatest, metaVersion: number): Storage {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
export function decorateStorage (registry: Registry, { modules }: MetadataLatest, _metaVersion: number): Storage {
|
||||
return modules.reduce((result: Storage, moduleMetadata): Storage => {
|
||||
if (moduleMetadata.storage.isNone) {
|
||||
return result;
|
||||
@@ -31,11 +32,11 @@ export function decorateStorage (registry: Registry, { modules }: MetadataLatest
|
||||
method,
|
||||
prefix,
|
||||
section
|
||||
}, { metaVersion });
|
||||
}, {});
|
||||
|
||||
return newModule;
|
||||
}, {} as ModuleStorage);
|
||||
|
||||
return result;
|
||||
}, { ...getStorage(registry, metaVersion) });
|
||||
}, { ...getStorage(registry) });
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Copyright 2017-2021 @polkadot/metadata authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types/create';
|
||||
import { compactAddLength, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { substrate } from './substrate';
|
||||
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
describe('substrate', (): void => {
|
||||
it('creates a well-known :code key', (): void => {
|
||||
expect(
|
||||
substrate.code(registry)()
|
||||
).toEqual(
|
||||
compactAddLength(u8aToU8a(':code'))
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -12,12 +12,12 @@ interface SubstrateMetadata {
|
||||
type: string;
|
||||
}
|
||||
|
||||
type Creator = (registry: Registry, metaVersion: number) => StorageEntry;
|
||||
type Creator = (registry: Registry) => StorageEntry;
|
||||
|
||||
// Small helper function to factorize code on this page.
|
||||
/** @internal */
|
||||
function createRuntimeFunction (method: string, key: string, { documentation, type }: SubstrateMetadata): (registry: Registry, metaVersion: number) => StorageEntry {
|
||||
return (registry: Registry, metaVersion: number): StorageEntry =>
|
||||
function createRuntimeFunction (method: string, key: string, { documentation, type }: SubstrateMetadata): (registry: Registry) => StorageEntry {
|
||||
return (registry: Registry): StorageEntry =>
|
||||
createFunction(registry, {
|
||||
meta: {
|
||||
documentation: registry.createType('Vec<Text>', [documentation]),
|
||||
@@ -28,7 +28,7 @@ function createRuntimeFunction (method: string, key: string, { documentation, ty
|
||||
method,
|
||||
prefix: 'Substrate',
|
||||
section: 'substrate'
|
||||
}, { key, metaVersion, skipHashing: true });
|
||||
}, { key, skipHashing: true });
|
||||
}
|
||||
|
||||
export const substrate: Record<string, Creator> = {
|
||||
|
||||
Reference in New Issue
Block a user