Era exposure cleanups via .entries (#2003)

This commit is contained in:
Jaco Greeff
2020-03-06 11:06:10 +01:00
committed by GitHub
parent e9d24c3213
commit 89992ea03e
4 changed files with 105 additions and 96 deletions
+31 -33
View File
@@ -3,53 +3,51 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { EraIndex, Exposure } from '@polkadot/types/interfaces';
import { DeriveEraExposure } from '../types';
import { AccountId, EraIndex, Exposure } from '@polkadot/types/interfaces';
import { DeriveEraExposure, DeriveEraPointsAll } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { StorageKey } from '@polkadot/types';
import { memo } from '../util';
function queryClipped (api: ApiInterfaceRx): Observable<[[EraIndex, string][], Exposure[]]> {
function queryClipped (api: ApiInterfaceRx): Observable<[EraIndex, [AccountId, Exposure][]][]> {
return api.derive.staking.erasPoints().pipe(
switchMap((allPoints): Observable<[[EraIndex, string][], Exposure[]]> => {
const indexes: [EraIndex, string][] = [];
allPoints.forEach(({ all, era }): void => {
Object.keys(all).forEach((validatorId): void => {
indexes.push([era, validatorId]);
});
});
// FIXME use prefix queries, get going first
return combineLatest([
of(indexes),
indexes.length
? api.query.staking.erasStakersClipped.multi<Exposure>(indexes)
: of([] as Exposure[])
]);
})
switchMap((allPoints): Observable<[DeriveEraPointsAll[], [StorageKey, Exposure][][]]> =>
combineLatest([
of(allPoints),
combineLatest(
allPoints.map(({ era }): Observable<[StorageKey, Exposure][]> =>
api.query.staking.erasStakersClipped.entries(era)
)
)
])
),
map(([allPoints, erasStakers]): [EraIndex, [AccountId, Exposure][]][] =>
allPoints.map(({ era }, index): [EraIndex, [AccountId, Exposure][]] => [
era,
erasStakers[index].map(([key, exposure]): [AccountId, Exposure] => [
api.registry.createType('AccountId', key.toU8a().slice(-32)),
exposure
])
])
)
);
}
export function erasExposure (api: ApiInterfaceRx): () => Observable<DeriveEraExposure[]> {
return memo((): Observable<DeriveEraExposure[]> =>
queryClipped(api).pipe(
map(([indexes, exposures]): DeriveEraExposure[] =>
indexes.reduce((result: DeriveEraExposure[], [era, validatorId], index): DeriveEraExposure[] => {
let entry = result.find((entry): boolean => entry.era.eq(era));
map((clipped): DeriveEraExposure[] =>
clipped.map(([era, validators]): DeriveEraExposure => ({
all: validators.reduce((all: Record<string, Exposure>, [validtorId, exposure]): Record<string, Exposure> => {
all[validtorId.toString()] = exposure;
if (!entry) {
entry = { all: {}, era };
result.push(entry);
}
entry.all[validatorId] = exposures[index];
return result;
}, [])
return all;
}, {}),
era
}))
)
)
);
+24 -6
View File
@@ -3,14 +3,31 @@
// of the Apache-2.0 license. See the LICENSE file for details.
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId } from '@polkadot/types/interfaces';
import { AccountId, Exposure } from '@polkadot/types/interfaces';
import { DeriveStakingValidators } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { map, switchMap } from 'rxjs/operators';
import { StorageKey } from '@polkadot/types';
import { memo } from '../util';
function queryNextStakers (api: ApiInterfaceRx): Observable<AccountId[]> {
// 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
return api.derive.session.indexes().pipe(
switchMap(({ activeEra }): Observable<[StorageKey, Exposure][]> =>
api.query.staking.erasStakers.entries(activeEra.addn(1))
),
map((entries): AccountId[] =>
// key is from concat with AccountId bytes right at the end
entries.map(([key]): AccountId =>
api.registry.createType('AccountId', key.toU8a().slice(-32))
)
)
);
}
/**
* @description Retrieve latest list of validators
*/
@@ -23,15 +40,16 @@ export function validators (api: ApiInterfaceRx): () => Observable<DeriveStaking
api.query.session
? api.query.session.validators()
: of([]),
// FIXME need a replacement for currentElected in new
api.query.staking?.currentElected
api.query.staking
? api.query.staking.erasStakers
? of(null)
? queryNextStakers(api)
: api.query.staking.currentElected<AccountId[]>()
: of([])
]).pipe(
map(([validators, nextElected]): DeriveStakingValidators => ({
nextElected: nextElected || validators,
nextElected: nextElected.length
? nextElected
: validators,
validators
}))
));