From a06db4cfb94198db2a0352efb479d4b8e442e880 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Sun, 28 Jul 2019 10:45:26 +0200 Subject: [PATCH] Swap isPedantic to warn (with fallback) (#1221) * Swap isPedantic to warn (with fallback) * Remove extra instanceof * One less check (toU8a implies codec) * Comment typo * Handle fallback grecefully --- packages/types/src/codec/createType.ts | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/packages/types/src/codec/createType.ts b/packages/types/src/codec/createType.ts index 0f34632e46..40e7f2f820 100644 --- a/packages/types/src/codec/createType.ts +++ b/packages/types/src/codec/createType.ts @@ -306,26 +306,33 @@ export function getTypeClass (value: TypeDef, 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; - // 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 && !value.isEmpty) { + // 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 || ( - (value instanceof ClassOf('StorageData')) && // input is from storage - (created instanceof Uint8Array) // we are a variable-length structure + created instanceof Uint8Array // strip the input length ? (value.toU8a(true).toString() === created.toU8a().toString()) - // compare raw + // compare raw. without additions : (value.toU8a(true).toString() === created.toU8a(true).toString()) ); - assert(hasMatch, `Input doesn't match output, received ${inHex}, created ${crHex}`); + 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}`); + } } return created;