From ccab89304b34ad308d5c16116d8b7a903e689093 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 19 Jan 2023 13:17:33 +0200 Subject: [PATCH] Convert encodedLength to $encodedLength (#5426) --- packages/api-contract/src/Abi/index.ts | 2 +- packages/types-codec/src/abstract/Array.ts | 9 +++++++-- packages/types-codec/src/abstract/Base.ts | 7 ++++++- packages/types-codec/src/abstract/Int.ts | 9 +++++++-- packages/types-codec/src/base/Compact.spec.ts | 6 +++--- packages/types-codec/src/base/Compact.ts | 7 ++++++- packages/types-codec/src/base/DoNotConstruct.ts | 7 ++++++- packages/types-codec/src/base/Enum.spec.ts | 2 +- packages/types-codec/src/base/Enum.ts | 9 +++++++-- packages/types-codec/src/base/Null.ts | 7 ++++++- packages/types-codec/src/base/Option.ts | 11 ++++++++--- packages/types-codec/src/base/Tuple.spec.ts | 2 +- packages/types-codec/src/base/Tuple.ts | 4 ++-- packages/types-codec/src/base/VecFixed.spec.ts | 4 ++-- packages/types-codec/src/base/VecFixed.ts | 4 ++-- packages/types-codec/src/extended/BTreeMap.spec.ts | 2 +- packages/types-codec/src/extended/BTreeSet.spec.ts | 10 +++++----- packages/types-codec/src/extended/BTreeSet.ts | 9 +++++++-- packages/types-codec/src/extended/BitVec.spec.ts | 2 +- packages/types-codec/src/extended/BitVec.ts | 2 +- packages/types-codec/src/extended/Bytes.ts | 2 +- packages/types-codec/src/extended/Map.spec.ts | 8 ++++---- packages/types-codec/src/extended/Map.ts | 9 +++++++-- packages/types-codec/src/extended/OptionBool.ts | 2 +- packages/types-codec/src/extended/Type.spec.ts | 2 +- packages/types-codec/src/native/Bool.spec.ts | 2 +- packages/types-codec/src/native/Bool.ts | 13 ++++++------- packages/types-codec/src/native/Date.ts | 13 ++++++------- packages/types-codec/src/native/Float.ts | 11 ++++++++--- packages/types-codec/src/native/Json.ts | 13 ++++++------- packages/types-codec/src/native/Raw.spec.ts | 2 +- packages/types-codec/src/native/Raw.ts | 7 ++++++- packages/types-codec/src/native/Set.ts | 7 ++++++- packages/types-codec/src/native/Struct.spec.ts | 2 +- packages/types-codec/src/native/Struct.ts | 9 +++++++-- packages/types-codec/src/native/Text.spec.ts | 4 ++-- packages/types-codec/src/native/Text.ts | 7 ++++++- packages/types-codec/src/types/codec.ts | 14 +++++++++----- packages/types-codec/src/utils/decodeU8a.ts | 6 +++--- packages/types-codec/src/utils/warnGet.ts | 4 +++- packages/types/src/ethereum/LookupSource.ts | 4 ++-- packages/types/src/extrinsic/Extrinsic.ts | 2 +- packages/types/src/extrinsic/ExtrinsicEra.ts | 8 ++++---- packages/types/src/extrinsic/v4/Extrinsic.ts | 4 ++-- .../types/src/extrinsic/v4/ExtrinsicSignature.ts | 4 ++-- packages/types/src/generic/LookupSource.ts | 4 ++-- .../src/interfaces/identity/IdentityFields.spec.ts | 2 +- packages/types/src/primitive/Data.ts | 2 +- packages/types/src/primitive/StorageKey.ts | 2 +- 49 files changed, 181 insertions(+), 103 deletions(-) diff --git a/packages/api-contract/src/Abi/index.ts b/packages/api-contract/src/Abi/index.ts index cc833a1551..a57dc0aaec 100644 --- a/packages/api-contract/src/Abi/index.ts +++ b/packages/api-contract/src/Abi/index.ts @@ -239,7 +239,7 @@ export class Abi { return args.map(({ type: { lookupName, type } }): Codec => { const value = this.registry.createType(lookupName || type, data.subarray(offset)); - offset += value.encodedLength; + offset += value.$encodedLength; return value; }); diff --git a/packages/types-codec/src/abstract/Array.ts b/packages/types-codec/src/abstract/Array.ts index 67d4283787..1ba8aa73be 100644 --- a/packages/types-codec/src/abstract/Array.ts +++ b/packages/types-codec/src/abstract/Array.ts @@ -41,6 +41,11 @@ export abstract class AbstractArray extends Array implements return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -54,13 +59,13 @@ export abstract class AbstractArray extends Array implements /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { // We need to loop through all entries since they may have a variable length themselves, // e.g. when a Vec or Compact is contained withing, it has a variable length based on data let total = compactToU8a(this.length).length; for (let i = 0; i < this.length; i++) { - total += this[i].encodedLength; + total += this[i].$encodedLength; } return total; diff --git a/packages/types-codec/src/abstract/Base.ts b/packages/types-codec/src/abstract/Base.ts index 27c6ad6801..c0ee09ced5 100644 --- a/packages/types-codec/src/abstract/Base.ts +++ b/packages/types-codec/src/abstract/Base.ts @@ -30,6 +30,11 @@ export abstract class AbstractBase implements Codec { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -43,7 +48,7 @@ export abstract class AbstractBase implements Codec { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { return this.toU8a().length; } diff --git a/packages/types-codec/src/abstract/Int.ts b/packages/types-codec/src/abstract/Int.ts index 9e9ac422a6..4ee277bee1 100644 --- a/packages/types-codec/src/abstract/Int.ts +++ b/packages/types-codec/src/abstract/Int.ts @@ -75,9 +75,9 @@ function decodeAbstractInt (value: Exclude | Record { it('has the correct encodedLength for constructor values (string BlockNumber)', (): void => { expect( - new (Compact.with('BlockNumber'))(registry, 0xfffffff9).encodedLength + new (Compact.with('BlockNumber'))(registry, 0xfffffff9).$encodedLength ).toEqual(5); }); it('has the correct encodedLength for constructor values (class BlockNumber)', (): void => { expect( - new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).encodedLength + new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).$encodedLength ).toEqual(5); }); it('has the correct encodedLength for constructor values (u32)', (): void => { expect( - new (Compact.with(U32))(registry, 0xffff9).encodedLength + new (Compact.with(U32))(registry, 0xffff9).$encodedLength ).toEqual(4); }); diff --git a/packages/types-codec/src/base/Compact.ts b/packages/types-codec/src/base/Compact.ts index 69b8cd6f9b..81e6930f35 100644 --- a/packages/types-codec/src/base/Compact.ts +++ b/packages/types-codec/src/base/Compact.ts @@ -71,6 +71,11 @@ export class Compact implements ICompact { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -98,7 +103,7 @@ export class Compact implements ICompact { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { return this.toU8a().length; } diff --git a/packages/types-codec/src/base/DoNotConstruct.ts b/packages/types-codec/src/base/DoNotConstruct.ts index b0f3ac036b..6e9f2e96c5 100644 --- a/packages/types-codec/src/base/DoNotConstruct.ts +++ b/packages/types-codec/src/base/DoNotConstruct.ts @@ -31,6 +31,11 @@ export class DoNotConstruct implements Codec { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -52,7 +57,7 @@ export class DoNotConstruct implements Codec { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { throw this.#neverError; } diff --git a/packages/types-codec/src/base/Enum.spec.ts b/packages/types-codec/src/base/Enum.spec.ts index 2d4c806c74..5cf9f2f7a7 100644 --- a/packages/types-codec/src/base/Enum.spec.ts +++ b/packages/types-codec/src/base/Enum.spec.ts @@ -430,7 +430,7 @@ describe('Enum', (): void => { const test = new Test(registry, 123, 1); expect(test.toHex()).toEqual('0x017b000000'); - expect(test.encodedLength).toEqual(1 + 4); + expect(test.$encodedLength).toEqual(1 + 4); }); it('encodes a single entry correctly', (): void => { diff --git a/packages/types-codec/src/base/Enum.ts b/packages/types-codec/src/base/Enum.ts index 53e29a23db..daf3584f2f 100644 --- a/packages/types-codec/src/base/Enum.ts +++ b/packages/types-codec/src/base/Enum.ts @@ -223,6 +223,11 @@ export class Enum implements IEnum { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -287,8 +292,8 @@ export class Enum implements IEnum { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { - return 1 + this.#raw.encodedLength; + public get $encodedLength (): number { + return 1 + this.#raw.$encodedLength; } /** diff --git a/packages/types-codec/src/base/Null.ts b/packages/types-codec/src/base/Null.ts index 79d47f0155..b6912359b7 100644 --- a/packages/types-codec/src/base/Null.ts +++ b/packages/types-codec/src/base/Null.ts @@ -14,10 +14,10 @@ import { warnGet } from '../utils'; * Implements a type that does not contain anything (apart from `null`) */ export class Null implements Codec { - readonly encodedLength = 0; readonly registry: Registry; public $createdAtHash?: IU8a; + readonly $encodedLength = 0; public $initialU8aLength = 0; public $isStorageFallback?: boolean; readonly $isEmpty = true; @@ -31,6 +31,11 @@ export class Null implements Codec { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); diff --git a/packages/types-codec/src/base/Option.ts b/packages/types-codec/src/base/Option.ts index 16cc6aabd8..f77f7877c4 100644 --- a/packages/types-codec/src/base/Option.ts +++ b/packages/types-codec/src/base/Option.ts @@ -97,6 +97,11 @@ export class Option implements IOption { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -126,9 +131,9 @@ export class Option implements IOption { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { // boolean byte (has value, doesn't have) along with wrapped length - return 1 + this.#raw.encodedLength; + return 1 + this.#raw.$encodedLength; } /** @@ -256,7 +261,7 @@ export class Option implements IOption { return this.#raw.toU8a(true); } - const u8a = new Uint8Array(this.encodedLength); + const u8a = new Uint8Array(this.$encodedLength); if (this.isSome) { u8a.set([1]); diff --git a/packages/types-codec/src/base/Tuple.spec.ts b/packages/types-codec/src/base/Tuple.spec.ts index 6dde14ce90..843e66ed84 100644 --- a/packages/types-codec/src/base/Tuple.spec.ts +++ b/packages/types-codec/src/base/Tuple.spec.ts @@ -50,7 +50,7 @@ describe('Tuple', (): void => { const INPUT = '0xcc0200000000'; const test = registry.createType('(u32, [u32; 0], u16)', INPUT); - expect(test.encodedLength).toEqual(4 + 0 + 2); + expect(test.$encodedLength).toEqual(4 + 0 + 2); expect(test.toHex()).toEqual(INPUT); }); }); diff --git a/packages/types-codec/src/base/Tuple.ts b/packages/types-codec/src/base/Tuple.ts index 2fbc82a54d..5b6a482e20 100644 --- a/packages/types-codec/src/base/Tuple.ts +++ b/packages/types-codec/src/base/Tuple.ts @@ -93,11 +93,11 @@ export class Tuple extends AbstractArray implements ITuple { /** * @description The length of the value when encoded as a Uint8Array */ - public override get encodedLength (): number { + public override get $encodedLength (): number { let total = 0; for (let i = 0; i < this.length; i++) { - total += this[i].encodedLength; + total += this[i].$encodedLength; } return total; diff --git a/packages/types-codec/src/base/VecFixed.spec.ts b/packages/types-codec/src/base/VecFixed.spec.ts index a4e40a8970..500e734557 100644 --- a/packages/types-codec/src/base/VecFixed.spec.ts +++ b/packages/types-codec/src/base/VecFixed.spec.ts @@ -50,14 +50,14 @@ describe('VecFixed', (): void => { it('has empty Uint8Array when length is 0', (): void => { const test = new (VecFixed.with(Text, 0))(registry); - expect(test.encodedLength).toEqual(0); + expect(test.$encodedLength).toEqual(0); expect(test.toU8a()).toEqual(new Uint8Array([])); }); it('has equivalent to 1 Uint8Array when length is 1', (): void => { const test = new (VecFixed.with(Text, 1))(registry, ['hello']); - expect(test.encodedLength).toEqual(1 + 5); + expect(test.$encodedLength).toEqual(1 + 5); expect(test.toU8a()).toEqual(new Uint8Array([20, 104, 101, 108, 108, 111])); }); diff --git a/packages/types-codec/src/base/VecFixed.ts b/packages/types-codec/src/base/VecFixed.ts index dbe1670b06..aef423849d 100644 --- a/packages/types-codec/src/base/VecFixed.ts +++ b/packages/types-codec/src/base/VecFixed.ts @@ -63,11 +63,11 @@ export class VecFixed extends AbstractArray { /** * @description The length of the value when encoded as a Uint8Array */ - public override get encodedLength (): number { + public override get $encodedLength (): number { let total = 0; for (let i = 0; i < this.length; i++) { - total += this[i].encodedLength; + total += this[i].$encodedLength; } return total; diff --git a/packages/types-codec/src/extended/BTreeMap.spec.ts b/packages/types-codec/src/extended/BTreeMap.spec.ts index 15ccf44b96..ca821bee67 100644 --- a/packages/types-codec/src/extended/BTreeMap.spec.ts +++ b/packages/types-codec/src/extended/BTreeMap.spec.ts @@ -87,7 +87,7 @@ describe('BTreeMap', (): void => { it('correctly encodes length', (): void => { expect( new ( - BTreeMap.with(Text, U32))(registry, mockU32TextMap).encodedLength + BTreeMap.with(Text, U32))(registry, mockU32TextMap).$encodedLength ).toEqual(13); }); diff --git a/packages/types-codec/src/extended/BTreeSet.spec.ts b/packages/types-codec/src/extended/BTreeSet.spec.ts index 4695af2e9a..2b5bb09474 100644 --- a/packages/types-codec/src/extended/BTreeSet.spec.ts +++ b/packages/types-codec/src/extended/BTreeSet.spec.ts @@ -130,7 +130,7 @@ describe('BTreeSet', (): void => { it('correctly encodes length', (): void => { expect( new ( - BTreeSet.with(U32))(registry, mockU32Set).encodedLength + BTreeSet.with(U32))(registry, mockU32Set).$encodedLength ).toEqual(17); }); @@ -139,10 +139,10 @@ describe('BTreeSet', (): void => { // only the length byte expect(none.toHex()).toEqual('0x00'); - expect(none.encodedLength).toEqual(1); + expect(none.$encodedLength).toEqual(1); expect( (new (BTreeSet.with(U32))(registry, none.toHex())).$initialU8aLength - ).toEqual(none.encodedLength); + ).toEqual(none.$encodedLength); }); it('correctly encodes/decodes filled', (): void => { @@ -150,10 +150,10 @@ describe('BTreeSet', (): void => { // length byte + 2 values, 2 << 2 with u32 values expect(some.toHex()).toEqual('0x080100000002000000'); - expect(some.encodedLength).toEqual(1 + (4 * 2)); + expect(some.$encodedLength).toEqual(1 + (4 * 2)); expect( (new (BTreeSet.with(U32))(registry, some.toHex())).$initialU8aLength - ).toEqual(some.encodedLength); + ).toEqual(some.$encodedLength); }); }); diff --git a/packages/types-codec/src/extended/BTreeSet.ts b/packages/types-codec/src/extended/BTreeSet.ts index 968ca765e2..169774631b 100644 --- a/packages/types-codec/src/extended/BTreeSet.ts +++ b/packages/types-codec/src/extended/BTreeSet.ts @@ -93,6 +93,11 @@ export class BTreeSet extends Set implements ISet return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -114,11 +119,11 @@ export class BTreeSet extends Set implements ISet /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { let len = compactToU8a(this.size).length; for (const v of this.values()) { - len += v.encodedLength; + len += v.$encodedLength; } return len; diff --git a/packages/types-codec/src/extended/BitVec.spec.ts b/packages/types-codec/src/extended/BitVec.spec.ts index 16da1454c9..b573fcf8da 100644 --- a/packages/types-codec/src/extended/BitVec.spec.ts +++ b/packages/types-codec/src/extended/BitVec.spec.ts @@ -19,7 +19,7 @@ describe('BitVec', (): void => { it('has the right encodedLength', (): void => { expect( - bitvec.encodedLength + bitvec.$encodedLength ).toEqual((test.length - 2) / 2); }); diff --git a/packages/types-codec/src/extended/BitVec.ts b/packages/types-codec/src/extended/BitVec.ts index 05dc18d968..cc89cb1d4f 100644 --- a/packages/types-codec/src/extended/BitVec.ts +++ b/packages/types-codec/src/extended/BitVec.ts @@ -60,7 +60,7 @@ export class BitVec extends Raw { /** * @description The length of the value when encoded as a Uint8Array */ - public override get encodedLength (): number { + public override get $encodedLength (): number { return this.length + compactToU8a(this.#decodedLength).length; } diff --git a/packages/types-codec/src/extended/Bytes.ts b/packages/types-codec/src/extended/Bytes.ts index 3e31706ab1..c3e0b4e4c2 100644 --- a/packages/types-codec/src/extended/Bytes.ts +++ b/packages/types-codec/src/extended/Bytes.ts @@ -50,7 +50,7 @@ export class Bytes extends Raw { /** * @description The length of the value when encoded as a Uint8Array */ - public override get encodedLength (): number { + public override get $encodedLength (): number { return this.length + compactToU8a(this.length).length; } diff --git a/packages/types-codec/src/extended/Map.spec.ts b/packages/types-codec/src/extended/Map.spec.ts index 8a12042b2b..e7f7a85cd3 100644 --- a/packages/types-codec/src/extended/Map.spec.ts +++ b/packages/types-codec/src/extended/Map.spec.ts @@ -81,10 +81,10 @@ describe('CodecMap', (): void => { // only the length byte expect(none.toHex()).toEqual('0x00'); - expect(none.encodedLength).toEqual(1); + expect(none.$encodedLength).toEqual(1); expect( new CodecMap(registry, Text, Text, none.toHex()).$initialU8aLength - ).toEqual(none.encodedLength); + ).toEqual(none.$encodedLength); }); it('correctly encodes/decodes filled', (): void => { @@ -95,10 +95,10 @@ describe('CodecMap', (): void => { // length byte + 2 values, 2 << 2 with Text values expect(some.toHex()).toEqual('0x0804310c666f6f04320c626172'); - expect(some.encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2)); + expect(some.$encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2)); expect( new CodecMap(registry, Text, Text, some.toHex()).$initialU8aLength - ).toEqual(some.encodedLength); + ).toEqual(some.$encodedLength); }); }); diff --git a/packages/types-codec/src/extended/Map.ts b/packages/types-codec/src/extended/Map.ts index 20350421b6..a7677ed9ea 100644 --- a/packages/types-codec/src/extended/Map.ts +++ b/packages/types-codec/src/extended/Map.ts @@ -121,6 +121,11 @@ export class CodecMap extends return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -134,11 +139,11 @@ export class CodecMap extends /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { let len = compactToU8a(this.size).length; for (const [k, v] of this.entries()) { - len += k.encodedLength + v.encodedLength; + len += k.$encodedLength + v.$encodedLength; } return len; diff --git a/packages/types-codec/src/extended/OptionBool.ts b/packages/types-codec/src/extended/OptionBool.ts index d83937d939..4593af16f8 100644 --- a/packages/types-codec/src/extended/OptionBool.ts +++ b/packages/types-codec/src/extended/OptionBool.ts @@ -39,7 +39,7 @@ export class OptionBool extends Option { /** * @description The length of the value when encoded as a Uint8Array */ - public override get encodedLength (): number { + public override get $encodedLength (): number { return 1; } diff --git a/packages/types-codec/src/extended/Type.spec.ts b/packages/types-codec/src/extended/Type.spec.ts index e53679ca0f..d6026b69d2 100644 --- a/packages/types-codec/src/extended/Type.spec.ts +++ b/packages/types-codec/src/extended/Type.spec.ts @@ -106,7 +106,7 @@ describe('Type', (): void => { const u8a = new Type(registry, original).toU8a(); const decoded = new Type(registry, u8a); - expect(decoded.encodedLength).toEqual(original.length + 1); // extra byte for length + expect(decoded.$encodedLength).toEqual(original.length + 1); // extra byte for length expect(decoded.toString()).toEqual(expected); }); diff --git a/packages/types-codec/src/native/Bool.spec.ts b/packages/types-codec/src/native/Bool.spec.ts index 54804d77d2..9dfea85fab 100644 --- a/packages/types-codec/src/native/Bool.spec.ts +++ b/packages/types-codec/src/native/Bool.spec.ts @@ -37,7 +37,7 @@ describe('Bool', (): void => { }); it('correctly encodes length', (): void => { - expect(new Bool(registry, true).encodedLength).toEqual(1); + expect(new Bool(registry, true).$encodedLength).toEqual(1); }); describe('utils', (): void => { diff --git a/packages/types-codec/src/native/Bool.ts b/packages/types-codec/src/native/Bool.ts index c6b1231cc8..722085a5a8 100644 --- a/packages/types-codec/src/native/Bool.ts +++ b/packages/types-codec/src/native/Bool.ts @@ -18,6 +18,7 @@ export class bool extends Boolean implements Codec { readonly registry: Registry; public $createdAtHash?: IU8a; + readonly $encodedLength = 1; public $initialU8aLength = 1; public $isStorageFallback?: boolean; @@ -38,6 +39,11 @@ export class bool extends Boolean implements Codec { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -48,13 +54,6 @@ export class bool extends Boolean implements Codec { return warnGet(this, 'isEmpty'); } - /** - * @description The length of the value when encoded as a Uint8Array - */ - public get encodedLength (): number { - return 1; - } - /** * @description returns a hash of the contents */ diff --git a/packages/types-codec/src/native/Date.ts b/packages/types-codec/src/native/Date.ts index 439a2f51c8..d0b087aecb 100644 --- a/packages/types-codec/src/native/Date.ts +++ b/packages/types-codec/src/native/Date.ts @@ -39,6 +39,7 @@ export class CodecDate extends Date implements INumber { readonly registry: Registry; public $createdAtHash?: IU8a; + readonly $encodedLength = BITLENGTH / 8; public $initialU8aLength = BITLENGTH / 8; public $isStorageFallback?: boolean; @@ -53,6 +54,11 @@ export class CodecDate extends Date implements INumber { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -63,13 +69,6 @@ export class CodecDate extends Date implements INumber { return warnGet(this, 'isEmpty'); } - /** - * @description The length of the value when encoded as a Uint8Array - */ - public get encodedLength (): number { - return BITLENGTH / 8; - } - /** * @description returns a hash of the contents */ diff --git a/packages/types-codec/src/native/Float.ts b/packages/types-codec/src/native/Float.ts index 7f25d346d8..09cfd81ba0 100644 --- a/packages/types-codec/src/native/Float.ts +++ b/packages/types-codec/src/native/Float.ts @@ -21,10 +21,10 @@ interface Options { * in some eth_* RPCs */ export class Float extends Number implements IFloat { - readonly encodedLength: number; readonly registry: Registry; public $createdAtHash?: IU8a; + readonly $encodedLength: number; public $initialU8aLength?: number; public $isStorageFallback?: boolean; @@ -40,8 +40,8 @@ export class Float extends Number implements IFloat { ); this.#bitLength = bitLength; - this.encodedLength = bitLength / 8; - this.$initialU8aLength = this.encodedLength; + this.$encodedLength = bitLength / 8; + this.$initialU8aLength = this.$encodedLength; this.registry = registry; } @@ -50,6 +50,11 @@ export class Float extends Number implements IFloat { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); diff --git a/packages/types-codec/src/native/Json.ts b/packages/types-codec/src/native/Json.ts index de97f2b3ab..6722532790 100644 --- a/packages/types-codec/src/native/Json.ts +++ b/packages/types-codec/src/native/Json.ts @@ -25,6 +25,7 @@ export class Json extends Map implements Codec { readonly registry: Registry; public $createdAtHash?: IU8a; + readonly $encodedLength = 0; public $initialU8aLength?: number; public $isStorageFallback?: boolean; @@ -43,6 +44,11 @@ export class Json extends Map implements Codec { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -53,13 +59,6 @@ export class Json extends Map implements Codec { return warnGet(this, 'isEmpty'); } - /** - * @description Always 0, never encodes as a Uint8Array - */ - public get encodedLength (): number { - return 0; - } - /** * @description returns a hash of the contents */ diff --git a/packages/types-codec/src/native/Raw.spec.ts b/packages/types-codec/src/native/Raw.spec.ts index bab0eaf7da..746f9aed97 100644 --- a/packages/types-codec/src/native/Raw.spec.ts +++ b/packages/types-codec/src/native/Raw.spec.ts @@ -45,7 +45,7 @@ describe('Raw', (): void => { }); it('correctly encodes length', (): void => { - expect(u8a.encodedLength).toEqual(5); + expect(u8a.$encodedLength).toEqual(5); }); it('allows wrapping of a pre-existing instance', (): void => { diff --git a/packages/types-codec/src/native/Raw.ts b/packages/types-codec/src/native/Raw.ts index 16f79f2c68..3eb9fed3df 100644 --- a/packages/types-codec/src/native/Raw.ts +++ b/packages/types-codec/src/native/Raw.ts @@ -44,6 +44,11 @@ export class Raw extends Uint8Array implements IU8a { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -57,7 +62,7 @@ export class Raw extends Uint8Array implements IU8a { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { return this.length; } diff --git a/packages/types-codec/src/native/Set.ts b/packages/types-codec/src/native/Set.ts index bdc4cdea1b..e587c31998 100644 --- a/packages/types-codec/src/native/Set.ts +++ b/packages/types-codec/src/native/Set.ts @@ -114,6 +114,11 @@ export class CodecSet extends Set implements ISet { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -148,7 +153,7 @@ export class CodecSet extends Set implements ISet { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { return this.#byteLength; } diff --git a/packages/types-codec/src/native/Struct.spec.ts b/packages/types-codec/src/native/Struct.spec.ts index bc25d7fefe..28389afae2 100644 --- a/packages/types-codec/src/native/Struct.spec.ts +++ b/packages/types-codec/src/native/Struct.spec.ts @@ -207,7 +207,7 @@ describe('Struct', (): void => { txt: Text, u32: U32 }) - )(registry, { foo: 'bazzing', bar: 69 }).encodedLength + )(registry, { foo: 'bazzing', bar: 69 }).$encodedLength ).toEqual(5); }); diff --git a/packages/types-codec/src/native/Struct.ts b/packages/types-codec/src/native/Struct.ts index 54538cc3d4..c71b6481d7 100644 --- a/packages/types-codec/src/native/Struct.ts +++ b/packages/types-codec/src/native/Struct.ts @@ -133,6 +133,11 @@ export class Struct< return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -188,11 +193,11 @@ export class Struct< /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { let total = 0; for (const v of this.values()) { - total += v.encodedLength; + total += v.$encodedLength; } return total; diff --git a/packages/types-codec/src/native/Text.spec.ts b/packages/types-codec/src/native/Text.spec.ts index 522f374658..3647159e8b 100644 --- a/packages/types-codec/src/native/Text.spec.ts +++ b/packages/types-codec/src/native/Text.spec.ts @@ -59,14 +59,14 @@ describe('Text', (): void => { it('calulates the length & encoded length correctly for ASCII', (): void => { const test = new Text(registry, 'abcde'); - expect(test.encodedLength).toEqual(6); + expect(test.$encodedLength).toEqual(6); expect(test).toHaveLength(5); }); it('calulates the length & encoded length correctly for non-ASCII', (): void => { const test = new Text(registry, '中文'); - expect(test.encodedLength).toEqual(7); + expect(test.$encodedLength).toEqual(7); expect(test).toHaveLength(2); }); diff --git a/packages/types-codec/src/native/Text.ts b/packages/types-codec/src/native/Text.ts index bd79653ac5..4106463fcd 100644 --- a/packages/types-codec/src/native/Text.ts +++ b/packages/types-codec/src/native/Text.ts @@ -72,6 +72,11 @@ export class Text extends String implements IText { return warnGet(this, 'createdAtHash'); } + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + public get encodedLength (): number { + return warnGet(this, 'encodedLength'); + } + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ public get initialU8aLength (): number | undefined { return warnGet(this, 'initialU8aLength'); @@ -85,7 +90,7 @@ export class Text extends String implements IText { /** * @description The length of the value when encoded as a Uint8Array */ - public get encodedLength (): number { + public get $encodedLength (): number { return this.toU8a().length; } diff --git a/packages/types-codec/src/types/codec.ts b/packages/types-codec/src/types/codec.ts index 32d37b7951..e43828af8d 100644 --- a/packages/types-codec/src/types/codec.ts +++ b/packages/types-codec/src/types/codec.ts @@ -18,6 +18,9 @@ interface CodecDeprecated { /** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */ readonly createdAtHash: IU8a | undefined; + /** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */ + readonly encodedLength: number; + /** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */ readonly initialU8aLength: number | undefined; @@ -41,6 +44,12 @@ export interface Codec extends CodecDeprecated { */ $createdAtHash?: IU8a; + /** + * @description + * The length of the value when encoded as a Uint8Array (which is the SCALE representation) + */ + readonly $encodedLength: number; + /** * @description * The length of the initial encoded value (Only available when the value was @@ -66,11 +75,6 @@ export interface Codec extends CodecDeprecated { */ $isStorageFallback?: boolean; - /** - * @description The length of the value when encoded as a Uint8Array - */ - readonly encodedLength: number; - /** * @description Returns a hash of the value */ diff --git a/packages/types-codec/src/utils/decodeU8a.ts b/packages/types-codec/src/utils/decodeU8a.ts index 2ec9705ab1..d950d2a720 100644 --- a/packages/types-codec/src/utils/decodeU8a.ts +++ b/packages/types-codec/src/utils/decodeU8a.ts @@ -41,7 +41,7 @@ export function decodeU8a (registry: Registry, result: while (i < count) { const value = new Types[i](registry, u8a.subarray(offset)); - offset += value.$initialU8aLength || value.encodedLength; + offset += value.$initialU8aLength || value.$encodedLength; result[i] = value; i++; } @@ -67,7 +67,7 @@ export function decodeU8aStruct (registry: Registry, result: [string, Codec][], while (i < count) { const value = new Types[i](registry, u8a.subarray(offset)); - offset += value.$initialU8aLength || value.encodedLength; + offset += value.$initialU8aLength || value.$encodedLength; result[i] = [keys[i], value]; i++; } @@ -93,7 +93,7 @@ export function decodeU8aVec (registry: Registry, resu while (i < count) { const value = new Type(registry, u8a.subarray(offset)); - offset += value.$initialU8aLength || value.encodedLength; + offset += value.$initialU8aLength || value.$encodedLength; result[i] = value; i++; } diff --git a/packages/types-codec/src/utils/warnGet.ts b/packages/types-codec/src/utils/warnGet.ts index 09cbe30da8..2097895bb5 100644 --- a/packages/types-codec/src/utils/warnGet.ts +++ b/packages/types-codec/src/utils/warnGet.ts @@ -1,10 +1,12 @@ // Copyright 2017-2023 @polkadot/types-codec authors & contributors // SPDX-License-Identifier: Apache-2.0 +type Getters = 'createdAtHash' | 'encodedLength' | 'initialU8aLength' | 'isBasic' | 'isEmpty' | 'isNone' | 'isUnsigned'; + const hasWarned: Record = {}; /** @description Warns (once, per class) on the use of a deprecated interface */ -export function warnGet (obj: Record, name: 'createdAtHash' | 'initialU8aLength' | 'isBasic' | 'isEmpty' | 'isNone' | 'isUnsigned'): T { +export function warnGet (obj: Record, name: Getters): T { const clazz = `${obj.constructor?.name || 'Codec'}`; const check = `${clazz}.${name}`; const pass = `$${name}`; diff --git a/packages/types/src/ethereum/LookupSource.ts b/packages/types/src/ethereum/LookupSource.ts index af611452f6..28f4633226 100644 --- a/packages/types/src/ethereum/LookupSource.ts +++ b/packages/types/src/ethereum/LookupSource.ts @@ -69,7 +69,7 @@ export class GenericEthereumLookupSource extends AbstractBase extends AbstractBase('ExtrinsicSignatureV4', [value, { isSigned }]); - const method = registry.createTypeUnsafe('Call', [value.subarray(signature.encodedLength)]); + const method = registry.createTypeUnsafe('Call', [value.subarray(signature.$encodedLength)]); return { method, @@ -54,7 +54,7 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl { /** * @description The length of the value when encoded as a Uint8Array */ - public override get encodedLength (): number { + public override get $encodedLength (): number { return this.toU8a().length; } diff --git a/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts b/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts index 7870e94cb5..0a696cfea0 100644 --- a/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts +++ b/packages/types/src/extrinsic/v4/ExtrinsicSignature.ts @@ -62,9 +62,9 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig /** * @description The length of the value when encoded as a Uint8Array */ - public override get encodedLength (): number { + public override get $encodedLength (): number { return this.isSigned - ? super.encodedLength + ? super.$encodedLength : 0; } diff --git a/packages/types/src/generic/LookupSource.ts b/packages/types/src/generic/LookupSource.ts index 6bf5602c38..a8861159a0 100644 --- a/packages/types/src/generic/LookupSource.ts +++ b/packages/types/src/generic/LookupSource.ts @@ -70,7 +70,7 @@ export class GenericLookupSource extends AbstractBase { it('has a custom u64 (64-bit) encodedLength', (): void => { expect( - registry.createType('IdentityFields').encodedLength + registry.createType('IdentityFields').$encodedLength ).toEqual(8); }); diff --git a/packages/types/src/primitive/Data.ts b/packages/types/src/primitive/Data.ts index e7412aec26..b88d3ca90f 100644 --- a/packages/types/src/primitive/Data.ts +++ b/packages/types/src/primitive/Data.ts @@ -108,7 +108,7 @@ export class Data extends Enum { /** * @description The encoded length */ - public override get encodedLength (): number { + public override get $encodedLength (): number { return this.toU8a().length; } diff --git a/packages/types/src/primitive/StorageKey.ts b/packages/types/src/primitive/StorageKey.ts index ce507df8b6..5c9f3fdf6f 100644 --- a/packages/types/src/primitive/StorageKey.ts +++ b/packages/types/src/primitive/StorageKey.ts @@ -108,7 +108,7 @@ function decodeHashers (registry: Registry, value: Uint8Arr ? registry.createTypeUnsafe(getSiName(registry.lookup, type), [value.subarray(offset + hashLen)]) : registry.createTypeUnsafe('Raw', [value.subarray(offset, offset + hashLen)]); - offset += hashLen + (canDecode ? decoded.encodedLength : 0); + offset += hashLen + (canDecode ? decoded.$encodedLength : 0); result[i] = decoded; }