Use custom hasher for Map (#857)

* Use blake2 instead of two_x

* Add v4

* Update to new v4 static

* Update json

* Revert

* Use hasher enum

* Fix metadata v4 test

* Fix metadata v4 test

* Fix versioned metadata

* refactor & clean

* Use consistent naming

* Make jsons consistent

* Add backward compat

* Fix tests

* Start working on type-storage

* Fix output

* Grumble

* Update to latest static

* Clean code

* Fix imports

* Remove useless imports

* Fix tests

* Use prettier output for toJSON in metadata

* Implement different hashers

* Remove generic storageKey

* Bump version

* Add backwards-compat

* Fix hashing function, use Twox128

* Grumbles

* Add tests
This commit is contained in:
Amaury Martiny
2019-04-25 15:34:35 +02:00
committed by GitHub
parent 38c2215bb1
commit 4dd320fc89
63 changed files with 5622 additions and 1402 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@polkadot/types",
"version": "0.77.0-beta.7",
"version": "0.77.0",
"description": "Implementation of the Parity codec",
"main": "index.js",
"keywords": [
@@ -14,17 +14,20 @@ import MetadataV0 from './v0';
import MetadataV1 from './v1';
import MetadataV2 from './v2';
import MetadataV3 from './v3';
import MetadataV4 from './v4';
import v1ToV0 from './v1/toV0';
import v2ToV1 from './v2/toV1';
import v3ToV2 from './v3/toV2';
import v4ToV3 from './v4/toV3';
class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2> {
class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2 | MetadataV3 | MetadataV4> {
constructor (value?: any) {
super({
MetadataV0, // once rolled-out, can replace this with Null
MetadataV1, // once rolled-out, can replace this with Null
MetadataV2, // once rolled-out, can replace this with Null
MetadataV3
MetadataV3, // once rolled-out, can replace this with Null
MetadataV4
}, value);
}
@@ -56,6 +59,13 @@ class MetadataEnum extends EnumType<Null | MetadataV1 | MetadataV2> {
return this.value as MetadataV3;
}
/**
* @description Returns the wrapped values as a V4 object
*/
get asV4 (): MetadataV4 {
return this.value as MetadataV4;
}
/**
* @description The version this metadata represents
*/
@@ -73,6 +83,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
private _convertedV0?: MetadataV0;
private _convertedV1?: MetadataV1;
private _convertedV2?: MetadataV2;
private _convertedV3?: MetadataV3;
constructor (value?: any) {
super({
@@ -125,7 +136,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
return this.metadata.asV1;
}
assert([2, 3].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v1`);
assert([2, 3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v1`);
if (isUndefined(this._convertedV1)) {
this._convertedV1 = v2ToV1(this.metadata.asV2);
@@ -142,7 +153,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
return this.metadata.asV2;
}
assert(this.metadata.version === 3, `Cannot convert metadata from v${this.metadata.version} to v1`);
assert([3, 4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`);
if (isUndefined(this._convertedV2)) {
this._convertedV2 = v3ToV2(this.metadata.asV3);
@@ -155,7 +166,15 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
* @description Returns the wrapped values as a V3 object
*/
get asV3 (): MetadataV3 {
assert(this.metadata.version === 3, `Cannot convert metadata from v${this.metadata.version} to v3`);
if (this.metadata.version === 3) {
return this.metadata.asV3;
}
assert([4].includes(this.metadata.version), `Cannot convert metadata from v${this.metadata.version} to v2`);
if (isUndefined(this._convertedV3)) {
this._convertedV3 = v4ToV3(this.metadata.asV4);
}
return this.metadata.asV3;
}
@@ -163,4 +182,13 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
getUniqTypes (throwError: boolean): Array<string> {
return (this.metadata.value as any as MetadataInterface).getUniqTypes(throwError);
}
/**
* @description Returns the wrapped values as a V3 object
*/
get asV4 (): MetadataV4 {
assert(this.metadata.version === 4, `Cannot convert metadata from v${this.metadata.version} to v4`);
return this.metadata.asV4;
}
}
+8
View File
@@ -31,6 +31,14 @@ export class EventMetadata extends Struct {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.documentation;
}
/**
* @description The name for the event
*/
@@ -33,7 +33,7 @@ describe('Metadata', () => {
mod.storage.unwrap().functions.forEach((fn) => {
it(`creates default types for ${mod.prefix}.${fn.name}, type ${fn.type}`, () => {
expect(
() => createType(fn.type.toString(), fn.default)
() => createType(fn.type.toString(), fn.fallback)
).not.toThrow();
});
});
+10 -178
View File
@@ -2,18 +2,15 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyNumber } from '../../types';
import Enum from '../../codec/Enum';
import EnumType from '../../codec/EnumType';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
import U16 from '../../primitive/U16';
import { StorageMetadata } from './Storage';
export class FunctionArgumentMetadata extends Struct {
constructor (value?: any) {
super({
@@ -61,6 +58,14 @@ export class FunctionMetadata extends Struct {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.documentation;
}
/**
* @description The `[sectionIndex, methodIndex]` call id
*/
@@ -122,179 +127,6 @@ export class ModuleMetadata extends Struct {
}
}
export class StorageFunctionModifier extends Enum {
constructor (value?: any) {
super(['Optional', 'Default', 'Required'], value);
}
/**
* @description `true` if the storage entry is optional
*/
get isOptional (): boolean {
return this.toNumber() === 0;
}
}
export class MapType extends Struct {
private _isLinked = false;
constructor (value?: any) {
super({
key: Type,
value: Type
}, value);
if (value && value.isLinked) {
this._isLinked = true;
}
}
/**
* @description The mapped key as [[Type]]
*/
get key (): Type {
return this.get('key') as Type;
}
/**
* @description The mapped value as [[Type]]
*/
get value (): Type {
return this.get('value') as Type;
}
/**
* @description Is this an enumerable linked map
*/
get isLinked (): boolean {
return this._isLinked;
}
}
class PlainType extends Type {
}
export class StorageFunctionType extends EnumType<PlainType | MapType> {
constructor (value?: any, index?: number) {
super({
PlainType,
MapType
}, value, index);
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description The value as a mapped value
*/
get asMap (): MapType {
return this.value as MapType;
}
/**
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
return this.value as PlainType;
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
if (this.isMap) {
if (this.asMap.isLinked) {
return `(${this.asMap.value.toString()}, Linkage<${this.asMap.key.toString()}>)`;
}
return this.asMap.value.toString();
}
return this.asType.toString();
}
}
type StorageFunctionMetadataValue = {
name: string | Text,
modifier: StorageFunctionModifier | AnyNumber,
type: StorageFunctionType,
default: Bytes,
documentation: Vector<Text> | Array<string>
};
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: Text,
modifier: StorageFunctionModifier,
type: StorageFunctionType,
default: Bytes,
documentation: Vector.with(Text)
}, value);
}
/**
* @description The default value of the storage function
*/
get default (): Bytes {
return this.get('default') as Bytes;
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The key name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The modifier
*/
get modifier (): StorageFunctionModifier {
return this.get('modifier') as StorageFunctionModifier;
}
/**
* @description The [[StorageFunctionType]]
*/
get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
export class StorageMetadata extends Struct {
constructor (value?: any) {
super({
prefix: Text,
functions: Vector.with(StorageFunctionMetadata)
}, value);
}
/**
* @description The [[StorageFunctionMetadata]] for the section
*/
get functions (): Vector<StorageFunctionMetadata> {
return this.get('functions') as Vector<StorageFunctionMetadata>;
}
/**
* @description The section prefix
*/
get prefix (): Text {
return this.get('prefix') as Text;
}
}
export class RuntimeModuleMetadata extends Struct {
constructor (value?: any) {
super({
+207
View File
@@ -0,0 +1,207 @@
// Copyright 2017-2019 @polkadot/types 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 { AnyNumber } from '../../types';
import Enum from '../../codec/Enum';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
export class StorageFunctionModifier extends Enum {
constructor (value?: any) {
super(['Optional', 'Default', 'Required'], value);
}
/**
* @description `true` if the storage entry is optional
*/
get isOptional (): boolean {
return this.toNumber() === 0;
}
toJSON (): any {
// This looks prettier in the generated JSON
return this.toString();
}
}
export class MapType extends Struct {
private _isLinked = false;
constructor (value?: any) {
super({
key: Type,
value: Type
}, value);
if (value && value.isLinked) {
this._isLinked = true;
}
}
/**
* @description The mapped key as [[Type]]
*/
get key (): Type {
return this.get('key') as Type;
}
/**
* @description The mapped value as [[Type]]
*/
get value (): Type {
return this.get('value') as Type;
}
/**
* @description Is this an enumerable linked map
*/
get isLinked (): boolean {
return this._isLinked;
}
}
export class PlainType extends Type {
}
export class StorageFunctionType extends EnumType<PlainType | MapType> {
constructor (value?: any, index?: number) {
super({
PlainType,
MapType
}, value, index);
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description The value as a mapped value
*/
get asMap (): MapType {
return this.value as MapType;
}
/**
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
return this.value as PlainType;
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
if (this.isMap) {
if (this.asMap.isLinked) {
return `(${this.asMap.value.toString()}, Linkage<${this.asMap.key.toString()}>)`;
}
return this.asMap.value.toString();
}
return this.asType.toString();
}
}
export type StorageFunctionMetadataValue = {
name: string | Text,
modifier: StorageFunctionModifier | AnyNumber,
type: StorageFunctionType,
fallback: Bytes,
documentation: Vector<Text> | Array<string>
};
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: Text,
modifier: StorageFunctionModifier,
type: StorageFunctionType,
fallback: Bytes,
documentation: Vector.with(Text)
}, value);
}
/**
* @description The default value of the storage function
* @deprecated Use `.fallback` instead.
*/
get default (): Bytes {
return this.fallback;
}
/**
* @description The default value of the storage function
*/
get fallback (): Bytes {
return this.get('fallback') as Bytes;
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.documentation;
}
/**
* @description The key name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The modifier
*/
get modifier (): StorageFunctionModifier {
return this.get('modifier') as StorageFunctionModifier;
}
/**
* @description The [[StorageFunctionType]]
*/
get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
export class StorageMetadata extends Struct {
constructor (value?: any) {
super({
prefix: Text,
functions: Vector.with(StorageFunctionMetadata)
}, value);
}
/**
* @description The [[StorageFunctionMetadata]] for the section
*/
get functions (): Vector<StorageFunctionMetadata> {
return this.get('functions') as Vector<StorageFunctionMetadata>;
}
/**
* @description The section prefix
*/
get prefix (): Text {
return this.get('prefix') as Text;
}
}
File diff suppressed because it is too large Load Diff
+10 -2
View File
@@ -43,7 +43,7 @@ export class MetadataCall extends Struct {
// id: u16,
name: Text,
args: Vector.with(MetadataCallArg),
docs: Vector.with(Text)
documentation: Vector.with(Text)
}, value);
}
@@ -57,8 +57,16 @@ export class MetadataCall extends Struct {
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.get('docs') as Vector<Text>;
return this.documentation;
}
// /**
+10 -2
View File
@@ -17,7 +17,7 @@ export class MetadataEvent extends Struct {
super({
name: Text,
args: Vector.with(Type),
docs: Vector.with(Text)
documentation: Vector.with(Text)
}, value);
}
@@ -31,8 +31,16 @@ export class MetadataEvent extends Struct {
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.get('docs') as Vector<Text>;
return this.documentation;
}
/**
+4 -4
View File
@@ -12,7 +12,7 @@ import { flattenUniq, validateTypes } from '../util';
import { MetadataCall } from './Calls';
import { MetadataEvent } from './Events';
import { MetadataStorage } from './Storage';
import { StorageFunctionMetadata } from './Storage';
/**
* @name MetadataModule
@@ -24,7 +24,7 @@ export class MetadataModule extends Struct {
super({
name: Text,
prefix: Text,
storage: Option.with(Vector.with(MetadataStorage)),
storage: Option.with(Vector.with(StorageFunctionMetadata)),
calls: Option.with(Vector.with(MetadataCall)),
events: Option.with(Vector.with(MetadataEvent))
}, value);
@@ -61,8 +61,8 @@ export class MetadataModule extends Struct {
/**
* @description the associated module storage
*/
get storage (): Option<Vector<MetadataStorage>> {
return this.get('storage') as Option<Vector<MetadataStorage>>;
get storage (): Option<Vector<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vector<StorageFunctionMetadata>>;
}
}
+15 -160
View File
@@ -2,164 +2,19 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Bytes from '../../primitive/Bytes';
import Null from '../../primitive/Null';
import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
import {
MapType,
PlainType,
StorageFunctionModifier,
StorageFunctionType,
StorageFunctionMetadata
} from '../v0/Storage';
export class Default extends Null {}
export class Optional extends Null {}
export class MetadataStorageModifier extends EnumType<Optional | Default> {
constructor (value?: any, index?: number) {
super({
Optional,
Default
}, value, index);
}
/**
* @description `true` if the storage entry is optional
*/
get isOptional (): boolean {
return this.toNumber() === 0;
}
toJSON (): any {
return this.toString();
}
}
export class MapType extends Struct {
private _isLinked = false;
constructor (value?: any) {
super({
key: Type,
value: Type
}, value);
if (value && value.isLinked) {
this._isLinked = true;
}
}
/**
* @description The mapped key as [[Type]]
*/
get key (): Type {
return this.get('key') as Type;
}
/**
* @description The mapped value as [[Type]]
*/
get value (): Type {
return this.get('value') as Type;
}
/**
* @description Is this an enumerable linked map
*/
get isLinked (): boolean {
return this._isLinked;
}
}
export class PlainType extends Type {
}
export class MetadataStorageType extends EnumType<PlainType | MapType> {
constructor (value?: any, index?: number) {
super({
PlainType,
MapType
}, value, index);
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description The value as a mapped value
*/
get asMap (): MapType {
return this.value as MapType;
}
/**
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
return this.value as PlainType;
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
return this.isMap
? this.asMap.value.toString()
: this.asType.toString();
}
}
/**
* @name MetadataModule
* @description
* The definition of a storage function
*/
export class MetadataStorage extends Struct {
constructor (value?: any) {
super({
name: Text,
modifier: MetadataStorageModifier,
type: MetadataStorageType,
fallback: Bytes,
docs: Vector.with(Text)
}, value);
}
/**
* @description The [[Text]] documentation
*/
get docs (): Vector<Text> {
return this.get('docs') as Vector<Text>;
}
/**
* @description The [[Bytes]] fallback default
*/
get fallback (): Bytes {
return this.get('fallback') as Bytes;
}
/**
* @description The [[MetadataArgument]] for arguments
*/
get modifier (): MetadataStorageModifier {
return this.get('modifier') as MetadataStorageModifier;
}
/**
* @description The call name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The [[MetadataStorageType]]
*/
get type (): MetadataStorageType {
return this.get('type') as MetadataStorageType;
}
}
// Re-export classes that haven't changed between V0 and V1
export {
MapType,
PlainType,
StorageFunctionModifier,
StorageFunctionType,
StorageFunctionMetadata
};
File diff suppressed because it is too large Load Diff
+9 -8
View File
@@ -4,7 +4,8 @@
import { OuterDispatchCall, OuterDispatchMetadata } from '../v0/Calls';
import { EventMetadata, OuterEventMetadata, OuterEventMetadataEvent } from '../v0/Events';
import { CallMetadata, FunctionMetadata, RuntimeModuleMetadata, ModuleMetadata, StorageFunctionMetadata, StorageMetadata, StorageFunctionType } from '../v0/Modules';
import { CallMetadata, FunctionMetadata, RuntimeModuleMetadata, ModuleMetadata } from '../v0/Modules';
import { StorageFunctionMetadata, StorageMetadata, StorageFunctionType } from '../v0/Storage';
import MetadataV0 from '../v0/Metadata';
import MetadataV1, { MetadataModuleV1 } from '.';
@@ -16,13 +17,13 @@ function storageV0 (mod: MetadataModuleV1): StorageMetadata | null {
return new StorageMetadata({
prefix: mod.prefix,
functions: mod.storage.unwrap().map(({ docs, fallback, modifier, name, type }) =>
functions: mod.storage.unwrap().map(({ documentation, fallback, modifier, name, type }) =>
new StorageFunctionMetadata({
name,
modifier: modifier.toNumber(),
type: new StorageFunctionType(type),
default: fallback,
documentation: docs
fallback,
documentation
})
)
});
@@ -35,12 +36,12 @@ function moduleV0 (mod: MetadataModuleV1): ModuleMetadata {
name: 'Call',
functions: mod.calls.isNone
? []
: mod.calls.unwrap().map(({ args, docs, name }, id) =>
: mod.calls.unwrap().map(({ args, documentation, name }, id) =>
new FunctionMetadata({
id,
name,
arguments: args,
documentation: docs
documentation
})
)
})
@@ -81,11 +82,11 @@ function outerEventV0 (v1: MetadataV1): OuterEventMetadata {
.map((mod) =>
new OuterEventMetadataEvent([
mod.name,
mod.events.unwrap().map(({ args, docs, name }) =>
mod.events.unwrap().map(({ args, documentation, name }) =>
new EventMetadata({
name,
arguments: args,
documentation: docs
documentation
})
)
])
+4 -4
View File
@@ -12,7 +12,7 @@ import { flattenUniq, validateTypes } from '../util';
import { MetadataCall } from '../v1/Calls';
import { MetadataEvent } from '../v1/Events';
import { MetadataStorage } from './Storage';
import { StorageFunctionMetadata } from './Storage';
/**
* @name MetadataModule
@@ -24,7 +24,7 @@ export class MetadataModule extends Struct {
super({
name: Text,
prefix: Text,
storage: Option.with(Vector.with(MetadataStorage)),
storage: Option.with(Vector.with(StorageFunctionMetadata)),
calls: Option.with(Vector.with(MetadataCall)),
events: Option.with(Vector.with(MetadataEvent))
}, value);
@@ -61,8 +61,8 @@ export class MetadataModule extends Struct {
/**
* @description the associated module storage
*/
get storage (): Option<Vector<MetadataStorage>> {
return this.get('storage') as Option<Vector<MetadataStorage>>;
get storage (): Option<Vector<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vector<StorageFunctionMetadata>>;
}
}
+44 -20
View File
@@ -2,19 +2,22 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyNumber } from '../../types';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Bool from '../../primitive/Bool';
import Bytes from '../../primitive/Bytes';
import Null from '../../primitive/Null';
import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
import { MetadataStorageModifier } from '../v1/Storage';
import { PlainType, StorageFunctionModifier } from '../v1/Storage';
export class Default extends Null {}
export class Optional extends Null {}
// Re-export classes that haven't changed between V1 and V2
export {
PlainType,
StorageFunctionModifier
};
export class MapType extends Struct {
constructor (value?: any) {
@@ -47,10 +50,7 @@ export class MapType extends Struct {
}
}
export class PlainType extends Type {
}
export class MetadataStorageType extends EnumType<PlainType | MapType> {
export class StorageFunctionType extends EnumType<PlainType | MapType> {
constructor (value?: any, index?: number) {
super({
PlainType,
@@ -89,27 +89,51 @@ export class MetadataStorageType extends EnumType<PlainType | MapType> {
}
}
export type StorageFunctionMetadataValue = {
name: string | Text,
modifier: StorageFunctionModifier | AnyNumber,
type: StorageFunctionType,
fallback: Bytes,
documentation: Vector<Text> | Array<string>
};
/**
* @name MetadataModule
* @description
* The definition of a storage function
*/
export class MetadataStorage extends Struct {
constructor (value?: any) {
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: Text,
modifier: MetadataStorageModifier,
type: MetadataStorageType,
modifier: StorageFunctionModifier,
type: StorageFunctionType,
fallback: Bytes,
docs: Vector.with(Text)
documentation: Vector.with(Text)
}, value);
}
/**
* @description The default value of the storage function
* @deprecated Use `.fallback` instead.
*/
get default (): Bytes {
return this.fallback;
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.get('docs') as Vector<Text>;
return this.documentation;
}
/**
@@ -122,8 +146,8 @@ export class MetadataStorage extends Struct {
/**
* @description The [[MetadataArgument]] for arguments
*/
get modifier (): MetadataStorageModifier {
return this.get('modifier') as MetadataStorageModifier;
get modifier (): StorageFunctionModifier {
return this.get('modifier') as StorageFunctionModifier;
}
/**
@@ -134,9 +158,9 @@ export class MetadataStorage extends Struct {
}
/**
* @description The [[MetadataStorageType]]
* @description The [[StorageFunctionType]]
*/
get type (): MetadataStorageType {
return this.get('type') as MetadataStorageType;
get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
File diff suppressed because it is too large Load Diff
+4 -4
View File
@@ -12,7 +12,7 @@ import { flattenUniq, validateTypes } from '../util';
import { MetadataCall } from '../v1/Calls';
import { MetadataEvent } from '../v1/Events';
import { MetadataStorage } from './Storage';
import { StorageFunctionMetadata } from './Storage';
/**
* @name MetadataModule
@@ -24,7 +24,7 @@ export class MetadataModule extends Struct {
super({
name: Text,
prefix: Text,
storage: Option.with(Vector.with(MetadataStorage)),
storage: Option.with(Vector.with(StorageFunctionMetadata)),
calls: Option.with(Vector.with(MetadataCall)),
events: Option.with(Vector.with(MetadataEvent))
}, value);
@@ -61,8 +61,8 @@ export class MetadataModule extends Struct {
/**
* @description the associated module storage
*/
get storage (): Option<Vector<MetadataStorage>> {
return this.get('storage') as Option<Vector<MetadataStorage>>;
get storage (): Option<Vector<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vector<StorageFunctionMetadata>>;
}
}
+55 -19
View File
@@ -2,13 +2,25 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyNumber } from '../../types';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
import { MetadataStorageModifier } from '../v1/Storage';
import { MapType, PlainType } from '../v2/Storage';
import {
MapType,
PlainType,
StorageFunctionModifier
} from '../v2/Storage';
// Re-export classes that haven't changed between V2 and V3
export {
MapType,
PlainType,
StorageFunctionModifier
};
export class DoubleMapType extends Struct {
constructor (value?: any) {
@@ -16,7 +28,7 @@ export class DoubleMapType extends Struct {
key1: Text,
key2: Text,
value: Text,
keyHasher: Text
key2Hasher: Text
}, value);
}
@@ -35,10 +47,10 @@ export class DoubleMapType extends Struct {
}
/**
* @description The mapped key as [[Text]]
* @description The hashing algorithm used to hash key2, as [[Text]]
*/
get keyHasher (): Text {
return this.get('keyHasher') as Text;
get key2Hasher (): Text {
return this.get('key2Hasher') as Text;
}
/**
@@ -49,7 +61,7 @@ export class DoubleMapType extends Struct {
}
}
export class MetadataStorageType extends EnumType<PlainType | MapType | DoubleMapType> {
export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMapType> {
constructor (value?: any, index?: number) {
super({
PlainType,
@@ -107,27 +119,51 @@ export class MetadataStorageType extends EnumType<PlainType | MapType | DoubleMa
}
}
export type StorageFunctionMetadataValue = {
name: string | Text,
modifier: StorageFunctionModifier | AnyNumber,
type: StorageFunctionType,
fallback: Bytes,
documentation: Vector<Text> | Array<string>
};
/**
* @name MetadataModule
* @name StorageFunctionMetadata
* @description
* The definition of a storage function
*/
export class MetadataStorage extends Struct {
constructor (value?: any) {
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: Text,
modifier: MetadataStorageModifier,
type: MetadataStorageType,
modifier: StorageFunctionModifier,
type: StorageFunctionType,
fallback: Bytes,
docs: Vector.with(Text)
documentation: Vector.with(Text)
}, value);
}
/**
* @description The default value of the storage function
* @deprecated Use `.fallback` instead.
*/
get default (): Bytes {
return this.fallback;
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.get('docs') as Vector<Text>;
return this.documentation;
}
/**
@@ -140,8 +176,8 @@ export class MetadataStorage extends Struct {
/**
* @description The [[MetadataArgument]] for arguments
*/
get modifier (): MetadataStorageModifier {
return this.get('modifier') as MetadataStorageModifier;
get modifier (): StorageFunctionModifier {
return this.get('modifier') as StorageFunctionModifier;
}
/**
@@ -152,9 +188,9 @@ export class MetadataStorage extends Struct {
}
/**
* @description The [[MetadataStorageType]]
* @description The [[StorageFunctionType]]
*/
get type (): MetadataStorageType {
return this.get('type') as MetadataStorageType;
get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,22 @@
// Copyright 2017-2019 @polkadot/types 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 '../Metadata';
import latestParsed from './latest.substrate.v4.json';
import rpcData from './static';
describe('MetadataV4', () => {
const metadata = new Metadata(rpcData);
it('decodes latest properly', () => {
const str = JSON.stringify(metadata.toJSON());
console.error(str);
console.error(metadata.getUniqTypes(true));
expect(metadata.version).toBe(4);
expect(metadata.asV4.modules.length).not.toBe(0);
expect(str).toEqual(JSON.stringify(latestParsed));
});
});
+130
View File
@@ -0,0 +1,130 @@
// Copyright 2017-2019 @polkadot/types 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 { MetadataInterface } from '../types';
import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Text from '../../primitive/Text';
import { flattenUniq, validateTypes } from '../util';
import { MetadataCall } from '../v1/Calls';
import { MetadataEvent } from '../v1/Events';
import { StorageFunctionMetadata } from './Storage';
/**
* @name MetadataModule
* @description
* The definition of a module in the system
*/
export class MetadataModule extends Struct {
constructor (value?: any) {
super({
name: Text,
prefix: Text,
storage: Option.with(Vector.with(StorageFunctionMetadata)),
calls: Option.with(Vector.with(MetadataCall)),
events: Option.with(Vector.with(MetadataEvent))
}, value);
}
/**
* @description the module calls
*/
get calls (): Option<Vector<MetadataCall>> {
return this.get('calls') as Option<Vector<MetadataCall>>;
}
/**
* @description the module events
*/
get events (): Option<Vector<MetadataEvent>> {
return this.get('events') as Option<Vector<MetadataEvent>>;
}
/**
* @description the module name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description the module prefix
*/
get prefix (): Text {
return this.get('prefix') as Text;
}
/**
* @description the associated module storage
*/
get storage (): Option<Vector<StorageFunctionMetadata>> {
return this.get('storage') as Option<Vector<StorageFunctionMetadata>>;
}
}
/**
* @name MetadataV4
* @description
* The runtime metadata as a decoded structure
*/
export default class MetadataV4 extends Struct implements MetadataInterface {
constructor (value?: any) {
super({
modules: Vector.with(MetadataModule)
}, value);
}
/**
* @description The associated modules for this structure
*/
get modules (): Vector<MetadataModule> {
return this.get('modules') as Vector<MetadataModule>;
}
private get callNames () {
return this.modules.map((mod) =>
mod.calls.isNone
? []
: mod.calls.unwrap().map((fn) =>
fn.args.map((arg) => arg.type.toString())
)
);
}
private get eventNames () {
return this.modules.map((mod) =>
mod.events.isNone
? []
: mod.events.unwrap().map((event) =>
event.args.map((arg) => arg.toString())
)
);
}
private get storageNames () {
return this.modules.map((mod) =>
mod.storage.isNone
? []
: mod.storage.unwrap().map((fn) =>
fn.type.isMap
? [fn.type.asMap.key.toString(), fn.type.asMap.value.toString()]
: [fn.type.asType.toString()]
)
);
}
/**
* @description Helper to retrieve a list of all type that are found, sorted and de-deuplicated
*/
getUniqTypes (throwError: boolean): Array<string> {
const types = flattenUniq([this.callNames, this.eventNames, this.storageNames]);
validateTypes(types, throwError);
return types;
}
}
+290
View File
@@ -0,0 +1,290 @@
// Copyright 2017-2019 @polkadot/types 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 { AnyNumber } from '../../types';
import Enum from '../../codec/Enum';
import EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Bool from '../../primitive/Bool';
import Bytes from '../../primitive/Bytes';
import Text from '../../primitive/Text';
import Type from '../../primitive/Type';
import {
PlainType,
StorageFunctionModifier
} from '../v3/Storage';
// Re-export classes that haven't changed between V3 and V4
export {
PlainType,
StorageFunctionModifier
};
export class StorageHasher extends Enum {
constructor (value?: any) {
super(['Blake2_128', 'Blake2_256', 'Twox128', 'Twox256', 'Twox128Concat'], value);
}
/**
* @description Is the enum Blake2_128?
*/
get isBlake2128 (): boolean {
return this.toNumber() === 0;
}
/**
* @description Is the enum Blake2_256?
*/
get isBlake2256 (): boolean {
return this.toNumber() === 1;
}
/**
* @description Is the enum Twox128?
*/
get isTwox128 (): boolean {
return this.toNumber() === 2;
}
/**
* @description Is the enum Twox256?
*/
get isTwox256 (): boolean {
return this.toNumber() === 3;
}
/**
* @description Is the enum isTwox128Concat?
*/
get isTwox128Concat (): boolean {
return this.toNumber() === 4;
}
toJSON (): any {
// This looks prettier in the generated JSON
return this.toString();
}
}
export class MapType extends Struct {
constructor (value?: any) {
super({
hasher: StorageHasher,
key: Type,
value: Type,
isLinked: Bool
}, value);
}
/**
* @description The hash algorithm used to hash keys, as [[StorageHasher]]
*/
get hasher (): StorageHasher {
return this.get('hasher') as StorageHasher;
}
/**
* @description Is this an enumerable linked map
*/
get isLinked (): boolean {
return (this.get('isLinked') as Bool).valueOf();
}
/**
* @description The mapped key as [[Type]]
*/
get key (): Type {
return this.get('key') as Type;
}
/**
* @description The mapped value as [[Type]]
*/
get value (): Type {
return this.get('value') as Type;
}
}
export class DoubleMapType extends Struct {
constructor (value?: any) {
super({
hasher: StorageHasher,
key1: Text,
key2: Text,
value: Text,
key2Hasher: Text
}, value);
}
/**
* @description The hashing algorithm used to hash keys, as [[Text]]
*/
get hasher (): Text {
return this.get('hasher') as Text;
}
/**
* @description The mapped key as [[Text]]
*/
get key1 (): Text {
return this.get('key1') as Text;
}
/**
* @description The mapped key as [[Text]]
*/
get key2 (): Text {
return this.get('key2') as Text;
}
/**
* @description The hashing algorithm used to hash key2, as [[Text]]
*/
get key2Hasher (): Text {
return this.get('key2Hasher') as Text;
}
/**
* @description The mapped key as [[Text]]
*/
get value (): Text {
return this.get('value') as Text;
}
}
export class StorageFunctionType extends EnumType<PlainType | MapType | DoubleMapType> {
constructor (value?: any, index?: number) {
super({
PlainType,
MapType,
DoubleMapType
}, value, index);
}
/**
* @description The value as a mapped value
*/
get asDoubleMap (): DoubleMapType {
return this.value as DoubleMapType;
}
/**
* @description The value as a mapped value
*/
get asMap (): MapType {
return this.value as MapType;
}
/**
* @description The value as a [[Type]] value
*/
get asType (): PlainType {
return this.value as PlainType;
}
/**
* @description `true` if the storage entry is a doublemap
*/
get isDoubleMap (): boolean {
return this.toNumber() === 2;
}
/**
* @description `true` if the storage entry is a map
*/
get isMap (): boolean {
return this.toNumber() === 1;
}
/**
* @description Returns the string representation of the value
*/
toString (): string {
if (this.isDoubleMap) {
return this.asDoubleMap.toString();
}
return this.isMap
? this.asMap.value.toString()
: this.asType.toString();
}
}
export type StorageFunctionMetadataValue = {
name: string | Text,
modifier: StorageFunctionModifier | AnyNumber,
type: StorageFunctionType,
fallback: Bytes,
documentation: Vector<Text> | Array<string>
};
/**
* @name StorageFunctionMetadata
* @description
* The definition of a storage function
*/
export class StorageFunctionMetadata extends Struct {
constructor (value?: StorageFunctionMetadataValue | Uint8Array) {
super({
name: Text,
modifier: StorageFunctionModifier,
type: StorageFunctionType,
fallback: Bytes,
documentation: Vector.with(Text)
}, value);
}
/**
* @description The default value of the storage function
* @deprecated Use `.fallback` instead.
*/
get default (): Bytes {
return this.fallback;
}
/**
* @description The default value of the storage function
*/
get fallback (): Bytes {
return this.get('fallback') as Bytes;
}
/**
* @description The [[Text]] documentation
*/
get documentation (): Vector<Text> {
return this.get('documentation') as Vector<Text>;
}
/**
* @description The [[Text]] documentation
* @deprecated Use `.documentation` instead.
*/
get docs (): Vector<Text> {
return this.documentation;
}
/**
* @description The key name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The modifier
*/
get modifier (): StorageFunctionModifier {
return this.get('modifier') as StorageFunctionModifier;
}
/**
* @description The [[StorageFunctionType]]
*/
get type (): StorageFunctionType {
return this.get('type') as StorageFunctionType;
}
}
+11
View File
@@ -0,0 +1,11 @@
// Copyright 2017-2019 @polkadot/types 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 { default as MetadataV4, MetadataModule as MetadataModuleV4 } from './Metadata';
export default MetadataV4;
export {
MetadataModuleV4
};
File diff suppressed because it is too large Load Diff
File diff suppressed because one or more lines are too long
+10
View File
@@ -0,0 +1,10 @@
// Copyright 2017-2019 @polkadot/types 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 MetadataV3 from '../v3/Metadata';
import MetadataV4 from '.';
export default function toV3 (v4: MetadataV4): MetadataV3 {
return new MetadataV3(v4);
}
+6 -5
View File
@@ -7,11 +7,12 @@ import { AnyU8a } from '../types';
import { isFunction } from '@polkadot/util';
import Bytes from './Bytes';
import { StorageFunctionMetadata } from '../Metadata/v0/Modules';
import { StorageFunctionMetadata as MetaV0 } from '../Metadata/v0/Storage';
import { StorageFunctionMetadata as MetaV4 } from '../Metadata/v4/Storage';
export interface StorageFunction {
(arg?: any): Uint8Array;
meta: StorageFunctionMetadata;
meta: MetaV0 | MetaV4;
method: string;
section: string;
toJSON: () => any;
@@ -25,7 +26,7 @@ export interface StorageFunction {
* constructed by passing in a raw key or a StorageFunction with (optional) arguments.
*/
export default class StorageKey extends Bytes {
private _meta: StorageFunctionMetadata | null;
private _meta: MetaV0 | MetaV4 | null;
private _outputType: string | null;
constructor (value: AnyU8a | StorageKey | StorageFunction | [StorageFunction, any]) {
@@ -49,7 +50,7 @@ export default class StorageKey extends Bytes {
return value as Uint8Array;
}
static getMeta (value: StorageKey | StorageFunction | [StorageFunction, any]): StorageFunctionMetadata | null {
static getMeta (value: StorageKey | StorageFunction | [StorageFunction, any]): MetaV0 | MetaV4 | null {
if (value instanceof StorageKey) {
return value.meta;
} else if (isFunction(value)) {
@@ -80,7 +81,7 @@ export default class StorageKey extends Bytes {
/**
* @description The metadata or `null` when not available
*/
get meta (): StorageFunctionMetadata | null {
get meta (): MetaV0 | MetaV4 | null {
return this._meta;
}
@@ -9,7 +9,7 @@ _These are keys that are always available to the runtime implementation_
- **summary**: Number of authorities.
▸ **authorityPrefix**(): `u32`
- **summary**: Prefix under which authorities are storied.
- **summary**: Prefix under which authorities are stored.
▸ **changesTrieConfig**(): `u32`
- **summary**: Changes trie configuration is stored under this key.