Skip to content

Commit fd568c0

Browse files
authored
Optimize Compact decoding in Uint8Array streams (#4808)
* Optimize Compact decoding in Uint8Array streams * Adjust test * Adjust
1 parent 2edcef6 commit fd568c0

File tree

5 files changed

+13
-12
lines changed

5 files changed

+13
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Changes:
88
- Add explicit support for `Call` typegen (inclusive of non-defaults)
99
- Deupe `wss://` handling in `polkadot-types-from-{chain, defs}`
1010
- Allow for optional `definitions.ts` in typegen (only use chain)
11+
- Optimize `Compact<*>` decoding in Uint8Array streams
1112
- Update to latest Substrate, Kusama & Polkadot static metadata
1213

1314

packages/types-codec/src/base/Compact.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { BN } from '@polkadot/util';
55
import type { HexString } from '@polkadot/util/types';
66
import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a, Registry } from '../types';
77

8-
import { compactFromU8a, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
8+
import { compactFromU8a, compactToU8a, isU8a } from '@polkadot/util';
99

1010
import { typeToConstructor } from '../utils';
1111

@@ -48,17 +48,17 @@ export class Compact<T extends INumber> implements ICompact<T> {
4848

4949
/** @internal */
5050
public static decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number] {
51-
if (value instanceof Compact) {
51+
if (isU8a(value)) {
52+
const [decodedLength, bn] = compactFromU8a(value);
53+
54+
return [new Type(registry, bn), decodedLength];
55+
} else if (value instanceof Compact) {
5256
return [new Type(registry, value.#raw), 0];
5357
} else if (value instanceof Type) {
5458
return [value, 0];
55-
} else if (isString(value) || isNumber(value) || isBn(value) || isBigInt(value)) {
56-
return [new Type(registry, value), 0];
5759
}
5860

59-
const [decodedLength, bn] = compactFromU8a(value);
60-
61-
return [new Type(registry, bn), decodedLength];
61+
return [new Type(registry, value), 0];
6262
}
6363

6464
/**

packages/types/src/metadata/decorate/extrinsics/createUnchecked.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function createUnchecked (registry: Registry, section: string, callIndex:
1616
const expectedArgs = callMetadata.fields;
1717
const funcName = stringCamelCase(callMetadata.name);
1818

19-
const extrinsicFn = (...args: any[]): Call => {
19+
const extrinsicFn = (...args: unknown[]): Call => {
2020
assert(expectedArgs.length === args.length, () => `Extrinsic ${section}.${funcName} expects ${expectedArgs.length} arguments, got ${args.length}.`);
2121

2222
return registry.createTypeUnsafe('Call', [{ args, callIndex }, callMetadata]);

packages/types/src/metadata/decorate/extrinsics/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('decorateExtrinsics', (): void => {
6464

6565
it('should return properly-encoded transactions', (): void => {
6666
expect(
67-
registry.createType('Extrinsic', extrinsics.timestamp.set([10101])).toU8a()
67+
registry.createType('Extrinsic', extrinsics.timestamp.set(32)).toU8a()
6868
).toEqual(
6969
new Uint8Array([
7070
// length (encoded)
@@ -74,7 +74,7 @@ describe('decorateExtrinsics', (): void => {
7474
// index
7575
3, 0,
7676
// values, Compact<Moment>
77-
116
77+
32 << 2
7878
])
7979
);
8080
});

packages/types/src/metadata/decorate/index.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('Decorated', () => {
3131
const tx = decorateExtrinsics(registry, metadata.asLatest, metadata.version);
3232

3333
expect(
34-
registry.createType('Extrinsic', tx.timestamp.set([10101])).toU8a()
34+
registry.createType('Extrinsic', tx.timestamp.set(63)).toU8a()
3535
).toEqual(
3636
new Uint8Array([
3737
// length (encoded)
@@ -41,7 +41,7 @@ describe('Decorated', () => {
4141
// index
4242
3, 0,
4343
// values, Compact<Moment>
44-
116
44+
63 << 2
4545
])
4646
);
4747
});

0 commit comments

Comments
 (0)