Protect against overflows in target calcs (#4633)

This commit is contained in:
Jaco Greeff 2021-02-09 10:44:17 +01:00 committed by GitHub
parent 7329f882d2
commit 1a5f8fa4e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 16 deletions

View File

@ -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

View File

@ -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;
}
});

View File

@ -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 {