Convert encodedLength to $encodedLength (#5426)

This commit is contained in:
Jaco
2023-01-19 13:17:33 +02:00
committed by GitHub
parent 044465eee4
commit ccab89304b
49 changed files with 181 additions and 103 deletions
+1 -1
View File
@@ -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;
});
+7 -2
View File
@@ -41,6 +41,11 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> 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<T extends Codec> extends Array<T> 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;
+6 -1
View File
@@ -30,6 +30,11 @@ export abstract class AbstractBase<T extends Codec> 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<T extends Codec> 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;
}
+7 -2
View File
@@ -75,9 +75,9 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
*/
export abstract class AbstractInt extends BN implements INumber {
readonly registry: Registry;
readonly encodedLength: number;
public $createdAtHash?: IU8a;
readonly $encodedLength: number;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly $isUnsigned: boolean;
@@ -99,7 +99,7 @@ export abstract class AbstractInt extends BN implements INumber {
this.registry = registry;
this.#bitLength = bitLength;
this.encodedLength = this.$initialU8aLength = this.#bitLength / 8;
this.$encodedLength = this.$initialU8aLength = this.#bitLength / 8;
this.$isUnsigned = !isSigned;
const isNegative = this.isNeg();
@@ -117,6 +117,11 @@ export abstract class AbstractInt extends BN 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');
@@ -44,19 +44,19 @@ describe('Compact', (): void => {
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);
});
+6 -1
View File
@@ -71,6 +71,11 @@ export class Compact<T extends INumber> implements ICompact<T> {
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<T extends INumber> implements ICompact<T> {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
public get $encodedLength (): number {
return this.toU8a().length;
}
@@ -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;
}
+1 -1
View File
@@ -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 => {
+7 -2
View File
@@ -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;
}
/**
+6 -1
View File
@@ -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');
+8 -3
View File
@@ -97,6 +97,11 @@ export class Option<T extends Codec> implements IOption<T> {
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<T extends Codec> implements IOption<T> {
/**
* @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<T extends Codec> implements IOption<T> {
return this.#raw.toU8a(true);
}
const u8a = new Uint8Array(this.encodedLength);
const u8a = new Uint8Array(this.$encodedLength);
if (this.isSome) {
u8a.set([1]);
+1 -1
View File
@@ -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);
});
});
+2 -2
View File
@@ -93,11 +93,11 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
/**
* @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;
@@ -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]));
});
+2 -2
View File
@@ -63,11 +63,11 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
/**
* @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;
@@ -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);
});
@@ -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);
});
});
@@ -93,6 +93,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
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<V extends Codec = Codec> extends Set<V> implements ISet<V>
/**
* @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;
@@ -19,7 +19,7 @@ describe('BitVec', (): void => {
it('has the right encodedLength', (): void => {
expect(
bitvec.encodedLength
bitvec.$encodedLength
).toEqual((test.length - 2) / 2);
});
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;
}
@@ -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);
});
});
+7 -2
View File
@@ -121,6 +121,11 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> 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<K extends Codec = Codec, V extends Codec = Codec> 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;
@@ -39,7 +39,7 @@ export class OptionBool extends Option<Bool> {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get encodedLength (): number {
public override get $encodedLength (): number {
return 1;
}
@@ -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);
});
+1 -1
View File
@@ -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 => {
+6 -7
View File
@@ -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
*/
+6 -7
View File
@@ -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
*/
+8 -3
View File
@@ -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');
+6 -7
View File
@@ -25,6 +25,7 @@ export class Json extends Map<string, any> 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<string, any> 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<string, any> 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
*/
+1 -1
View File
@@ -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 => {
+6 -1
View File
@@ -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;
}
+6 -1
View File
@@ -114,6 +114,11 @@ export class CodecSet extends Set<string> implements ISet<string> {
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<string> implements ISet<string> {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
public get $encodedLength (): number {
return this.#byteLength;
}
@@ -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);
});
+7 -2
View File
@@ -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;
+2 -2
View File
@@ -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);
});
+6 -1
View File
@@ -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;
}
+9 -5
View File
@@ -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
*/
+3 -3
View File
@@ -41,7 +41,7 @@ export function decodeU8a <T extends Codec = Codec> (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 <T extends Codec = Codec> (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++;
}
+3 -1
View File
@@ -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<string, boolean> = {};
/** @description Warns (once, per class) on the use of a deprecated interface */
export function warnGet <T> (obj: Record<string, any>, name: 'createdAtHash' | 'initialU8aLength' | 'isBasic' | 'isEmpty' | 'isNone' | 'isUnsigned'): T {
export function warnGet <T> (obj: Record<string, any>, name: Getters): T {
const clazz = `${obj.constructor?.name || 'Codec'}`;
const check = `${clazz}.${name}`;
const pass = `$${name}`;
+2 -2
View File
@@ -69,7 +69,7 @@ export class GenericEthereumLookupSource extends AbstractBase<GenericEthereumAcc
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get encodedLength (): number {
public override get $encodedLength (): number {
const rawLength = this._rawLength;
return rawLength + (
@@ -86,7 +86,7 @@ export class GenericEthereumLookupSource extends AbstractBase<GenericEthereumAcc
protected get _rawLength (): number {
return this.inner instanceof GenericAccountIndex
? GenericAccountIndex.calcLength(this.inner)
: this.inner.encodedLength;
: this.inner.$encodedLength;
}
/**
+1 -1
View File
@@ -131,7 +131,7 @@ abstract class ExtrinsicBase<A extends AnyTuple> extends AbstractBase<ExtrinsicV
/**
* @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;
}
+4 -4
View File
@@ -142,7 +142,7 @@ export class MortalEra extends Tuple {
/**
* @description Encoded length for mortals occupy 2 bytes, different from the actual Tuple since it is encoded. This is a shortcut fro `toU8a().length`
*/
public override get encodedLength (): number {
public override get $encodedLength (): number {
return 2;
}
@@ -247,10 +247,10 @@ export class GenericExtrinsicEra extends Enum implements IExtrinsicEra {
/**
* @description Override the encoded length method
*/
public override get encodedLength (): number {
public override get $encodedLength (): number {
return this.isImmortalEra
? this.asImmortalEra.encodedLength
: this.asMortalEra.encodedLength;
? this.asImmortalEra.$encodedLength
: this.asMortalEra.$encodedLength;
}
/**
+2 -2
View File
@@ -40,7 +40,7 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
} else if (isU8a(value)) {
// here we decode manually since we need to pull through the version information
const signature = registry.createTypeUnsafe<ExtrinsicSignatureV4>('ExtrinsicSignatureV4', [value, { isSigned }]);
const method = registry.createTypeUnsafe<Call>('Call', [value.subarray(signature.encodedLength)]);
const method = registry.createTypeUnsafe<Call>('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;
}
@@ -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;
}
+2 -2
View File
@@ -70,7 +70,7 @@ export class GenericLookupSource extends AbstractBase<GenericAccountId | Generic
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get encodedLength (): number {
public override get $encodedLength (): number {
const rawLength = this._rawLength;
return rawLength + (
@@ -87,7 +87,7 @@ export class GenericLookupSource extends AbstractBase<GenericAccountId | Generic
protected get _rawLength (): number {
return this.inner instanceof GenericAccountIndex
? GenericAccountIndex.calcLength(this.inner)
: this.inner.encodedLength;
: this.inner.$encodedLength;
}
/**
@@ -8,7 +8,7 @@ describe('IdentityFields', (): void => {
it('has a custom u64 (64-bit) encodedLength', (): void => {
expect(
registry.createType('IdentityFields').encodedLength
registry.createType('IdentityFields').$encodedLength
).toEqual(8);
});
+1 -1
View File
@@ -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;
}
+1 -1
View File
@@ -108,7 +108,7 @@ function decodeHashers <A extends AnyTuple> (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;
}