Optimize Compact decoding in Uint8Array streams (#4808)

* Optimize Compact decoding in Uint8Array streams

* Adjust test

* Adjust
This commit is contained in:
Jaco
2022-05-09 13:19:05 +03:00
committed by GitHub
parent 2edcef6ff5
commit fd568c0341
5 changed files with 13 additions and 12 deletions
+7 -7
View File
@@ -5,7 +5,7 @@ import type { BN } from '@polkadot/util';
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a, Registry } from '../types';
import { compactFromU8a, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
import { compactFromU8a, compactToU8a, isU8a } from '@polkadot/util';
import { typeToConstructor } from '../utils';
@@ -48,17 +48,17 @@ export class Compact<T extends INumber> implements ICompact<T> {
/** @internal */
public static decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number] {
if (value instanceof Compact) {
if (isU8a(value)) {
const [decodedLength, bn] = compactFromU8a(value);
return [new Type(registry, bn), decodedLength];
} else if (value instanceof Compact) {
return [new Type(registry, value.#raw), 0];
} else if (value instanceof Type) {
return [value, 0];
} else if (isString(value) || isNumber(value) || isBn(value) || isBigInt(value)) {
return [new Type(registry, value), 0];
}
const [decodedLength, bn] = compactFromU8a(value);
return [new Type(registry, bn), decodedLength];
return [new Type(registry, value), 0];
}
/**
@@ -16,7 +16,7 @@ export function createUnchecked (registry: Registry, section: string, callIndex:
const expectedArgs = callMetadata.fields;
const funcName = stringCamelCase(callMetadata.name);
const extrinsicFn = (...args: any[]): Call => {
const extrinsicFn = (...args: unknown[]): Call => {
assert(expectedArgs.length === args.length, () => `Extrinsic ${section}.${funcName} expects ${expectedArgs.length} arguments, got ${args.length}.`);
return registry.createTypeUnsafe('Call', [{ args, callIndex }, callMetadata]);
@@ -64,7 +64,7 @@ describe('decorateExtrinsics', (): void => {
it('should return properly-encoded transactions', (): void => {
expect(
registry.createType('Extrinsic', extrinsics.timestamp.set([10101])).toU8a()
registry.createType('Extrinsic', extrinsics.timestamp.set(32)).toU8a()
).toEqual(
new Uint8Array([
// length (encoded)
@@ -74,7 +74,7 @@ describe('decorateExtrinsics', (): void => {
// index
3, 0,
// values, Compact<Moment>
116
32 << 2
])
);
});
@@ -31,7 +31,7 @@ describe('Decorated', () => {
const tx = decorateExtrinsics(registry, metadata.asLatest, metadata.version);
expect(
registry.createType('Extrinsic', tx.timestamp.set([10101])).toU8a()
registry.createType('Extrinsic', tx.timestamp.set(63)).toU8a()
).toEqual(
new Uint8Array([
// length (encoded)
@@ -41,7 +41,7 @@ describe('Decorated', () => {
// index
3, 0,
// values, Compact<Moment>
116
63 << 2
])
);
});