Adjust AbstractInt asserts (#4898)

* Adjust AbstractInt asserts

* Adjust

* Number.isInteger
This commit is contained in:
Jaco
2022-06-07 14:43:39 +03:00
committed by GitHub
parent ea36b6d486
commit df74330b67
2 changed files with 23 additions and 13 deletions
@@ -31,7 +31,7 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array>, 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');
}
+22 -12
View File
@@ -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;
}
/**