BlockNumber, Nonce -> u32 (#1284)

* BlockNumber -> u32

* Interface types extend, not re-use

* Merge master, rebuild

* Merge typo

* Lazy create struct types (extended definitions)

* Cleanup tests for Nonce u32

* Remove polkadot u64 BlockNumber (substrate default)

* Warn on type dup & still clear

* Allow type overrides to work at any point
This commit is contained in:
Jaco Greeff
2019-08-11 15:07:00 +02:00
committed by GitHub
parent 1aa7899a7a
commit e5cc6836eb
17 changed files with 126 additions and 84 deletions
+1 -1
View File
@@ -110,7 +110,7 @@ describe('Compact', (): void => {
it('has the correct bitLength for constructor values (BlockNumber)', (): void => {
expect(
new Compact(ClassOf('BlockNumber'), 0xfffffff9).bitLength()
).toEqual(64);
).toEqual(32);
});
it('has the correct encodedLength for constructor values (string BlockNumber)', (): void => {
+5 -5
View File
@@ -194,8 +194,8 @@ describe('Struct', (): void => {
expect(
new Struct({
blockNumber: ClassOf('Option<BlockNumber>')
}, { blockNumber: '0x1234567890abcdef' }).toString()
).toEqual('{"blockNumber":"0x1234567890abcdef"}');
}, { blockNumber: '0x0000000010abcdef' }).toString()
).toEqual('{"blockNumber":279694831}');
});
it('generates sane toRawType', (): void => {
@@ -212,9 +212,9 @@ describe('Struct', (): void => {
).toEqual(JSON.stringify({
accountId: 'AccountId',
balanceCompact: 'Compact<Balance>', // Override in Uint
blockNumber: 'u64',
compactNumber: 'Compact<u64>',
optionNumber: 'Option<u64>',
blockNumber: 'u32',
compactNumber: 'Compact<u32>',
optionNumber: 'Option<u32>',
counter: 'u32',
vector: 'Vec<AccountId>'
}));
+5 -4
View File
@@ -14,6 +14,7 @@ import { ClassOf } from '../codec/createType';
import Call from '../primitive/Generic/Call';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
import U128 from '../primitive/U128';
import Tuple from './Tuple';
describe('Tuple', (): void => {
@@ -111,14 +112,14 @@ describe('Tuple', (): void => {
describe('toRawType', (): void => {
it('generates sane value with array types', (): void => {
expect(
new Tuple([U32, ClassOf('BlockNumber')]).toRawType()
).toEqual('(u32,u64)');
new Tuple([U128, ClassOf('BlockNumber')]).toRawType()
).toEqual('(u128,u32)');
});
it('generates sane value with object types', (): void => {
expect(
new Tuple({ number: U32, blockNumber: ClassOf('BlockNumber') }).toRawType()
).toEqual('(u32,u64)');
new Tuple({ number: U128, blockNumber: ClassOf('BlockNumber') }).toRawType()
).toEqual('(u128,u32)');
});
});
});
+13 -3
View File
@@ -4,11 +4,21 @@
import '../injector';
import { Constructor } from '../types';
import { TypeRegistry } from './typeRegistry';
import Struct from '../codec/Struct';
import Text from '../primitive/Text';
import U32 from '../primitive/U32';
// Copied from interfacesTs
function isChildClass (Parent: Constructor<any>, Child?: Constructor<any>): boolean {
return Child
// eslint-disable-next-line no-prototype-builtins
? Parent === Child || Parent.isPrototypeOf(Child)
: false;
}
describe('TypeRegistry', (): void => {
let registry: TypeRegistry;
@@ -27,7 +37,7 @@ describe('TypeRegistry', (): void => {
it('can register type with a different name', (): void => {
registry.register('TextRenamed', Text);
expect(registry.get('TextRenamed')).toBe(Text);
expect(isChildClass(Text, registry.get('TextRenamed'))).toBe(true);
});
describe('object registration', (): void => {
@@ -36,8 +46,8 @@ describe('TypeRegistry', (): void => {
Text,
U32Renamed: U32
});
expect(registry.get('Text')).toBe(Text);
expect(registry.get('U32Renamed')).toBe(U32);
expect(isChildClass(Text, registry.get('Text'))).toBe(true);
expect(isChildClass(U32, registry.get('U32Renamed'))).toBe(true);
});
it('can create types from string', (): void => {
+51 -8
View File
@@ -8,6 +8,7 @@ import { Codec, Constructor } from '../types';
import { TypeDef, TypeDefInfo } from './types';
import createType, { createClass, createTypeUnsafe, getTypeClass, getTypeDef, typeSplit, ClassOf } from './createType';
import getTypeRegistry from './typeRegistry';
import CodecSet from './Set';
describe('typeSplit', (): void => {
@@ -223,10 +224,10 @@ describe('createClass', (): void => {
});
it('should return equivalents for Bytes & Vec<u8>', (): void => {
const a = createClass('Vec<u8>');
const b = createClass('Bytes');
const A = createClass('Vec<u8>');
const B = createClass('Bytes');
expect(a).toBe(b);
expect(new A() instanceof B).toBe(true);
});
});
@@ -288,17 +289,59 @@ describe('createType', (): void => {
});
it('instanceof should work (complex type)', (): void => {
const complexType = '{"balance":"Balance","account_id":"AccountId","log":"(u64, u32)","fromSrml":"Gas"}';
getTypeRegistry().register({
TestComplex: {
balance: 'Balance',
accountId: 'AccountId',
log: '(u64, u32)',
fromSrml: 'Gas'
}
});
const value = createTypeUnsafe(complexType, [{
const value = createTypeUnsafe('TestComplex', [{
balance: 123,
accountId: '',
accountId: '0x1234567812345678123456781234567812345678123456781234567812345678',
log: [456, 789],
fromSrml: 0
}]);
const ComplexType = createClass(complexType);
expect(value instanceof ComplexType).toBe(true);
expect(value instanceof createClass('TestComplex')).toBe(true);
});
it('allows for re-registration of a type', (): void => {
const balDef = createType('Balance');
expect(balDef instanceof ClassOf('Balance'));
expect(balDef.bitLength()).toEqual(128);
getTypeRegistry().register({ Balance: 'u32' });
const balu32 = createType('Balance');
expect(balu32 instanceof ClassOf('Balance'));
expect(balu32.bitLength()).toEqual(32);
});
it('allows for re-registration of a type (affecting derives)', (): void => {
getTypeRegistry().register({
Balance: 'u128',
TestComplex: {
balance: 'Balance',
accountId: 'AccountId',
log: '(u64, u32)',
fromSrml: 'Gas'
}
});
const cmpDef: any = createTypeUnsafe('TestComplex');
expect(cmpDef.balance.bitLength()).toEqual(128);
getTypeRegistry().register({ Balance: 'u32' });
const cmpu32: any = createTypeUnsafe('TestComplex');
expect(cmpu32.balance.bitLength()).toEqual(32);
});
});
});
+3 -10
View File
@@ -5,7 +5,6 @@
import { Codec, Constructor, InterfaceTypes } from '../types';
import { TypeDef, TypeDefInfo, TypeDefExtVecFixed } from './types';
import memoizee from 'memoizee';
import { assert } from '@polkadot/util';
import { InterfaceRegistry } from '../interfaceRegistry';
@@ -173,18 +172,12 @@ export function getTypeDef (_type: string, name?: string): TypeDef {
return value;
}
// Memoized helper of the `createClass` function below
const memoizedCreateClass = memoizee(<T extends Codec = Codec, K extends string = string>(
type: K
): Constructor<FromReg<T, K>> => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return getTypeClass<FromReg<T, K>>(getTypeDef(type));
}, { length: 1 });
export function createClass<T extends Codec = Codec, K extends string = string> (
type: K
): Constructor<FromReg<T, K>> {
return memoizedCreateClass(type);
// return memoizedCreateClass(type);
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return getTypeClass<FromReg<T, K>>(getTypeDef(type));
}
// An unsafe version of the `createType` below. It's unsafe because the `type`
+14 -16
View File
@@ -37,25 +37,23 @@ export class TypeRegistry {
}
}
private registerObject (obj: RegistryTypes, overwrite: boolean = true): void {
private registerObject (obj: RegistryTypes): void {
Object.entries(obj).forEach(([name, type]): void => {
if (overwrite || !this.get(name)) {
if (isFunction(type)) {
// This _looks_ a bit funny, but `typeof Clazz === 'function'
this._classes.set(name, type);
} else {
// when registering, remove the old value
if (this._classes.has(name)) {
this._classes.delete(name);
}
if (isFunction(type)) {
// This _looks_ a bit funny, but `typeof Clazz === 'function'
this._classes.set(name, type);
} else {
const def = isString(type)
? type
: JSON.stringify(type);
// We only create types on-demand, so just register the definition
this._definitions.set(name, (
isString(type)
? type
: JSON.stringify(type)
));
if (this._classes.has(name)) {
console.warn(`The type '${name}' is already existing as a class, re-registering definition`);
this._classes.delete(name);
}
this._definitions.set(name, def);
}
});
}
@@ -12,7 +12,7 @@ export default {
Balance: 'u128',
BalanceOf: 'Balance',
Block: 'GenericBlock',
BlockNumber: 'u64',
BlockNumber: 'u32',
Call: 'GenericCall',
ConsensusEngineId: 'GenericConsensusEngineId',
Digest: 'GenericDigest',
@@ -28,7 +28,7 @@ export default {
extrinsicsRoot: 'Hash',
digest: 'Digest'
},
Index: 'u64',
Index: 'u32',
Justification: 'Bytes',
KeyValue: '(StorageKey, StorageData)',
KeyTypeId: 'u32',
@@ -29,8 +29,8 @@ export type BalanceOf = Balance;
/** GenericBlock */
export type Block = GenericBlock;
/** u64 */
export type BlockNumber = u64;
/** u32 */
export type BlockNumber = u32;
/** GenericCall */
export type Call = GenericCall;
@@ -79,8 +79,8 @@ export interface Header extends Struct {
/** GenericImmortalEra */
export type ImmortalEra = GenericImmortalEra;
/** u64 */
export type Index = u64;
/** u32 */
export type Index = u32;
/** Bytes */
export type Justification = Bytes;
@@ -23,7 +23,7 @@ describe('Block', (): void => {
JSON.stringify({
header: JSON.stringify({
parentHash: 'H256',
number: 'Compact<u64>',
number: 'Compact<u32>',
stateRoot: 'H256',
extrinsicsRoot: 'H256',
digest: JSON.stringify({