diff --git a/CHANGELOG.md b/CHANGELOG.md index 756d2fc64d..39d9acb2e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ Changes: - Add codec support for specialized `Option` - Optimization for `createClass` with shortcut typeDef creation - Optimization of `registry.getOrUnknown` with lookup cache +- Optimization of Array allocations in decoding +- Use `compactFromU8aLim` & `u8aConcatStrict` variants +- Adjust `.inspect()` for `ExtrinsicPayload` - Adjust Kusama `StakingLedger` for runtime 1051 diff --git a/packages/types-codec/src/extended/Bytes.ts b/packages/types-codec/src/extended/Bytes.ts index 2667a3859b..2587bdcc7f 100644 --- a/packages/types-codec/src/extended/Bytes.ts +++ b/packages/types-codec/src/extended/Bytes.ts @@ -54,13 +54,15 @@ export class Bytes extends Raw { /** * @description Returns a breakdown of the hex encoding for this Codec */ - override inspect (): Inspect { + override inspect (isBare?: boolean): Inspect { const clength = compactToU8a(this.length); return { - outer: this.length - ? [clength, super.toU8a()] - : [clength] + outer: isBare + ? [super.toU8a()] + : this.length + ? [clength, super.toU8a()] + : [clength] }; } diff --git a/packages/types-codec/src/native/Struct.ts b/packages/types-codec/src/native/Struct.ts index 18e3a57865..cf01968b21 100644 --- a/packages/types-codec/src/native/Struct.ts +++ b/packages/types-codec/src/native/Struct.ts @@ -231,12 +231,16 @@ export class Struct< /** * @description Returns a breakdown of the hex encoding for this Codec */ - inspect (): Inspect { + inspect (isBare?: BareOpts): Inspect { const inner = new Array(); for (const [k, v] of this.entries()) { inner.push({ - ...v.inspect(), + ...v.inspect( + !isBare || isBoolean(isBare) + ? isBare + : isBare[k] + ), name: stringCamelCase(k as string) }); } diff --git a/packages/types-codec/src/types/codec.ts b/packages/types-codec/src/types/codec.ts index 213519cb31..fea96002ce 100644 --- a/packages/types-codec/src/types/codec.ts +++ b/packages/types-codec/src/types/codec.ts @@ -60,7 +60,7 @@ export interface Codec { /** * @description Returns a breakdown of the hex encoding for this Codec */ - inspect (): Inspect; + inspect (isBare?: BareOpts): Inspect; /** * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation diff --git a/packages/types/src/extrinsic/v4/ExtrinsicPayload.spec.ts b/packages/types/src/extrinsic/v4/ExtrinsicPayload.spec.ts new file mode 100644 index 0000000000..d7a66f6810 --- /dev/null +++ b/packages/types/src/extrinsic/v4/ExtrinsicPayload.spec.ts @@ -0,0 +1,35 @@ +// Copyright 2017-2022 @polkadot/types authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import rpcMetadata from '@polkadot/types-support/metadata/static-substrate'; + +import { TypeRegistry } from '../../create'; +import { decorateExtrinsics, Metadata } from '../../metadata'; +import { GenericExtrinsicPayloadV4 as ExtrinsicPayload } from '.'; + +const registry = new TypeRegistry(); +const metadata = new Metadata(registry, rpcMetadata); + +registry.setMetadata(metadata); + +const tx = decorateExtrinsics(registry, metadata.asLatest, metadata.version); + +describe('ExtrinsicPayload', (): void => { + it('has a sane inspect', (): void => { + // we don't expect this to fail, however it is actually a good + // reference for the ordering in base Substrate + expect(new ExtrinsicPayload(registry, { method: tx.timestamp.set(0).toHex() } as never).inspect()).toEqual({ + inner: [ + { name: 'method', outer: [new Uint8Array([3, 0, 0])] }, + { name: 'era', outer: [new Uint8Array([0]), new Uint8Array([0])] }, + { name: 'nonce', outer: [new Uint8Array([0])] }, + { name: 'tip', outer: [new Uint8Array([0])] }, + { name: 'assetId', outer: [new Uint8Array([0])] }, + { name: 'specVersion', outer: [new Uint8Array([0, 0, 0, 0])] }, + { name: 'transactionVersion', outer: [new Uint8Array([0, 0, 0, 0])] }, + { name: 'genesisHash', outer: [new Uint8Array(32)] }, + { name: 'blockHash', outer: [new Uint8Array(32)] } + ] + }); + }); +}); diff --git a/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts b/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts index de6a7c9a3c..c4363d0f03 100644 --- a/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/v4/ExtrinsicPayload.ts @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import type { SignOptions } from '@polkadot/keyring/types'; -import type { Registry } from '@polkadot/types-codec/types'; +import type { Inspect, Registry } from '@polkadot/types-codec/types'; import type { HexString } from '@polkadot/util/types'; import type { ExtrinsicEra } from '../../interfaces/extrinsics'; import type { Hash } from '../../interfaces/runtime'; @@ -37,6 +37,13 @@ export class GenericExtrinsicPayloadV4 extends Struct { }; } + /** + * @description Returns a breakdown of the hex encoding for this Codec + */ + public override inspect (): Inspect { + return super.inspect({ method: true }); + } + /** * @description The block [[Hash]] the signature applies to (mortal/immortal) */