Decode meta <T as HasCompact>::Type (#358)
* Decode meta with HasCompact * Revert saved meta (until create done) * Construct Compact.with, allow Compact extends Base * Remove (now invalid) comment * createType creates Compact<*> * Allow Compact<Moment> * Update Type.ts * Update Type.ts * Update Type.ts
This commit is contained in:
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber } from './types';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
|
||||
export default class Compact128 extends Compact {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 128);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber } from './types';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
|
||||
export default class Compact16 extends Compact {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 16, false);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber } from './types';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
|
||||
export default class Compact256 extends Compact {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 256);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber } from './types';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
|
||||
export default class Compact32 extends Compact {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 32, false);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { AnyNumber } from './types';
|
||||
|
||||
import Compact from './codec/Compact';
|
||||
|
||||
export default class Compact64 extends Compact {
|
||||
constructor (value?: AnyNumber) {
|
||||
super(value, 64, false);
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,10 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { hexToU8a } from '@polkadot/util';
|
||||
|
||||
import Metadata from './Metadata';
|
||||
import rpcdata from './Metadata.rpc';
|
||||
import bbqBirch from './json/Metadata.001.json';
|
||||
import compacted from './json/Metadata.002.json';
|
||||
|
||||
describe('Metadata', () => {
|
||||
it('decodes properly', () => {
|
||||
@@ -19,16 +18,15 @@ describe('Metadata', () => {
|
||||
expect(decoded.events.length).not.toBe(0);
|
||||
});
|
||||
|
||||
it('decodes when length not present (HACK)', () => {
|
||||
const u8a = hexToU8a(rpcdata);
|
||||
const decoded = new Metadata(u8a.subarray(2));
|
||||
|
||||
expect(decoded.events.length).not.toBe(0);
|
||||
});
|
||||
|
||||
it('decodes BBQ properly', () => {
|
||||
const decoded = new Metadata(bbqBirch.result);
|
||||
|
||||
expect(decoded.events.length).not.toBe(0);
|
||||
});
|
||||
|
||||
it('decodes Compacted properly', () => {
|
||||
const decoded = new Metadata(compacted.result);
|
||||
|
||||
expect(decoded.events.length).not.toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,9 +7,9 @@ import { bnToBn, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import { AnyNumber } from './types';
|
||||
import Base from './codec/Base';
|
||||
import U64 from './U64';
|
||||
import UInt, { UIntBitLength } from './codec/UInt';
|
||||
|
||||
const BITLENGTH = 64;
|
||||
const BITLENGTH: UIntBitLength = 64;
|
||||
|
||||
// A wrapper around seconds/timestamps. Internally the representation only has
|
||||
// second precicion (aligning with Rust), so any numbers passed an/out are always
|
||||
@@ -27,7 +27,7 @@ export default class Moment extends Base<Date> {
|
||||
return value.raw;
|
||||
} else if (value instanceof Date) {
|
||||
return new Date(Math.ceil(value.getTime() / 1000) * 1000);
|
||||
} else if (value instanceof U64) {
|
||||
} else if (value instanceof UInt) {
|
||||
value = value.toBn();
|
||||
} else if (isU8a(value)) {
|
||||
value = u8aToBn(value.subarray(0, BITLENGTH / 8), true);
|
||||
@@ -40,6 +40,10 @@ export default class Moment extends Base<Date> {
|
||||
);
|
||||
}
|
||||
|
||||
get bitLength (): UIntBitLength {
|
||||
return BITLENGTH;
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
return BITLENGTH / 8;
|
||||
}
|
||||
|
||||
@@ -55,4 +55,10 @@ describe('Type', () => {
|
||||
).toString()
|
||||
).toEqual('Proposal'); // eslint-disable-line
|
||||
});
|
||||
|
||||
it('unwraps compact', () => {
|
||||
expect(
|
||||
new Type('<T::Balance as HasCompact>::Type').toString()
|
||||
).toEqual('Compact<Balance>');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,7 +6,7 @@ import Text from './Text';
|
||||
|
||||
type Mapper = (value: string) => string;
|
||||
|
||||
const ALLOWED_BOXES = ['Option', 'Vec'];
|
||||
const ALLOWED_BOXES = ['Compact', 'Option', 'Vec'];
|
||||
|
||||
// This is a extended version of String, specifically to handle types. Here we rely full on
|
||||
// what string provides us, however we also "tweak" the types received from the runtime, i.e.
|
||||
@@ -37,6 +37,8 @@ export default class Type extends Text {
|
||||
// HACK(ery) Take the types and tweak them (slightly?) for consistency
|
||||
private _cleanupTypes (): Type {
|
||||
const mappings: Array<Mapper> = [
|
||||
// <T::Balance as HasCompact>
|
||||
this._cleanupCompact(),
|
||||
// Remove all the trait prefixes
|
||||
this._removeTraits(),
|
||||
// remove PairOf<T> -> (T, T)
|
||||
@@ -90,6 +92,24 @@ export default class Type extends Text {
|
||||
};
|
||||
}
|
||||
|
||||
private _cleanupCompact (): Mapper {
|
||||
return (value: string): string => {
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
if (value[index] !== '<') {
|
||||
continue;
|
||||
}
|
||||
|
||||
const end = this._findClosing(value, index + 1) - 14;
|
||||
|
||||
if (value.substr(end, 14) === ' as HasCompact') {
|
||||
value = `Compact<${value.substr(index + 1, end - index - 1)}>`;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
}
|
||||
|
||||
private _removeGenerics (): Mapper {
|
||||
return (value: string): string => {
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
@@ -139,8 +159,10 @@ export default class Type extends Text {
|
||||
.replace(/T::/g, '')
|
||||
// `system::` with `` - basically we find `<T as system::Trait>`
|
||||
.replace(/system::/g, '')
|
||||
// replace `<T as Trait>::` (possibly sanitiused just above)
|
||||
.replace(/<T as Trait>::/g, '');
|
||||
// replace `<T as Trait>::` (possibly sanitised just above)
|
||||
.replace(/<T as Trait>::/g, '')
|
||||
// replace `<...>::Type`
|
||||
.replace(/::Type/g, '');
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
+39
-20
@@ -4,6 +4,9 @@
|
||||
|
||||
import BN from 'bn.js';
|
||||
|
||||
import BlockNumber from '../BlockNumber';
|
||||
import Moment from '../Moment';
|
||||
import U32 from '../U32';
|
||||
import Compact from './Compact';
|
||||
import UInt from './UInt';
|
||||
|
||||
@@ -90,27 +93,43 @@ describe('Compact', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('has the correct encodedLength for constructor values (default)', () => {
|
||||
expect(
|
||||
new Compact(0xfffffff9, 64).encodedLength
|
||||
).toEqual(9);
|
||||
describe('constructor', () => {
|
||||
it('has the correct bitLength for constructor values (BlockNumber)', () => {
|
||||
expect(
|
||||
new Compact(BlockNumber, 0xfffffff9).bitLength
|
||||
).toEqual(64);
|
||||
});
|
||||
|
||||
it('has the correct encodedLength for constructor values (BlockNumber)', () => {
|
||||
expect(
|
||||
new Compact(BlockNumber, 0xfffffff9).encodedLength
|
||||
).toEqual(9);
|
||||
});
|
||||
|
||||
it('has the correct encodedLength for constructor values (u32)', () => {
|
||||
expect(
|
||||
new Compact(U32, 0xffff9).encodedLength
|
||||
).toEqual(4);
|
||||
});
|
||||
|
||||
it('constructs properly via U8a as U32', () => {
|
||||
expect(
|
||||
new Compact(U32, new Uint8Array([254, 255, 3, 0])).toBn()
|
||||
).toEqual(new BN(0xffff));
|
||||
});
|
||||
|
||||
it('constructs properly via number as Moment', () => {
|
||||
expect(
|
||||
new Compact(Moment, 1537968546).toString()
|
||||
).toEqual('2018-09-26T13:29:06.000Z');
|
||||
});
|
||||
});
|
||||
|
||||
it('has the correct encodedLength for constructor values (u32)', () => {
|
||||
expect(
|
||||
new Compact(0xfffffff9, 32).encodedLength
|
||||
).toEqual(5);
|
||||
});
|
||||
|
||||
it('constructs properly via U8a', () => {
|
||||
expect(
|
||||
new Compact(new Uint8Array([254, 255, 3, 0])).raw
|
||||
).toEqual(new BN(0xffff));
|
||||
});
|
||||
|
||||
it('correctly adds the length prefix', () => {
|
||||
expect(
|
||||
Compact.addLengthPrefix(Uint8Array.from([12, 13]))
|
||||
).toEqual(Uint8Array.from([2 << 2, 12, 13]));
|
||||
describe('helpers', () => {
|
||||
it('correctly adds the length prefix', () => {
|
||||
expect(
|
||||
Compact.addLengthPrefix(Uint8Array.from([12, 13]))
|
||||
).toEqual(Uint8Array.from([2 << 2, 12, 13]));
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -3,9 +3,11 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { bnToBn, bnToU8a, isU8a, u8aToBn, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
import { bnToBn, bnToU8a, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { AnyNumber, AnyU8a } from '../types';
|
||||
import { AnyNumber, Constructor } from '../types';
|
||||
import Base from './Base';
|
||||
import Moment from '../Moment';
|
||||
import UInt, { UIntBitLength } from './UInt';
|
||||
|
||||
export const DEFAULT_LENGTH_BITS = 32;
|
||||
@@ -29,9 +31,17 @@ const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1);
|
||||
// nn nn nn 11 [ / zz zz zz zz ]{4 + n}
|
||||
//
|
||||
// Note: we use *LOW BITS* of the LSB in LE encoding to encode the 2 bit key.
|
||||
export default class Compact extends UInt {
|
||||
constructor (value?: AnyU8a | AnyNumber, bitLength: UIntBitLength = DEFAULT_LENGTH_BITS, isHexJson: boolean = false) {
|
||||
super(Compact.decodeCompact(value, bitLength), bitLength, isHexJson);
|
||||
export default class Compact extends Base<UInt> {
|
||||
constructor (Type: Constructor<UInt | Moment>, value: AnyNumber = 0) {
|
||||
super(Compact.decodeCompact(Type, value));
|
||||
}
|
||||
|
||||
static with (Type: Constructor<UInt | Moment>): Constructor<Compact> {
|
||||
return class extends Compact {
|
||||
constructor (value?: any) {
|
||||
super(Type, value);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,13 +56,22 @@ export default class Compact extends UInt {
|
||||
);
|
||||
}
|
||||
|
||||
static decodeCompact (value: AnyU8a | AnyNumber | undefined, bitLength: UIntBitLength): any {
|
||||
if (isU8a(value)) {
|
||||
const [, length] = Compact.decodeU8a(value, bitLength);
|
||||
static decodeCompact (Type: Constructor<UInt | Moment>, value: AnyNumber): Moment | UInt {
|
||||
if (isString(value)) {
|
||||
return new Type(
|
||||
isHex(value)
|
||||
? hexToBn(value)
|
||||
: new BN(value, 10)
|
||||
);
|
||||
} else if (isNumber(value) || isBn(value)) {
|
||||
return new Type(bnToBn(value));
|
||||
} else if (isU8a(value)) {
|
||||
const [, _value] = Compact.decodeU8a(value, new Type(0).bitLength);
|
||||
|
||||
return length;
|
||||
return new Type(_value);
|
||||
}
|
||||
return value as any;
|
||||
|
||||
return new Type(value.toBn());
|
||||
}
|
||||
|
||||
static decodeU8a (_input: Uint8Array | string, bitLength: UIntBitLength): [number, BN] {
|
||||
@@ -93,11 +112,35 @@ export default class Compact extends UInt {
|
||||
);
|
||||
}
|
||||
|
||||
get bitLength (): UIntBitLength {
|
||||
return this.raw.bitLength;
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
toBn (): any {
|
||||
return this.raw.toBn();
|
||||
}
|
||||
|
||||
toHex (): any {
|
||||
return this.raw.toHex();
|
||||
}
|
||||
|
||||
toJSON (): any {
|
||||
return this.raw.toJSON();
|
||||
}
|
||||
|
||||
toNumber (): number {
|
||||
return this.raw.toNumber();
|
||||
}
|
||||
|
||||
toString (): string {
|
||||
return this.raw.toString();
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
return Compact.encodeU8a(this.raw, this._bitLength);
|
||||
return Compact.encodeU8a(this.raw.toBn(), this.bitLength);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +47,10 @@ export default class UInt extends Base<BN> {
|
||||
return bnToBn(value);
|
||||
}
|
||||
|
||||
get bitLength (): UIntBitLength {
|
||||
return this._bitLength;
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
return this._bitLength / 8;
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ describe('typeSplit', () => {
|
||||
it('checks for unclosed vec', () => {
|
||||
expect(
|
||||
() => typeSplit('Text, Vec<u64')
|
||||
).toThrow(/Invalid Vector/);
|
||||
).toThrow(/Invalid Compact\/Vector/);
|
||||
});
|
||||
|
||||
it('checks for unclosed tuple', () => {
|
||||
@@ -63,15 +63,23 @@ describe('getTypeValue', () => {
|
||||
|
||||
it('returns a type structure', () => {
|
||||
expect(
|
||||
getTypeDef('(u32, Vec<u64>, (Text, Vec<(Bool, u128)>))')
|
||||
getTypeDef('(u32, Compact<u32>, Vec<u64>, (Text, Vec<(Bool, u128)>))')
|
||||
).toEqual({
|
||||
info: TypeDefInfo.Tuple,
|
||||
type: '(u32, Vec<u64>, (Text, Vec<(Bool, u128)>))',
|
||||
type: '(u32, Compact<u32>, Vec<u64>, (Text, Vec<(Bool, u128)>))',
|
||||
sub: [
|
||||
{
|
||||
info: TypeDefInfo.Plain,
|
||||
type: 'u32'
|
||||
},
|
||||
{
|
||||
info: TypeDefInfo.Compact,
|
||||
type: 'Compact<u32>',
|
||||
sub: {
|
||||
info: TypeDefInfo.Plain,
|
||||
type: 'u32'
|
||||
}
|
||||
},
|
||||
{
|
||||
info: TypeDefInfo.Vector,
|
||||
type: 'Vec<u64>',
|
||||
|
||||
@@ -7,10 +7,13 @@ import { assert } from '@polkadot/util';
|
||||
import { Constructor } from '../types';
|
||||
import Text from '../Text';
|
||||
import Base from './Base';
|
||||
import Compact from './Compact';
|
||||
import Tuple from './Tuple';
|
||||
import UInt from './UInt';
|
||||
import Vector from './Vector';
|
||||
|
||||
export enum TypeDefInfo {
|
||||
Compact,
|
||||
Plain,
|
||||
Tuple,
|
||||
Vector
|
||||
@@ -50,12 +53,12 @@ export function typeSplit (type: string): Array<string> {
|
||||
break;
|
||||
|
||||
case '<':
|
||||
// inc vec depth
|
||||
// inc compact/vec depth
|
||||
vDepth++;
|
||||
break;
|
||||
|
||||
case '>':
|
||||
// dec vec depth
|
||||
// dec compact/vec depth
|
||||
vDepth--;
|
||||
break;
|
||||
|
||||
@@ -65,7 +68,7 @@ export function typeSplit (type: string): Array<string> {
|
||||
}
|
||||
|
||||
assert(tDepth === 0, `Invalid Tuple in ${type}`);
|
||||
assert(vDepth === 0, `Invalid Vector in ${type}`);
|
||||
assert(vDepth === 0, `Invalid Compact/Vector in ${type}`);
|
||||
|
||||
// the final leg of the journey
|
||||
result.push(type.substr(start, type.length - start).trim());
|
||||
@@ -90,6 +93,14 @@ export function getTypeDef (_type: Text | string): TypeDef {
|
||||
value.sub = innerTypes.map((inner) =>
|
||||
getTypeDef(inner)
|
||||
);
|
||||
} else if (type.substr(0, 8) === 'Compact<') {
|
||||
assert(type[type.length - 1] === '>', `Expected Compact wrapped with <>`);
|
||||
|
||||
// strip wrapping Compact<>
|
||||
const subType = type.substr(8, type.length - 8 - 1);
|
||||
|
||||
value.info = TypeDefInfo.Compact;
|
||||
value.sub = getTypeDef(subType);
|
||||
} else if (type.substr(0, 4) === 'Vec<') {
|
||||
assert(type[type.length - 1] === '>', `Expected Vec wrapped with <>`);
|
||||
|
||||
@@ -117,6 +128,14 @@ export function getTypeClass (value: TypeDef): Constructor {
|
||||
return result;
|
||||
}, {} as { [index: string]: Constructor })
|
||||
);
|
||||
} else if (value.info === TypeDefInfo.Compact) {
|
||||
if (!value.sub || Array.isArray(value.sub)) {
|
||||
throw new Error(`Expected subtype for Compact`);
|
||||
}
|
||||
|
||||
return Compact.with(
|
||||
getTypeClass(value.sub) as Constructor<UInt>
|
||||
);
|
||||
} else if (value.info === TypeDefInfo.Vector) {
|
||||
if (!value.sub || Array.isArray(value.sub)) {
|
||||
throw new Error(`Expected subtype for Vector`);
|
||||
|
||||
@@ -19,11 +19,6 @@ export { default as BlockNumber } from './BlockNumber';
|
||||
// NOTE We are exporting Bool as bool to match with Rust
|
||||
export { default as bool } from './Bool';
|
||||
export { default as Bytes } from './Bytes';
|
||||
export { default as Compact16 } from './Compact16';
|
||||
export { default as Compact32 } from './Compact32';
|
||||
export { default as Compact64 } from './Compact64';
|
||||
export { default as Compact128 } from './Compact128';
|
||||
export { default as Compact256 } from './Compact256';
|
||||
export { default as Data } from './Data';
|
||||
// NOTE Only used internally via Header
|
||||
// export { default as Digest } from './Digest';
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user