diff --git a/CHANGELOG.md b/CHANGELOG.md index a0bb6888d4..0a8d88843a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fix subscriptions never unsubscribing after the id type swap in 1.18.1 - Promise API will default to using `getStorage` on non-subscription calls, reducing RPC overhead +- Optimize derive BN allocations, using in-place operators as applicable ## 1.20.1 Jun 22, 2020 diff --git a/packages/api-derive/src/democracy/util.ts b/packages/api-derive/src/democracy/util.ts index 3916554d18..2764c00dee 100644 --- a/packages/api-derive/src/democracy/util.ts +++ b/packages/api-derive/src/democracy/util.ts @@ -82,15 +82,15 @@ function calcVotesPrev (votesFor: DeriveReferendumVote[]): DeriveReferendumVoteS if (vote.isAye) { state.allAye.push(derived); state.voteCountAye++; - state.votedAye = state.votedAye.add(counted); + state.votedAye.iadd(counted); } else { state.allNay.push(derived); state.voteCountNay++; - state.votedNay = state.votedNay.add(counted); + state.votedNay.iadd(counted); } state.voteCount++; - state.votedTotal = state.votedTotal.add(counted); + state.votedTotal.iadd(counted); return state; }, { diff --git a/packages/api-derive/src/session/progress.ts b/packages/api-derive/src/session/progress.ts index 2218121942..286b226b37 100644 --- a/packages/api-derive/src/session/progress.ts +++ b/packages/api-derive/src/session/progress.ts @@ -17,9 +17,9 @@ type ResultSlots = [u64, u64, u64, Option]; type ResultSlotsFlat = [u64, u64, u64, SessionIndex]; function createDerive (api: ApiInterfaceRx, info: DeriveSessionInfo, [currentSlot, epochIndex, epochOrGenesisStartSlot, activeEraStartSessionIndex]: ResultSlotsFlat): DeriveSessionProgress { - const epochStartSlot = epochIndex.mul(info.sessionLength).add(epochOrGenesisStartSlot); + const epochStartSlot = epochIndex.mul(info.sessionLength).iadd(epochOrGenesisStartSlot); const sessionProgress = currentSlot.sub(epochStartSlot); - const eraProgress = info.currentIndex.sub(activeEraStartSessionIndex).mul(info.sessionLength).add(sessionProgress); + const eraProgress = info.currentIndex.sub(activeEraStartSessionIndex).imul(info.sessionLength).iadd(sessionProgress); return { ...info, diff --git a/packages/api-derive/src/staking/account.ts b/packages/api-derive/src/staking/account.ts index bc70f6e397..6dda004772 100644 --- a/packages/api-derive/src/staking/account.ts +++ b/packages/api-derive/src/staking/account.ts @@ -21,7 +21,7 @@ function groupByEra (list: UnlockChunk[]): Record { map[key] = !map[key] ? value.unwrap() - : map[key].add(value.unwrap()); + : map[key].iadd(value.unwrap()); return map; }, {}); @@ -32,8 +32,8 @@ function calculateUnlocking (api: ApiInterfaceRx, stakingLedger: StakingLedger | return undefined; } - const unlockingChunks = stakingLedger.unlocking.filter(({ era }): boolean => - era.unwrap().sub(sessionInfo.activeEra).gtn(0) + const unlockingChunks = stakingLedger.unlocking.filter(({ era }) => + era.unwrap().gt(sessionInfo.activeEra) ); if (!unlockingChunks.length) { @@ -43,11 +43,13 @@ function calculateUnlocking (api: ApiInterfaceRx, stakingLedger: StakingLedger | // group the unlock chunks that have the same era and sum their values const groupedResult = groupByEra(unlockingChunks); const results = Object.entries(groupedResult).map(([eraString, value]): DeriveUnlocking => ({ - remainingEras: new BN(eraString).sub(sessionInfo.activeEra), + remainingEras: new BN(eraString).isub(sessionInfo.activeEra), value: api.registry.createType('Balance', value) })); - return results.length ? results : undefined; + return results.length + ? results + : undefined; } function redeemableSum (api: ApiInterfaceRx, stakingLedger: StakingLedger | undefined, sessionInfo: DeriveSessionInfo): Balance { @@ -57,7 +59,7 @@ function redeemableSum (api: ApiInterfaceRx, stakingLedger: StakingLedger | unde return api.registry.createType('Balance', stakingLedger.unlocking.reduce((total, { era, value }): BN => { return sessionInfo.activeEra.gte(era.unwrap()) - ? total.add(value.unwrap()) + ? total.iadd(value.unwrap()) : total; }, new BN(0))); } @@ -70,6 +72,21 @@ function parseResult (api: ApiInterfaceRx, sessionInfo: DeriveSessionInfo, query }; } +/** + * @description From a list of stashes, fill in all the relevant staking details + */ +export function accounts (api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[]) => Observable { + return memo((accountIds: (Uint8Array | string)[]): Observable => + api.derive.session.info().pipe( + switchMap((sessionInfo) => + api.derive.staking.queryMulti(accountIds).pipe( + map((queries) => queries.map((query) => parseResult(api, sessionInfo, query))) + ) + ) + ) + ); +} + /** * @description From a stash, retrieve the controllerId and fill in all the relevant staking details */ @@ -80,20 +97,3 @@ export function account (api: ApiInterfaceRx): (accountId: Uint8Array | string) ) ); } - -/** - * @description From a list of stashes, fill in all the relevant staking details - */ -export function accounts (api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[]) => Observable { - return memo((accountIds: (Uint8Array | string)[]): Observable => - api.derive.session.info().pipe( - switchMap((sessionInfo) => - api.derive.staking.queryMulti(accountIds).pipe( - map((queries) => - queries.map((query) => parseResult(api, sessionInfo, query)) - ) - ) - ) - ) - ); -} diff --git a/packages/api-derive/src/staking/stakerRewards.ts b/packages/api-derive/src/staking/stakerRewards.ts index 37859777d0..e4acb76e57 100644 --- a/packages/api-derive/src/staking/stakerRewards.ts +++ b/packages/api-derive/src/staking/stakerRewards.ts @@ -52,7 +52,7 @@ function parseRewards (api: ApiInterfaceRx, stashId: AccountId, [, erasPoints, e : ZERO; } - value = avail.sub(valCut).mul(staked).div(expTotal).add(validatorId === stakerId ? valCut : ZERO); + value = avail.sub(valCut).imul(staked).div(expTotal).iadd(validatorId === stakerId ? valCut : ZERO); } validators[validatorId] = { diff --git a/packages/api-derive/src/staking/validators.ts b/packages/api-derive/src/staking/validators.ts index 9f970a37d7..4ce352b9de 100644 --- a/packages/api-derive/src/staking/validators.ts +++ b/packages/api-derive/src/staking/validators.ts @@ -18,7 +18,7 @@ export function nextElected (api: ApiInterfaceRx): () => Observable // only populate for next era in the last session, so track both here - entries are not // subscriptions, so we need a trigger - currentIndex acts as that trigger to refresh switchMap(({ currentEra }) => api.query.staking.erasStakers.keys(currentEra)), - map((keys) => keys.map((key): AccountId => key.args[1] as AccountId)) + map((keys) => keys.map((key) => key.args[1] as AccountId)) ) : api.query.staking.currentElected() ); diff --git a/packages/api-derive/src/tx/signingInfo.ts b/packages/api-derive/src/tx/signingInfo.ts index 9f32bcfdd7..acf95e4e41 100644 --- a/packages/api-derive/src/tx/signingInfo.ts +++ b/packages/api-derive/src/tx/signingInfo.ts @@ -20,7 +20,7 @@ interface Result { function latestNonce (api: ApiInterfaceRx, address: string): Observable { return api.derive.balances.account(address).pipe( - map(({ accountNonce }): Index => accountNonce) + map(({ accountNonce }) => accountNonce) ); } @@ -31,7 +31,7 @@ function signingHeader (api: ApiInterfaceRx): Observable
{ switchMap((hash) => api.rpc.chain.getHeader(hash)) ) ]).pipe( - map(([current, finalized]): Header => + map(([current, finalized]) => // determine the hash to use, current when lag > max, else finalized current.number.unwrap().sub(finalized.number.unwrap()).gt(MAX_FINALITY_LAG) ? current @@ -57,7 +57,7 @@ export function signingInfo (api: ApiInterfaceRx): (address: string, nonce?: Any header, mortalLength: MORTAL_PERIOD .div(api.consts.babe?.expectedBlockTime || api.consts.timestamp?.minimumPeriod.muln(2) || FALLBACK_PERIOD) - .add(MAX_FINALITY_LAG) + .iadd(MAX_FINALITY_LAG) .toNumber(), nonce }))