Pull parameter_types! from metadata (#1066)

* @polkadot/{extrinsics,storage} moved to @polkadot/metadata

* Fix previous commit

* Fix previous commit

* Expose parameter types using api.consts

* Grumbles; update v6 static metadata; add UncleEntryItem (fix #1067)

* Update api.query.* to api.consts.* where changed

* Update tests and stray api.query.staking.sessionsPerEra

* Grumbles

* Grumbles

* Revert updates to api-derive (taken care of by #1068)

* Grumbles; fix UncleEntryItem

* Rename "@polkadot/metadata" => "@polkadot/api-metadata"

* Skip derive test

* Grumbles

* Grumbles

* Grumbles

* Cleanup merge, ensure metadata is latest

* Move api-metadata to actual api-metadata folder

* README api-metadata description (& folder link)

* Remove stray type-extrinsics package.json
This commit is contained in:
Axel Chalon
2019-07-09 08:54:28 +02:00
committed by Jaco Greeff
parent 4984002111
commit 9abbaab991
58 changed files with 202 additions and 305 deletions
@@ -0,0 +1,3 @@
# @polkadot/api-metadata/storage
A base interface to the storage layer
@@ -0,0 +1,114 @@
// Copyright 2017-2019 @polkadot/api-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 { StorageHasher, Text } from '@polkadot/types';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { stringToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
import createFunction from './createFunction';
describe('createFunction', () => {
it('should create timestamp.now correctly', () => {
expect(
createFunction(
'Timestamp',
'Now',
{ type: {} } as any
)()
).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)', () => {
expect(
createFunction(
'Substrate',
'authorityCount',
{ type: {} } as any,
{
key: ':auth:len',
skipHashing: true
}
).method
).toEqual('authorityCount');
});
it('allows overrides on key (unhashed)', () => {
const key = ':auth:len';
expect(
createFunction(
'Substrate',
'authorityCount',
{ type: {} } as any,
{
key,
skipHashing: true
}
)()
).toEqual(
u8aConcat(
Uint8Array.from([key.length << 2]),
stringToU8a(':auth:len')
)
);
});
describe('the created double map function', () => {
let storageFn: StorageEntry;
beforeAll(() => {
storageFn = createFunction(
'GenericAsset',
'FreeBalance', {
name: 'metaName',
type: {
isDoubleMap: true,
asDoubleMap: {
hasher: new StorageHasher('Blake2_256'),
key1: new Text('AccountId'),
key2: new Text('AccountId'),
value: new Text('Balance'),
key2Hasher: new Text('twox_128')
}
}
} as any
);
});
it('should return correct key', () => {
const result = storageFn(['5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP', '5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP']);
expect(u8aToHex(result)).toEqual('0xc000fa40e72d7173e69ee54b980345ea01cb81e64258502e0247af4303dee91ec0aec2ecd3a60ab080cff7b52a8f6d543b');
});
it('needs two arguments', () => {
expect(
() => storageFn(['5DXUeE5N5LtkW97F2PzqYPyqNkxqSWESdGSPTX6AvkUAhwKP'])
).toThrow(/metaName expects two arguments/);
});
});
it('allows creates double map function with a Null type key', () => {
const storageFn = createFunction(
'System',
'EventTopics',
{
type: {
isDoubleMap: true,
asDoubleMap: {
hasher: new StorageHasher('Blake2_256'),
key1: new Text('Null'),
key2: new Text('Hash'),
value: new Text('Vec<(BlockNumber,EventIndex)>'),
key2Hasher: new Text('blake2_256')
}
}
} as any
);
// 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]])));
});
});
@@ -0,0 +1,100 @@
// Copyright 2017-2019 @polkadot/api-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 { createType, Bytes, Compact, StorageKey, Text, U8a } from '@polkadot/types';
import { PlainType, StorageEntryMetadata, StorageEntryModifier, StorageEntryType } from '@polkadot/types/Metadata/v6/Storage';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { assert, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util';
import getHasher, { HasherFunction } from './getHasher';
export interface CreateItemOptions {
key?: string;
skipHashing?: boolean; // We don't hash the keys defined in ./substrate.ts
}
/**
* From the schema of a function in the module's storage, generate the function
* that will return the correct storage key.
*
* @param schema - The function's definition schema to create the function from.
* The schema is taken from state_getMetadata.
* @param options - Additional options when creating the function. These options
* are not known at runtime (from state_getMetadata), they need to be supplied
* by us manually at compile time.
*/
export default function createFunction (section: Text | string, method: Text | string, meta: StorageEntryMetadata, options: CreateItemOptions = {}): StorageEntry {
const stringKey = options.key
? options.key
: `${section.toString()} ${method.toString()}`;
const rawKey = stringToU8a(stringKey);
// Get the hashing function
let hasher: HasherFunction;
let key2Hasher: HasherFunction;
if (meta.type.isDoubleMap) {
hasher = getHasher(meta.type.asDoubleMap.hasher);
key2Hasher = getHasher(meta.type.asDoubleMap.key2Hasher);
} else if (meta.type.isMap) {
hasher = getHasher(meta.type.asMap.hasher);
} else {
hasher = getHasher();
}
// Can only have zero or one argument:
// - storage.balances.freeBalance(address)
// - storage.timestamp.blockPeriod()
const storageFn = (arg?: any): Uint8Array => {
let key = rawKey;
if (meta.type.isDoubleMap) {
assert(!isUndefined(arg) && !isNull(arg) && !isUndefined(arg[0]) && !isNull(arg[0]) && !isUndefined(arg[1]) && !isNull(arg[1]), `${meta.name} expects two arguments`);
const type1 = meta.type.asDoubleMap.key1.toString();
const type2 = meta.type.asDoubleMap.key2.toString();
const param1Encoded = u8aConcat(key, createType(type1, arg[0]).toU8a(true));
const param1Hashed = hasher(param1Encoded);
const param2Hashed = key2Hasher(createType(type2, arg[1]).toU8a(true));
return Compact.addLengthPrefix(u8aConcat(param1Hashed, param2Hashed));
}
if (meta.type.isMap) {
assert(!isUndefined(arg) && !isNull(arg), `${meta.name} expects one argument`);
const type = meta.type.asMap.key.toString();
const param = createType(type, arg).toU8a();
key = u8aConcat(key, param);
}
// StorageKey is a Bytes, so is length-prefixed
return Compact.addLengthPrefix(
options.skipHashing
? key
: hasher(key)
);
};
if (meta.type.isMap && meta.type.asMap.isLinked) {
const keyHash = new U8a(hasher(`head of ${stringKey}`));
const keyFn: any = () => keyHash;
keyFn.meta = new StorageEntryMetadata({
name: meta.name,
modifier: new StorageEntryModifier('Required'),
type: new StorageEntryType(new PlainType(meta.type.asMap.key), 0),
fallback: new Bytes(),
documentation: meta.documentation
});
storageFn.headKey = new StorageKey(keyFn);
}
storageFn.meta = meta;
storageFn.method = stringLowerFirst(method.toString());
storageFn.section = stringLowerFirst(section.toString());
storageFn.toJSON = (): any => meta.toJSON();
return storageFn as StorageEntry;
}
@@ -0,0 +1,35 @@
// Copyright 2017-2019 @polkadot/api-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 testingPairs from '@polkadot/keyring/testingPairs';
import { Metadata } from '@polkadot/types';
import json from '@polkadot/types/Metadata/v6/static';
import { u8aToHex } from '@polkadot/util';
import fromMetadata from '../fromMetadata';
const keyring = testingPairs({ type: 'ed25519' });
// Use the pre-generated metadata
const metadata = new Metadata(json);
const storage = fromMetadata(metadata);
describe('fromMetadata', () => {
it('should throw if the storage function expects an argument', () => {
expect(() => storage.balances.freeBalance()).toThrowError(/expects one argument/);
});
it('should return a value if the storage function does not expect an argument', () => {
expect(() => storage.timestamp.now()).not.toThrow();
});
it('should return the correct length-prefixed storage key', () => {
expect(
u8aToHex(
storage.balances.freeBalance(keyring.alice.address)
)
).toEqual(
'0x807f864e18e3dd8b58386310d2fe0919eef27c6e558564b7f67f22d99d20f587bb'
);
});
});
@@ -0,0 +1,39 @@
// Copyright 2017-2019 @polkadot/api-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 { StorageHasher } from '@polkadot/types/primitive';
import { blake2AsU8a, xxhashAsU8a } from '@polkadot/util-crypto';
type HasherInput = string | Buffer | Uint8Array;
export type HasherFunction = (data: HasherInput) => Uint8Array;
export default function getHasher (hasher?: StorageHasher): HasherFunction {
// This one is the default for PlainType storage keys
if (!hasher) {
return (data: HasherInput) => xxhashAsU8a(data, 128);
}
if (hasher.isBlake2128) {
return (data: HasherInput) => blake2AsU8a(data, 128);
}
if (hasher.isBlake2256) {
return (data: HasherInput) => blake2AsU8a(data, 256);
}
if (hasher.isTwox128) {
return (data: HasherInput) => xxhashAsU8a(data, 128);
}
if (hasher.isTwox256) {
return (data: HasherInput) => xxhashAsU8a(data, 256);
}
// FIXME Add Twox128Concat
// All cases should be handled above, but if not, return Twox128 for
// backwards-compatbility
return (data: HasherInput) => xxhashAsU8a(data, 128);
}
@@ -0,0 +1,35 @@
// Copyright 2017-2019 @polkadot/api-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 { ModuleStorage, Storage } from '../types';
import Metadata from '@polkadot/types/Metadata';
import { stringCamelCase, stringLowerFirst } from '@polkadot/util';
import createFunction from './createFunction';
import { storage } from './storage';
/**
* Return an object with the modules and the functions to access their storage.
*
* @param metadata - The metadata
*/
export default function fromMetadata (metadata: Metadata): Storage {
return metadata.asV6.modules.reduce((result, moduleMetadata) => {
if (moduleMetadata.storage.isNone) {
return result;
}
const { name, prefix } = moduleMetadata;
// For access, we change the index names, i.e. Balances.FreeBalance -> balances.freeBalance
result[stringCamelCase(name.toString())] = moduleMetadata.storage.unwrap().reduce((newModule, storageFnMeta) => {
newModule[stringLowerFirst(storageFnMeta.name.toString())] = createFunction(prefix, storageFnMeta.name, storageFnMeta);
return newModule;
}, {} as ModuleStorage);
return result;
}, { ...storage });
}
@@ -0,0 +1,21 @@
// Copyright 2017-2019 @polkadot/api-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 { storage } from './storage';
describe('storage', () => {
it('should return well known keys', () => {
expect(typeof storage.substrate).toBe('object');
// @deprecated: The ':auth:' (authorityPrefix) and ':auth:len' (authorityCount) storage keys
// have been removed in https://github.com/paritytech/substrate/pull/2802
expect(storage.substrate.authorityCount).toBeTruthy();
expect(storage.substrate.authorityPrefix).toBeTruthy();
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();
});
});
@@ -0,0 +1,10 @@
// Copyright 2017-2019 @polkadot/api-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 { Storage } from '../types';
import * as substrate from './substrate';
export const storage: Storage = {
substrate // Prefill storage with well known keys, as not returned by state_getMetadata
};
@@ -0,0 +1,40 @@
// Copyright 2017-2019 @polkadot/api-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 { authorityCount, authorityPrefix, changesTrieConfig, childStorageKeyPrefix, code, extrinsicIndex, heapPages } from './substrate';
describe('substrate', () => {
// @deprecated: The ':auth:' (authorityPrefix) and ':auth:len' (authorityCount) storage keys
// have been removed in https://github.com/paritytech/substrate/pull/2802
it('authorityCount should return the correct storage key', () => {
expect(authorityCount()).toEqual(Uint8Array.from([36, 58, 97, 117, 116, 104, 58, 108, 101, 110])); // Length-prefixed
});
it('authorityPrefix should return the correct storage key', () => {
expect(authorityPrefix()).toEqual(Uint8Array.from([24, 58, 97, 117, 116, 104, 58])); // Length-prefixed
});
it('changesTrieConfig should return the correct storage key', () => {
expect(changesTrieConfig()).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', () => {
expect(childStorageKeyPrefix()).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', () => {
expect(code()).toEqual(Uint8Array.from([20, 58, 99, 111, 100, 101])); // Length-prefixed
});
it('extrinsicIndex should return the correct storage key', () => {
expect(extrinsicIndex()).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', () => {
expect(heapPages()).toEqual(Uint8Array.from([40, 58, 104, 101, 97, 112, 112, 97, 103, 101, 115])); // Length-prefixed
});
});
@@ -0,0 +1,68 @@
// Copyright 2017-2019 @polkadot/api-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 { StorageEntryMetadata, StorageEntryModifier, StorageEntryType } from '@polkadot/types/Metadata/v6/Storage';
import { StorageEntry } from '@polkadot/types/primitive/StorageKey';
import { Text, Vector } from '@polkadot/types';
import createFunction from './createFunction';
interface SubstrateMetadata {
documentation: string;
type: string;
}
// Small helper function to factorize code on this page.
const createRuntimeFunction = (method: string, key: string, { documentation, type }: SubstrateMetadata): StorageEntry =>
createFunction(
new Text('Substrate'),
new Text(method),
{
documentation: new Vector(Text, [documentation]),
modifier: new StorageEntryModifier(1), // default
type: new StorageEntryType(type, 0),
toJSON: (): any => key
} as StorageEntryMetadata,
{
key,
skipHashing: true
}
);
// @deprecated: The ':auth:' (authorityPrefix) and ':auth:len' (authorityCount) storage keys
// have been removed in https://github.com/paritytech/substrate/pull/2802
export const authorityCount = createRuntimeFunction('authorityCount', ':auth:len', {
documentation: 'Number of authorities.',
type: 'u32'
});
export const authorityPrefix = createRuntimeFunction('authorityPrefix', ':auth:', {
documentation: 'Prefix under which authorities are stored.',
type: 'u32'
});
export const code = createRuntimeFunction('code', ':code', {
documentation: 'Wasm code of the runtime.',
type: 'Bytes'
});
export const heapPages = createRuntimeFunction('heapPages', ':heappages', {
documentation: 'Number of wasm linear memory pages required for execution of the runtime.',
type: 'u64'
});
export const extrinsicIndex = createRuntimeFunction('extrinsicIndex', ':extrinsic_index', {
documentation: 'Current extrinsic index (u32) is stored under this key.',
type: 'u32'
});
export const changesTrieConfig = createRuntimeFunction('changesTrieConfig', ':changes_trie', {
documentation: 'Changes trie configuration is stored under this key.',
type: 'u32'
});
export const childStorageKeyPrefix = createRuntimeFunction('childStorageKeyPrefix', ':child_storage:', {
documentation: 'Prefix of child storage keys.',
type: 'u32'
});
@@ -0,0 +1,7 @@
// Copyright 2017-2019 @polkadot/api-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 { storage } from './fromMetadata/storage';
export default storage;
@@ -0,0 +1,12 @@
// Copyright 2017-2019 @polkadot/api-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 { Metadata } from '@polkadot/types';
import metadataRpc from '@polkadot/types/Metadata/v6/static';
import fromMetadata from './fromMetadata';
export default fromMetadata(
new Metadata(metadataRpc)
);
@@ -0,0 +1,14 @@
// Copyright 2017-2019 @polkadot/api-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 { StorageEntry } from '@polkadot/types/primitive/StorageKey';
export interface ModuleStorage {
[key: string]: StorageEntry;
}
export interface Storage {
[key: string]: ModuleStorage; // Will hold modules returned by state_getMetadata
substrate: { [key: string]: StorageEntry };
}