From 38e8ad659a9c153f71edc3a93b1efaca0d443c9b Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 28 Jul 2019 09:23:22 +0200 Subject: [PATCH] Cleanup isPedantic mismatches (#1220) * Don't assert, warn on isPedantic mismatches * Re-apply assert --- packages/types/src/codec/Linkage.ts | 7 +++++++ packages/types/src/codec/Option.ts | 8 ++++++-- packages/types/src/codec/createType.ts | 23 ++++++++++------------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/types/src/codec/Linkage.ts b/packages/types/src/codec/Linkage.ts index eb44e3486d..adcfcd98c9 100644 --- a/packages/types/src/codec/Linkage.ts +++ b/packages/types/src/codec/Linkage.ts @@ -33,6 +33,13 @@ export default class Linkage extends Struct { return this.get('next') as Option; } + /** + * @description Returns the base runtime type name for this instance + */ + public toRawType (): string { + return `Linkage<${this.next.toRawType(true)}>`; + } + /** * @description Custom toU8a which with bare mode does not return the linkage if empty */ diff --git a/packages/types/src/codec/Option.ts b/packages/types/src/codec/Option.ts index 00d55c14f4..9a46189860 100644 --- a/packages/types/src/codec/Option.ts +++ b/packages/types/src/codec/Option.ts @@ -115,8 +115,12 @@ export default class Option extends Base { /** * @description Returns the base runtime type name for this instance */ - public toRawType (): string { - return `Option<${new this._Type().toRawType()}>`; + public toRawType (isBare?: boolean): string { + const wrapped = new this._Type().toRawType(); + + return isBare + ? wrapped + : `Option<${wrapped}>`; } /** diff --git a/packages/types/src/codec/createType.ts b/packages/types/src/codec/createType.ts index 91d01780ee..0f34632e46 100644 --- a/packages/types/src/codec/createType.ts +++ b/packages/types/src/codec/createType.ts @@ -313,22 +313,19 @@ function initType (Type: Con // in pedantic mode, actually check that the encoding matches that supplied - this // is much slower, but ensures that we have a 100% grasp on the actual provided value - if (isPedantic && value && value.toHex && value.toU8a) { + if (isPedantic && value && value.toHex && value.toU8a && !value.isEmpty) { const inHex = value.toHex(true); const crHex = created.toHex(true); - - assert( - inHex === crHex || // check that the hex matches, if matching, all-ok - ( - (value instanceof ClassOf('StorageData')) && // input is from storage - (created instanceof Uint8Array) // we are a variable-length structure - // strip the input length - ? (value.toU8a(true).toString() === created.toU8a().toString()) - // compare raw - : (value.toU8a(true).toString() === created.toU8a(true).toString()) // check raw - ), - `Input doesn't match output, received ${inHex}, created ${crHex}` + const hasMatch = inHex === crHex || ( + (value instanceof ClassOf('StorageData')) && // input is from storage + (created instanceof Uint8Array) // we are a variable-length structure + // strip the input length + ? (value.toU8a(true).toString() === created.toU8a().toString()) + // compare raw + : (value.toU8a(true).toString() === created.toU8a(true).toString()) ); + + assert(hasMatch, `Input doesn't match output, received ${inHex}, created ${crHex}`); } return created;