From 44d241f489270279de5c26890eba8c1ab1fad867 Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 1 Nov 2021 09:03:13 +0100 Subject: [PATCH] Internal adjustments (part 3) (#4135) --- packages/types/src/codec/AbstractArray.ts | 8 +++- packages/types/src/codec/Struct.ts | 9 ++++- packages/types/src/codec/Tuple.ts | 8 +++- packages/types/src/codec/VecFixed.ts | 8 +++- packages/types/src/codec/utils/decodeU8a.ts | 41 ++++++++++++--------- 5 files changed, 53 insertions(+), 21 deletions(-) diff --git a/packages/types/src/codec/AbstractArray.ts b/packages/types/src/codec/AbstractArray.ts index 42a0d1cb15..cac1e5fbfc 100644 --- a/packages/types/src/codec/AbstractArray.ts +++ b/packages/types/src/codec/AbstractArray.ts @@ -41,7 +41,13 @@ export abstract class AbstractArray extends Array implements public get encodedLength (): number { // We need to loop through all entries since they may have a variable length themselves, // e.g. when a Vec or Compact is contained withing, it has a variable length based on data - return this.reduce((total, e) => total + e.encodedLength, compactToU8a(this.length).length); + let total = compactToU8a(this.length).length; + + for (let i = 0; i < this.length; i++) { + total += this[i].encodedLength; + } + + return total; } /** diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 7941af1043..d2699296a7 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -202,7 +202,14 @@ export class Struct< * @description The length of the value when encoded as a Uint8Array */ public get encodedLength (): number { - return this.toArray().reduce((length, e) => length + e.encodedLength, 0); + const arr = this.toArray(); + let total = 0; + + for (let i = 0; i < arr.length; i++) { + total += arr[i].encodedLength; + } + + return total; } /** diff --git a/packages/types/src/codec/Tuple.ts b/packages/types/src/codec/Tuple.ts index 218feae081..87917e4a15 100644 --- a/packages/types/src/codec/Tuple.ts +++ b/packages/types/src/codec/Tuple.ts @@ -82,7 +82,13 @@ export class Tuple extends AbstractArray implements ITuple { * @description The length of the value when encoded as a Uint8Array */ public override get encodedLength (): number { - return this.reduce((total, e) => total + e.encodedLength, 0); + let total = 0; + + for (let i = 0; i < this.length; i++) { + total += this[i].encodedLength; + } + + return total; } /** diff --git a/packages/types/src/codec/VecFixed.ts b/packages/types/src/codec/VecFixed.ts index e3e199df9a..123610e94a 100644 --- a/packages/types/src/codec/VecFixed.ts +++ b/packages/types/src/codec/VecFixed.ts @@ -59,7 +59,13 @@ export class VecFixed extends AbstractArray { * @description The length of the value when encoded as a Uint8Array */ public override get encodedLength (): number { - return this.reduce((total, e) => total + e.encodedLength, 0); + let total = 0; + + for (let i = 0; i < this.length; i++) { + total += this[i].encodedLength; + } + + return total; } public override toU8a (): Uint8Array { diff --git a/packages/types/src/codec/utils/decodeU8a.ts b/packages/types/src/codec/utils/decodeU8a.ts index ec4638f021..11113f8f83 100644 --- a/packages/types/src/codec/utils/decodeU8a.ts +++ b/packages/types/src/codec/utils/decodeU8a.ts @@ -5,8 +5,16 @@ import type { Codec, Constructor, Registry } from '../../types'; import { isFunction, u8aToHex } from '@polkadot/util'; -function identityZip (key: string, value: T): E { - return value as unknown as E; +function formatFailure (error: Error, key: string, type: string, u8a: Uint8Array): string { + return `decodeU8a: failed at ${u8aToHex(u8a)}…${key ? ` on ${key}` : ''}${type ? `: ${type}` : ''}:: ${error.message}`; +} + +function getRawType (registry: Registry, Type: Constructor): string { + try { + return new Type(registry).toRawType(); + } catch { + return ''; + } } /** @@ -17,7 +25,7 @@ function identityZip (key: string, value: T): E { * @param result - The result array (will be returned with values pushed) * @param types - The array of Constructor to decode the U8a against. */ -export function decodeU8a (registry: Registry, u8a: Uint8Array, types: Constructor | Constructor[] | { [index: string]: Constructor }, count?: number, zip: (key: string, value: T) => E = identityZip): [E[], number] { +export function decodeU8a (registry: Registry, u8a: Uint8Array, types: Constructor | Constructor[] | { [index: string]: Constructor }, count?: number, zip?: (key: string, value: T) => E): [E[], number] { const [Type, Types, keys]: [Constructor | null, Constructor[], string[]] = isFunction(types) ? [types, [], []] : Array.isArray(types) @@ -27,26 +35,25 @@ export function decodeU8a (registry: Registry, count = count || Types.length; const result = new Array(count); - let decodedLength = 0; + let offset = 0; for (let i = 0; i < count; i++) { try { - const value = new (Type || Types[i])(registry, u8a.subarray(decodedLength)); + const value = new (Type || Types[i])(registry, u8a.subarray(offset)); - result[i] = zip(keys[i], value as T); - decodedLength += value.initialU8aLength || value.encodedLength; + offset += value.initialU8aLength || value.encodedLength; + result[i] = zip + ? zip(keys[i], value as T) + : value as unknown as E; } catch (error) { - let rawType: string; - - try { - rawType = new (Type || Types[i])(registry).toRawType(); - } catch { - rawType = ''; - } - - throw new Error(`decodeU8a: failed at ${u8aToHex(u8a.subarray(decodedLength, decodedLength + 8))}…${keys[i] ? ` on ${keys[i]}` : ''}${rawType ? `: ${rawType}` : ''}:: ${(error as Error).message}`); + throw new Error(formatFailure( + error as Error, + keys[i], + getRawType(registry, Type || Types[i]), + u8a.subarray(offset, offset + 8) + )); } } - return [result, decodedLength]; + return [result, offset]; }