Add derive.chain.{getHeader,subscribeNewHead} with author (#602)
* Add derive.chain.{getHeader,subscribeNewHead} with author
* tslint cast, ignore
This commit is contained in:
+1
-1
@@ -24,7 +24,7 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.2.2",
|
||||
"@polkadot/dev": "^0.23.12",
|
||||
"@polkadot/dev": "^0.23.13",
|
||||
"@polkadot/ts": "^0.1.51"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
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';
|
||||
import { HeaderAndValidators } from './subscribeNewHead';
|
||||
|
||||
/**
|
||||
* Get the a specific block header and extend it with the author
|
||||
*/
|
||||
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) =>
|
||||
new HeaderExtended(header, validators)
|
||||
),
|
||||
catchError(() =>
|
||||
// where rpc.chain.getHeader throws, we will land here - it can happen that
|
||||
// we supplied an invalid hash. (Due to defaults, storeage will have an
|
||||
// empty value, so only the RPC is affected). So return undefined
|
||||
// tslint:disable-next-line
|
||||
of() as Observable<undefined>
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -4,3 +4,5 @@
|
||||
|
||||
export * from './bestNumber';
|
||||
export * from './bestNumberFinalised';
|
||||
export * from './getHeader';
|
||||
export * from './subscribeNewHead';
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { Observable, combineLatest, of } from 'rxjs';
|
||||
import { filter, map, switchMap } 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';
|
||||
|
||||
export type HeaderAndValidators = [Header, Array<AccountId>];
|
||||
|
||||
/**
|
||||
* Subscribe to block headers and extend it with the author
|
||||
*/
|
||||
export function subscribeNewHead (api: ApiInterface$Rx) {
|
||||
return (): Observable<HeaderExtended> =>
|
||||
(api.rpc.chain.subscribeNewHead() as Observable<Header>)
|
||||
.pipe(
|
||||
filter((header: Header) =>
|
||||
header && !!header.blockNumber
|
||||
),
|
||||
switchMap((header: Header): Observable<HeaderAndValidators> =>
|
||||
combineLatest(
|
||||
of(header),
|
||||
// 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>>
|
||||
)
|
||||
),
|
||||
map(([header, validators]: HeaderAndValidators) =>
|
||||
new HeaderExtended(header, validators)
|
||||
),
|
||||
drr()
|
||||
);
|
||||
}
|
||||
@@ -29,7 +29,7 @@ export interface DecoratedRpc<OnCall> {
|
||||
|
||||
export interface QueryableStorageFunction<OnCall> extends StorageFunction {
|
||||
(arg?: any): OnCall;
|
||||
at: (hash: Hash, arg?: any) => OnCall;
|
||||
at: (hash: Uint8Array | string, arg?: any) => OnCall;
|
||||
}
|
||||
|
||||
export interface QueryableModuleStorage<OnCall> {
|
||||
|
||||
@@ -9,6 +9,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
|
||||
import Compact from './codec/Compact';
|
||||
import Struct from './codec/Struct';
|
||||
|
||||
import AccountId from './AccountId';
|
||||
import BlockNumber from './BlockNumber';
|
||||
import Digest, { DigestItem } from './Digest';
|
||||
import Hash from './Hash';
|
||||
@@ -81,3 +82,30 @@ export default class Header extends Struct {
|
||||
return this.get('stateRoot') as Hash;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @name HeaderExtended
|
||||
* @description
|
||||
* A [[Block]] header with an additional `author` field that indicates the block author
|
||||
*/
|
||||
export class HeaderExtended extends Header {
|
||||
private _author?: AccountId;
|
||||
|
||||
constructor (header: Header, sessionValidators: Array<AccountId>) {
|
||||
super(header);
|
||||
|
||||
const { digest: { logs } } = header;
|
||||
const digestItem = logs.find(({ type }) => type === 'Seal');
|
||||
|
||||
this._author = digestItem && sessionValidators.length
|
||||
? sessionValidators[digestItem.asSeal.slot.toNumber() % sessionValidators.length]
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Convenience method, returns the author for the block
|
||||
*/
|
||||
get author (): AccountId | undefined {
|
||||
return this._author;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1293,10 +1293,10 @@
|
||||
resolved "https://registry.yarnpkg.com/@nodelib/fs.stat/-/fs.stat-1.1.3.tgz#2b5a3ab3f918cca48a8c754c08168e3f03eba61b"
|
||||
integrity sha512-shAmDyaQC4H92APFoIaVDHCx5bStIocgvbwQyxPRrbUY20V1EYTbSDchWbuwlMG3V17cprZhA6+78JfB+3DTPw==
|
||||
|
||||
"@polkadot/dev@^0.23.12":
|
||||
version "0.23.12"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.23.12.tgz#327c9d326b7a13604f917ce2b6df2686c6e0a73e"
|
||||
integrity sha512-rXLkaYTWqBl3+60grGeyIJFRjl69U7SJfdLrVY+R8hy4/ki4OjOurXnf13sX+gWtVNNNKMmRn+gg6fQ11E59Ow==
|
||||
"@polkadot/dev@^0.23.13":
|
||||
version "0.23.13"
|
||||
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.23.13.tgz#a1933eb029bc2d6221ab55c89d47abfe22f013d6"
|
||||
integrity sha512-g/WsW8VakLwwKpIoJ2vv7QTp1NOUHbXrHNu4F3soeCFV66MjeTYCKRaH5ipsc6sEK0S/rBZFdAmQcEbi3KJbRg==
|
||||
dependencies:
|
||||
"@babel/cli" "^7.2.3"
|
||||
"@babel/core" "^7.2.2"
|
||||
|
||||
Reference in New Issue
Block a user