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';
+2 -1
View File
@@ -5,9 +5,10 @@
// Simple non-runnable checks to test type definitions in the editor itself
import { ApiPromise } from '@polkadot/api';
import { HeaderExtended } from '@polkadot/api-derive';
import testKeyring from '@polkadot/keyring/testingPairs';
import { IExtrinsic, IMethod } from '@polkadot/types/types';
import { Header, HeaderExtended } from '@polkadot/types';
import { Header } from '@polkadot/types';
export default async function test () {
const api = await ApiPromise.create();
+2 -3
View File
@@ -149,7 +149,6 @@ These types are not used in the runtime, but are rather used in RPC results:
| [[DigestItem]] | A [[EnumType]] the specifies the specific item in the logs of a [[Digest]] |
| [[ExtrinsicStatus]] | An EnumType that indicates the status of the Extrinsic as been submitted |
| [[Header]] | A [[Block]] header |
| [[HeaderExtended]] | A [[Block]] header with an additional `author` field that indicates the block author] |
| [[Health]] | A system health indicator, reported back over RPC |
| [[Json]] | Wraps the a JSON structure retrieve via RPC. It extends the standard JS Map |
| [[NetworkState]] | Wraps the properties retrieved from the chain via the `system.network_state` RPC call |
@@ -163,8 +162,8 @@ These types are not used in the runtime, but are rather used in RPC results:
## Derive types
These types are are specific for the Polkadot-JS API, so you won't find a representation of them in the SCALE codec or the Substrate core. They are used in the [api-derive](https://www.npmjs.com/package/@polkadot/api-derive) methods.
These types are are specific for the Polkadot-JS derive API, so you won't find a representation of them in the SCALE codec or the Substrate core. They are used in the [api-derive](https://www.npmjs.com/package/@polkadot/api-derive) methods.
| **Types** | |
| --- | --- |
| [[HeaderExtended]] | A [[Block]] header with an additional `author` field that indicates the block author] |
| [[HeaderExtended]] | A [[Header]] header with an additional `author` field that indicates the block author] |
-62
View File
@@ -8,8 +8,6 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
import Compact from '../codec/Compact';
import Struct from '../codec/Struct';
import U64 from '../primitive/U64';
import AccountId from '../primitive/AccountId';
import Hash from '../primitive/Hash';
import BlockNumber from '../type/BlockNumber';
import Digest, { DigestItem } from './Digest';
@@ -89,63 +87,3 @@ 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 | 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
};
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ export { default as Block } from './Block';
export { default as ChainProperties } from './ChainProperties';
export { default as Digest, DigestItem } from './Digest';
export { default as ExtrinsicStatus } from './ExtrinsicStatus';
export { default as Header, HeaderExtended } from './Header';
export { default as Header } from './Header';
export { default as Health } from './Health';
export { default as Json } from './Json';
export { default as NetworkState } from './NetworkState';