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 { map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Bytes, Vec } from '@polkadot/types';
import { Bytes, Vec, u32 } from '@polkadot/types';
import { drr } from '../util/drr';
@@ -17,23 +17,26 @@ import { drr } from '../util/drr';
*/
export function receivedHeartbeats (api: ApiInterfaceRx): () => Observable<DerivedHeartbeats> {
return (): Observable<DerivedHeartbeats> => {
return api.query.imOnline && api.query.imOnline.receivedHeartbeats
? api.queryMulti<[Vec<AccountId>, SessionIndex]>([
api.query.imOnline.keys,
api.query.session.currentIndex
return api.query.imOnline && api.query.imOnline.receivedHeartbeats && api.query.imOnline.authoredBlocks
? api.queryMulti<[SessionIndex, Vec<AccountId>]>([
api.query.session.currentIndex,
api.query.session.validators
]).pipe(
switchMap(([keys, currentIndex]): Observable<[AccountId[], Bytes[]]> =>
switchMap(([currentIndex, validators]): Observable<[AccountId[], Bytes[], u32[]]> =>
combineLatest([
of(keys),
api.query.imOnline.receivedHeartbeats.multi<Bytes>(
keys.map((_address, index): [SessionIndex, number] => [currentIndex, index])
)
of(validators),
api.query.imOnline.receivedHeartbeats.multi<Bytes>(validators.map((_address, index): [SessionIndex, number] => [currentIndex, index])),
api.query.imOnline.authoredBlocks.multi<u32>(validators.map((address): [SessionIndex, AccountId] => [currentIndex, address]))
])
),
map(([keys, heartbeats]): DerivedHeartbeats =>
keys.reduce((result: DerivedHeartbeats, key, index): DerivedHeartbeats => ({
map(([validators, heartbeats, numBlocks]): DerivedHeartbeats =>
validators.reduce((result: DerivedHeartbeats, validator, index): DerivedHeartbeats => ({
...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()
+7 -1
View File
@@ -60,7 +60,13 @@ export interface DerivedFees {
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 {
blockNumber: BlockNumber;