From 41cf32c8089a6f3b56cd4a416384c235581c98f2 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 29 Oct 2019 17:49:42 +0100 Subject: [PATCH] Flatten more combineLatest into queryMulti (#1528) --- package.json | 4 +- packages/api-derive/src/session/info.ts | 68 ++++++++++--------- .../api-derive/src/session/sessionProgress.ts | 9 +-- packages/api-derive/src/staking/info.ts | 42 ++++++------ yarn.lock | 16 ++--- 5 files changed, 73 insertions(+), 66 deletions(-) diff --git a/package.json b/package.json index c2e215281d..7db00958df 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "@babel/core": "^7.6.4", "@babel/register": "^7.6.2", "@babel/runtime": "^7.6.3", - "@polkadot/dev": "^0.32.0-beta.11", - "@polkadot/ts": "^0.1.82" + "@polkadot/dev": "^0.32.0-beta.12", + "@polkadot/ts": "^0.1.83" } } diff --git a/packages/api-derive/src/session/info.ts b/packages/api-derive/src/session/info.ts index 5381ca127c..69e89e2727 100644 --- a/packages/api-derive/src/session/info.ts +++ b/packages/api-derive/src/session/info.ts @@ -5,7 +5,7 @@ import { BlockNumber, EraIndex, SessionIndex } from '@polkadot/types/interfaces'; import { DerivedSessionInfo } from '../types'; -import { Observable, combineLatest, of } from 'rxjs'; +import { Observable, combineLatest } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; import { Option, u64, createType } from '@polkadot/types'; @@ -17,9 +17,9 @@ type Result94Session = [SessionIndex, Option, BlockNumber, BlockNum type Result94 = [BlockNumber, Result94Session]; type ResultIndex = [SessionIndex, EraIndex]; -type ResultSlots = [u64, u64, u64]; +type ResultSlots = [u64, u64, u64, SessionIndex, EraIndex]; type ResultType = [boolean, boolean, u64, SessionIndex]; -type Result = [ResultType, ResultSlots, ResultIndex]; +type Result = [ResultType, ResultSlots]; // internal helper to just split the logic - take all inputs, do the calculations and combine function createDerived94 ([bestNumber, [currentIndex, _lastLengthChange, sessionLength, lastEraLengthChange, sessionsPerEra]]: Result94): DerivedSessionInfo { @@ -49,7 +49,7 @@ function createDerived94 ([bestNumber, [currentIndex, _lastLengthChange, session }; } -function createDerivedLatest ([[hasBabe, isGenesisSlot, epochDuration, sessionsPerEra], [currentSlot, epochIndex, epochOrGenesisStartSlot], [currentIndex, currentEra]]: Result): DerivedSessionInfo { +function createDerivedLatest ([[hasBabe, isGenesisSlot, epochDuration, sessionsPerEra], [currentSlot, epochIndex, epochOrGenesisStartSlot, currentIndex, currentEra]]: Result): DerivedSessionInfo { const epochStartSlot = isGenesisSlot ? epochIndex .mul(epochDuration) @@ -91,33 +91,37 @@ function info94 (api: ApiInterfaceRx): Observable { ); } -function infoLatest (api: ApiInterfaceRx): Observable { - const hasBabe = !!api.consts.babe; - - return combineLatest([ - of([ - hasBabe, - hasBabe && !!api.query.babe.genesisSlot, - hasBabe - ? api.consts.babe.epochDuration as u64 - : createType('u64', 1), - api.consts.staking.sessionsPerEra as SessionIndex - ] as ResultType), - hasBabe - ? api.queryMulti([ - api.query.babe.currentSlot, - api.query.babe.epochIndex, - api.query.babe.genesisSlot - ? api.query.babe.genesisSlot - : api.query.babe.epochStartSlot - ]) - : of([createType('u64', 1), createType('u64', 1), createType('u64', 1)] as ResultSlots), - api.queryMulti([ - api.query.session.currentIndex, - api.query.staking.currentEra - ]) +function infoLatestAura (api: ApiInterfaceRx): Observable { + return api.queryMulti([ + api.query.session.currentIndex, + api.query.staking.currentEra ]).pipe( - map(createDerivedLatest), + map(([currentIndex, currentEra]): DerivedSessionInfo => + createDerivedLatest([ + [false, false, createType('u64', 1), api.consts.staking.sessionsPerEra as SessionIndex], + [createType('u64', 1), createType('u64', 1), createType('u64', 1), currentIndex, currentEra] + ]) + ), + drr() + ); +} + +function infoLatestBabe (api: ApiInterfaceRx): Observable { + return api.queryMulti([ + api.query.babe.currentSlot, + api.query.babe.epochIndex, + api.query.babe.genesisSlot + ? api.query.babe.genesisSlot + : api.query.babe.epochStartSlot, + api.query.session.currentIndex, + api.query.staking.currentEra + ]).pipe( + map((slots: ResultSlots): DerivedSessionInfo => + createDerivedLatest([ + [true, !!api.query.babe.genesisSlot, api.consts.babe.epochDuration as u64, api.consts.staking.sessionsPerEra as SessionIndex], + slots + ]) + ), drr() ); } @@ -132,6 +136,8 @@ export function info (api: ApiInterfaceRx): () => Observable // https://github.com/paritytech/substrate/commit/dbf322620948935d2bbae214504e6c668c3073ed#diff-c29f42d6b931fa93ba038dbbbfec3055 return api.query.session.lastLengthChange ? info94(api) // 1.x - : infoLatest(api); // 2.x + : api.consts.babe + ? infoLatestBabe(api) // 2.x with Babe + : infoLatestAura(api); // 2.x with Aura (not all info there) }; } diff --git a/packages/api-derive/src/session/sessionProgress.ts b/packages/api-derive/src/session/sessionProgress.ts index 6568062b4c..b8f4f66b74 100644 --- a/packages/api-derive/src/session/sessionProgress.ts +++ b/packages/api-derive/src/session/sessionProgress.ts @@ -2,7 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import BN from 'bn.js'; +import { BlockNumber } from '@polkadot/types/interfaces'; + import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { ApiInterfaceRx } from '@polkadot/api/types'; @@ -10,10 +11,10 @@ import { ApiInterfaceRx } from '@polkadot/api/types'; import { drr } from '../util/drr'; import { info } from './info'; -export function sessionProgress (api: ApiInterfaceRx): () => Observable { - return (): Observable => +export function sessionProgress (api: ApiInterfaceRx): () => Observable { + return (): Observable => info(api)().pipe( - map(({ sessionProgress }): BN => sessionProgress), + map(({ sessionProgress }): BlockNumber => sessionProgress), drr() ); } diff --git a/packages/api-derive/src/staking/info.ts b/packages/api-derive/src/staking/info.ts index e4a719346c..a3b82a7e36 100644 --- a/packages/api-derive/src/staking/info.ts +++ b/packages/api-derive/src/staking/info.ts @@ -22,9 +22,7 @@ import { eraLength } from '../session/eraLength'; import { recentlyOffline } from './recentlyOffline'; import { drr } from '../util/drr'; -type MultiResult = [Option, [ - Option | Vec<[AccountId, Keys] & Codec>, Option, [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs] -]]; +type MultiResult = [Option | Vec<[AccountId, Keys] & Codec>, Option, [AccountId[]], RewardDestination, Exposure, [ValidatorPrefs], Option?]; // groups the supplied chunks by era, i.e. { [era]: BN(total of values) } function groupByEra (list: UnlockChunk[]): Record { @@ -114,23 +112,25 @@ function unwrapSessionIds (stashId: AccountId, queuedKeys: Option | V } function retrieveMulti (api: ApiInterfaceRx, stashId: AccountId, controllerId: AccountId): Observable { - return combineLatest([ - // TODO We really want this as part of the multi, however can only do that - // once we drop substrate 1.x support (nulti requires values for all) - api.query.session.nextKeys - ? api.query.session.nextKeys>(api.consts.session.dedupKeyPrefix, stashId) - : of(createType('Option', null)), - api.queryMulti([ - api.query.session.queuedKeys - ? [api.query.session.queuedKeys] - : [api.query.session.nextKeyFor, controllerId], - [api.query.staking.ledger, controllerId], - [api.query.staking.nominators, stashId], - [api.query.staking.payee, stashId], - [api.query.staking.stakers, stashId], - [api.query.staking.validators, stashId] - ]) - ]) as Observable; + // A bit horrible, since we push below to forge everything into 1 query - we do this we we end up with one + // query to retrieve everything + const queries: Array = [ + api.query.session.queuedKeys + ? [api.query.session.queuedKeys] + : [api.query.session.nextKeyFor, controllerId], + [api.query.staking.ledger, controllerId], + [api.query.staking.nominators, stashId], + [api.query.staking.payee, stashId], + [api.query.staking.stakers, stashId], + [api.query.staking.validators, stashId] + ]; + + // + if (api.query.session.nextKeys) { + queries.push([api.query.session.nextKeys, [api.consts.session.dedupKeyPrefix, stashId]]); + } + + return api.queryMulti(queries) as Observable; } function retrieveInfo (api: ApiInterfaceRx, accountId: AccountId, stashId: AccountId, controllerId: AccountId): Observable { @@ -140,7 +140,7 @@ function retrieveInfo (api: ApiInterfaceRx, accountId: AccountId, stashId: Accou recentlyOffline(api)(), retrieveMulti(api, stashId, controllerId) ]).pipe( - map(([eraLength, bestNumber, recentlyOffline, [nextKeys, [queuedKeys, _stakingLedger, [nominators], rewardDestination, stakers, [validatorPrefs]]]]): DerivedStaking => { + map(([eraLength, bestNumber, recentlyOffline, [queuedKeys, _stakingLedger, [nominators], rewardDestination, stakers, [validatorPrefs], nextKeys = createType('Option', null)]]): DerivedStaking => { const stakingLedger = _stakingLedger.unwrapOr(undefined); return { diff --git a/yarn.lock b/yarn.lock index 551433042b..7a41b39f40 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1789,10 +1789,10 @@ dependencies: "@types/node" "^12.11.1" -"@polkadot/dev@^0.32.0-beta.11": - version "0.32.0-beta.11" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.32.0-beta.11.tgz#930332443807db0c0f692d959ca18c7a84037be1" - integrity sha512-qowTLH6OI/ujmRki5N+QpCCYlyuwpI59IP/XNOw7IhpM/6CbxCdxsdGaoELCtxEIAeRMH2IksqD3eoU5YK8LxA== +"@polkadot/dev@^0.32.0-beta.12": + version "0.32.0-beta.12" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.32.0-beta.12.tgz#0c11d980c445c51ce6262d0a113ea4ca2d105d77" + integrity sha512-jHGH5y1yuZP1ZQMrqdWFtE8jFznrD6zkDPY2m209G6dL+jTlDw/NRWyBP+zHzhXMT3JSFyw7dcBNCdmecXUnMA== dependencies: "@babel/cli" "^7.6.4" "@babel/core" "^7.6.4" @@ -1844,10 +1844,10 @@ "@polkadot/util" "^1.6.1" "@polkadot/util-crypto" "^1.6.1" -"@polkadot/ts@^0.1.82": - version "0.1.82" - resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.82.tgz#e9c9742dcb4096358d740d1476c307595f97c9f9" - integrity sha512-7rC56/fb/ubwZ3k+zn3yf+HTmjXGy+IobDbO8+P/0REXj+MkhQp4ns3tMuIfanWZEp4S/bqwU7BvgyvvodGfCA== +"@polkadot/ts@^0.1.83": + version "0.1.83" + resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.83.tgz#9aa7cc96f66a79d6744d922707f52b62ab374143" + integrity sha512-cBIWE60GK/QtyEOpN+NmdKmBcMGdMzm5KgLjfiJaJBcSGFzI1Cd3+z1SPVwzy8Yk0bmdhxHij4lF9A9jEik9fQ== dependencies: "@types/chrome" "^0.0.91"