derive.chain.* allows for non-available session (#638)

* derive.chain.* allows for non-available session

* Cleanup typing

* Slightly more robust validators init
This commit is contained in:
Jaco Greeff
2019-01-26 16:45:11 +01:00
committed by GitHub
parent d482c3a917
commit 3ced943f36
3 changed files with 12 additions and 8 deletions
+8 -6
View File
@@ -5,7 +5,6 @@
import { Observable, combineLatest, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, Header } from '@polkadot/types/index';
import { HeaderExtended } from '@polkadot/types/Header';
import { drr } from '../util/drr';
@@ -25,11 +24,14 @@ import { HeaderAndValidators } from './subscribeNewHead';
*/
export function getHeader (api: ApiInterface$Rx) {
return (hash: Uint8Array | string): Observable<HeaderExtended | undefined> =>
combineLatest(
api.rpc.chain.getHeader(hash) as Observable<Header>,
api.query.session.validators.at(hash) as any as Observable<Array<AccountId>>
).pipe(
map(([header, validators]: HeaderAndValidators) =>
// tslint:disable-next-line
(combineLatest(
api.rpc.chain.getHeader(hash),
api.query.session
? api.query.session.validators.at(hash)
: of([])
) as Observable<HeaderAndValidators>).pipe(
map(([header, validators]) =>
new HeaderExtended(header, validators)
),
catchError(() =>
@@ -36,7 +36,9 @@ export function subscribeNewHead (api: ApiInterface$Rx) {
// theoretically we could combine at the first call with session.validators(), however
// we make 100% sure we actually get the validators at a specific block so when these
// change at an era boundary, we have the previous values to ensure our indexes are correct
api.query.session.validators.at(header.hash) as any as Observable<Array<AccountId>>
api.query.session
? api.query.session.validators.at(header.hash)
: of([])
)
),
map(([header, validators]) =>
+1 -1
View File
@@ -91,7 +91,7 @@ export default class Header extends Struct {
export class HeaderExtended extends Header {
private _author?: AccountId;
constructor (header: Header, sessionValidators: Array<AccountId>) {
constructor (header: Header, sessionValidators: Array<AccountId> = []) {
super(header);
const { digest: { logs } } = header;