Move HeaderExtended type to api-derive (#923)

* Move HeaderExtended type to api-derive

* Really remove HeaderExt from Header this time around
This commit is contained in:
Jaco Greeff
2019-05-17 11:44:14 +02:00
committed by Amaury Martiny
parent 43a1d95809
commit 36d7a85ed2
9 changed files with 81 additions and 69 deletions
+2 -1
View File
@@ -5,8 +5,9 @@
import { Observable, combineLatest, of } from 'rxjs';
import { catchError, map } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { Header, HeaderExtended } from '@polkadot/types';
import { Header } from '@polkadot/types';
import { HeaderExtended } from '../type';
import { drr } from '../util/drr';
import { HeaderAndValidators } from './subscribeNewHead';
@@ -5,8 +5,9 @@
import { Observable, combineLatest, of } from 'rxjs';
import { filter, map, switchMap } from 'rxjs/operators';
import { ApiInterface$Rx } from '@polkadot/api/types';
import { AccountId, Header, HeaderExtended } from '@polkadot/types';
import { AccountId, Header } from '@polkadot/types';
import { HeaderExtended } from '../type';
import { drr } from '../util/drr';
export type HeaderAndValidators = [Header, Array<AccountId>];
+2
View File
@@ -13,6 +13,8 @@ import * as democracy from './democracy';
import * as session from './session';
import * as staking from './staking';
export * from './type';
// Put all derived functions in an object, for easier Object.keys()-ing.
const functions = { accounts, balances, chain, democracy, session, staking };
@@ -0,0 +1,65 @@
// 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 { AccountId, Header, u64 } 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 {
private _author?: AccountId;
constructor (header: Header | null = null, sessionValidators: Array<AccountId> = []) {
super(header);
if (!header || !header.digest || !sessionValidators.length) {
return;
}
let item = header.digest.logs.find((log) => log.isConsensus);
let slot: u64 | undefined;
// extract author from the consensus (substrate 1.0, digest)
if (item) {
const consensus = item.asConsensus;
if (consensus.isAura) {
slot = consensus.asAura[0];
}
} else {
item = header.digest.logs.find((log) => log.isSeal);
// extract author from the seal (pre substrate 1.0, backwards compat)
if (item) {
slot = item.asSeal.slot;
}
}
// found a slot? Great, extract the validator
if (slot) {
this._author = sessionValidators[slot.toNumber() % sessionValidators.length];
}
}
/**
* @description Convenience method, returns the author for the block
*/
get author (): AccountId | undefined {
return this._author;
}
/**
* @description Creates the JSON representation
*/
toJSON (): any {
return {
...super.toJSON(),
author: this.author
? this.author.toJSON()
: undefined
};
}
}
+5
View File
@@ -0,0 +1,5 @@
// 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.
export { default as HeaderExtended } from './HeaderExtended';