Optimize query/queryMulti retrievals (#2371)

* Optimize query/queryMulti retrievals

* Current retrieval on a batch basis

* Dedup staking.account/accounts internally
This commit is contained in:
Jaco Greeff
2020-06-21 13:13:38 +02:00
committed by GitHub
parent 032a0a595c
commit d46d32d9bc
3 changed files with 83 additions and 68 deletions
+2 -6
View File
@@ -75,12 +75,8 @@ function parseResult (api: ApiInterfaceRx, sessionInfo: DeriveSessionInfo, query
*/ */
export function account (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingAccount> { export function account (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingAccount> {
return memo((accountId: Uint8Array | string): Observable<DeriveStakingAccount> => return memo((accountId: Uint8Array | string): Observable<DeriveStakingAccount> =>
api.derive.session.info().pipe( api.derive.staking.accounts([accountId]).pipe(
switchMap((sessionInfo) => map(([first]) => first)
api.derive.staking.query(accountId).pipe(
map((query) => parseResult(api, sessionInfo, query))
)
)
) )
); );
} }
+80 -59
View File
@@ -3,7 +3,7 @@
// of the Apache-2.0 license. See the LICENSE file for details. // of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types'; import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, Exposure, Keys, Nominations, RewardDestination, ValidatorPrefs } from '@polkadot/types/interfaces'; import { AccountId, EraIndex, Exposure, Keys, Nominations, RewardDestination, StakingLedger, ValidatorPrefs } from '@polkadot/types/interfaces';
import { ITuple } from '@polkadot/types/types'; import { ITuple } from '@polkadot/types/types';
import { DeriveStakingQuery } from '../types'; import { DeriveStakingQuery } from '../types';
@@ -16,7 +16,7 @@ import { memo } from '../util';
type MultiResult = [Option<AccountId>, Option<ITuple<[Nominations]> | Nominations>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Option<Keys>, Exposure]; type MultiResult = [Option<AccountId>, Option<ITuple<[Nominations]> | Nominations>, RewardDestination, ITuple<[ValidatorPrefs]> | ValidatorPrefs, Option<Keys>, Exposure];
function unwrapSessionIds (stashId: AccountId, queuedKeys: [AccountId, Keys][], nextKeys: Option<Keys>): { nextSessionIds: AccountId[]; sessionIds: AccountId[] } { function parseSessionIds (stashId: AccountId, queuedKeys: [AccountId, Keys][], nextKeys: Option<Keys>): { nextSessionIds: AccountId[]; sessionIds: AccountId[] } {
const sessionIds = (queuedKeys.find(([currentId]): boolean => const sessionIds = (queuedKeys.find(([currentId]): boolean =>
currentId.eq(stashId) currentId.eq(stashId)
) || [undefined, [] as AccountId[]])[1]; ) || [undefined, [] as AccountId[]])[1];
@@ -28,6 +28,31 @@ function unwrapSessionIds (stashId: AccountId, queuedKeys: [AccountId, Keys][],
}; };
} }
function parseController (stashId: AccountId, queuedKeys: Vec<ITuple<[AccountId, Keys]>>, [controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]: MultiResult, stakingLedgerOpt: Option<StakingLedger>): DeriveStakingQuery {
const controllerId = controllerIdOpt.unwrapOr(null);
const nominators = nominatorsOpt.unwrapOr(null);
return controllerId && stakingLedgerOpt.isSome
? {
accountId: stashId,
controllerId,
exposure,
nominators: nominators
? Array.isArray(nominators)
? nominators[0].targets
: nominators.targets
: [],
rewardDestination,
stakingLedger: stakingLedgerOpt.unwrapOr(undefined),
stashId,
validatorPrefs: Array.isArray(validatorPrefs)
? validatorPrefs[0]
: validatorPrefs,
...parseSessionIds(stashId, queuedKeys, nextKeys)
}
: { accountId: stashId, nextSessionIds: [], sessionIds: [] };
}
function retrievePrev (api: ApiInterfaceRx, stashId: AccountId): Observable<MultiResult> { function retrievePrev (api: ApiInterfaceRx, stashId: AccountId): Observable<MultiResult> {
return api.queryMulti<MultiResult>([ return api.queryMulti<MultiResult>([
[api.query.staking.bonded, stashId], [api.query.staking.bonded, stashId],
@@ -39,64 +64,43 @@ function retrievePrev (api: ApiInterfaceRx, stashId: AccountId): Observable<Mult
]); ]);
} }
function retrieveCurr (api: ApiInterfaceRx, stashId: AccountId): Observable<MultiResult> { function retrieveCurr (api: ApiInterfaceRx, stashIds: AccountId[], activeEra: EraIndex): Observable<MultiResult[]> {
return api.derive.session.indexes().pipe( return combineLatest([
switchMap(({ activeEra }) => api.query.staking.bonded.multi<Option<AccountId>>(stashIds),
api.queryMulti<MultiResult>([ api.query.staking.nominators.multi<Option<Nominations>>(stashIds),
[api.query.staking.bonded, stashId], api.query.staking.payee.multi<RewardDestination>(stashIds),
[api.query.staking.nominators, stashId], api.query.staking.validators.multi<ValidatorPrefs>(stashIds),
[api.query.staking.payee, stashId], api.consts.session?.dedupKeyPrefix
[api.query.staking.validators, stashId], ? api.query.session.nextKeys.multi<Option<Keys>>(stashIds.map((stashId) => [api.consts.session.dedupKeyPrefix, stashId]))
api.consts.session?.dedupKeyPrefix : api.query.session.nextKeys.multi<Option<Keys>>(stashIds),
? [api.query.session.nextKeys, [api.consts.session.dedupKeyPrefix, stashId]] api.query.staking.erasStakers.multi<Exposure>(stashIds.map((stashId) => [activeEra, stashId]))
: [api.query.session.nextKeys, stashId], ]).pipe(
[api.query.staking.erasStakers, [activeEra, stashId]] map(([controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]): MultiResult[] =>
]) controllerIdOpt.map((controllerIdOpt, index): MultiResult =>
[controllerIdOpt, nominatorsOpt[index], rewardDestination[index], validatorPrefs[index], nextKeys[index], exposure[index]]
)
) )
); );
} }
function retrieveController (api: ApiInterfaceRx, stashId: AccountId, queuedKeys: Vec<ITuple<[AccountId, Keys]>>, [controllerIdOpt, nominatorsOpt, rewardDestination, validatorPrefs, nextKeys, exposure]: MultiResult): Observable<DeriveStakingQuery> { function retrieveControllers (api: ApiInterfaceRx, optControllerIds: Option<AccountId>[]): Observable<Option<StakingLedger>[]> {
const controllerId = controllerIdOpt.unwrapOr(null); const ids = optControllerIds.filter((opt) => opt.isSome).map((opt) => opt.unwrap());
const nominators = nominatorsOpt.unwrapOr(null);
return controllerId if (!ids.length) {
? api.query.staking.ledger(controllerId).pipe( return of(optControllerIds.map(() => api.registry.createType('Option<StakingLedger>')));
map((stakingLedgerOpt): DeriveStakingQuery => ({ }
accountId: stashId,
controllerId,
exposure,
nominators: nominators
? Array.isArray(nominators)
? nominators[0].targets
: nominators.targets
: [],
rewardDestination,
stakingLedger: stakingLedgerOpt.unwrapOr(undefined),
stashId,
validatorPrefs: Array.isArray(validatorPrefs)
? validatorPrefs[0]
: validatorPrefs,
...unwrapSessionIds(stashId, queuedKeys, nextKeys)
}))
)
: of({ accountId: stashId, nextSessionIds: [], sessionIds: [] });
}
export function queryWithQueued (api: ApiInterfaceRx): (accountId: Uint8Array | string, queuedKeys: Vec<ITuple<[AccountId, Keys]>>) => Observable<DeriveStakingQuery> { return api.query.staking.ledger.multi<Option<StakingLedger>>(ids).pipe(
return memo((accountId: Uint8Array | string, queuedKeys: Vec<ITuple<[AccountId, Keys]>>): Observable<DeriveStakingQuery> => { map((optLedgers): Option<StakingLedger>[] => {
const stashId = api.registry.createType('AccountId', accountId); let offset = -1;
return ( return optControllerIds.map((opt): Option<StakingLedger> =>
isFunction(api.query.staking.erasStakers) opt.isSome
? retrieveCurr(api, stashId) ? optLedgers[++offset]
: retrievePrev(api, stashId) : api.registry.createType('Option<StakingLedger>')
).pipe( );
switchMap((result): Observable<DeriveStakingQuery> => })
retrieveController(api, stashId, queuedKeys, result) );
)
);
});
} }
/** /**
@@ -104,8 +108,8 @@ export function queryWithQueued (api: ApiInterfaceRx): (accountId: Uint8Array |
*/ */
export function query (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingQuery> { export function query (api: ApiInterfaceRx): (accountId: Uint8Array | string) => Observable<DeriveStakingQuery> {
return memo((accountId: Uint8Array | string): Observable<DeriveStakingQuery> => return memo((accountId: Uint8Array | string): Observable<DeriveStakingQuery> =>
api.query.session.queuedKeys<Vec<ITuple<[AccountId, Keys]>>>().pipe( api.derive.staking.queryMulti([accountId]).pipe(
switchMap((queuedKeys) => api.derive.staking.queryWithQueued(accountId, queuedKeys)) map(([first]) => first)
) )
); );
} }
@@ -113,10 +117,27 @@ export function query (api: ApiInterfaceRx): (accountId: Uint8Array | string) =>
export function queryMulti (api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[]) => Observable<DeriveStakingQuery[]> { export function queryMulti (api: ApiInterfaceRx): (accountIds: (Uint8Array | string)[]) => Observable<DeriveStakingQuery[]> {
return memo((accountIds: (Uint8Array | string)[]): Observable<DeriveStakingQuery[]> => return memo((accountIds: (Uint8Array | string)[]): Observable<DeriveStakingQuery[]> =>
accountIds.length accountIds.length
? api.query.session.queuedKeys<Vec<ITuple<[AccountId, Keys]>>>().pipe( ? combineLatest([
switchMap((queuedKeys) => combineLatest( api.query.session.queuedKeys<Vec<ITuple<[AccountId, Keys]>>>(),
accountIds.map((acc) => api.derive.staking.queryWithQueued(acc, queuedKeys)) api.derive.session.indexes()
)) ]).pipe(
switchMap(([queuedKeys, { activeEra }]): Observable<DeriveStakingQuery[]> => {
const stashIds = accountIds.map((accountId) => api.registry.createType('AccountId', accountId));
return (
isFunction(api.query.staking.erasStakers)
? retrieveCurr(api, stashIds, activeEra)
: combineLatest(stashIds.map((stashId) => retrievePrev(api, stashId)))
).pipe(
switchMap((results): Observable<DeriveStakingQuery[]> =>
retrieveControllers(api, results.map(([optController]) => optController)).pipe(
map((stakingLedgerOpts) =>
stashIds.map((stashId, index) => parseController(stashId, queuedKeys, results[index], stakingLedgerOpts[index]))
)
)
)
);
})
) )
: of([]) : of([])
); );
@@ -18,9 +18,7 @@ export function nextElected (api: ApiInterfaceRx): () => Observable<AccountId[]>
// only populate for next era in the last session, so track both here - entries are not // 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 // subscriptions, so we need a trigger - currentIndex acts as that trigger to refresh
switchMap(({ currentEra }) => api.query.staking.erasStakers.keys(currentEra)), switchMap(({ currentEra }) => api.query.staking.erasStakers.keys(currentEra)),
map((keys): AccountId[] => map((keys) => keys.map((key): AccountId => key.args[1] as AccountId))
keys.map((key): AccountId => key.args[1] as AccountId)
)
) )
: api.query.staking.currentElected<AccountId[]>() : api.query.staking.currentElected<AccountId[]>()
); );