Skip to content

Commit

Permalink
Merge 3798cf8 into 54349a2
Browse files Browse the repository at this point in the history
  • Loading branch information
jacogr committed Aug 11, 2019
2 parents 54349a2 + 3798cf8 commit 707508e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 61 deletions.
11 changes: 11 additions & 0 deletions packages/api/src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,17 @@ export default abstract class ApiBase<ApiType> {
const lastBlock: SignedBlock = await this._rpcCore.chain.getBlock().toPromise();

this._extrinsicType = lastBlock.block.extrinsics[0].type;

// HACK Assume that old versions, substrate 1.x is u64 BlockNumber/Nonce
// and has the old EventRecord format. Remove this ASAP with support for
// Alex dropped
if (this._extrinsicType === 1) {
getTypeRegistry().register({
BlockNumber: 'u64',
Index: 'u64',
EventRecord: 'EventRecord0to76'
});
}
}

this._extrinsics = this.decorateExtrinsics(extrinsics, this.decorateMethod);
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/codec/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Option<T extends Codec> extends Base<T> {
return new Null();
} else if (value instanceof Option) {
return Option.decodeOption(Type, value.value);
} else if (value instanceof Type || (Type.Fallback && value instanceof Type.Fallback)) {
} else if (value instanceof Type) {
// don't re-create, use as it (which also caters for derived types)
return value;
} else if (isU8a(value)) {
Expand Down
6 changes: 0 additions & 6 deletions packages/types/src/codec/Vec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ export default class Vec<T extends Codec> extends AbstractArray<T> {
public constructor (value?: any[]) {
super(Type, value);
}

// @ts-ignore
public static Fallback = Type.Fallback
// @ts-ignore
? Vec.with(Type.Fallback)
: undefined;
};
}

Expand Down
4 changes: 0 additions & 4 deletions packages/types/src/codec/VecFixed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export default class VecFixed<T extends Codec> extends AbstractArray<T> {
public constructor (value?: any[]) {
super(Type, length, value);
}

public static Fallback = Type.Fallback
? VecFixed.with(Type.Fallback, length)
: undefined;
};
}

Expand Down
56 changes: 20 additions & 36 deletions packages/types/src/codec/createType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export function getTypeClassArray (defs: TypeDef[]): (InterfaceTypes)[] {
}

// Returns the type Class for construction
export function getTypeClass<T extends Codec = Codec> (value: TypeDef, Fallback?: Constructor<T>): Constructor<T> {
export function getTypeClass<T extends Codec = Codec> (value: TypeDef): Constructor<T> {
const Type = getTypeRegistry().get<T>(value.type);

if (Type) {
Expand Down Expand Up @@ -293,50 +293,34 @@ export function getTypeClass<T extends Codec = Codec> (value: TypeDef, Fallback?
return ClassOf('Null') as unknown as Constructor<T>;
}

if (Fallback) {
return Fallback;
}

throw new Error(`Unable to determine type from ${JSON.stringify(value)}`);
}

// Initializes a type with a value. This also checks for fallbacks and in the cases
// where isPedantic is specified (storage decoding), also check the format/structure
function initType<T extends Codec = Codec, K extends string = string> (Type: Constructor<FromReg<T, K>>, params: any[] = [], isPedantic?: boolean): FromReg<T, K> {
try {
const created = new Type(...params);
const [value] = params;

// With isPedantic, actually check that the encoding matches that supplied. This
// is much slower, but verifies that we have the correct types defined
if (isPedantic && value && value.toU8a && !value.isEmpty) {
const inHex = value.toHex(true);
const crHex = created.toHex(true);
const hasMatch = inHex === crHex || (
created instanceof Uint8Array
// strip the input length
? (value.toU8a(true).toString() === created.toU8a().toString())
// compare raw. without additions
: (value.toU8a(true).toString() === created.toU8a(true).toString())
);

if (!hasMatch) {
if (Type.Fallback) {
return initType(Type.Fallback as Constructor<FromReg<T, K>>, params, isPedantic);
}

console.warn(`${created.toRawType()}:: Input doesn't match output, received ${inHex}, created ${crHex}`);
}
}
const created = new Type(...params);
const [value] = params;

// With isPedantic, actually check that the encoding matches that supplied. This
// is much slower, but verifies that we have the correct types defined
if (isPedantic && value && value.toU8a && !value.isEmpty) {
const inHex = value.toHex(true);
const crHex = created.toHex(true);
const hasMatch = inHex === crHex || (
created instanceof Uint8Array
// strip the input length
? (value.toU8a(true).toString() === created.toU8a().toString())
// compare raw. without additions
: (value.toU8a(true).toString() === created.toU8a(true).toString())
);

return created;
} catch (error) {
if (Type.Fallback) {
return initType(Type.Fallback as Constructor<FromReg<T, K>>, params, isPedantic);
if (!hasMatch) {
console.warn(`${created.toRawType()}:: Input doesn't match output, received ${inHex}, created ${crHex}`);
}

throw error;
}

return created;
}

// An unsafe version of the `createType` below. It's unsafe because the `type`
Expand Down
5 changes: 1 addition & 4 deletions packages/types/src/codec/typeRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ export class TypeRegistry {

// NOTE If we didn't extend here, we would have strange artifacts. An example is
// Balance, with this, new Balance() instanceof u128 is true, but Balance !== u128
Type = class extends BaseType {
// ensure we carry through any fallbacks identified - since they are now lower
public static Fallback = BaseType.Fallback;
};
Type = class extends BaseType {};

this._classes.set(name, Type);
}
Expand Down
6 changes: 0 additions & 6 deletions packages/types/src/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 { ClassOf } from './codec/createType';
import getTypeRegistry from './codec/typeRegistry';
import * as definitions from './interfaces/definitions';
import * as baseTypes from './index.types';
Expand All @@ -20,11 +19,6 @@ export function injectTypes (): void {
Object.values(definitions).forEach(({ types }): void =>
registry.register(types)
);

// FIXME Register the fallbacks. The issue with this atm is that it forcibly
// creates the actual classes here, which is something we don't really want,
// we want it do be done on-demand, not up-front.
ClassOf('Vec<EventRecord>').Fallback = ClassOf('Vec<EventRecord0to76>');
}

injectTypes();
3 changes: 1 addition & 2 deletions packages/types/src/primitive/Generic/Event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import Tuple from '../../codec/Tuple';
import Metadata from '../../Metadata';
import { EventMetadata as EventMetadataV7 } from '../../Metadata/v7/Events';
import Null from '../Null';
import Unconstructable from '../Unconstructable';

const EventTypes: Record<string, Constructor<EventData>> = {};

Expand Down Expand Up @@ -121,7 +120,7 @@ export default class Event extends Struct {
const methodName = meta.name.toString();
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
const typeDef = meta.args.map((arg): TypeDef => getTypeDef(arg.toString()));
const Types = typeDef.map((typeDef): Constructor<Codec> => getTypeClass(typeDef, Unconstructable.with(typeDef)));
const Types = typeDef.map((typeDef): Constructor<Codec> => getTypeClass(typeDef));

EventTypes[eventIndex.toString()] = class extends EventData {
public constructor (value: Uint8Array) {
Expand Down
2 changes: 0 additions & 2 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ export interface IHash extends U8a { }
export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';

export interface Constructor<T = Codec> {
Fallback?: Constructor<Codec>;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
new(...value: any[]): T;
}
Expand Down

0 comments on commit 707508e

Please sign in to comment.