Update extrinsics FromMetadata. Drop storages with doublemap type when converting v3 to v2 (#888)

* update extrinsics fromMetadata

* drop storages with doublemap when convert v3 to v2

* update Event.injectMetadata
This commit is contained in:
Alex Wang
2019-05-13 10:49:29 +02:00
committed by Amaury Martiny
parent 777a73f7c2
commit 97032e7e86
21 changed files with 313 additions and 56 deletions
@@ -239,7 +239,7 @@ export default class MetadataVersioned extends Struct implements MetadataInterfa
}
/**
* @description Returns the wrapped values as a V3 object
* @description Returns the wrapped values as a V4 object
*/
get asV4 (): MetadataV4 {
assert(this.metadata.version === 4, `Cannot convert metadata from v${this.metadata.version} to v4`);
@@ -7,13 +7,14 @@ import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata';
import createType from '../../codec/createType';
import Method from '../../primitive/Method';
import Metadata from './Metadata';
import Metadata from '../Metadata';
import MetadataV0 from './Metadata';
import latestParsed from './latest.substrate.json';
import rpcData from './static';
describe('Metadata', () => {
it('decodes latest properly', () => {
const metadata = new Metadata(rpcData);
const metadata = new MetadataV0(rpcData);
const str = JSON.stringify(metadata.toJSON());
expect(metadata.events.length).not.toBe(0);
@@ -25,7 +26,7 @@ describe('Metadata', () => {
Method.injectMethods(extrinsicsFromMeta(metadata));
metadata.modules.forEach((mod) => {
metadata.asV0.modules.forEach((mod) => {
if (mod.storage.isNone) {
return;
}
@@ -29,7 +29,7 @@ describe('MetadataV1', () => {
});
describe('storage with default values', () => {
Method.injectMethods(extrinsicsFromMeta(metadata.asV0));
Method.injectMethods(extrinsicsFromMeta(metadata));
metadata.asV1.modules
.filter(({ storage }) => storage.isSome)
@@ -35,7 +35,7 @@ describe('MetadataV2', () => {
});
describe('storage with default values', () => {
Method.injectMethods(extrinsicsFromMeta(metadata.asV0));
Method.injectMethods(extrinsicsFromMeta(metadata));
metadata.asV2.modules
.filter(({ storage }) => storage.isSome)
+30 -2
View File
@@ -2,9 +2,37 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import Option from '../../codec/Option';
import Vector from '../../codec/Vector';
import MetadataV2 from '../v2/Metadata';
import MetadataV3 from '.';
import { MetadataModule } from './Metadata';
import { StorageFunctionMetadata } from './Storage';
export default function toV2 (v2: MetadataV3): MetadataV2 {
return new MetadataV2(v2);
function storageV2 (storage: Option<Vector<StorageFunctionMetadata>>): Option<Vector<StorageFunctionMetadata>> {
if (storage.isNone) {
return storage;
}
const storageV2 = storage.unwrap().filter(({ type }) => !type.isDoubleMap);
return new Option<Vector<StorageFunctionMetadata>>(
Vector.with(StorageFunctionMetadata),
new Vector<StorageFunctionMetadata>(StorageFunctionMetadata, storageV2)
);
}
function modulesV2 (v3: MetadataV3): Array<MetadataModule> {
return v3.modules.map((mod) =>
new MetadataModule({
name: mod.name,
prefix: mod.prefix,
storage: storageV2(mod.storage)
}));
}
export default function toV2 (v3: MetadataV3): MetadataV2 {
return new MetadataV2({
modules: modulesV2(v3)
});
}
@@ -2,6 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata';
import createType from '../../codec/createType';
import Method from '../../primitive/Method';
import Metadata from '../Metadata';
import latestParsed from './latest.substrate.v4.json';
import rpcData from './static';
@@ -19,4 +24,20 @@ describe('MetadataV4', () => {
expect(metadata.asV4.modules.length).not.toBe(0);
expect(str).toEqual(JSON.stringify(latestParsed));
});
describe('storage with default values', () => {
Method.injectMethods(extrinsicsFromMeta(metadata));
metadata.asV4.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();
});
})
);
});
});
+20 -12
View File
@@ -9,7 +9,8 @@ import { assert, isHex, isObject, isU8a, hexToU8a } from '@polkadot/util';
import { getTypeDef, getTypeClass } from '../codec/createType';
import Struct from '../codec/Struct';
import U8aFixed from '../codec/U8aFixed';
import { FunctionMetadata, FunctionArgumentMetadata } from '../Metadata/v0/Modules';
import { FunctionMetadata as MetaV0, FunctionArgumentMetadata } from '../Metadata/v0/Modules';
import { MetadataCall as MetaV4 } from '../Metadata/v1/Calls';
interface DecodeMethodInput {
args: any;
@@ -18,13 +19,13 @@ interface DecodeMethodInput {
interface DecodedMethod extends DecodeMethodInput {
argsDef: ArgsDef;
meta: FunctionMetadata;
meta: MetaV0 | MetaV4;
}
export interface MethodFunction {
(...args: any[]): Method;
callIndex: Uint8Array;
meta: FunctionMetadata;
meta: MetaV0 | MetaV4;
method: string;
section: string;
toJSON: () => any;
@@ -63,9 +64,9 @@ export class MethodIndex extends U8aFixed {
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
*/
export default class Method extends Struct implements IMethod {
protected _meta: FunctionMetadata;
protected _meta: MetaV0 | MetaV4;
constructor (value: any, meta?: FunctionMetadata) {
constructor (value: any, meta?: MetaV0 | MetaV4) {
const decoded = Method.decodeMethod(value, meta);
super({
@@ -86,7 +87,7 @@ export default class Method extends Struct implements IMethod {
* @param _meta - Metadata to use, so that `injectMethods` lookup is not
* necessary.
*/
private static decodeMethod (value: DecodedMethod | Uint8Array | string, _meta?: FunctionMetadata): DecodedMethod {
private static decodeMethod (value: DecodedMethod | Uint8Array | string, _meta?: MetaV0 | MetaV4): DecodedMethod {
if (isHex(value)) {
return Method.decodeMethod(hexToU8a(value), _meta);
} else if (isU8a(value)) {
@@ -127,14 +128,21 @@ export default class Method extends Struct implements IMethod {
return {
args: new Uint8Array(),
argsDef: {},
meta: new FunctionMetadata(),
meta: new MetaV0(),
callIndex: new Uint8Array([255, 255])
};
}
// If the extrinsic function has an argument of type `Origin`, we ignore it
static filterOrigin (meta?: FunctionMetadata): Array<FunctionArgumentMetadata> {
static filterOrigin (meta?: MetaV0 | MetaV4): Array<FunctionArgumentMetadata> {
// FIXME should be `arg.type !== Origin`, but doesn't work...
if (meta instanceof MetaV4) {
return meta
? meta.args.filter(({ type }) =>
type.toString() !== 'Origin'
)
: [];
}
return meta
? meta.arguments.filter(({ type }) =>
type.toString() !== 'Origin'
@@ -161,7 +169,7 @@ export default class Method extends Struct implements IMethod {
*
* @param meta - The function metadata used to get the definition.
*/
private static getArgsDef (meta: FunctionMetadata): ArgsDef {
private static getArgsDef (meta: MetaV0 | MetaV4): ArgsDef {
return Method.filterOrigin(meta).reduce((result, { name, type }) => {
const Type = getTypeClass(
getTypeDef(type)
@@ -215,15 +223,15 @@ export default class Method extends Struct implements IMethod {
* @description `true` if the `Origin` type is on the method (extrinsic method)
*/
get hasOrigin (): boolean {
const firstArg = this.meta.arguments[0];
const firstArg = this.meta instanceof MetaV4 ? this.meta.args[0] : this.meta.arguments[0];
return !!firstArg && firstArg.type.toString() === 'Origin';
}
/**
* @description The [[FunctionMetadata]]
* @description The [[MetaV0]]
*/
get meta (): FunctionMetadata {
get meta (): MetaV0 | MetaV4 {
return this._meta;
}
}
+46 -20
View File
@@ -11,8 +11,9 @@ import Tuple from '../codec/Tuple';
import U8aFixed from '../codec/U8aFixed';
import Null from '../primitive/Null';
import { TypeDef, getTypeClass, getTypeDef } from '../codec/createType';
import MetadataV0 from '../Metadata/v0';
import { EventMetadata } from '../Metadata/v0/Events';
import Metadata from '../Metadata';
import { EventMetadata as MetaV0 } from '../Metadata/v0/Events';
import { MetadataEvent as MetaV1 } from '../Metadata/v1/Events';
const EventTypes: { [index: string]: Constructor<EventData> } = {};
@@ -22,12 +23,12 @@ const EventTypes: { [index: string]: Constructor<EventData> } = {};
* Wrapper for the actual data that forms part of an [[Event]]
*/
export class EventData extends Tuple {
private _meta: EventMetadata;
private _meta: MetaV0 | MetaV1;
private _method: string;
private _section: string;
private _typeDef: Array<TypeDef>;
constructor (Types: Array<Constructor>, value: Uint8Array, typeDef: Array<TypeDef>, meta: EventMetadata, section: string, method: string) {
constructor (Types: Array<Constructor>, value: Uint8Array, typeDef: Array<TypeDef>, meta: MetaV0 | MetaV1, section: string, method: string) {
super(Types, value);
this._meta = meta;
@@ -39,7 +40,7 @@ export class EventData extends Tuple {
/**
* @description The wrapped [[EventMetadata]]
*/
get meta (): EventMetadata {
get meta (): MetaV0 | MetaV1 {
return this._meta;
}
@@ -119,23 +120,48 @@ export default class Event extends Struct {
// This is called/injected by the API on init, allowing a snapshot of
// the available system events to be used in lookups
static injectMetadata (metadata: MetadataV0): void {
metadata.events.forEach((section, sectionIndex) => {
const sectionName = stringCamelCase(section.name.toString());
static injectMetadata (metadata: Metadata): void {
if (metadata.version === 0) {
const metadataV0 = metadata.asV0;
metadataV0.events.forEach((section, sectionIndex) => {
const sectionName = stringCamelCase(section.name.toString());
section.events.forEach((meta, methodIndex) => {
const methodName = meta.name.toString();
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
const typeDef = meta.arguments.map((arg) => getTypeDef(arg));
const Types = typeDef.map(getTypeClass);
section.events.forEach((meta, methodIndex) => {
const methodName = meta.name.toString();
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
const typeDef = meta.arguments.map((arg) => getTypeDef(arg));
const Types = typeDef.map(getTypeClass);
EventTypes[eventIndex.toString()] = class extends EventData {
constructor (value: Uint8Array) {
super(Types, value, typeDef, meta, sectionName, methodName);
}
};
EventTypes[eventIndex.toString()] = class extends EventData {
constructor (value: Uint8Array) {
super(Types, value, typeDef, meta, sectionName, methodName);
}
};
});
});
});
} else {
const metadataV4 = metadata.asV4;
let sectionIndex = 0;
metadataV4.modules.forEach((section) => {
const sectionName = stringCamelCase(section.name.toString());
if (!section.events.isNone && !section.events.isEmpty) {
section.events.unwrap().forEach((meta, methodIndex) => {
const methodName = meta.name.toString();
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
const typeDef = meta.args.map((arg) => getTypeDef(arg));
const Types = typeDef.map(getTypeClass);
EventTypes[eventIndex.toString()] = class extends EventData {
constructor (value: Uint8Array) {
super(Types, value, typeDef, meta, sectionName, methodName);
}
};
});
sectionIndex += 1;
}
});
}
}
/**
@@ -155,7 +181,7 @@ export default class Event extends Struct {
/**
* @description The [[EventMetadata]] with the documentation
*/
get meta (): EventMetadata {
get meta (): MetaV0 | MetaV1 {
return this.data.meta;
}
+18 -2
View File
@@ -13,12 +13,13 @@ import EventRecord from './EventRecord';
import Metadata from '../Metadata';
import metadataV0 from '../Metadata/v0/static';
import metadataV1 from '../Metadata/static';
import metadataV4 from '../Metadata/v4/static';
describe('EventRecord', () => {
describe('v0', () => {
beforeEach(() => {
Event.injectMetadata(
new Metadata(metadataV0).asV0
new Metadata(metadataV0)
);
});
@@ -53,7 +54,22 @@ describe('EventRecord', () => {
describe.skip('v1', () => {
beforeEach(() => {
Event.injectMetadata(
new Metadata(metadataV1).asV0
new Metadata(metadataV1)
);
});
it('decodes correctly', () => {
const records: Vector<EventRecord> = createType('Vec<EventRecord>', json3.params.result.changes[0][1]) as any;
const er = records[0];
expect(er.phase.type).toEqual('ApplyExtrinsic');
});
});
describe.skip('v4', () => {
beforeEach(() => {
Event.injectMetadata(
new Metadata(metadataV4)
);
});
+3 -2
View File
@@ -10,7 +10,8 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
import Compact from '../codec/Compact';
import Struct from '../codec/Struct';
import { FunctionMetadata } from '../Metadata/v0/Modules';
import { FunctionMetadata as MetaV0 } from '../Metadata/v0/Modules';
import { MetadataCall as MetaV1 } from '../Metadata/v1/Calls';
import Method from '../primitive/Method';
import Address from './Address';
import ExtrinsicSignature from './ExtrinsicSignature';
@@ -140,7 +141,7 @@ export default class Extrinsic extends Struct implements IExtrinsic {
/**
* @description The [[FunctionMetadata]] that describes the extrinsic
*/
get meta (): FunctionMetadata {
get meta (): MetaV0 | MetaV1 {
return this.method.meta;
}
+3 -2
View File
@@ -7,7 +7,8 @@ import BN from 'bn.js';
import U8a from './codec/U8a';
import Method from './primitive/Method';
import Address from './type/Address';
import { FunctionMetadata } from './Metadata/v0/Modules';
import { FunctionMetadata as MetaV0 } from './Metadata/v0/Modules';
import { MetadataCall as MetaV4 } from './Metadata/v1/Calls';
export type CodecArg = Codec | BN | Boolean | String | Uint8Array | boolean | number | string | undefined | CodecArgArray | CodecArgObject;
@@ -112,7 +113,7 @@ export interface IMethod extends Codec {
readonly callIndex: Uint8Array;
readonly data: Uint8Array;
readonly hasOrigin: boolean;
readonly meta: FunctionMetadata;
readonly meta: MetaV0 | MetaV4;
}
export interface IExtrinsicSignature extends Codec {