From 1a5f8fa4e18a03eb3ec8718f8ef7e7018e935226 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 9 Feb 2021 10:44:17 +0100 Subject: [PATCH] Protect against overflows in target calcs (#4633) --- packages/apps-config/src/settings/ss58.ts | 13 +++++-------- packages/page-staking/src/useSortedTargets.ts | 7 +++++-- packages/react-hooks/src/useLedger.ts | 7 +------ 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/apps-config/src/settings/ss58.ts b/packages/apps-config/src/settings/ss58.ts index 93af62a0ba..d0b406adcd 100644 --- a/packages/apps-config/src/settings/ss58.ts +++ b/packages/apps-config/src/settings/ss58.ts @@ -2,18 +2,15 @@ // SPDX-License-Identifier: Apache-2.0 import type { TFunction } from 'i18next'; -import type { Network } from '@polkadot/networks/types'; import type { Option } from './types'; import known from '@polkadot/networks'; -interface NetworkNamed extends Network { - network: string; -} - -const networks = known - .filter((n): n is NetworkNamed => !!n.network) - .map(({ displayName, network, prefix }) => ({ info: network, text: displayName, value: prefix })); +const networks = known.map(({ displayName, network, prefix }) => ({ + info: network, + text: displayName, + value: prefix +})); // Definitions here are with the following values - // info: the name of a logo as defined in ../logos, specifically in namedLogos diff --git a/packages/page-staking/src/useSortedTargets.ts b/packages/page-staking/src/useSortedTargets.ts index fa440f03bd..b52e98db9e 100644 --- a/packages/page-staking/src/useSortedTargets.ts +++ b/packages/page-staking/src/useSortedTargets.ts @@ -9,7 +9,7 @@ import BN from 'bn.js'; import { useMemo } from 'react'; import { calcInflation, useAccounts, useApi, useCall } from '@polkadot/react-hooks'; -import { arrayFlatten, BN_MILLION, BN_ONE, BN_ZERO } from '@polkadot/util'; +import { arrayFlatten, BN_HUNDRED, BN_MAX_INTEGER, BN_ONE, BN_ZERO } from '@polkadot/util'; interface LastEra { activeEra: BN; @@ -172,7 +172,10 @@ function extractInfo (api: ApiPromise, allAccounts: string[], electedDerive: Der // add the explicit stakedReturn !avgStaked.isZero() && elected.forEach((e): void => { if (!e.skipRewards) { - e.stakedReturn = inflation.stakedReturn * avgStaked.mul(BN_MILLION).div(e.bondTotal).toNumber() / BN_MILLION.toNumber(); + const adjusted = avgStaked.mul(BN_HUNDRED).imuln(inflation.stakedReturn).div(e.bondTotal); + + // in some cases, we may have overflows... protect against those + e.stakedReturn = (adjusted.gt(BN_MAX_INTEGER) ? BN_MAX_INTEGER : adjusted).toNumber() / BN_HUNDRED.toNumber(); e.stakedReturnCmp = e.stakedReturn * (100 - e.commissionPer) / 100; } }); diff --git a/packages/react-hooks/src/useLedger.ts b/packages/react-hooks/src/useLedger.ts index f6351fe861..615f7813fe 100644 --- a/packages/react-hooks/src/useLedger.ts +++ b/packages/react-hooks/src/useLedger.ts @@ -3,7 +3,6 @@ import type { ApiPromise } from '@polkadot/api'; import type { LedgerTypes } from '@polkadot/hw-ledger/types'; -import type { Network } from '@polkadot/networks/types'; import { useCallback, useMemo } from 'react'; @@ -23,17 +22,13 @@ interface State extends StateBase { getLedger: () => Ledger; } -interface NetworkNamed extends Network { - network: string; -} - const EMPTY_STATE: StateBase = { isLedgerCapable: false, isLedgerEnabled: false }; const hasWebUsb = !!(window as unknown as { USB?: unknown }).USB; -const ledgerChains = networks.filter((n): n is NetworkNamed => !!(n.hasLedgerSupport && n.network)); +const ledgerChains = networks.filter((n) => !!n.hasLedgerSupport); let ledger: Ledger | null = null; function retrieveLedger (api: ApiPromise): Ledger {