From 13ae360c7a51bfc271a1ff18100c93bf15e212a8 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 7 May 2019 16:01:35 +0200 Subject: [PATCH] assert type on all asXxx getters (#879) * assert type on all asXxx getters * Convert via built-in asVx * Implicit index * Re-add V0 (not dropped here, but dropped somewhere) * MetadataDeprectated -> MetadataDeprecated --- .../types/src/Metadata/MetadataVersioned.ts | 71 ++++++++++++++++--- packages/types/src/Metadata/v0/Storage.ts | 27 +++++-- packages/types/src/Metadata/v2/Storage.ts | 23 +++++- packages/types/src/Metadata/v3/Storage.ts | 15 ++++ packages/types/src/Metadata/v4/Storage.ts | 15 ++++ packages/types/src/rpc/ExtrinsicStatus.ts | 16 +++++ packages/types/src/type/AttestedCandidate.ts | 6 ++ packages/types/src/type/ContractInfo.ts | 4 +- packages/types/src/type/Digest.ts | 35 +++++++++ packages/types/src/type/EventRecord.ts | 6 ++ packages/types/src/type/MisbehaviorReport.ts | 38 ++++++++++ 11 files changed, 237 insertions(+), 19 deletions(-) diff --git a/packages/types/src/Metadata/MetadataVersioned.ts b/packages/types/src/Metadata/MetadataVersioned.ts index ec45ba1dfe..db226e180f 100644 --- a/packages/types/src/Metadata/MetadataVersioned.ts +++ b/packages/types/src/Metadata/MetadataVersioned.ts @@ -20,13 +20,16 @@ import v2ToV1 from './v2/toV1'; import v3ToV2 from './v3/toV2'; import v4ToV3 from './v4/toV3'; -class MetadataEnum extends EnumType { +class MetadataDeprecated extends Null { +} + +class MetadataEnum extends EnumType { constructor (value?: any) { super({ - MetadataV0, // once rolled-out, can replace this with Null - MetadataV1, // once rolled-out, can replace this with Null - MetadataV2, // once rolled-out, can replace this with Null - MetadataV3, // once rolled-out, can replace this with Null + MetadataV0, // once rolled-out, can replace this with MetadataDeprecated + MetadataV1, // once rolled-out, can replace this with MetadataDeprecated + MetadataV2, // once rolled-out, can replace this with MetadataDeprecated + MetadataV3, // once rolled-out, can replace this with MetadataDeprecated MetadataV4 }, value); } @@ -35,6 +38,8 @@ class MetadataEnum extends EnumType { * @description Returns the item as an [[Alive]] */ get asAlive (): Alive { - assert(this.isAlive, `Cannot convert index ${this.toNumber()} to 'Alive'`); + assert(this.isAlive, `Cannot convert '${this.type}' via asAlive`); return this.value as Alive; } @@ -83,7 +83,7 @@ export default class ContractInfo extends EnumType { * @description Returns the item as an [[Tombstone]] */ get asTombstone (): Tombstone { - assert(this.isTombstone, `Cannot convert index ${this.toNumber()} to 'Tombstone'`); + assert(this.isTombstone, `Cannot convert '${this.type}' via asTombstone`); return this.value as Tombstone; } diff --git a/packages/types/src/type/Digest.ts b/packages/types/src/type/Digest.ts index ae90191681..7e1be5fc73 100644 --- a/packages/types/src/type/Digest.ts +++ b/packages/types/src/type/Digest.ts @@ -2,6 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { assert } from '@polkadot/util'; + import EnumType from '../codec/EnumType'; import Struct from '../codec/Struct'; import Tuple from '../codec/Tuple'; @@ -103,6 +105,8 @@ export class Consensus extends Tuple { * @description The slot and signature extracted from the raw data (assuming Aura) */ get asAura (): [U64, Signature] { + assert(this.isAura, 'Invalid engine for asAura conversion'); + const raw = this.data.toU8a(true); return [ @@ -133,6 +137,8 @@ export class DigestItem extends EnumType { * @description Returns the item as a [[ApplyExtrinsic]] */ get asApplyExtrinsic (): ApplyExtrinsic { + assert(this.isApplyExtrinsic, `Cannot convert '${this.type}' via asApplyExtrinsic`); + return this.value as ApplyExtrinsic; } @@ -48,6 +52,8 @@ export class Phase extends EnumType { * @description Returns the item as a [[Finalization]] */ get asFinalization (): Finalization { + assert(this.isFinalization, `Cannot convert '${this.type}' via asFinalization`); + return this.value as Finalization; } diff --git a/packages/types/src/type/MisbehaviorReport.ts b/packages/types/src/type/MisbehaviorReport.ts index 07100c71df..9ce18c7aa5 100644 --- a/packages/types/src/type/MisbehaviorReport.ts +++ b/packages/types/src/type/MisbehaviorReport.ts @@ -4,6 +4,8 @@ import { AnyNumber } from '../types'; +import { assert } from '@polkadot/util'; + import EnumType from '../codec/EnumType'; import Struct from '../codec/Struct'; import U32 from '../primitive/U32'; @@ -132,6 +134,8 @@ export class MisbehaviorKind extends EnumType