memo for range & entries

This commit is contained in:
Jaco Greeff
2020-03-09 22:57:09 +01:00
parent bcbd4b8d40
commit c1facd8b1c
5 changed files with 18 additions and 13 deletions
@@ -7,7 +7,7 @@ import { Exposure } from '@polkadot/types/interfaces';
import { DeriveEraExposure, DeriveEraExposures, DeriveEraPointsAll, DeriveEraValPoints } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { map, switchMap, take } from 'rxjs/operators';
import { StorageKey } from '@polkadot/types';
import { memo } from '../util';
@@ -35,7 +35,7 @@ export function erasExposure (api: ApiInterfaceRx): (withActive?: boolean) => Ob
// we could just do entries over the full set, however the set can be quite large - split it into
// batches - may need to re-visit this, or alternatively use pages keys for exceptionally large sets
allPoints.map(({ era }): Observable<[StorageKey, Exposure][]> =>
api.query.staking.erasStakers.entries(era)
api.query.staking.erasStakers.entries(era).pipe(take(1))
)
)
])
@@ -7,7 +7,7 @@ import { ActiveEraInfo, Balance, EraIndex, EraRewardPoints } from '@polkadot/typ
import { DeriveEraPointsAll, DeriveEraValPoints } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { map, switchMap, take } from 'rxjs/operators';
import { Option, u32 } from '@polkadot/types';
import { memo } from '../util';
@@ -53,10 +53,10 @@ export function erasPoints (api: ApiInterfaceRx): (withActive?: boolean) => Obse
combineLatest([
of(indexes),
indexes.length
? api.query.staking.erasRewardPoints.multi<EraRewardPoints>(indexes)
? api.query.staking.erasRewardPoints.multi<EraRewardPoints>(indexes).pipe(take(1))
: of([]),
indexes.length
? api.query.staking.erasValidatorReward.multi<Option<Balance>>(indexes)
? api.query.staking.erasValidatorReward.multi<Option<Balance>>(indexes).pipe(take(1))
: of([])
])
),
@@ -7,7 +7,7 @@ import { AccountId, Exposure } from '@polkadot/types/interfaces';
import { DeriveStakingValidators } from '../types';
import { Observable, combineLatest, of } from 'rxjs';
import { map, switchMap } from 'rxjs/operators';
import { map, switchMap, take } from 'rxjs/operators';
import { StorageKey } from '@polkadot/types';
import { memo } from '../util';
@@ -17,7 +17,7 @@ function queryNextStakers (api: ApiInterfaceRx): Observable<AccountId[]> {
// 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))
api.query.staking.erasStakers.entries(activeEra.addn(1)).pipe(take(1))
),
map((entries): AccountId[] =>
entries.map(([key]): AccountId => key.args[1] as AccountId)
+7 -6
View File
@@ -11,8 +11,9 @@ import { ApiInterfaceRx, ApiOptions, ApiTypes, DecorateMethod, DecoratedRpc, Dec
import BN from 'bn.js';
import { BehaviorSubject, Observable, combineLatest, of } from 'rxjs';
import { first, map, switchMap } from 'rxjs/operators';
import { map, switchMap, take } from 'rxjs/operators';
import decorateDerive, { ExactDerive } from '@polkadot/api-derive';
import { memo } from '@polkadot/api-derive/util';
import DecoratedMeta from '@polkadot/metadata/Decorated';
import getHasher from '@polkadot/metadata/Decorated/storage/fromMetadata/getHasher';
import RpcCore from '@polkadot/rpc-core';
@@ -314,8 +315,8 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
decorated.at = decorateMethod((hash: Hash, arg1?: Arg, arg2?: Arg): Observable<Codec> =>
this._rpcCore.state.getStorage(getArgs(arg1, arg2), hash));
decorated.entries = decorateMethod((doubleMapArg?: Arg): Observable<[StorageKey, Codec][]> =>
this.retrieveMapEntries(creator, doubleMapArg));
decorated.entries = decorateMethod(memo((doubleMapArg?: Arg): Observable<[StorageKey, Codec][]> =>
this.retrieveMapEntries(creator, doubleMapArg)));
decorated.hash = decorateMethod((arg1?: Arg, arg2?: Arg): Observable<Hash> =>
this._rpcCore.state.getStorageHash(getArgs(arg1, arg2)));
@@ -333,8 +334,8 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
args.map((arg: Arg[] | Arg): [StorageEntry, Arg | Arg[]] => [creator, arg])));
}
decorated.range = decorateMethod((range: [Hash, Hash?], arg1?: Arg, arg2?: Arg): Observable<[Hash, Codec][]> =>
this.decorateStorageRange(decorated, [arg1, arg2], range));
decorated.range = decorateMethod(memo((range: [Hash, Hash?], arg1?: Arg, arg2?: Arg): Observable<[Hash, Codec][]> =>
this.decorateStorageRange(decorated, [arg1, arg2], range)));
decorated.size = decorateMethod((arg1?: Arg, arg2?: Arg): Observable<u64> =>
this._rpcCore.state.getStorageSize(getArgs(arg1, arg2)));
@@ -457,7 +458,7 @@ export default abstract class Decorate<ApiType extends ApiTypes> extends Events
of(keys.map((key) => key.decodeArgsFromMeta(meta))),
// since we have a default constructed key, we have Option<Raw> as the result
// TODO Don't keep the subscription here active, retrieve and get out of Dodge
this._rpcCore.state.subscribeStorage<Option<Raw>[]>(keys).pipe(first())
this._rpcCore.state.subscribeStorage<Option<Raw>[]>(keys).pipe(take(1))
])
),
map(([keys, values]): [StorageKey, Codec][] =>
+4
View File
@@ -360,6 +360,10 @@ export default class Rpc implements RpcInterface {
// - null - The storage key is empty
return keys.reduce((results, key: StorageKey): Codec[] => {
try {
// if (changes.some(([, val]) => val && val.length > 5000)) {
// console.error(`LARGE ${key.section}.${key.method}`);
// }
results.push(this.formatStorageSet(key, changes, withCache));
} catch (error) {
console.error(`Unable to decode storage ${key.section}.${key.method}:`, error.message);