Make Block, Header & Digest generic (#1196)

* Make Block, Header & Digest generic

* It lints

* Linting passes, tests passes
This commit is contained in:
Jaco Greeff
2019-07-26 14:34:41 +02:00
committed by GitHub
parent 441148889d
commit 70bc823d25
34 changed files with 252 additions and 260 deletions
+3 -4
View File
@@ -2,12 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BlockNumber } from '@polkadot/types/interfaces';
import { BlockNumber, Header } from '@polkadot/types/interfaces';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Header } from '@polkadot/types';
import { drr } from '../util/drr';
@@ -27,8 +26,8 @@ export function bestNumber (api: ApiInterfaceRx): () => Observable<BlockNumber>
return (): Observable<BlockNumber> =>
(api.rpc.chain.subscribeNewHead() as Observable<Header>)
.pipe(
filter((header: Header): boolean => !!header && !!header.blockNumber),
map(({ blockNumber }: Header): BlockNumber => blockNumber),
filter((header: Header): boolean => !!header && !!header.number),
map((header: Header): BlockNumber => header.number.unwrap()),
drr()
);
}
@@ -2,12 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { BlockNumber } from '@polkadot/types/interfaces';
import { BlockNumber, Header } from '@polkadot/types/interfaces';
import { Observable } from 'rxjs';
import { filter, map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Header } from '@polkadot/types';
import { drr } from '../util/drr';
@@ -28,8 +27,8 @@ export function bestNumberFinalized (api: ApiInterfaceRx): () => Observable<Bloc
return (): Observable<BlockNumber> =>
(api.rpc.chain.subscribeFinalizedHeads() as Observable<Header>)
.pipe(
filter((header: Header): boolean => !!header && !!header.blockNumber),
map(({ blockNumber }: Header): BlockNumber => blockNumber),
filter((header: Header): boolean => !!header && !!header.number),
map((header: Header): BlockNumber => header.number.unwrap()),
drr()
);
}
+4 -3
View File
@@ -2,10 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Header } from '@polkadot/types/interfaces';
import { Observable, combineLatest, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { Header } from '@polkadot/types';
import { HeaderExtended } from '../type';
import { drr } from '../util/drr';
@@ -20,9 +21,9 @@ import { HeaderAndValidators } from './subscribeNewHead';
* <BR>
*
* ```javascript
* const { author, blockNumber } = await api.derive.chain.getHeader('0x123...456');
* const { author, number } = await api.derive.chain.getHeader('0x123...456');
*
* console.log(`block #${blockNumber} was authored by ${author}`);
* console.log(`block #${number} was authored by ${author}`);
* ```
*/
export function getHeader (api: ApiInterfaceRx): (hash: Uint8Array | string) => Observable<HeaderExtended | undefined> {
@@ -2,10 +2,12 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Header } from '@polkadot/types/interfaces';
import { Observable, combineLatest, of } from 'rxjs';
import { filter, map, switchMap } from 'rxjs/operators';
import { ApiInterfaceRx } from '@polkadot/api/types';
import { AccountId, Header } from '@polkadot/types';
import { AccountId } from '@polkadot/types';
import { HeaderExtended } from '../type';
import { drr } from '../util/drr';
@@ -29,7 +31,7 @@ export function subscribeNewHead (api: ApiInterfaceRx): () => Observable<HeaderE
return (): Observable<HeaderExtended> =>
(api.rpc.chain.subscribeNewHead() as Observable<Header>)
.pipe(
filter((header: Header): boolean => !!header && !!header.blockNumber),
filter((header: Header): boolean => !!header && !!header.number),
switchMap((header: Header): Observable<HeaderAndValidators> =>
(combineLatest([
of(header),
@@ -2,15 +2,18 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AccountId, Header, u64 } from '@polkadot/types';
import { Header } from '@polkadot/types/interfaces';
import { AnyJsonObject } from '@polkadot/types/types';
import { AccountId, u64, ClassOf } from '@polkadot/types';
/**
* @name HeaderExtended
* @description
* A [[Block]] header with an additional `author` field that indicates the block author
*/
export default class HeaderExtended extends Header {
export default class HeaderExtended extends ClassOf('Header') {
private _author?: AccountId;
public constructor (header: Header | null = null, sessionValidators: AccountId[] = []) {