Session validators availability detection (#1759)
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||||
|
|
||||||
import { Observable, combineLatest } from 'rxjs';
|
import { Observable, combineLatest, of } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
|
|
||||||
import { HeaderExtended } from '../type';
|
import { HeaderExtended } from '../type';
|
||||||
@@ -27,9 +27,11 @@ export function subscribeNewHeads (api: ApiInterfaceRx): () => Observable<Header
|
|||||||
return memo((): Observable<HeaderExtended> =>
|
return memo((): Observable<HeaderExtended> =>
|
||||||
combineLatest([
|
combineLatest([
|
||||||
api.rpc.chain.subscribeNewHeads(),
|
api.rpc.chain.subscribeNewHeads(),
|
||||||
api.derive.staking.validators()
|
api.query.session
|
||||||
|
? api.query.session.validators()
|
||||||
|
: of([])
|
||||||
]).pipe(
|
]).pipe(
|
||||||
map(([header, { validators }]): HeaderExtended =>
|
map(([header, validators]): HeaderExtended =>
|
||||||
new HeaderExtended(api.registry, header, validators)
|
new HeaderExtended(api.registry, header, validators)
|
||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -2,13 +2,11 @@
|
|||||||
// This software may be modified and distributed under the terms
|
// This software may be modified and distributed under the terms
|
||||||
// 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 { AccountId } from '@polkadot/types/interfaces';
|
|
||||||
import { ApiInterfaceRx } from '@polkadot/api/types';
|
import { ApiInterfaceRx } from '@polkadot/api/types';
|
||||||
import { DeriveStakingValidators } from '../types';
|
import { DeriveStakingValidators } from '../types';
|
||||||
|
|
||||||
import { Observable, of } from 'rxjs';
|
import { Observable, combineLatest, of } from 'rxjs';
|
||||||
import { map } from 'rxjs/operators';
|
import { map } from 'rxjs/operators';
|
||||||
import { Vec } from '@polkadot/types';
|
|
||||||
|
|
||||||
import { memo } from '../util';
|
import { memo } from '../util';
|
||||||
|
|
||||||
@@ -17,16 +15,17 @@ import { memo } from '../util';
|
|||||||
*/
|
*/
|
||||||
export function validators (api: ApiInterfaceRx): () => Observable<DeriveStakingValidators> {
|
export function validators (api: ApiInterfaceRx): () => Observable<DeriveStakingValidators> {
|
||||||
return memo((): Observable<DeriveStakingValidators> =>
|
return memo((): Observable<DeriveStakingValidators> =>
|
||||||
(
|
// Sadly the node-template is (for some obscure reason) not comprehensive, so while the derive works
|
||||||
// Sadly the node-template is (for some obscure reason) not comprehensive, so while the derive works
|
// in all actual real-world deployed chains, it does create some confusion for limited template chains
|
||||||
// in all actual real-world deployed chains, it does create some confusion for limited template chains
|
// NOTE: Not doing multi queries here, since we have validators as a single in the derived newHead
|
||||||
api.query.session && api.query.staking
|
combineLatest([
|
||||||
? api.queryMulti<[Vec<AccountId>, Vec<AccountId>]>([
|
api.query.session
|
||||||
api.query.session.validators,
|
? api.query.session.validators()
|
||||||
api.query.staking.currentElected
|
: of([]),
|
||||||
])
|
api.query.staking
|
||||||
: of([[], []] as [AccountId[], AccountId[]])
|
? api.query.staking.currentElected()
|
||||||
).pipe(
|
: of([])
|
||||||
|
]).pipe(
|
||||||
map(([validators, currentElected]): DeriveStakingValidators => ({
|
map(([validators, currentElected]): DeriveStakingValidators => ({
|
||||||
currentElected, validators
|
currentElected, validators
|
||||||
}))
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user