Skip to content

Commit 63c5853

Browse files
authored
Adjust inspect for ExtrinsicPayload (#4862)
* Adjust inspect for ExtrinsicPayload * Adjust
1 parent 4166cb7 commit 63c5853

File tree

6 files changed

+59
-8
lines changed

6 files changed

+59
-8
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Changes:
77
- Add codec support for specialized `Option<bool>`
88
- Optimization for `createClass` with shortcut typeDef creation
99
- Optimization of `registry.getOrUnknown` with lookup cache
10+
- Optimization of Array allocations in decoding
11+
- Use `compactFromU8aLim` & `u8aConcatStrict` variants
12+
- Adjust `.inspect()` for `ExtrinsicPayload`
1013
- Adjust Kusama `StakingLedger` for runtime 1051
1114

1215

packages/types-codec/src/extended/Bytes.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,15 @@ export class Bytes extends Raw {
5454
/**
5555
* @description Returns a breakdown of the hex encoding for this Codec
5656
*/
57-
override inspect (): Inspect {
57+
override inspect (isBare?: boolean): Inspect {
5858
const clength = compactToU8a(this.length);
5959

6060
return {
61-
outer: this.length
62-
? [clength, super.toU8a()]
63-
: [clength]
61+
outer: isBare
62+
? [super.toU8a()]
63+
: this.length
64+
? [clength, super.toU8a()]
65+
: [clength]
6466
};
6567
}
6668

packages/types-codec/src/native/Struct.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,16 @@ export class Struct<
231231
/**
232232
* @description Returns a breakdown of the hex encoding for this Codec
233233
*/
234-
inspect (): Inspect {
234+
inspect (isBare?: BareOpts): Inspect {
235235
const inner = new Array<Inspect>();
236236

237237
for (const [k, v] of this.entries()) {
238238
inner.push({
239-
...v.inspect(),
239+
...v.inspect(
240+
!isBare || isBoolean(isBare)
241+
? isBare
242+
: isBare[k]
243+
),
240244
name: stringCamelCase(k as string)
241245
});
242246
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export interface Codec {
6060
/**
6161
* @description Returns a breakdown of the hex encoding for this Codec
6262
*/
63-
inspect (): Inspect;
63+
inspect (isBare?: BareOpts): Inspect;
6464

6565
/**
6666
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2017-2022 @polkadot/types authors & contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import rpcMetadata from '@polkadot/types-support/metadata/static-substrate';
5+
6+
import { TypeRegistry } from '../../create';
7+
import { decorateExtrinsics, Metadata } from '../../metadata';
8+
import { GenericExtrinsicPayloadV4 as ExtrinsicPayload } from '.';
9+
10+
const registry = new TypeRegistry();
11+
const metadata = new Metadata(registry, rpcMetadata);
12+
13+
registry.setMetadata(metadata);
14+
15+
const tx = decorateExtrinsics(registry, metadata.asLatest, metadata.version);
16+
17+
describe('ExtrinsicPayload', (): void => {
18+
it('has a sane inspect', (): void => {
19+
// we don't expect this to fail, however it is actually a good
20+
// reference for the ordering in base Substrate
21+
expect(new ExtrinsicPayload(registry, { method: tx.timestamp.set(0).toHex() } as never).inspect()).toEqual({
22+
inner: [
23+
{ name: 'method', outer: [new Uint8Array([3, 0, 0])] },
24+
{ name: 'era', outer: [new Uint8Array([0]), new Uint8Array([0])] },
25+
{ name: 'nonce', outer: [new Uint8Array([0])] },
26+
{ name: 'tip', outer: [new Uint8Array([0])] },
27+
{ name: 'assetId', outer: [new Uint8Array([0])] },
28+
{ name: 'specVersion', outer: [new Uint8Array([0, 0, 0, 0])] },
29+
{ name: 'transactionVersion', outer: [new Uint8Array([0, 0, 0, 0])] },
30+
{ name: 'genesisHash', outer: [new Uint8Array(32)] },
31+
{ name: 'blockHash', outer: [new Uint8Array(32)] }
32+
]
33+
});
34+
});
35+
});

packages/types/src/extrinsic/v4/ExtrinsicPayload.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
import type { SignOptions } from '@polkadot/keyring/types';
5-
import type { Registry } from '@polkadot/types-codec/types';
5+
import type { Inspect, Registry } from '@polkadot/types-codec/types';
66
import type { HexString } from '@polkadot/util/types';
77
import type { ExtrinsicEra } from '../../interfaces/extrinsics';
88
import type { Hash } from '../../interfaces/runtime';
@@ -37,6 +37,13 @@ export class GenericExtrinsicPayloadV4 extends Struct {
3737
};
3838
}
3939

40+
/**
41+
* @description Returns a breakdown of the hex encoding for this Codec
42+
*/
43+
public override inspect (): Inspect {
44+
return super.inspect({ method: true });
45+
}
46+
4047
/**
4148
* @description The block [[Hash]] the signature applies to (mortal/immortal)
4249
*/

0 commit comments

Comments
 (0)