Internal adjustments (part 3) (#4135)
This commit is contained in:
@@ -41,7 +41,13 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> 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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -82,7 +82,13 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
* @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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -59,7 +59,13 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
* @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 {
|
||||
|
||||
@@ -5,8 +5,16 @@ import type { Codec, Constructor, Registry } from '../../types';
|
||||
|
||||
import { isFunction, u8aToHex } from '@polkadot/util';
|
||||
|
||||
function identityZip <T, E> (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 <T, E> (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 <T extends Codec = Codec, E = T> (registry: Registry, u8a: Uint8Array, types: Constructor | Constructor[] | { [index: string]: Constructor }, count?: number, zip: (key: string, value: T) => E = identityZip): [E[], number] {
|
||||
export function decodeU8a <T extends Codec = Codec, E = T> (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 <T extends Codec = Codec, E = T> (registry: Registry,
|
||||
count = count || Types.length;
|
||||
|
||||
const result = new Array<E>(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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user