From df74330b67c2d934ce8f001aa697c19e7a7e42a5 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 7 Jun 2022 13:43:39 +0200 Subject: [PATCH] Adjust AbstractInt asserts (#4898) * Adjust AbstractInt asserts * Adjust * Number.isInteger --- .../types-codec/src/abstract/AbstractInt.ts | 2 +- packages/types/src/extrinsic/ExtrinsicEra.ts | 34 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/packages/types-codec/src/abstract/AbstractInt.ts b/packages/types-codec/src/abstract/AbstractInt.ts index 038e8764d1..7231e1d0b3 100644 --- a/packages/types-codec/src/abstract/AbstractInt.ts +++ b/packages/types-codec/src/abstract/AbstractInt.ts @@ -31,7 +31,7 @@ function decodeAbstractInt (value: Exclude, isNegative: b } else if (isHex(value, -1, true)) { return hexToBn(value, { isLe: false, isNegative }).toString(); } else if (isNumber(value)) { - assert(value <= Number.MAX_SAFE_INTEGER && value >= Number.MIN_SAFE_INTEGER && Math.floor(value) === value, 'Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1'); + assert(value <= Number.MAX_SAFE_INTEGER && value >= Number.MIN_SAFE_INTEGER && Number.isInteger(value), 'Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1'); } else if (isString(value)) { assert(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation'); } diff --git a/packages/types/src/extrinsic/ExtrinsicEra.ts b/packages/types/src/extrinsic/ExtrinsicEra.ts index f7c6621e30..4bd9f5f17b 100644 --- a/packages/types/src/extrinsic/ExtrinsicEra.ts +++ b/packages/types/src/extrinsic/ExtrinsicEra.ts @@ -188,26 +188,36 @@ export class MortalEra extends Tuple { // eslint-disable-next-line @typescript-eslint/no-unused-vars public override toU8a (isBare?: boolean): Uint8Array { const period = this.period.toNumber(); - const phase = this.phase.toNumber(); - const quantizeFactor = Math.max(period >> 12, 1); - const trailingZeros = getTrailingZeros(period); - const encoded = Math.min(15, Math.max(1, trailingZeros - 1)) + (((phase / quantizeFactor) << 4)); - const first = encoded >> 8; - const second = encoded & 0xff; + const encoded = Math.min( + 15, + Math.max(1, getTrailingZeros(period) - 1) + ) + ( + ( + this.phase.toNumber() / Math.max(period >> 12, 1) + ) << 4 + ); - return new Uint8Array([second, first]); + return new Uint8Array([ + encoded & 0xff, + encoded >> 8 + ]); } /** * @description Get the block number of the start of the era whose properties this object describes that `current` belongs to. */ public birth (current: BN | bigint | number | string): number { + const phase = this.phase.toNumber(); + const period = this.period.toNumber(); + // FIXME No toNumber() here - return Math.floor( - ( - Math.max(bnToBn(current).toNumber(), this.phase.toNumber()) - this.phase.toNumber() - ) / this.period.toNumber() - ) * this.period.toNumber() + this.phase.toNumber(); + return ( + ~~( + ( + Math.max(bnToBn(current).toNumber(), phase) - phase + ) / period + ) * period + ) + phase; } /**