From 36d7a85ed2044fae35ec8a81a50a2165fbfdc427 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 17 May 2019 11:44:14 +0200 Subject: [PATCH] Move HeaderExtended type to api-derive (#923) * Move HeaderExtended type to api-derive * Really remove HeaderExt from Header this time around --- packages/api-derive/src/chain/getHeader.ts | 3 +- .../api-derive/src/chain/subscribeNewHead.ts | 3 +- packages/api-derive/src/index.ts | 2 + .../api-derive/src/type/HeaderExtended.ts | 65 +++++++++++++++++++ packages/api-derive/src/type/index.ts | 5 ++ packages/api/src/checkTypes.manual.ts | 3 +- packages/types/README.md | 5 +- packages/types/src/rpc/Header.ts | 62 ------------------ packages/types/src/rpc/index.ts | 2 +- 9 files changed, 81 insertions(+), 69 deletions(-) create mode 100644 packages/api-derive/src/type/HeaderExtended.ts create mode 100644 packages/api-derive/src/type/index.ts diff --git a/packages/api-derive/src/chain/getHeader.ts b/packages/api-derive/src/chain/getHeader.ts index eb8f1fad72..654cad1dcc 100644 --- a/packages/api-derive/src/chain/getHeader.ts +++ b/packages/api-derive/src/chain/getHeader.ts @@ -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'; diff --git a/packages/api-derive/src/chain/subscribeNewHead.ts b/packages/api-derive/src/chain/subscribeNewHead.ts index a4f7a6bb8d..356dc87f22 100644 --- a/packages/api-derive/src/chain/subscribeNewHead.ts +++ b/packages/api-derive/src/chain/subscribeNewHead.ts @@ -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]; diff --git a/packages/api-derive/src/index.ts b/packages/api-derive/src/index.ts index a59cb1096e..63baa55fae 100644 --- a/packages/api-derive/src/index.ts +++ b/packages/api-derive/src/index.ts @@ -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 }; diff --git a/packages/api-derive/src/type/HeaderExtended.ts b/packages/api-derive/src/type/HeaderExtended.ts new file mode 100644 index 0000000000..c047e8e505 --- /dev/null +++ b/packages/api-derive/src/type/HeaderExtended.ts @@ -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 = []) { + 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 + }; + } +} diff --git a/packages/api-derive/src/type/index.ts b/packages/api-derive/src/type/index.ts new file mode 100644 index 0000000000..8145e0d27c --- /dev/null +++ b/packages/api-derive/src/type/index.ts @@ -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'; diff --git a/packages/api/src/checkTypes.manual.ts b/packages/api/src/checkTypes.manual.ts index 4af3cc28af..c0615d3231 100644 --- a/packages/api/src/checkTypes.manual.ts +++ b/packages/api/src/checkTypes.manual.ts @@ -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(); diff --git a/packages/types/README.md b/packages/types/README.md index 4927d76777..6dfddb4609 100644 --- a/packages/types/README.md +++ b/packages/types/README.md @@ -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] | diff --git a/packages/types/src/rpc/Header.ts b/packages/types/src/rpc/Header.ts index 52407aab2e..d8577895e0 100644 --- a/packages/types/src/rpc/Header.ts +++ b/packages/types/src/rpc/Header.ts @@ -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 = []) { - 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 - }; - } -} diff --git a/packages/types/src/rpc/index.ts b/packages/types/src/rpc/index.ts index ecb66ef92a..0c445949ef 100644 --- a/packages/types/src/rpc/index.ts +++ b/packages/types/src/rpc/index.ts @@ -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';