Add ISize type (same as USize, not-to-be used) (#5556)

This commit is contained in:
Jaco
2023-03-23 10:58:38 +02:00
committed by GitHub
parent 1cbbe3bb12
commit 3b9d03bb2e
22 changed files with 125 additions and 19 deletions
+2
View File
@@ -10,6 +10,8 @@ Changes:
- Use Promise resolve/reject to track state in typegen on-chain metadata retrievals
- Basic support for markdown generation for different chains (only Kusama & Polkadot currently added)
- Add not-to-be-used `ISize` type support (same as `USize`, throwing on use - added for completeness)
- Expand tests around `toU8a()` where the `isBare` flag is is being used
## 10.1.4 Mar 19, 2023
@@ -6,7 +6,7 @@
import '@polkadot/types/types/registry';
import type { Data, StorageKey } from '@polkadot/types';
import type { BitVec, Bool, Bytes, F32, F64, I128, I16, I256, I32, I64, I8, Json, Null, OptionBool, Raw, Text, Type, U128, U16, U256, U32, U64, U8, USize, bool, f32, f64, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from '@polkadot/types-codec';
import type { BitVec, Bool, Bytes, F32, F64, I128, I16, I256, I32, I64, I8, ISize, Json, Null, OptionBool, Raw, Text, Type, U128, U16, U256, U32, U64, U8, USize, bool, f32, f64, i128, i16, i256, i32, i64, i8, isize, u128, u16, u256, u32, u64, u8, usize } from '@polkadot/types-codec';
import type { AssetApproval, AssetApprovalKey, AssetBalance, AssetDestroyWitness, AssetDetails, AssetMetadata, TAssetBalance, TAssetDepositBalance } from '@polkadot/types/interfaces/assets';
import type { BlockAttestations, IncludedBlocks, MoreAttestations } from '@polkadot/types/interfaces/attestations';
import type { RawAuraPreDigest } from '@polkadot/types/interfaces/aura';
@@ -549,6 +549,8 @@ declare module '@polkadot/types/types/registry' {
I64: I64;
i8: i8;
I8: I8;
isize: isize;
ISize: ISize;
IdentificationTuple: IdentificationTuple;
IdentityFields: IdentityFields;
IdentityInfo: IdentityInfo;
@@ -90,6 +90,10 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
};
}
/**
* @internal
* @description Internal per-item inspection of internal values
*/
public inspectInner (): Inspect[] {
const inner = new Array<Inspect>(this.length);
@@ -185,6 +189,11 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
: u8aConcatStrict([compactToU8a(this.length), ...encoded]);
}
/**
* @internal
* @description Internal per-item SCALE encoding of contained values
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8aInner (isBare?: boolean): Uint8Array[] {
const encoded = new Array<Uint8Array>(this.length);
-1
View File
@@ -260,7 +260,6 @@ export abstract class AbstractInt extends BN implements INumber {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (_isBare?: boolean): Uint8Array {
return bnToU8a(this, {
-1
View File
@@ -193,7 +193,6 @@ export class Compact<T extends INumber> implements ICompact<T> {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (_isBare?: boolean): Uint8Array {
return compactToU8a(this.#raw.toBn());
@@ -53,38 +53,65 @@ export class DoNotConstruct implements Codec {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
eq (): boolean {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
public inspect (): Inspect {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
toHex (): HexString {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
toHuman (): AnyJson {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
toJSON (): AnyJson {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
toPrimitive (): AnyJson {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
toRawType (): string {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
toString (): string {
throw this.#neverError;
}
/**
* @description Unimplemented
*/
toU8a (): Uint8Array {
throw this.#neverError;
}
@@ -460,5 +460,28 @@ describe('Enum', (): void => {
});
});
describe('toU8a', (): void => {
const Test = Enum.with({
A: Text,
B: U32
});
it('has a correct toU8a() output', (): void => {
expect(
new Test(registry, { B: 69 }).toU8a()
).toEqual(
new Uint8Array([1, 69, 0, 0, 0])
);
});
it('has a correct toU8a(true) output', (): void => {
expect(
new Test(registry, { B: 69 }).toU8a(true)
).toEqual(
new Uint8Array([69, 0, 0, 0])
);
});
});
perf('Enum', 20_000, [[new Uint8Array([0, 31, 32, 33, 34])]], (v: Uint8Array) => new PEnum(registry, v));
});
-1
View File
@@ -89,7 +89,6 @@ export class Null implements Codec {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (_isBare?: boolean): Uint8Array {
return new Uint8Array();
@@ -147,6 +147,12 @@ describe('Option', (): void => {
).toEqual(new Uint8Array([1, 4 << 2, 49, 50, 51, 52]));
});
it('allows bare specifiers on toU8a', (): void => {
expect(
new Option(registry, Text, '1234').toU8a(true)
).toEqual(new Uint8Array([49, 50, 51, 52]));
});
it('converts toU8a() without', (): void => {
expect(
new Option(registry, Text).toU8a()
+11 -1
View File
@@ -86,7 +86,7 @@ describe('Vec', (): void => {
expect(vector.toString()).toEqual('[1, 23, 345, 4567, 56789]');
});
it('encodes with length prefix', (): void => {
it('encodes with length prefix on toU8a()', (): void => {
expect(vector.toU8a()).toEqual(new Uint8Array([
5 << 2,
1 << 2, 49,
@@ -97,6 +97,16 @@ describe('Vec', (): void => {
]));
});
it('encodes without length prefix on toU8a(true)', (): void => {
expect(vector.toU8a(true)).toEqual(new Uint8Array([
1 << 2, 49,
2 << 2, 50, 51,
3 << 2, 51, 52, 53,
4 << 2, 52, 53, 54, 55,
5 << 2, 53, 54, 55, 56, 57
]));
});
it('exposes the type', (): void => {
expect(vector.Type).toEqual('Text');
});
+4 -1
View File
@@ -73,6 +73,9 @@ export class BitVec extends Raw {
};
}
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public override toHuman (): string {
return `0b${
[...this.toU8a(true)]
@@ -94,7 +97,7 @@ export class BitVec extends Raw {
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public override toU8a (isBare?: boolean): Uint8Array {
const bitVec = super.toU8a();
const bitVec = super.toU8a(isBare);
return isBare
? bitVec
+8 -2
View File
@@ -34,10 +34,16 @@ export class Linkage<T extends Codec> extends Struct {
};
}
/**
* @description Returns the next item the Linkage is pointing to
*/
public get previous (): Option<T> {
return this.get('previous') as Option<T>;
}
/**
* @description Returns the previous item the Linkage is pointing to
*/
public get next (): Option<T> {
return this.get('next') as Option<T>;
}
@@ -52,12 +58,12 @@ export class Linkage<T extends Codec> extends Struct {
/**
* @description Custom toU8a which with bare mode does not return the linkage if empty
*/
public override toU8a (): Uint8Array {
public override toU8a (isBare?: boolean): Uint8Array {
// As part of a storage query (where these appear), in the case of empty, the values
// are NOT populated by the node - follow the same logic, leaving it empty
return this.isEmpty
? EMPTY
: super.toU8a();
: super.toU8a(isBare);
}
}
-1
View File
@@ -130,7 +130,6 @@ export class bool extends Boolean implements Codec {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (_isBare?: boolean): Uint8Array {
return new Uint8Array([this.valueOf() ? 1 : 0]);
-1
View File
@@ -162,7 +162,6 @@ export class CodecDate extends Date implements INumber {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (_isBare?: boolean): Uint8Array {
return bnToU8a(this.toNumber(), U8A_OPTS);
+4 -2
View File
@@ -128,7 +128,9 @@ export class Float extends Number implements IFloat {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
*/
public toU8a (): Uint8Array {
return floatToU8a(this, { bitLength: this.#bitLength });
public toU8a (_isBare?: boolean): Uint8Array {
return floatToU8a(this, {
bitLength: this.#bitLength
});
}
}
-1
View File
@@ -153,7 +153,6 @@ export class Raw extends Uint8Array implements IU8a {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (_isBare?: boolean): Uint8Array {
return Uint8Array.from(this);
-1
View File
@@ -257,7 +257,6 @@ export class CodecSet extends Set<string> implements ISet<string> {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (_isBare?: boolean): Uint8Array {
return bnToU8a(this.valueEncoded, {
@@ -0,0 +1,21 @@
// Copyright 2017-2023 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Registry } from '../types/index.js';
import { i32 } from './I32.js';
/**
* @name ISize
* @description
* A System default signed number, typically used in RPC to report non-consensus
* data. It is a wrapper for [[I32]] as a WASM default (as generated by Rust bindings).
* It is not to be used, since it creates consensus mismatches.
*/
export class isize extends i32 {
constructor (registry: Registry, value?: unknown) {
super(registry, value);
throw new Error('The `osize` type should not be used. Since it is platform-specific, it creates incompatibilities between native (generally i64) and WASM (always i32) code. Use one of the `i32` or `i64` types explicitly.');
}
}
@@ -9,6 +9,7 @@ export { i32, i32 as I32 } from './I32.js';
export { i64, i64 as I64 } from './I64.js';
export { i128, i128 as I128 } from './I128.js';
export { i256, i256 as I256 } from './I256.js';
export { isize, isize as ISize } from './ISize.js';
export { u8, u8 as U8 } from './U8.js';
export { u16, u16 as U16 } from './U16.js';
export { u32, u32 as U32 } from './U32.js';
+2 -1
View File
@@ -111,7 +111,8 @@ export interface Codec {
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
* @param isBare true when the value has none of the type-specific prefixes (internal use, only available on
* some Codec types, specifically those that add encodings such as length of indexes)
*/
toU8a (isBare?: BareOpts): Uint8Array;
}
+1 -1
View File
@@ -4,7 +4,7 @@
/**
* @summary Type definitions that are used in the system
*/
export { BitVec, bool, Bool, Bytes, f32, F32, f64, F64, i8, I8, i16, I16, i32, I32, i64, I64, i128, I128, i256, I256, Null, OptionBool, Text, Type, u8, U8, u16, U16, u32, U32, u64, U64, u128, U128, u256, U256, usize, USize } from '@polkadot/types-codec';
export { BitVec, bool, Bool, Bytes, f32, F32, f64, F64, i8, I8, i16, I16, i32, I32, i64, I64, i128, I128, i256, I256, isize, ISize, Null, OptionBool, Text, Type, u8, U8, u16, U16, u32, U32, u64, U64, u128, U128, u256, U256, usize, USize } from '@polkadot/types-codec';
export { Data } from './Data.js';
export { StorageKey } from './StorageKey.js';
+3 -3
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { Observable } from 'rxjs';
import type { BitVec, Bool, bool, Bytes, F32, f32, F64, f64, I8, i8, I16, i16, I32, i32, I64, i64, I128, i128, I256, i256, Json, Null, OptionBool, Raw, Text, Type, U8, u8, U16, u16, U32, u32, U64, u64, U128, u128, U256, u256, USize, usize } from '@polkadot/types-codec';
import type { BitVec, Bool, bool, Bytes, F32, f32, F64, f64, I8, i8, I16, i16, I32, i32, I64, i64, I128, i128, I256, i256, ISize, isize, Json, Null, OptionBool, Raw, Text, Type, U8, u8, U16, u16, U32, u32, U64, u64, U128, u128, U256, u256, USize, usize } from '@polkadot/types-codec';
import type { RegistryTypes } from '@polkadot/types-codec/types';
import type { BN } from '@polkadot/util';
import type { HexString } from '@polkadot/util/types';
@@ -21,9 +21,9 @@ export interface InterfaceTypes {
// base codec
BitVec: BitVec, Bytes: Bytes, Json: Json, Null: Null, OptionBool: OptionBool, Raw: Raw, Text: Text, Type: Type,
// base codec - upper variants
Bool: Bool, F32: F32, F64: F64, I128: I128, I16: I16, I256: I256, I32: I32, I64: I64, I8: I8, U128: U128, U16: U16, U256: U256, U32: U32, U64: U64, U8: U8, USize: USize,
Bool: Bool, F32: F32, F64: F64, I128: I128, I16: I16, I256: I256, I32: I32, I64: I64, I8: I8, ISize: ISize, U128: U128, U16: U16, U256: U256, U32: U32, U64: U64, U8: U8, USize: USize,
// base codec - lower variants
bool: bool, f32: f32, f64: f64, i128: i128, i16: i16, i256: i256, i32: i32, i64: i64, i8: i8, u128: u128, u16: u16, u256: u256, u32: u32, u64: u64, u8: u8, usize: usize,
bool: bool, f32: f32, f64: f64, i128: i128, i16: i16, i256: i256, i32: i32, i64: i64, i8: i8, isize: isize, u128: u128, u16: u16, u256: u256, u32: u32, u64: u64, u8: u8, usize: usize,
// extrinsic
Extrinsic: GenericExtrinsic, ExtrinsicEra: GenericExtrinsicEra, ExtrinsicPayload: GenericExtrinsicPayload, SignerPayload: GenericSignerPayload,
// generic