Adjust staking info retrieval (better batching) (#4887)

This commit is contained in:
Jaco
2022-06-01 23:59:20 +03:00
committed by GitHub
parent ea25189d2f
commit e73a347700
4 changed files with 29 additions and 29 deletions
+21 -21
View File
@@ -3,7 +3,7 @@
import type { Observable } from 'rxjs'; import type { Observable } from 'rxjs';
import type { Option, Vec } from '@polkadot/types'; import type { Option, Vec } from '@polkadot/types';
import type { AccountId, AccountIndex, Address, Balance, BalanceLockTo212, BlockNumber, VestingSchedule } from '@polkadot/types/interfaces'; import type { AccountId, Balance, BalanceLockTo212, BlockNumber, VestingSchedule } from '@polkadot/types/interfaces';
import type { PalletBalancesBalanceLock, PalletBalancesReserveData, PalletVestingVestingInfo } from '@polkadot/types/lookup'; import type { PalletBalancesBalanceLock, PalletBalancesReserveData, PalletVestingVestingInfo } from '@polkadot/types/lookup';
import type { DeriveApi, DeriveBalancesAccount, DeriveBalancesAccountData, DeriveBalancesAll, DeriveBalancesAllAccountData, DeriveBalancesAllVesting } from '../types'; import type { DeriveApi, DeriveBalancesAccount, DeriveBalancesAccountData, DeriveBalancesAll, DeriveBalancesAllAccountData, DeriveBalancesAllVesting } from '../types';
@@ -14,7 +14,7 @@ import { BN, BN_ZERO, bnMax, bnMin, isFunction } from '@polkadot/util';
import { memo } from '../util'; import { memo } from '../util';
type ResultBalance = [PalletVestingVestingInfo[] | null, ((PalletBalancesBalanceLock | BalanceLockTo212)[])[], PalletBalancesReserveData[][]]; type ResultBalance = [PalletVestingVestingInfo[] | null, ((PalletBalancesBalanceLock | BalanceLockTo212)[])[], PalletBalancesReserveData[][]];
type Result = [DeriveBalancesAccount, BlockNumber, ResultBalance]; type Result = [DeriveBalancesAccount, ResultBalance, BlockNumber];
interface AllLocked { interface AllLocked {
allLocked: boolean, allLocked: boolean,
@@ -99,7 +99,7 @@ function calcVesting (bestNumber: BlockNumber, shared: DeriveBalancesAllAccountD
}; };
} }
function calcBalances (api: DeriveApi, [data, bestNumber, [vesting, allLocks, namedReserves]]: Result): DeriveBalancesAll { function calcBalances (api: DeriveApi, [data, [vesting, allLocks, namedReserves], bestNumber]: Result): DeriveBalancesAll {
const shared = calcShared(api, bestNumber, data, allLocks[0]); const shared = calcShared(api, bestNumber, data, allLocks[0]);
return { return {
@@ -115,7 +115,7 @@ function calcBalances (api: DeriveApi, [data, bestNumber, [vesting, allLocks, na
} }
// old // old
function queryOld (api: DeriveApi, accountId: AccountId): Observable<ResultBalance> { function queryOld (api: DeriveApi, accountId: AccountId | string): Observable<ResultBalance> {
return combineLatest([ return combineLatest([
api.query.balances.locks(accountId), api.query.balances.locks(accountId),
api.query.balances.vesting<Option<VestingSchedule>>(accountId) api.query.balances.vesting<Option<VestingSchedule>>(accountId)
@@ -142,7 +142,7 @@ function queryOld (api: DeriveApi, accountId: AccountId): Observable<ResultBalan
const isNonNullable = <T>(nullable: T): nullable is NonNullable<T> => !!nullable; const isNonNullable = <T>(nullable: T): nullable is NonNullable<T> => !!nullable;
function createCalls <T> (calls: (((a: AccountId) => Observable<T>) | null | undefined)[]): [boolean[], ((a: AccountId) => Observable<T>)[]] { function createCalls <T> (calls: (((a: unknown) => Observable<T>) | null | undefined)[]): [boolean[], ((a: unknown) => Observable<T>)[]] {
return [ return [
calls.map((c) => !c), calls.map((c) => !c),
calls.filter(isNonNullable) calls.filter(isNonNullable)
@@ -150,7 +150,7 @@ function createCalls <T> (calls: (((a: AccountId) => Observable<T>) | null | und
} }
// current (balances, vesting) // current (balances, vesting)
function queryCurrent (api: DeriveApi, accountId: AccountId, balanceInstances: string[] = ['balances']): Observable<ResultBalance> { function queryCurrent (api: DeriveApi, accountId: AccountId | string, balanceInstances: string[] = ['balances']): Observable<ResultBalance> {
const [lockEmpty, lockQueries] = createCalls<Vec<PalletBalancesBalanceLock>>( const [lockEmpty, lockQueries] = createCalls<Vec<PalletBalancesBalanceLock>>(
balanceInstances.map((m) => balanceInstances.map((m) =>
(api.derive as DeriveCustomLocks)[m]?.customLocks || api.query[m as 'balances']?.locks (api.derive as DeriveCustomLocks)[m]?.customLocks || api.query[m as 'balances']?.locks
@@ -210,23 +210,23 @@ function queryCurrent (api: DeriveApi, accountId: AccountId, balanceInstances: s
* }); * });
* ``` * ```
*/ */
export function all (instanceId: string, api: DeriveApi): (address: AccountIndex | AccountId | Address | string) => Observable<DeriveBalancesAll> { export function all (instanceId: string, api: DeriveApi): (address: AccountId | string) => Observable<DeriveBalancesAll> {
const balanceInstances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'balances'); const balanceInstances = api.registry.getModuleInstances(api.runtimeVersion.specName.toString(), 'balances');
return memo(instanceId, (address: AccountIndex | AccountId | Address | string): Observable<DeriveBalancesAll> => return memo(instanceId, (address: AccountId | string): Observable<DeriveBalancesAll> =>
api.derive.balances.account(address).pipe( combineLatest([
switchMap((account): Observable<Result> => api.derive.balances.account(address),
(!account.accountId.isEmpty isFunction(api.query.system?.account) || isFunction(api.query.balances?.account)
? combineLatest([ ? queryCurrent(api, address, balanceInstances)
of(account), : queryOld(api, address)
api.derive.chain.bestNumber(), ]).pipe(
isFunction(api.query.system?.account) || isFunction(api.query.balances?.account) switchMap(([account, locks]) =>
? queryCurrent(api, account.accountId, balanceInstances) combineLatest([
: queryOld(api, account.accountId) of(account),
]) of(locks),
: of([account, api.registry.createType('BlockNumber'), [null, [], []]]) api.derive.chain.bestNumber()
) ])
), ),
map((result): DeriveBalancesAll => calcBalances(api, result)) map((result) => calcBalances(api, result))
)); ));
} }
+1 -1
View File
@@ -40,7 +40,7 @@ export function keysMulti (instanceId: string, api: DeriveApi): (stashIds: (Uint
of(queuedKeys), of(queuedKeys),
api.consts.session?.dedupKeyPrefix api.consts.session?.dedupKeyPrefix
? api.query.session.nextKeys.multi(stashIds.map((stashId) => [api.consts.session.dedupKeyPrefix, stashId])) ? api.query.session.nextKeys.multi(stashIds.map((stashId) => [api.consts.session.dedupKeyPrefix, stashId]))
: api.query.session.nextKeys.multi(stashIds) : combineLatest(stashIds.map((s) => api.query.session.nextKeys(s)))
]) ])
), ),
map(([queuedKeys, nextKeys]) => map(([queuedKeys, nextKeys]) =>
+6 -6
View File
@@ -34,7 +34,7 @@ function getLedgers (api: DeriveApi, optIds: (Option<AccountId> | null)[], { wit
return ( return (
ids.length ids.length
? api.query.staking.ledger.multi(ids) ? combineLatest(ids.map((s) => api.query.staking.ledger(s)))
: of([]) : of([])
).pipe( ).pipe(
map((optLedgers): Option<PalletStakingStakingLedger>[] => { map((optLedgers): Option<PalletStakingStakingLedger>[] => {
@@ -57,19 +57,19 @@ function getStashInfo (api: DeriveApi, stashIds: AccountId[], activeEra: EraInde
return combineLatest([ return combineLatest([
withController || withLedger withController || withLedger
? api.query.staking.bonded.multi(stashIds) ? combineLatest(stashIds.map((s) => api.query.staking.bonded(s)))
: of(stashIds.map(() => null)), : of(stashIds.map(() => null)),
withNominations withNominations
? api.query.staking.nominators.multi(stashIds) ? combineLatest(stashIds.map((s) => api.query.staking.nominators(s)))
: of(stashIds.map(() => emptyNoms)), : of(stashIds.map(() => emptyNoms)),
withDestination withDestination
? api.query.staking.payee.multi(stashIds) ? combineLatest(stashIds.map((s) => api.query.staking.payee(s)))
: of(stashIds.map(() => emptyRewa)), : of(stashIds.map(() => emptyRewa)),
withPrefs withPrefs
? api.query.staking.validators.multi(stashIds) ? combineLatest(stashIds.map((s) => api.query.staking.validators(s)))
: of(stashIds.map(() => emptyPrefs)), : of(stashIds.map(() => emptyPrefs)),
withExposure withExposure
? api.query.staking.erasStakers.multi(stashIds.map((stashId) => [activeEra, stashId])) ? combineLatest(stashIds.map((s) => api.query.staking.erasStakers(activeEra, s)))
: of(stashIds.map(() => emptyExpo)) : of(stashIds.map(() => emptyExpo))
]); ]);
} }
+1 -1
View File
@@ -44,7 +44,7 @@ interface FullDecoration<ApiType extends ApiTypes> {
// the max amount of keys/values that we will retrieve at once // the max amount of keys/values that we will retrieve at once
const PAGE_SIZE_K = 1000; // limit aligned with the 1k on the node (trie lookups are heavy) const PAGE_SIZE_K = 1000; // limit aligned with the 1k on the node (trie lookups are heavy)
const PAGE_SIZE_V = 250; // limited since the data may be > 16MB (e.g. misfiring elections) const PAGE_SIZE_V = 250; // limited since the data may be > 16MB (e.g. misfiring elections)
const PAGE_SIZE_Q = 25; // queue of pending storage queries (mapped together, next tick) const PAGE_SIZE_Q = 50; // queue of pending storage queries (mapped together, next tick)
const l = logger('api/init'); const l = logger('api/init');