Expand heartbeats with num blocks (#1534)

This commit is contained in:
Jaco Greeff
2019-10-31 19:32:53 +01:00
committed by GitHub
parent 5aa64dddf3
commit 276da86dbe
2 changed files with 23 additions and 14 deletions
@@ -8,7 +8,7 @@ import { DerivedHeartbeats } from '../types';
import { of, Observable, combineLatest } from 'rxjs'; import { of, Observable, combineLatest } from 'rxjs';
import { map, switchMap } from 'rxjs/operators'; import { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types'; import { ApiInterfaceRx } from '@polkadot/api/types';
import { Bytes, Vec } from '@polkadot/types'; import { Bytes, Vec, u32 } from '@polkadot/types';
import { drr } from '../util/drr'; import { drr } from '../util/drr';
@@ -17,23 +17,26 @@ import { drr } from '../util/drr';
*/ */
export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<DerivedHeartbeats> { export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<DerivedHeartbeats> {
return (): Observable<DerivedHeartbeats> => { return (): Observable<DerivedHeartbeats> => {
return api.query.imOnline && api.query.imOnline.receivedHeartbeats return api.query.imOnline && api.query.imOnline.receivedHeartbeats && api.query.imOnline.authoredBlocks
? api.queryMulti<[Vec<AccountId>, SessionIndex]>([ ? api.queryMulti<[SessionIndex, Vec<AccountId>]>([
api.query.imOnline.keys, api.query.session.currentIndex,
api.query.session.currentIndex api.query.session.validators
]).pipe( ]).pipe(
switchMap(([keys, currentIndex]): Observable<[AccountId[], Bytes[]]> => switchMap(([currentIndex, validators]): Observable<[AccountId[], Bytes[], u32[]]> =>
combineLatest([ combineLatest([
of(keys), of(validators),
api.query.imOnline.receivedHeartbeats.multi<Bytes>( api.query.imOnline.receivedHeartbeats.multi<Bytes>(validators.map((_address, index): [SessionIndex, number] => [currentIndex, index])),
keys.map((_address, index): [SessionIndex, number] => [currentIndex, index]) api.query.imOnline.authoredBlocks.multi<u32>(validators.map((address): [SessionIndex, AccountId] => [currentIndex, address]))
)
]) ])
), ),
map(([keys, heartbeats]): DerivedHeartbeats => map(([validators, heartbeats, numBlocks]): DerivedHeartbeats =>
keys.reduce((result: DerivedHeartbeats, key, index): DerivedHeartbeats => ({ validators.reduce((result: DerivedHeartbeats, validator, index): DerivedHeartbeats => ({
...result, ...result,
[key.toString()]: !!heartbeats[index] && !heartbeats[index].isEmpty [validator.toString()]: {
blockCount: numBlocks[index],
hasMessage: !heartbeats[index].isEmpty,
isOnline: !heartbeats[index].isEmpty || numBlocks[index].gtn(0)
}
}), {}) }), {})
), ),
drr() drr()
+7 -1
View File
@@ -60,7 +60,13 @@ export interface DerivedFees {
transferFee: Balance; transferFee: Balance;
} }
export type DerivedHeartbeats = Record<string, boolean>; export interface DerivedHeartbeatAuthor {
blockCount: u32;
hasMessage: boolean;
isOnline: boolean;
}
export type DerivedHeartbeats = Record<string, DerivedHeartbeatAuthor>;
export interface RecentlyOffline { export interface RecentlyOffline {
blockNumber: BlockNumber; blockNumber: BlockNumber;