Optimize heartbeat retrievals (#2590)
This commit is contained in:
@@ -13,6 +13,25 @@ import { BN_ZERO } from '@polkadot/util';
|
||||
|
||||
import { memo } from '../util';
|
||||
|
||||
function mapResult ([result, validators, heartbeats, numBlocks]: [DeriveHeartbeats, AccountId[], Option<Bytes>[], u32[]]): DeriveHeartbeats {
|
||||
validators.forEach((validator, index): void => {
|
||||
const validatorId = validator.toString();
|
||||
const blockCount = numBlocks[index];
|
||||
const hasMessage = !heartbeats[index].isEmpty;
|
||||
const prev = result[validatorId];
|
||||
|
||||
if (!prev || prev.hasMessage !== hasMessage || !prev.blockCount.eq(blockCount)) {
|
||||
result[validatorId] = {
|
||||
blockCount,
|
||||
hasMessage,
|
||||
isOnline: hasMessage || blockCount.gt(BN_ZERO)
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Return a boolean array indicating whether the passed accounts had received heartbeats in the current session
|
||||
*/
|
||||
@@ -20,8 +39,9 @@ export function receivedHeartbeats (instanceId: string, api: ApiInterfaceRx): ()
|
||||
return memo(instanceId, (): Observable<DeriveHeartbeats> =>
|
||||
api.query.imOnline?.receivedHeartbeats
|
||||
? api.derive.staking.overview().pipe(
|
||||
switchMap(({ currentIndex, validators }): Observable<[AccountId[], Option<Bytes>[], u32[]]> =>
|
||||
switchMap(({ currentIndex, validators }): Observable<[DeriveHeartbeats, AccountId[], Option<Bytes>[], u32[]]> =>
|
||||
combineLatest([
|
||||
of({}),
|
||||
of(validators),
|
||||
api.query.imOnline.receivedHeartbeats.multi<Option<Bytes>>(
|
||||
validators.map((_address, index) => [currentIndex, index])),
|
||||
@@ -29,16 +49,8 @@ export function receivedHeartbeats (instanceId: string, api: ApiInterfaceRx): ()
|
||||
validators.map((address) => [currentIndex, address]))
|
||||
])
|
||||
),
|
||||
map(([validators, heartbeats, numBlocks]): DeriveHeartbeats =>
|
||||
validators.reduce((result: DeriveHeartbeats, validator, index): DeriveHeartbeats => ({
|
||||
...result,
|
||||
[validator.toString()]: {
|
||||
blockCount: numBlocks[index],
|
||||
hasMessage: !heartbeats[index].isEmpty,
|
||||
isOnline: !heartbeats[index].isEmpty || numBlocks[index].gt(BN_ZERO)
|
||||
}
|
||||
}), {})
|
||||
)
|
||||
map(mapResult)
|
||||
)
|
||||
: of({}));
|
||||
: of({})
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user