Updates for V1 formatting changes (#651)

* Updates for V1 formatting changes

* Rework V1 with the new simplified structures

* Parsing parses

* asV0 in-place, rework class splits

* e2e testing completed

* Update args, docs & fallback (shorter, non-collision naming)

* More v0-like event output
This commit is contained in:
Jaco Greeff
2019-01-30 15:41:49 +01:00
committed by GitHub
parent 82531493da
commit aea223f024
14 changed files with 3173 additions and 2824 deletions
+3 -3
View File
@@ -24,7 +24,7 @@ describe.skip('e2e transactions', () => {
it('makes a transfer (sign, then send)', async (done) => {
const nonce = await api.query.system.accountNonce(keyring.dave.address());
api.tx.balances
return api.tx.balances
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
.sign(keyring.dave, nonce)
.send(({ events, status, type }) => {
@@ -46,7 +46,7 @@ describe.skip('e2e transactions', () => {
});
it('makes a transfer (signAndSend)', async (done) => {
api.tx.balances
return api.tx.balances
.transfer('12ghjsRJpeJpUQaCQeHcBv9pRQA3tdcMxeL8cVk9JHWJGHjd', 12345)
.signAndSend(keyring.dave, ({ events, status, type }) => {
console.log('Transaction status:', type);
@@ -66,7 +66,7 @@ describe.skip('e2e transactions', () => {
});
});
it('makes a proposal', async () => {
it.skip('makes a proposal', async () => {
const nonce = await api.query.system.accountNonce(keyring.alice.address());
// don't wait for status, just get hash
+17 -20
View File
@@ -25,30 +25,27 @@ describe('Metadata', () => {
expect(str).toEqual(JSON.stringify(latestParsed));
});
describe('storage with default values', () => {
Method.injectMethods(extrinsicsFromMeta(metadata.asV0));
metadata.asV1.modules.forEach((mod) => {
if (mod.storage.isNone) {
return;
}
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)
).not.toThrow();
});
});
});
});
it('converts v1 to V0', () => {
const v0 = metadata.asV0;
const str = JSON.stringify(metadata.toJSON());
console.error(str);
// console.error(JSON.stringify(v0.toJSON()));
expect(metadata.getUniqTypes()).toEqual(v0.getUniqTypes());
});
describe('storage with default values', () => {
Method.injectMethods(extrinsicsFromMeta(metadata.asV0));
metadata.asV1.modules
.filter(({ storage }) => storage.isSome)
.map((mod) =>
mod.storage.unwrap().forEach(({ fallback, name, type }) => {
it(`creates default types for ${mod.prefix}.${name}, type ${type}`, () => {
expect(
() => createType(type.toString(), fallback)
).not.toThrow();
});
})
);
});
});
+2 -2
View File
@@ -2,6 +2,6 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import meta from './v1/static';
import metadata from './v1/static';
export default meta;
export default metadata;
+77
View File
@@ -0,0 +1,77 @@
// 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 Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Text from '../../Text';
import Type from '../../Type';
export class MetadataCallArg extends Struct {
constructor (value?: any) {
super({
name: Text,
type: Type
}, value);
}
/**
* @description The argument name
*/
get name (): Text {
return this.get('name') as Text;
}
/**
* @description The [[Type]]
*/
get type (): Type {
return this.get('type') as Type;
}
}
/**
* @name MetadataCall
* @description
* The definition of a call
*/
export class MetadataCall extends Struct {
constructor (value?: any) {
super({
// NOTE Just commented out, it is not available in current, but an open question
// around the usefulness of supplying this remains (could be re-added)
// id: u16,
name: Text,
args: Vector.with(MetadataCallArg),
docs: Vector.with(Text)
}, value);
}
/**
* @description The [[MetadataCallArg]] for arguments
*/
get args (): Vector<MetadataCallArg> {
return this.get('args') as Vector<MetadataCallArg>;
}
/**
* @description The [[Text]] documentation
*/
get docs (): Vector<Text> {
return this.get('docs') as Vector<Text>;
}
// /**
// * @description The call function id
// */
// get id (): u16 {
// return this.get('id') as u16;
// }
/**
* @description The call name
*/
get name (): Text {
return this.get('name') as Text;
}
}
+44
View File
@@ -0,0 +1,44 @@
// 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 Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Text from '../../Text';
import Type from '../../Type';
/**
* @name MetadataEvent
* @description
* The definition of an event
*/
export class MetadataEvent extends Struct {
constructor (value?: any) {
super({
name: Text,
args: Vector.with(Type),
docs: Vector.with(Text)
}, value);
}
/**
* @description The [[Type]] for args
*/
get args (): Vector<Type> {
return this.get('args') as Vector<Type>;
}
/**
* @description The [[Text]] documentation
*/
get docs (): Vector<Text> {
return this.get('docs') as Vector<Text>;
}
/**
* @description The call name
*/
get name (): Text {
return this.get('name') as Text;
}
}
+152
View File
@@ -0,0 +1,152 @@
// 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 EnumType from '../../codec/EnumType';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Bytes from '../../Bytes';
import Null from '../../Null';
import Text from '../../Text';
import Type from '../../Type';
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 {
constructor (value?: any) {
super({
key: Type,
value: Type
}, value);
}
/**
* @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 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;
}
}
+40 -39
View File
@@ -8,36 +8,40 @@ import Option from '../../codec/Option';
import Struct from '../../codec/Struct';
import Vector from '../../codec/Vector';
import Text from '../../Text';
import { flattenUniq, validateTypes } from '../util';
import { EventMetadata } from '../v0/Events';
import { CallMetadata, StorageMetadata } from '../v0/Modules';
import { OuterDispatchCall } from './outerDispatch';
class RuntimeModuleMetadata extends Struct {
import { MetadataCall } from './Calls';
import { MetadataEvent } from './Events';
import { MetadataStorage } 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(StorageMetadata),
calls: CallMetadata,
outerDispatch: Option.with(OuterDispatchCall),
events: Vector.with(EventMetadata)
storage: Option.with(Vector.with(MetadataStorage)),
calls: Option.with(Vector.with(MetadataCall)),
events: Option.with(Vector.with(MetadataEvent))
}, value);
}
/**
* @description the module call
* @description the module calls
*/
get calls (): CallMetadata {
return this.get('calls') as CallMetadata;
get calls (): Option<Vector<MetadataCall>> {
return this.get('calls') as Option<Vector<MetadataCall>>;
}
/**
* @description the module events
*/
get events (): Vector<EventMetadata> {
return this.get('events') as Vector<EventMetadata>;
get events (): Option<Vector<MetadataEvent>> {
return this.get('events') as Option<Vector<MetadataEvent>>;
}
/**
@@ -48,14 +52,7 @@ class RuntimeModuleMetadata extends Struct {
}
/**
* @description the outer dispatch
*/
get outerDispatch (): Option<OuterDispatchCall> {
return this.get('outerDispatch') as Option<OuterDispatchCall>;
}
/**
* @description the module name
* @description the module prefix
*/
get prefix (): Text {
return this.get('prefix') as Text;
@@ -64,8 +61,8 @@ class RuntimeModuleMetadata extends Struct {
/**
* @description the associated module storage
*/
get storage (): Option<StorageMetadata> {
return this.get('storage') as Option<StorageMetadata>;
get storage (): Option<Vector<MetadataStorage>> {
return this.get('storage') as Option<Vector<MetadataStorage>>;
}
}
@@ -77,42 +74,46 @@ class RuntimeModuleMetadata extends Struct {
export default class MetadataV1 extends Struct implements MetadataInterface {
constructor (value?: any) {
super({
modules: Vector.with(RuntimeModuleMetadata)
modules: Vector.with(MetadataModule)
}, value);
}
/**
* @description The associated modules for this structure
*/
get modules (): Vector<RuntimeModuleMetadata> {
return this.get('modules') as Vector<RuntimeModuleMetadata>;
get modules (): Vector<MetadataModule> {
return this.get('modules') as Vector<MetadataModule>;
}
private get callNames () {
return this.modules.map((module) =>
module.calls.functions.map((fn) =>
fn.arguments.map((argument) => argument.type.toString())
)
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((module) =>
module.events.map((event) =>
event.arguments.map((argument) => argument.toString())
)
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((module) =>
module.storage.isSome
? module.storage.unwrap().functions.map((fn) =>
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()]
)
: []
);
}
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
@@ -1,30 +0,0 @@
// 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 Struct from '../../codec/Struct';
import Text from '../../Text';
import U16 from '../../U16';
export class OuterDispatchCall extends Struct {
constructor (value?: any) {
super({
name: Text,
index: U16
}, value);
}
/**
* @description The [[U16]] index for the call
*/
get index (): U16 {
return this.get('index') as U16;
}
/**
* @description The name for the call
*/
get name (): Text {
return this.get('name') as Text;
}
}
File diff suppressed because one or more lines are too long
+71 -38
View File
@@ -3,67 +3,100 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { OuterDispatchCall, OuterDispatchMetadata } from '../v0/Calls';
import { OuterEventMetadata, OuterEventMetadataEvent } from '../v0/Events';
import { RuntimeModuleMetadata, ModuleMetadata } from '../v0/Modules';
import { EventMetadata, OuterEventMetadata, OuterEventMetadataEvent } from '../v0/Events';
import { CallMetadata, FunctionMetadata, RuntimeModuleMetadata, ModuleMetadata, StorageFunctionMetadata, StorageMetadata } from '../v0/Modules';
import MetadataV0 from '../v0';
import MetadataV1 from './index';
import MetadataV1, { MetadataModule as MetadataModuleV1 } from './index';
function storageV0 (mod: MetadataModuleV1): StorageMetadata | null {
if (mod.storage.isNone) {
return null;
}
return new StorageMetadata({
prefix: mod.prefix,
functions: mod.storage.unwrap().map(({ docs, fallback, name, type }) =>
new StorageFunctionMetadata({
name,
modifier: 0, // unused, don't specify
type,
default: fallback,
documentation: docs
})
)
});
}
function moduleV0 (mod: MetadataModuleV1): ModuleMetadata {
return new ModuleMetadata({
name: 'Module',
call: new CallMetadata({
name: 'Call',
functions: mod.calls.isNone
? []
: mod.calls.unwrap().map(({ args, docs, name }, id) =>
new FunctionMetadata({
id,
name,
arguments: args,
documentation: docs
})
)
})
});
}
function modulesV0 (v1: MetadataV1): Array<RuntimeModuleMetadata> {
return v1.modules.map((mod) => {
return new RuntimeModuleMetadata({
prefix: mod.prefix,
module: new ModuleMetadata({
name: 'Module',
call: mod.calls
}),
storage: mod.storage.isNone
? null
: mod.storage.unwrap()
});
});
return v1.modules.map((mod) =>
new RuntimeModuleMetadata({
prefix: mod.name, // passed from name, compact with casing
module: moduleV0(mod),
storage: storageV0(mod)
})
);
}
function outerDispatchV0 (v1: MetadataV1): OuterDispatchMetadata {
return new OuterDispatchMetadata({
name: 'Call',
// only add the where we have an actual outer dispatch available, only these
// are assigned indexes for this dispatch
calls: v1.modules
.filter(({ outerDispatch }) => outerDispatch.isSome)
.map((mod) => {
const dispatch = mod.outerDispatch.unwrap();
return new OuterDispatchCall({
name: dispatch.name,
prefix: mod.prefix,
index: dispatch.index
});
})
.filter(({ calls }) => calls.isSome)
.map((mod, index) =>
new OuterDispatchCall({
// name and prefix are swapped in the actual V0 data, i.e. name is UpperCase
name: mod.prefix,
prefix: mod.name,
index
})
)
});
}
function outerEventV0 (v1: MetadataV1): OuterEventMetadata {
return new OuterEventMetadata({
name: 'Event',
// Events are different to storage and calls - it does not use the module prefix, but
// rather based on whether there are events or not, it gets included and indexed
events: v1.modules
.filter(({ events }) => events && events.length)
.filter(({ events }) => events.isSome)
.map((mod) =>
new OuterEventMetadataEvent([mod.prefix, mod.events])
new OuterEventMetadataEvent([
mod.name,
mod.events.unwrap().map(({ args, docs, name }) =>
new EventMetadata({
name,
arguments: args,
documentation: docs
})
)
])
)
});
}
export default function toV0 (v1: MetadataV1): MetadataV0 {
const outerEvent = outerEventV0(v1);
const modules = modulesV0(v1);
const outerDispatch = outerDispatchV0(v1);
return new MetadataV0({
outerEvent,
modules,
outerDispatch
outerEvent: outerEventV0(v1),
modules: modulesV0(v1),
outerDispatch: outerDispatchV0(v1)
});
}
-1
View File
@@ -35,7 +35,6 @@ export default class Option<T extends Codec> extends Base<T> implements Codec {
return isNull(value) || isUndefined(value) || value instanceof Null
? new Null()
: new Type(value);
}
static with<O extends Codec> (Type: Constructor): Constructor<Option<O>> {
+1 -2
View File
@@ -39,8 +39,7 @@ export default class Vector<
}
const u8a = u8aToU8a(value);
let [offset, _length] = Compact.decodeU8a(u8a);
const [offset, _length] = Compact.decodeU8a(u8a);
const length = _length.toNumber();
return decodeU8a(u8a.subarray(offset), new Array(length).fill(Type)) as T[];