From 7e3da636ea655897c12f769f942a5a78e8ea31a5 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 11 Aug 2019 16:09:50 +0200 Subject: [PATCH] Cleanup, simplify, remove Fallback (type injection) (#1294) --- packages/api/src/Base.ts | 11 ++++ packages/types/src/codec/Option.ts | 2 +- packages/types/src/codec/Vec.ts | 6 --- packages/types/src/codec/VecFixed.ts | 4 -- packages/types/src/codec/createType.ts | 54 +++++++------------ packages/types/src/codec/typeRegistry.ts | 5 +- packages/types/src/injector.ts | 6 --- packages/types/src/primitive/Generic/Event.ts | 3 +- packages/types/src/types.ts | 2 - 9 files changed, 33 insertions(+), 60 deletions(-) diff --git a/packages/api/src/Base.ts b/packages/api/src/Base.ts index 3db2fdd550..89a6584a43 100644 --- a/packages/api/src/Base.ts +++ b/packages/api/src/Base.ts @@ -566,6 +566,17 @@ export default abstract class ApiBase { 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); diff --git a/packages/types/src/codec/Option.ts b/packages/types/src/codec/Option.ts index f10328f9f7..054d8a3a89 100644 --- a/packages/types/src/codec/Option.ts +++ b/packages/types/src/codec/Option.ts @@ -34,7 +34,7 @@ export default class Option extends Base { 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)) { diff --git a/packages/types/src/codec/Vec.ts b/packages/types/src/codec/Vec.ts index 83fdcc6da7..2238735be7 100644 --- a/packages/types/src/codec/Vec.ts +++ b/packages/types/src/codec/Vec.ts @@ -58,12 +58,6 @@ export default class Vec extends AbstractArray { public constructor (value?: any[]) { super(Type, value); } - - // @ts-ignore - public static Fallback = Type.Fallback - // @ts-ignore - ? Vec.with(Type.Fallback) - : undefined; }; } diff --git a/packages/types/src/codec/VecFixed.ts b/packages/types/src/codec/VecFixed.ts index ec33ac7983..970ee43b36 100644 --- a/packages/types/src/codec/VecFixed.ts +++ b/packages/types/src/codec/VecFixed.ts @@ -47,10 +47,6 @@ export default class VecFixed extends AbstractArray { public constructor (value?: any[]) { super(Type, length, value); } - - public static Fallback = Type.Fallback - ? VecFixed.with(Type.Fallback, length) - : undefined; }; } diff --git a/packages/types/src/codec/createType.ts b/packages/types/src/codec/createType.ts index fb5d697d79..27c896c909 100644 --- a/packages/types/src/codec/createType.ts +++ b/packages/types/src/codec/createType.ts @@ -209,7 +209,7 @@ export function getTypeClassArray (defs: TypeDef[]): (InterfaceTypes)[] { } // Returns the type Class for construction -export function getTypeClass (value: TypeDef, Fallback?: Constructor): Constructor { +export function getTypeClass (value: TypeDef): Constructor { const Type = getTypeRegistry().get(value.type); if (Type) { @@ -293,50 +293,34 @@ export function getTypeClass (value: TypeDef, Fallback? return ClassOf('Null') as unknown as Constructor; } - 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 (Type: Constructor>, params: any[] = [], isPedantic?: boolean): FromReg { - try { - const created = new Type(...params); - const [value] = params; + 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()) - ); + // 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>, params, isPedantic); - } - - console.warn(`${created.toRawType()}:: Input doesn't match output, received ${inHex}, created ${crHex}`); - } + if (!hasMatch) { + console.warn(`${created.toRawType()}:: Input doesn't match output, received ${inHex}, created ${crHex}`); } - - return created; - } catch (error) { - if (Type.Fallback) { - return initType(Type.Fallback as Constructor>, params, isPedantic); - } - - throw error; } + + return created; } // An unsafe version of the `createType` below. It's unsafe because the `type` diff --git a/packages/types/src/codec/typeRegistry.ts b/packages/types/src/codec/typeRegistry.ts index 22d3cec946..0ff960cd05 100644 --- a/packages/types/src/codec/typeRegistry.ts +++ b/packages/types/src/codec/typeRegistry.ts @@ -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); } diff --git a/packages/types/src/injector.ts b/packages/types/src/injector.ts index e7ea54145e..6a2fc91255 100644 --- a/packages/types/src/injector.ts +++ b/packages/types/src/injector.ts @@ -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'; @@ -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').Fallback = ClassOf('Vec'); } injectTypes(); diff --git a/packages/types/src/primitive/Generic/Event.ts b/packages/types/src/primitive/Generic/Event.ts index 2db7b629af..9310fe903c 100644 --- a/packages/types/src/primitive/Generic/Event.ts +++ b/packages/types/src/primitive/Generic/Event.ts @@ -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> = {}; @@ -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 => getTypeClass(typeDef, Unconstructable.with(typeDef))); + const Types = typeDef.map((typeDef): Constructor => getTypeClass(typeDef)); EventTypes[eventIndex.toString()] = class extends EventData { public constructor (value: Uint8Array) { diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index ef5a3018c2..3ebae9a4d3 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -123,8 +123,6 @@ export interface IHash extends U8a { } export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a'; export interface Constructor { - Fallback?: Constructor; - // eslint-disable-next-line @typescript-eslint/no-explicit-any new(...value: any[]): T; }