From 993bc05892ac9ce04ae0d4e9fa0a1b9d9b92936f Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 17 Jun 2019 10:41:57 +0200 Subject: [PATCH] USize is now non-constructable (#1022) * USize is now unconstructable * usize CHANGELOG entry --- CHANGELOG.md | 1 + packages/types/src/index.spec.ts | 5 +++-- packages/types/src/primitive/USize.ts | 9 ++++++++- packages/types/src/rpc/Health.ts | 10 +++++----- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 253ad74392..97c943789d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # 0.82.0-beta.x - **Breaking change** The `ContractsAbi` type has been moved from `@polkadot/types` to `import { Abi } from '@polkadot/api-contract`. This paves the way for an enhanced contracts interface, instead of dealing with low-level API calls. +- **Breaking change** `usize` is now a blacklisted type that will throw on construction. Since it is platform-specific, it creates incompatibilities between native (generally u64) and WASM (always u32) code. Use one of the `u32` or `u64` types explicitly. - Update with latest substrate 2.x types # 0.81.1 diff --git a/packages/types/src/index.spec.ts b/packages/types/src/index.spec.ts index c7c0701e28..981042bdef 100644 --- a/packages/types/src/index.spec.ts +++ b/packages/types/src/index.spec.ts @@ -8,6 +8,7 @@ import { Constructor } from './types'; import * as Classes from './index.types'; const Types = Classes as { [index: string]: Constructor }; +const UNCONSTRUCTABLE = ['origin', 'usize']; describe('types', () => { describe('default creation', () => { @@ -16,7 +17,7 @@ describe('types', () => { const constructFn = () => new Types[name](); - if (name === 'Origin') { + if (UNCONSTRUCTABLE.includes(name.toLowerCase())) { expect(constructFn).toThrow(); } else { expect(constructFn).not.toThrow(); @@ -33,7 +34,7 @@ describe('types', () => { const constructFn = () => new Types[name](new Types.Bytes()); - if (name === 'Origin') { + if (UNCONSTRUCTABLE.includes(name.toLowerCase())) { expect(constructFn).toThrow(); } else { expect(constructFn).not.toThrow(); diff --git a/packages/types/src/primitive/USize.ts b/packages/types/src/primitive/USize.ts index cf33a68d56..1b4acf74e6 100644 --- a/packages/types/src/primitive/USize.ts +++ b/packages/types/src/primitive/USize.ts @@ -8,9 +8,16 @@ import U32 from './U32'; * @name USize * @description * A System default unsigned number, typically used in RPC to report non-consensus - * data. It is a wrapper for [[U32]] as a WASM default (as generated by Rust bindings) + * data. It is a wrapper for [[U32]] as a WASM default (as generated by Rust bindings). + * It is not to be used, since it created consensus mismatches. */ export default class USize extends U32 { + constructor (value?: any) { + super(value); + + throw new Error('The `usize` type should not be used. Since it is platform-specific, it creates incompatibilities between native (generally u64) and WASM (always u32) code. Use one of the `u32` or `u64` types explicitly.'); + } + /** * @description Returns the base runtime type name for this instance */ diff --git a/packages/types/src/rpc/Health.ts b/packages/types/src/rpc/Health.ts index 4992051321..19aa59db5e 100644 --- a/packages/types/src/rpc/Health.ts +++ b/packages/types/src/rpc/Health.ts @@ -4,7 +4,7 @@ import Struct from '../codec/Struct'; import Bool from '../primitive/Bool'; -import USize from '../primitive/USize'; +import U64 from '../primitive/U64'; /** * @name Health @@ -14,7 +14,7 @@ import USize from '../primitive/USize'; export default class Health extends Struct { constructor (value?: any) { super({ - peers: USize, + peers: U64, isSyncing: Bool, shouldHavePeers: Bool }, value); @@ -28,10 +28,10 @@ export default class Health extends Struct { } /** - * @description The numbers of peers as [[U32]] + * @description The numbers of peers as [[U64]] */ - get peers (): USize { - return this.get('peers') as USize; + get peers (): U64 { + return this.get('peers') as U64; } /**