Remove recursion on decodeU8a (#3063)
* Remove recursion on decodeU8a * Remove skipped tests * Remove extra allocation
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
import type { H256 } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, Constructor, InterfaceTypes, Registry } from '../types';
|
||||
|
||||
import { compactFromU8a, compactToU8a, hexToU8a, isHex, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { compactFromU8a, compactToU8a, isHex, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareSet, decodeU8a, typeToConstructor } from './utils';
|
||||
|
||||
@@ -67,9 +67,7 @@ function decodeSet<V extends Codec = Codec> (registry: Registry, valType: Constr
|
||||
|
||||
const ValClass = typeToConstructor(registry, valType);
|
||||
|
||||
if (isHex(value)) {
|
||||
return decodeSet(registry, ValClass, hexToU8a(value));
|
||||
} else if (isU8a(value)) {
|
||||
if (isHex(value) || isU8a(value)) {
|
||||
return decodeSetFromU8a<V>(registry, ValClass, u8aToU8a(value));
|
||||
} else if (Array.isArray(value) || value instanceof Set) {
|
||||
return decodeSetFromSet<V>(registry, ValClass, value);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import type { H256 } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, Constructor, InterfaceTypes, Registry } from '../types';
|
||||
|
||||
import { compactFromU8a, compactToU8a, hexToU8a, isHex, isObject, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { compactFromU8a, compactToU8a, isHex, isObject, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { compareMap, decodeU8a, typeToConstructor } from './utils';
|
||||
|
||||
@@ -74,9 +74,7 @@ function decodeMap<K extends Codec = Codec, V extends Codec = Codec> (registry:
|
||||
|
||||
if (!value) {
|
||||
return new Map<K, V>();
|
||||
} else if (isHex(value)) {
|
||||
return decodeMap(registry, KeyClass, ValClass, hexToU8a(value));
|
||||
} else if (isU8a(value)) {
|
||||
} else if (isU8a(value) || isHex(value)) {
|
||||
return decodeMapFromU8a<K, V>(registry, KeyClass, ValClass, u8aToU8a(value));
|
||||
} else if (value instanceof Map) {
|
||||
return decodeMapFromMap<K, V>(registry, KeyClass, ValClass, value);
|
||||
|
||||
@@ -4,16 +4,7 @@
|
||||
import type { H256 } from '../interfaces/runtime';
|
||||
import type { AnyJson, AnyU8a, IU8a, Registry } from '../types';
|
||||
|
||||
import { assert, isAscii, isU8a, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
/** @internal */
|
||||
function decodeU8a (value?: any): Uint8Array {
|
||||
if (isU8a(value)) {
|
||||
return value;
|
||||
}
|
||||
|
||||
return u8aToU8a(value);
|
||||
}
|
||||
import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
/**
|
||||
* @name Raw
|
||||
@@ -28,7 +19,7 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
public readonly registry: Registry;
|
||||
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
super(decodeU8a(value));
|
||||
super(u8aToU8a(value));
|
||||
|
||||
this.registry = registry;
|
||||
}
|
||||
@@ -92,7 +83,7 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
!this.some((value, index) => value !== other[index]);
|
||||
}
|
||||
|
||||
return this.eq(decodeU8a(other));
|
||||
return this.eq(u8aToU8a(other as any));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import type { AnyNumber, AnyString, AnyU8a, Codec, Constructor, InterfaceTypes, Registry } from '../types';
|
||||
|
||||
import { hexToU8a, isHex, isU8a, u8aConcat } from '@polkadot/util';
|
||||
import { isHex, isU8a, u8aConcat, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import { AbstractArray } from './AbstractArray';
|
||||
import { decodeU8a, mapToTypeMap, typeToConstructor } from './utils';
|
||||
@@ -20,10 +20,8 @@ type TupleTypes = (Constructor | keyof InterfaceTypes)[] | {
|
||||
|
||||
/** @internal */
|
||||
function decodeTuple (registry: Registry, _Types: TupleConstructors, value?: AnyTuple): Codec[] {
|
||||
if (isU8a(value)) {
|
||||
return decodeU8a(registry, value, _Types);
|
||||
} else if (isHex(value)) {
|
||||
return decodeTuple(registry, _Types, hexToU8a(value));
|
||||
if (isU8a(value) || isHex(value)) {
|
||||
return decodeU8a(registry, u8aToU8a(value), _Types);
|
||||
}
|
||||
|
||||
const Types: Constructor[] = Array.isArray(_Types)
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
import type { Codec, Constructor, Registry } from '../../types';
|
||||
|
||||
import { arrayFlatten } from '@polkadot/util';
|
||||
|
||||
/**
|
||||
* Given an u8a, and an array of Type constructors, decode the u8a against the
|
||||
* types, and return an array of decoded values.
|
||||
@@ -16,13 +14,15 @@ export function decodeU8a (registry: Registry, u8a: Uint8Array, _types: Construc
|
||||
const types = Array.isArray(_types)
|
||||
? _types
|
||||
: Object.values(_types);
|
||||
const result: Codec[] = [];
|
||||
let offset = 0;
|
||||
|
||||
if (!types.length) {
|
||||
return [];
|
||||
for (let i = 0; i < types.length; i++) {
|
||||
const value = new types[i](registry, u8a.subarray(offset));
|
||||
|
||||
result.push(value);
|
||||
offset += value.encodedLength;
|
||||
}
|
||||
|
||||
const Type = types[0];
|
||||
const value = new Type(registry, u8a);
|
||||
|
||||
return arrayFlatten([[value], decodeU8a(registry, u8a.subarray(value.encodedLength), types.slice(1))]);
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user