Dedupe staking derive caches (#4301)

* Dedupe staking derive caches

* Dedupe

* Adjust

* Adjust

* typing

* lintin

* Adjust
This commit is contained in:
Jaco
2021-12-08 17:17:38 +02:00
committed by GitHub
parent 4a7bfe96e1
commit 9a2cd1c446
13 changed files with 100 additions and 111 deletions
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { EraIndex } from '@polkadot/types/interfaces';
import { deriveCache } from '../util';
export function getEraCache <T> (CACHE_KEY: string, era: EraIndex, withActive?: boolean): [string, T | undefined] {
const cacheKey = `${CACHE_KEY}-${era.toString()}`;
return [
cacheKey,
withActive
? undefined
: deriveCache.get<T>(cacheKey)
];
}
export function getEraMultiCache <T> (CACHE_KEY: string, eras: EraIndex[], withActive?: boolean): T[] {
const cached: T[] = withActive
? []
: eras
.map((e) => deriveCache.get<T>(`${CACHE_KEY}-${e.toString()}`))
.filter((v): v is T => !!v);
return cached;
}
@@ -8,9 +8,11 @@ import type { AccountId, EraIndex } from '@polkadot/types/interfaces';
import type { PalletStakingExposure } from '@polkadot/types/lookup';
import type { DeriveEraExposure, DeriveEraNominatorExposure, DeriveEraValidatorExposure } from '../types';
import { combineLatest, map, of, switchMap } from 'rxjs';
import { combineLatest, map, of } from 'rxjs';
import { deriveCache, memo } from '../util';
import { getEraCache } from './cache';
import { erasHistoricApply } from './util';
type KeysAndExposures = [StorageKey<[EraIndex, AccountId]>, PalletStakingExposure][];
@@ -38,10 +40,7 @@ function mapStakers (era: EraIndex, stakers: KeysAndExposures): DeriveEraExposur
export function _eraExposure (instanceId: string, api: ApiInterfaceRx): (era: EraIndex, withActive: boolean) => Observable<DeriveEraExposure> {
return memo(instanceId, (era: EraIndex, withActive: boolean): Observable<DeriveEraExposure> => {
const cacheKey = `${CACHE_KEY}-${era.toString()}`;
const cached = withActive
? undefined
: deriveCache.get<DeriveEraExposure>(cacheKey);
const [cacheKey, cached] = getEraCache<DeriveEraExposure>(CACHE_KEY, era, withActive);
return cached
? of(cached)
@@ -71,10 +70,4 @@ export function _erasExposure (instanceId: string, api: ApiInterfaceRx): (eras:
);
}
export function erasExposure (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraExposure[]> {
return memo(instanceId, (withActive = false): Observable<DeriveEraExposure[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._erasExposure(eras, withActive))
)
);
}
export const erasExposure = erasHistoricApply('_erasExposure');
+5 -14
View File
@@ -7,12 +7,13 @@ import type { EraIndex } from '@polkadot/types/interfaces';
import type { PalletStakingEraRewardPoints } from '@polkadot/types/lookup';
import type { DeriveEraPoints, DeriveEraValPoints } from '../types';
import { map, of, switchMap } from 'rxjs';
import { map, of } from 'rxjs';
import { BN_ZERO } from '@polkadot/util';
import { deriveCache, memo } from '../util';
import { filterEras } from './util';
import { getEraMultiCache } from './cache';
import { erasHistoricApply, filterEras } from './util';
const CACHE_KEY = 'eraPoints';
@@ -40,11 +41,7 @@ export function _erasPoints (instanceId: string, api: ApiInterfaceRx): (eras: Er
return of([]);
}
const cached: DeriveEraPoints[] = withActive
? []
: eras
.map((era) => deriveCache.get<DeriveEraPoints>(`${CACHE_KEY}-${era.toString()}`))
.filter((value): value is DeriveEraPoints => !!value);
const cached = getEraMultiCache<DeriveEraPoints>(CACHE_KEY, eras, withActive);
const remaining = filterEras(eras, cached);
return !remaining.length
@@ -64,10 +61,4 @@ export function _erasPoints (instanceId: string, api: ApiInterfaceRx): (eras: Er
});
}
export function erasPoints (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPoints[]> {
return memo(instanceId, (withActive = false): Observable<DeriveEraPoints[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._erasPoints(eras, withActive))
)
);
}
export const erasPoints = erasHistoricApply('_erasPoints');
+5 -12
View File
@@ -8,9 +8,11 @@ import type { EraIndex } from '@polkadot/types/interfaces';
import type { PalletStakingValidatorPrefs } from '@polkadot/types/lookup';
import type { DeriveEraPrefs, DeriveEraValPrefs } from '../types';
import { combineLatest, map, of, switchMap } from 'rxjs';
import { combineLatest, map, of } from 'rxjs';
import { deriveCache, memo } from '../util';
import { getEraCache } from './cache';
import { erasHistoricApply } from './util';
const CACHE_KEY = 'eraPrefs';
@@ -26,10 +28,7 @@ function mapPrefs (era: EraIndex, all: [StorageKey, PalletStakingValidatorPrefs]
export function _eraPrefs (instanceId: string, api: ApiInterfaceRx): (era: EraIndex, withActive: boolean) => Observable<DeriveEraPrefs> {
return memo(instanceId, (era: EraIndex, withActive: boolean): Observable<DeriveEraPrefs> => {
const cacheKey = `${CACHE_KEY}-${era.toString()}`;
const cached = withActive
? undefined
: deriveCache.get<DeriveEraPrefs>(cacheKey);
const [cacheKey, cached] = getEraCache<DeriveEraPrefs>(CACHE_KEY, era, withActive);
return cached
? of(cached)
@@ -59,10 +58,4 @@ export function _erasPrefs (instanceId: string, api: ApiInterfaceRx): (eras: Era
);
}
export function erasPrefs (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraPrefs[]> {
return memo(instanceId, (withActive = false): Observable<DeriveEraPrefs[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._erasPrefs(eras, withActive))
)
);
}
export const erasPrefs = erasHistoricApply('_erasPrefs');
+5 -14
View File
@@ -7,10 +7,11 @@ import type { Option } from '@polkadot/types';
import type { Balance, EraIndex } from '@polkadot/types/interfaces';
import type { DeriveEraRewards } from '../types';
import { map, of, switchMap } from 'rxjs';
import { map, of } from 'rxjs';
import { deriveCache, memo } from '../util';
import { filterEras } from './util';
import { getEraMultiCache } from './cache';
import { erasHistoricApply, filterEras } from './util';
const CACHE_KEY = 'eraRewards';
@@ -27,11 +28,7 @@ export function _erasRewards (instanceId: string, api: ApiInterfaceRx): (eras: E
return of([]);
}
const cached: DeriveEraRewards[] = withActive
? []
: eras
.map((era) => deriveCache.get<DeriveEraRewards>(`${CACHE_KEY}-${era.toString()}`))
.filter((value): value is DeriveEraRewards => !!value);
const cached = getEraMultiCache<DeriveEraRewards>(CACHE_KEY, eras, withActive);
const remaining = filterEras(eras, cached);
if (!remaining.length) {
@@ -53,10 +50,4 @@ export function _erasRewards (instanceId: string, api: ApiInterfaceRx): (eras: E
});
}
export function erasRewards (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraRewards[]> {
return memo(instanceId, (withActive = false): Observable<DeriveEraRewards[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._erasRewards(eras, withActive))
)
);
}
export const erasRewards = erasHistoricApply('_erasRewards');
+5 -12
View File
@@ -8,9 +8,11 @@ import type { BalanceOf, EraIndex, Perbill } from '@polkadot/types/interfaces';
import type { ITuple } from '@polkadot/types/types';
import type { DeriveEraSlashes, DeriveEraValSlash } from '../types';
import { combineLatest, map, of, switchMap } from 'rxjs';
import { combineLatest, map, of } from 'rxjs';
import { deriveCache, memo } from '../util';
import { getEraCache } from './cache';
import { erasHistoricApply } from './util';
const CACHE_KEY = 'eraSlashes';
@@ -31,10 +33,7 @@ function mapSlashes (era: EraIndex, noms: [StorageKey, Option<BalanceOf>][], val
export function _eraSlashes (instanceId: string, api: ApiInterfaceRx): (era: EraIndex, withActive: boolean) => Observable<DeriveEraSlashes> {
return memo(instanceId, (era: EraIndex, withActive: boolean): Observable<DeriveEraSlashes> => {
const cacheKey = `${CACHE_KEY}-${era.toString()}`;
const cached = withActive
? undefined
: deriveCache.get<DeriveEraSlashes>(cacheKey);
const [cacheKey, cached] = getEraCache<DeriveEraSlashes>(CACHE_KEY, era, withActive);
return cached
? of(cached)
@@ -69,10 +68,4 @@ export function _erasSlashes (instanceId: string, api: ApiInterfaceRx): (eras: E
);
}
export function erasSlashes (instanceId: string, api: ApiInterfaceRx): (withActive?: boolean) => Observable<DeriveEraSlashes[]> {
return memo(instanceId, (withActive = false): Observable<DeriveEraSlashes[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._erasSlashes(eras, withActive))
)
);
}
export const erasSlashes = erasHistoricApply('_erasSlashes');
+3 -10
View File
@@ -7,9 +7,10 @@ import type { EraIndex } from '@polkadot/types/interfaces';
import type { PalletStakingExposure } from '@polkadot/types/lookup';
import type { DeriveOwnExposure } from '../types';
import { map, of, switchMap } from 'rxjs';
import { map, of } from 'rxjs';
import { memo } from '../util';
import { erasHistoricApplyAccount } from './util';
export function _ownExposures (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveOwnExposure[]> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -35,12 +36,4 @@ export function ownExposure (instanceId: string, api: ApiInterfaceRx): (accountI
);
}
export function ownExposures (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveOwnExposure[]> {
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveOwnExposure[]> => {
return api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) =>
api.derive.staking._ownExposures(accountId, eras, withActive)
)
);
});
}
export const ownExposures = erasHistoricApplyAccount('_ownExposures');
+3 -10
View File
@@ -8,9 +8,10 @@ import type { BalanceOf, EraIndex, Perbill } from '@polkadot/types/interfaces';
import type { ITuple } from '@polkadot/types/types';
import type { DeriveStakerSlashes } from '../types';
import { map, of, switchMap } from 'rxjs';
import { map, of } from 'rxjs';
import { memo } from '../util';
import { erasHistoricApplyAccount } from './util';
export function _ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerSlashes[]> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -41,12 +42,4 @@ export function ownSlash (instanceId: string, api: ApiInterfaceRx): (accountId:
);
}
export function ownSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerSlashes[]> {
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerSlashes[]> => {
return api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) =>
api.derive.staking._ownSlashes(accountId, eras, withActive)
)
);
});
}
export const ownSlashes = erasHistoricApplyAccount('_ownSlashes');
@@ -6,9 +6,10 @@ import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { EraIndex } from '@polkadot/types/interfaces';
import type { DeriveStakerPoints } from '../types';
import { map, switchMap } from 'rxjs';
import { map } from 'rxjs';
import { memo } from '../util';
import { erasHistoricApplyAccount } from './util';
export function _stakerPoints (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerPoints[]> {
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean): Observable<DeriveStakerPoints[]> => {
@@ -26,10 +27,4 @@ export function _stakerPoints (instanceId: string, api: ApiInterfaceRx): (accoun
});
}
export function stakerPoints (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerPoints[]> {
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerPoints[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._stakerPoints(accountId, eras, withActive))
)
);
}
export const stakerPoints = erasHistoricApplyAccount('_stakerPoints');
@@ -6,9 +6,10 @@ import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { EraIndex } from '@polkadot/types/interfaces';
import type { DeriveStakerPrefs } from '../types';
import { map, switchMap } from 'rxjs';
import { map } from 'rxjs';
import { memo } from '../util';
import { erasHistoricApplyAccount } from './util';
export function _stakerPrefs (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerPrefs[]> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -24,10 +25,4 @@ export function _stakerPrefs (instanceId: string, api: ApiInterfaceRx): (account
);
}
export function stakerPrefs (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerPrefs[]> {
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerPrefs[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._stakerPrefs(accountId, eras, withActive))
)
);
}
export const stakerPrefs = erasHistoricApplyAccount('_stakerPrefs');
@@ -6,9 +6,10 @@ import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { EraIndex } from '@polkadot/types/interfaces';
import type { DeriveStakerSlashes } from '../types';
import { map, switchMap } from 'rxjs';
import { map } from 'rxjs';
import { memo } from '../util';
import { erasHistoricApplyAccount } from './util';
export function _stakerSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean) => Observable<DeriveStakerSlashes[]> {
return memo(instanceId, (accountId: Uint8Array | string, eras: EraIndex[], withActive: boolean): Observable<DeriveStakerSlashes[]> => {
@@ -25,10 +26,4 @@ export function _stakerSlashes (instanceId: string, api: ApiInterfaceRx): (accou
});
}
export function stakerSlashes (instanceId: string, api: ApiInterfaceRx): (accountId: Uint8Array | string, withActive?: boolean) => Observable<DeriveStakerSlashes[]> {
return memo(instanceId, (accountId: Uint8Array | string, withActive = false): Observable<DeriveStakerSlashes[]> =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((eras) => api.derive.staking._stakerSlashes(accountId, eras, withActive))
)
);
}
export const stakerSlashes = erasHistoricApplyAccount('_stakerSlashes');
+30
View File
@@ -1,8 +1,38 @@
// Copyright 2017-2021 @polkadot/api-derive authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiInterfaceRx } from '@polkadot/api/types';
import type { EraIndex } from '@polkadot/types/interfaces';
import type { ExactDerive } from '../bundle';
import { switchMap } from 'rxjs';
import { memo } from '../util';
type ApplyReturn<T extends keyof ExactDerive['staking']> = ReturnType<ExactDerive['staking'][T]>;
export function filterEras <T extends { era: EraIndex }> (eras: EraIndex[], list: T[]): EraIndex[] {
return eras.filter((era) => !list.some((entry) => era.eq(entry.era)));
}
export function erasHistoricApply <F extends '_erasExposure' | '_erasPoints' | '_erasPrefs' | '_erasRewards' | '_erasSlashes'> (fn: F): (instanceId: string, api: ApiInterfaceRx) => (withActive?: boolean) => ApplyReturn<F> {
return (instanceId: string, api: ApiInterfaceRx) =>
// Cannot quite get the typing right, but it is right in the code
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
memo(instanceId, (withActive = false) =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((e) => api.derive.staking[fn](e, withActive))
)
) as any;
}
export function erasHistoricApplyAccount <F extends '_ownExposures' | '_ownSlashes' | '_stakerPoints' | '_stakerPrefs' | '_stakerSlashes'> (fn: F): (instanceId: string, api: ApiInterfaceRx) => (accountId: string | Uint8Array, withActive?: boolean) => ApplyReturn<F> {
return (instanceId: string, api: ApiInterfaceRx) =>
// Cannot quite get the typing right, but it is right in the code
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
memo(instanceId, (accountId: string | Uint8Array, withActive = false) =>
api.derive.staking.erasHistoric(withActive).pipe(
switchMap((e) => api.derive.staking[fn](accountId, e, withActive))
)
) as any;
}