From 70bc823d25a60f177511ef76cd5ab6a7f2255cdb Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 26 Jul 2019 14:34:41 +0200 Subject: [PATCH] Make Block, Header & Digest generic (#1196) * Make Block, Header & Digest generic * It lints * Linting passes, tests passes --- packages/api-derive/src/chain/bestNumber.ts | 7 +- .../src/chain/bestNumberFinalized.ts | 7 +- packages/api-derive/src/chain/getHeader.ts | 7 +- .../api-derive/src/chain/subscribeNewHead.ts | 6 +- .../api-derive/src/type/HeaderExtended.ts | 7 +- packages/api/src/Base.ts | 2 +- packages/api/src/SubmittableExtrinsic.ts | 14 ++- packages/api/src/checkTypes.manual.ts | 9 +- packages/api/src/promise/Api.ts | 4 +- packages/api/src/rx/Api.ts | 6 +- .../api/test/e2e/api/promise-alex.spec.ts | 6 +- .../test/e2e/api/promise-queries-dev.spec.ts | 4 +- .../e2e/api/promise-queries-doubleMap.spec.ts | 4 +- .../api/test/e2e/api/promise-queries.spec.ts | 10 +- packages/api/test/e2e/api/promise-tx.spec.ts | 10 +- packages/api/test/e2e/api/rx-queries.spec.ts | 3 +- packages/api/test/e2e/rpc-core/chain.spec.ts | 6 +- packages/rpc-provider/src/mock/index.ts | 11 ++- packages/types/src/interfaceRegistry.ts | 99 +++++++++++-------- .../types/src/interfaces/rpc/definitions.ts | 8 -- packages/types/src/interfaces/rpc/types.ts | 24 +---- .../runtime}/Header.spec.ts | 26 ++--- .../{rpc => runtime}/SignedBlock.spec.ts | 0 .../src/interfaces/runtime/definitions.ts | 32 +++++- .../types/src/interfaces/runtime/types.ts | 50 +++++++++- packages/types/src/interfaces/system/types.ts | 4 +- .../src/primitive/{ => Generic}/Block.spec.ts | 6 +- .../src/primitive/{ => Generic}/Block.ts | 23 +++-- .../{ => Generic}/ConsensusEngineId.ts | 6 +- .../primitive/{ => Generic}/Digest.spec.ts | 6 +- .../src/primitive/{ => Generic}/Digest.ts | 15 ++- packages/types/src/primitive/Generic/index.ts | 7 ++ packages/types/src/primitive/Header.ts | 78 --------------- packages/types/src/primitive/index.ts | 5 +- 34 files changed, 252 insertions(+), 260 deletions(-) rename packages/types/src/{primitive => interfaces/runtime}/Header.spec.ts (66%) rename packages/types/src/interfaces/{rpc => runtime}/SignedBlock.spec.ts (100%) rename packages/types/src/primitive/{ => Generic}/Block.spec.ts (92%) rename packages/types/src/primitive/{ => Generic}/Block.ts (69%) rename packages/types/src/primitive/{ => Generic}/ConsensusEngineId.ts (95%) rename packages/types/src/primitive/{ => Generic}/Digest.spec.ts (92%) rename packages/types/src/primitive/{ => Generic}/Digest.ts (93%) create mode 100644 packages/types/src/primitive/Generic/index.ts delete mode 100644 packages/types/src/primitive/Header.ts diff --git a/packages/api-derive/src/chain/bestNumber.ts b/packages/api-derive/src/chain/bestNumber.ts index bd90c4b74c..0725235867 100644 --- a/packages/api-derive/src/chain/bestNumber.ts +++ b/packages/api-derive/src/chain/bestNumber.ts @@ -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 return (): Observable => (api.rpc.chain.subscribeNewHead() as Observable
) .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() ); } diff --git a/packages/api-derive/src/chain/bestNumberFinalized.ts b/packages/api-derive/src/chain/bestNumberFinalized.ts index 72babee6b4..a2f92bd29f 100644 --- a/packages/api-derive/src/chain/bestNumberFinalized.ts +++ b/packages/api-derive/src/chain/bestNumberFinalized.ts @@ -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 => (api.rpc.chain.subscribeFinalizedHeads() as Observable
) .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() ); } diff --git a/packages/api-derive/src/chain/getHeader.ts b/packages/api-derive/src/chain/getHeader.ts index 8344cb15c2..7cfc1259d9 100644 --- a/packages/api-derive/src/chain/getHeader.ts +++ b/packages/api-derive/src/chain/getHeader.ts @@ -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'; *
* * ```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 { diff --git a/packages/api-derive/src/chain/subscribeNewHead.ts b/packages/api-derive/src/chain/subscribeNewHead.ts index 7364b63a52..cabaf1e919 100644 --- a/packages/api-derive/src/chain/subscribeNewHead.ts +++ b/packages/api-derive/src/chain/subscribeNewHead.ts @@ -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 => (api.rpc.chain.subscribeNewHead() as Observable
) .pipe( - filter((header: Header): boolean => !!header && !!header.blockNumber), + filter((header: Header): boolean => !!header && !!header.number), switchMap((header: Header): Observable => (combineLatest([ of(header), diff --git a/packages/api-derive/src/type/HeaderExtended.ts b/packages/api-derive/src/type/HeaderExtended.ts index ea1a51d460..ce9a6648bb 100644 --- a/packages/api-derive/src/type/HeaderExtended.ts +++ b/packages/api-derive/src/type/HeaderExtended.ts @@ -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[] = []) { diff --git a/packages/api/src/Base.ts b/packages/api/src/Base.ts index 59522b0aab..f0c61cae9b 100644 --- a/packages/api/src/Base.ts +++ b/packages/api/src/Base.ts @@ -114,7 +114,7 @@ export default abstract class ApiBase { * const api = new Api().isReady(); * * api.rpc.subscribeNewHead((header) => { - * console.log(`new block #${header.blockNumber.toNumber()}`); + * console.log(`new block #${header.number.toNumber()}`); * }); * ``` */ diff --git a/packages/api/src/SubmittableExtrinsic.ts b/packages/api/src/SubmittableExtrinsic.ts index 1bf51066c4..26ff428ad8 100644 --- a/packages/api/src/SubmittableExtrinsic.ts +++ b/packages/api/src/SubmittableExtrinsic.ts @@ -2,14 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ExtrinsicStatus, EventRecord, Hash, Index, SignedBlock } from '@polkadot/types/interfaces'; +import { ExtrinsicStatus, EventRecord, Hash, Header, Index, SignedBlock } from '@polkadot/types/interfaces'; import { AnyNumber, AnyU8a, Callback, Codec, IExtrinsic, IExtrinsicEra, IKeyringPair, SignatureOptions } from '@polkadot/types/types'; import { ApiInterfaceRx, ApiTypes } from './types'; import BN from 'bn.js'; import { Observable, combineLatest, of } from 'rxjs'; import { first, map, mergeMap, switchMap, tap } from 'rxjs/operators'; -import { createType, AccountId, Address, getTypeRegistry, Header, Method, Vec, ExtrinsicEra } from '@polkadot/types'; +import { createType, AccountId, Address, getTypeRegistry, Method, Vec, ExtrinsicEra } from '@polkadot/types'; import { isBn, isFunction, isNumber, isUndefined } from '@polkadot/util'; import filterEvents from './util/filterEvents'; @@ -194,12 +194,10 @@ export default function createSubmittableExtrinsic ( return expandOptions(options, { nonce }); } - const { blockNumber, hash } = header; - return expandOptions(options, { - blockHash: hash, + blockHash: header.hash, era: new ExtrinsicEra({ - current: blockNumber, + current: header.number, period: options.era || DEFAULT_MORTAL_LENGTH }), nonce @@ -274,7 +272,7 @@ export default function createSubmittableExtrinsic ( ...eraOptions, address, method: _extrinsic.method, - blockNumber: header ? header.blockNumber : 0, + blockNumber: header ? header.number : 0, genesisHash: api.genesisHash, version: api.extrinsicType }); @@ -290,7 +288,7 @@ export default function createSubmittableExtrinsic ( updateId = await api.signer.sign(_extrinsic, address, { ...eraOptions, - blockNumber: header ? header.blockNumber : new BN(0), + blockNumber: header ? header.number.toBn() : new BN(0), genesisHash: api.genesisHash }); } else { diff --git a/packages/api/src/checkTypes.manual.ts b/packages/api/src/checkTypes.manual.ts index 2f99ea1784..ba0528b15d 100644 --- a/packages/api/src/checkTypes.manual.ts +++ b/packages/api/src/checkTypes.manual.ts @@ -4,15 +4,14 @@ // Simple non-runnable checks to test type definitions in the editor itself -import { Index } from '@polkadot/types/interfaces'; +import { Header, Index } from '@polkadot/types/interfaces'; import { ApiPromise } from '@polkadot/api'; import { HeaderExtended } from '@polkadot/api-derive'; import { ConstantCodec } from '@polkadot/api-metadata/consts/types'; import testKeyring from '@polkadot/keyring/testingPairs'; import { IExtrinsic, IMethod } from '@polkadot/types/types'; -import { createType, Header } from '@polkadot/types'; -import { createTypeUnsafe } from '@polkadot/types/codec/createType'; +import createType, { createTypeUnsafe } from '@polkadot/types/codec/createType'; import { SubmittableResult } from './'; @@ -38,11 +37,11 @@ export default async function test (): Promise { }); await api.rpc.chain.subscribeNewHead
((header): void => { - console.log('current blockNumber:', header.blockNumber); + console.log('current blockNumber:', header.number); }); await api.rpc.chain.subscribeNewHead((header: Header): void => { - console.log('current blockNumber:', header.blockNumber); + console.log('current blockNumber:', header.number); }); await api.derive.chain.subscribeNewHead((header: HeaderExtended): void => { diff --git a/packages/api/src/promise/Api.ts b/packages/api/src/promise/Api.ts index acee881574..5c2a592600 100644 --- a/packages/api/src/promise/Api.ts +++ b/packages/api/src/promise/Api.ts @@ -40,7 +40,7 @@ import Combinator, { CombinatorCallback, CombinatorFunction } from './Combinator * * // make a subscription to the network head * api.rpc.chain.subscribeNewHead((header) => { - * console.log(`Chain is at #${header.blockNumber}`); + * console.log(`Chain is at #${header.number}`); * }); * ``` *
@@ -138,7 +138,7 @@ export default class ApiPromise extends ApiBase<'promise'> { * * new Api().isReady.then((api) => { * api.rpc.subscribeNewHead((header) => { - * console.log(`new block #${header.blockNumber.toNumber()}`); + * console.log(`new block #${header.number.toNumber()}`); * }); * }); * ``` diff --git a/packages/api/src/rx/Api.ts b/packages/api/src/rx/Api.ts index ea465be6ff..fe67b3854b 100644 --- a/packages/api/src/rx/Api.ts +++ b/packages/api/src/rx/Api.ts @@ -37,7 +37,7 @@ import ApiBase from '../Base'; * * // make a call to retrieve the current network head * api.rpc.chain.subscribeNewHead().subscribe((header) => { - * console.log(`Chain is at #${header.blockNumber}`); + * console.log(`Chain is at #${header.number}`); * }); * ``` *
@@ -131,7 +131,7 @@ export default class ApiRx extends ApiBase<'rxjs'> { * api.rpc.chain.subscribeNewHead() * )) * .subscribe((header) => { - * console.log(`new block #${header.blockNumber.toNumber()}`); + * console.log(`new block #${header.number.toNumber()}`); * }); * ``` */ @@ -157,7 +157,7 @@ export default class ApiRx extends ApiBase<'rxjs'> { * api.rpc.chain.subscribeNewHead() * )) * .subscribe((header) => { - * console.log(`new block #${header.blockNumber.toNumber()}`); + * console.log(`new block #${header.number.toNumber()}`); * }); * ``` */ diff --git a/packages/api/test/e2e/api/promise-alex.spec.ts b/packages/api/test/e2e/api/promise-alex.spec.ts index c9791f000e..5df9a6448b 100644 --- a/packages/api/test/e2e/api/promise-alex.spec.ts +++ b/packages/api/test/e2e/api/promise-alex.spec.ts @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { EventRecord } from '@polkadot/types/interfaces'; +import { EventRecord, Header } from '@polkadot/types/interfaces'; import WsProvider from '@polkadot/rpc-provider/ws'; -import { AccountId, ClassOf, Header, Option, Vec } from '@polkadot/types'; +import { AccountId, ClassOf, Option, Vec } from '@polkadot/types'; import ApiPromise from '../../../src/promise'; import describeE2E from '../../util/describeE2E'; @@ -55,7 +55,7 @@ describeE2E({ it('Subscribes to the best finalized header on ALEX', async (done): Promise<() => void> => { return ( api.rpc.chain.subscribeFinalizedHeads((head): void => { - expect(head instanceof Header).toBe(true); + expect(head instanceof ClassOf('Header')).toBe(true); done(); }) ); diff --git a/packages/api/test/e2e/api/promise-queries-dev.spec.ts b/packages/api/test/e2e/api/promise-queries-dev.spec.ts index c52e3342a4..219b8ded51 100644 --- a/packages/api/test/e2e/api/promise-queries-dev.spec.ts +++ b/packages/api/test/e2e/api/promise-queries-dev.spec.ts @@ -2,14 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Balance, Index } from '@polkadot/types/interfaces'; +import { Balance, Header, Index } from '@polkadot/types/interfaces'; import BN from 'bn.js'; import { DerivedBalances } from '@polkadot/api-derive/types'; import testingPairs from '@polkadot/keyring/testingPairs'; import WsProvider from '@polkadot/rpc-provider/ws'; -import { Header, Option, u32, createType } from '@polkadot/types'; +import { Option, u32, createType } from '@polkadot/types'; import ApiPromise from '../../../src/promise'; import describeE2E from '../../util/describeE2E'; diff --git a/packages/api/test/e2e/api/promise-queries-doubleMap.spec.ts b/packages/api/test/e2e/api/promise-queries-doubleMap.spec.ts index 1f413f794b..71d731ee79 100644 --- a/packages/api/test/e2e/api/promise-queries-doubleMap.spec.ts +++ b/packages/api/test/e2e/api/promise-queries-doubleMap.spec.ts @@ -2,9 +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 testingPairs from '@polkadot/keyring/testingPairs'; import WsProvider from '@polkadot/rpc-provider/ws'; -import { Header, Option } from '@polkadot/types'; +import { Option } from '@polkadot/types'; import { encodeAddress, randomAsU8a } from '@polkadot/util-crypto'; import ApiPromise from '../../../src/promise'; diff --git a/packages/api/test/e2e/api/promise-queries.spec.ts b/packages/api/test/e2e/api/promise-queries.spec.ts index 3c121d1476..e8608cfc1e 100644 --- a/packages/api/test/e2e/api/promise-queries.spec.ts +++ b/packages/api/test/e2e/api/promise-queries.spec.ts @@ -2,12 +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 { EventRecord, Hash, SessionIndex } from '@polkadot/types/interfaces'; +import { EventRecord, Hash, Header, SessionIndex } from '@polkadot/types/interfaces'; import { HeaderExtended } from '@polkadot/api-derive'; import WsProvider from '@polkadot/rpc-provider/ws'; import { LinkageResult } from '@polkadot/types/codec/Linkage'; -import { Header, Option, Vec, createType } from '@polkadot/types'; +import { Option, Vec, createType } from '@polkadot/types'; import ApiPromise from '../../../src/promise'; import describeE2E from '../../util/describeE2E'; @@ -34,7 +34,7 @@ describeE2E()('Promise e2e queries', (wsUrl): void => { it('subscribes to rpc', (done): Promise<() => void> => { return ( api.rpc.chain.subscribeNewHead((header: Header): void => { - expect(header.blockNumber.isZero()).toBe(false); + expect(header.number.isEmpty).toBe(false); done(); }) @@ -44,7 +44,7 @@ describeE2E()('Promise e2e queries', (wsUrl): void => { it('subscribes to finalized', (done): Promise<() => void> => { return ( api.rpc.chain.subscribeFinalizedHeads((header: Header): void => { - expect(header.blockNumber.isZero()).toBe(false); + expect(header.number.isEmpty).toBe(false); done(); }) @@ -54,7 +54,7 @@ describeE2E()('Promise e2e queries', (wsUrl): void => { it('subscribes to derive', (done): Promise<() => void> => { return ( api.derive.chain.subscribeNewHead((header: HeaderExtended): void => { - expect(header.blockNumber.isZero()).toBe(false); + expect(header.number.isEmpty).toBe(false); done(); }) diff --git a/packages/api/test/e2e/api/promise-tx.spec.ts b/packages/api/test/e2e/api/promise-tx.spec.ts index b4ebb8c3f4..007aff21c1 100644 --- a/packages/api/test/e2e/api/promise-tx.spec.ts +++ b/packages/api/test/e2e/api/promise-tx.spec.ts @@ -2,14 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { EventRecord, Hash, Index, SignedBlock } from '@polkadot/types/interfaces'; +import { EventRecord, Hash, Header, Index, SignedBlock } from '@polkadot/types/interfaces'; import Keyring from '@polkadot/keyring'; import testingPairs from '@polkadot/keyring/testingPairs'; import WsProvider from '@polkadot/rpc-provider/ws'; import { u8aToHex } from '@polkadot/util'; import { randomAsHex } from '@polkadot/util-crypto'; -import { ExtrinsicEra, Header } from '@polkadot/types'; +import { ExtrinsicEra } from '@polkadot/types'; import { SubmittableResult } from '../../../src'; import ApiPromise from '../../../src/promise'; @@ -161,7 +161,7 @@ describeE2E({ it('makes a transfer (specified era, previous block)', async (done): Promise => { const signedBlock = await api.rpc.chain.getBlock() as SignedBlock; - const currentHeight = signedBlock.block.header.number.subn(1); + const currentHeight = signedBlock.block.header.number.toBn().subn(1); const exERA = new ExtrinsicEra({ current: currentHeight, period: 10 }); const ex = api.tx.balances.transfer(keyring.eve.address, 12345); @@ -181,7 +181,7 @@ describeE2E({ const ex = api.tx.balances.transfer(keyring.eve.address, 12345); await api.rpc.chain.subscribeNewHead(async (header: Header): Promise => { - if (header.blockNumber.toNumber() === eraDeath - 1) { + if (header.number.toNumber() === eraDeath - 1) { try { await ex.signAndSend(keyring.alice, { blockHash, era: exERA, nonce } as any); } catch (error) { @@ -209,7 +209,7 @@ describeE2E({ const ex = api.tx.balances.transfer(keyring.eve.address, 12345); await api.rpc.chain.subscribeNewHead(async (header: Header): Promise => { - if (header.blockNumber.toNumber() === eraDeath - 1) { + if (header.number.toNumber() === eraDeath - 1) { try { await ex.signAndSend(keyring.alice.address, { blockHash, era: exERA, nonce } as any); } catch (error) { diff --git a/packages/api/test/e2e/api/rx-queries.spec.ts b/packages/api/test/e2e/api/rx-queries.spec.ts index 2fd8577497..cdd1b48e6b 100644 --- a/packages/api/test/e2e/api/rx-queries.spec.ts +++ b/packages/api/test/e2e/api/rx-queries.spec.ts @@ -2,13 +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 { Balance } from '@polkadot/types/interfaces'; +import { Balance, Header } from '@polkadot/types/interfaces'; import BN from 'bn.js'; import { Observable } from 'rxjs'; import { switchMap } from 'rxjs/operators'; -import { Header } from '@polkadot/types'; import testingPairs from '@polkadot/keyring/testingPairs'; import WsProvider from '@polkadot/rpc-provider/ws'; diff --git a/packages/api/test/e2e/rpc-core/chain.spec.ts b/packages/api/test/e2e/rpc-core/chain.spec.ts index 9abd57192e..ba12df0800 100644 --- a/packages/api/test/e2e/rpc-core/chain.spec.ts +++ b/packages/api/test/e2e/rpc-core/chain.spec.ts @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { RuntimeVersion } from '@polkadot/types/interfaces'; +import { Header, RuntimeVersion } from '@polkadot/types/interfaces'; -import { ClassOf, Header } from '@polkadot/types'; +import { ClassOf } from '@polkadot/types'; import WsProvider from '@polkadot/rpc-provider/ws'; import Rpc from '@polkadot/rpc-core'; @@ -23,7 +23,7 @@ describe.skip('e2e chain', (): void => { rpc.chain .subscribeNewHead() .subscribe((header: Header): void => { - expect(header).toBeInstanceOf(Header); + expect(header).toBeInstanceOf(ClassOf('Header')); if (++count === 3) { done(); diff --git a/packages/rpc-provider/src/mock/index.ts b/packages/rpc-provider/src/mock/index.ts index 360824019d..6385faf601 100644 --- a/packages/rpc-provider/src/mock/index.ts +++ b/packages/rpc-provider/src/mock/index.ts @@ -3,6 +3,7 @@ // 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 { Codec } from '@polkadot/types/types'; import { ProviderInterface, ProviderInterfaceEmitted, ProviderInterfaceEmitCb } from '../types'; import { MockStateSubscriptions, MockStateSubscriptionCallback, MockStateDb } from './types'; @@ -14,7 +15,7 @@ import testKeyring from '@polkadot/keyring/testing'; import storage from '@polkadot/api-metadata/storage/static'; import rpcMetadata from '@polkadot/types/Metadata/static'; import rpcSignedBlock from '@polkadot/types/json/SignedBlock.004.immortal.json'; -import { createType, Header } from '@polkadot/types'; +import { createType } from '@polkadot/types'; import { bnToU8a, logger, u8aToHex } from '@polkadot/util'; import { randomAsU8a } from '@polkadot/util-crypto'; @@ -156,12 +157,12 @@ export default class Mock implements ProviderInterface { } // create a new header (next block) - newHead = this.makeBlockHeader(newHead.blockNumber.toBn()); + newHead = this.makeBlockHeader(newHead.number.toBn()); // increment the balances and nonce for each account keyring.getPairs().forEach(({ publicKey }, index): void => { - this.setStateBn(storage.balances.freeBalance(publicKey), newHead.blockNumber.muln(3).iaddn(index)); - this.setStateBn(storage.system.accountNonce(publicKey), newHead.blockNumber.addn(index)); + this.setStateBn(storage.balances.freeBalance(publicKey), newHead.number.toBn().muln(3).iaddn(index)); + this.setStateBn(storage.system.accountNonce(publicKey), newHead.number.toBn().addn(index)); }); // set the timestamp for the current block @@ -182,7 +183,7 @@ export default class Mock implements ProviderInterface { private makeBlockHeader (prevNumber: BN): Header { const blockNumber = prevNumber.addn(1); - return new Header({ + return createType('Header', { digest: { logs: [] }, diff --git a/packages/types/src/interfaceRegistry.ts b/packages/types/src/interfaceRegistry.ts index 96a76d7009..7c63502cf6 100644 --- a/packages/types/src/interfaceRegistry.ts +++ b/packages/types/src/interfaceRegistry.ts @@ -1,8 +1,8 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Compact, Option, Vec } from './codec'; -import { AccountId, AccountIndex, Address, Block, Bool, Bytes, ConsensusEngineId, Data, Digest, DigestItem, Event, Extrinsic, ExtrinsicEra, Fixed64, H160, H256, H512, Header, I128, I16, I256, I32, I64, I8, ImmortalEra, Method, Moment, MortalEra, Null, Origin, SignaturePayload, StorageHasher, StorageKey, Text, Type, U128, U16, U256, U32, U64, U8, Vote, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive'; -import { AccountIdOf, Balance, BalanceOf, BlockNumber, Ed25519Signature, Hash, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Perbill, Permill, Phantom, SessionKeys, Signature, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime'; +import { AccountId, AccountIndex, Address, Bool, Bytes, Data, Event, Extrinsic, ExtrinsicEra, Fixed64, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H160, H256, H512, I128, I16, I256, I32, I64, I8, ImmortalEra, Method, Moment, MortalEra, Null, Origin, SignaturePayload, StorageHasher, StorageKey, Text, Type, U128, U16, U256, U32, U64, U8, Vote, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from './primitive'; +import { AccountIdOf, Balance, BalanceOf, Block, BlockNumber, Consensus, ConsensusEngineId, Digest, DigestItem, Ed25519Signature, Hash, Header, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Perbill, Permill, Phantom, PreRuntime, Seal, SealV0, SessionKeys, Signature, SignedBlock, Sr25519Signature, StorageData, ValidatorId, Weight, WeightMultiplier } from './interfaces/runtime'; import { AuthorityId } from './interfaces/consensus'; import { Keys, SessionIndex } from './interfaces/session'; import { AuthorityWeight, NextAuthority, PendingPause, PendingResume, StoredPendingChange, StoredState } from './interfaces/grandpa'; @@ -19,7 +19,7 @@ import { AttestedCandidate, AuctionIndex, BalanceUpload, Bidder, CandidateReceip import { EraIndex, EraRewards, Exposure, IndividualExposure, MomentOf, RewardDestination, StakingLedger, UnlockChunk, ValidatorPrefs } from './interfaces/staking'; import { DigestOf, EventId, EventIndex, EventRecord, EventRecord0to76, Key, Phase } from './interfaces/system'; import { TreasuryProposal } from './interfaces/treasury'; -import { ApiId, ChainProperties, Consensus, ExtrinsicStatus, Health, KeyValueOption, NetworkState, PeerInfo, PreRuntime, RuntimeVersion, RuntimeVersionApi, Seal, SealV0, SignedBlock, StorageChangeSet } from './interfaces/rpc'; +import { ApiId, ChainProperties, ExtrinsicStatus, Health, KeyValueOption, NetworkState, PeerInfo, RuntimeVersion, RuntimeVersionApi, StorageChangeSet } from './interfaces/rpc'; export interface InterfaceRegistry { AccountId: AccountId; @@ -35,9 +35,6 @@ export interface InterfaceRegistry { Event: Event; 'Option': Option; 'Vec': Vec; - Block: Block; - 'Option': Option; - 'Vec': Vec; Bool: Bool; 'Option': Option; 'Vec': Vec; @@ -47,19 +44,9 @@ export interface InterfaceRegistry { Bytes: Bytes; 'Option': Option; 'Vec': Vec; - ConsensusEngineId: ConsensusEngineId; - 'Compact': Compact; - 'Option': Option; - 'Vec': Vec; Data: Data; 'Option': Option; 'Vec': Vec; - Digest: Digest; - 'Option': Option; - 'Vec': Vec; - DigestItem: DigestItem; - 'Option': Option; - 'Vec': Vec; H160: H160; 'Option': Option; 'Vec': Vec; @@ -69,9 +56,6 @@ export interface InterfaceRegistry { H512: H512; 'Option': Option; 'Vec': Vec; - Header: Header; - 'Option
': Option
; - 'Vec
': Vec
; i8: i8; 'Option': Option; 'Vec': Vec; @@ -205,6 +189,19 @@ export interface InterfaceRegistry { SignaturePayload: SignaturePayload; 'Option': Option; 'Vec': Vec; + GenericBlock: GenericBlock; + 'Option': Option; + 'Vec': Vec; + GenericConsensusEngineId: GenericConsensusEngineId; + 'Compact': Compact; + 'Option': Option; + 'Vec': Vec; + GenericDigest: GenericDigest; + 'Option': Option; + 'Vec': Vec; + GenericDigestItem: GenericDigestItem; + 'Option': Option; + 'Vec': Vec; AccountIdOf: AccountIdOf; 'Option': Option; 'Vec': Vec; @@ -219,12 +216,52 @@ export interface InterfaceRegistry { 'Compact': Compact; 'Option': Option; 'Vec': Vec; + Signature: Signature; + 'Option': Option; + 'Vec': Vec; + Ed25519Signature: Ed25519Signature; + 'Option': Option; + 'Vec': Vec; + Sr25519Signature: Sr25519Signature; + 'Option': Option; + 'Vec': Vec; + ConsensusEngineId: ConsensusEngineId; + 'Compact': Compact; + 'Option': Option; + 'Vec': Vec; + PreRuntime: PreRuntime; + 'Option': Option; + 'Vec': Vec; + SealV0: SealV0; + 'Option': Option; + 'Vec': Vec; + Seal: Seal; + 'Option': Option; + 'Vec': Vec; + Consensus: Consensus; + 'Option': Option; + 'Vec': Vec; + DigestItem: DigestItem; + 'Option': Option; + 'Vec': Vec; + Digest: Digest; + 'Option': Option; + 'Vec': Vec; Hash: Hash; 'Option': Option; 'Vec': Vec; + Header: Header; + 'Option
': Option
; + 'Vec
': Vec
; Justification: Justification; 'Option': Option; 'Vec': Vec; + Block: Block; + 'Option': Option; + 'Vec': Vec; + SignedBlock: SignedBlock; + 'Option': Option; + 'Vec': Vec; KeyTypeId: KeyTypeId; 'Compact': Compact; 'Option': Option; @@ -250,15 +287,6 @@ export interface InterfaceRegistry { SessionKeys: SessionKeys; 'Option': Option; 'Vec': Vec; - Signature: Signature; - 'Option': Option; - 'Vec': Vec; - Ed25519Signature: Ed25519Signature; - 'Option': Option; - 'Vec': Vec; - Sr25519Signature: Sr25519Signature; - 'Option': Option; - 'Vec': Vec; StorageData: StorageData; 'Option': Option; 'Vec': Vec; @@ -581,9 +609,6 @@ export interface InterfaceRegistry { ChainProperties: ChainProperties; 'Option': Option; 'Vec': Vec; - Consensus: Consensus; - 'Option': Option; - 'Vec': Vec; ExtrinsicStatus: ExtrinsicStatus; 'Option': Option; 'Vec': Vec; @@ -599,24 +624,12 @@ export interface InterfaceRegistry { PeerInfo: PeerInfo; 'Option': Option; 'Vec': Vec; - PreRuntime: PreRuntime; - 'Option': Option; - 'Vec': Vec; RuntimeVersionApi: RuntimeVersionApi; 'Option': Option; 'Vec': Vec; RuntimeVersion: RuntimeVersion; 'Option': Option; 'Vec': Vec; - SealV0: SealV0; - 'Option': Option; - 'Vec': Vec; - Seal: Seal; - 'Option': Option; - 'Vec': Vec; - SignedBlock: SignedBlock; - 'Option': Option; - 'Vec': Vec; StorageChangeSet: StorageChangeSet; 'Option': Option; 'Vec': Vec; diff --git a/packages/types/src/interfaces/rpc/definitions.ts b/packages/types/src/interfaces/rpc/definitions.ts index 64ccec93f2..b6a7c49a1a 100644 --- a/packages/types/src/interfaces/rpc/definitions.ts +++ b/packages/types/src/interfaces/rpc/definitions.ts @@ -9,7 +9,6 @@ export default { tokenDecimals: 'u32', tokenSymbol: 'Text' }, - Consensus: '(ConsensusEngineId, Bytes)', ExtrinsicStatus: { _enum: { Future: 'Null', @@ -37,7 +36,6 @@ export default { bestHash: 'Hash', bestNumber: 'BlockNumber' }, - PreRuntime: '(ConsensusEngineId, Bytes)', RuntimeVersionApi: '(ApiId, u32)', RuntimeVersion: { specName: 'Text', @@ -47,12 +45,6 @@ export default { implVersion: 'u32', apis: 'Vec' }, - SealV0: '(u64, Signature)', - Seal: '(ConsensusEngineId, Bytes)', - SignedBlock: { - block: 'Block', - justification: 'Justification' - }, StorageChangeSet: { block: 'Hash', changes: 'Vec' diff --git a/packages/types/src/interfaces/rpc/types.ts b/packages/types/src/interfaces/rpc/types.ts index 189318dc5f..701d4d9970 100644 --- a/packages/types/src/interfaces/rpc/types.ts +++ b/packages/types/src/interfaces/rpc/types.ts @@ -2,8 +2,8 @@ import { Codec } from '../../types'; import { Enum, Option, Struct, Vec } from '../../codec'; -import { Block, Bytes, ConsensusEngineId, StorageKey, Text, bool, u32, u64, u8 } from '../../primitive'; -import { BlockNumber, Hash, Justification, Signature, StorageData } from '../runtime'; +import { StorageKey, Text, bool, u32, u64, u8 } from '../../primitive'; +import { BlockNumber, Hash, StorageData } from '../runtime'; /** Vec */ export type ApiId = Vec; @@ -16,9 +16,6 @@ export interface ChainProperties extends Struct { readonly tokenSymbol: Text; } -/** [ConsensusEngineId, Bytes] & Codec */ -export type Consensus = [ConsensusEngineId, Bytes] & Codec; - /** Enum */ export interface ExtrinsicStatus extends Enum { /** 0:: Future */ @@ -76,9 +73,6 @@ export interface PeerInfo extends Struct { readonly bestNumber: BlockNumber; } -/** [ConsensusEngineId, Bytes] & Codec */ -export type PreRuntime = [ConsensusEngineId, Bytes] & Codec; - /** Struct */ export interface RuntimeVersion extends Struct { /** Text */ @@ -98,20 +92,6 @@ export interface RuntimeVersion extends Struct { /** [ApiId, u32] & Codec */ export type RuntimeVersionApi = [ApiId, u32] & Codec; -/** [ConsensusEngineId, Bytes] & Codec */ -export type Seal = [ConsensusEngineId, Bytes] & Codec; - -/** [u64, Signature] & Codec */ -export type SealV0 = [u64, Signature] & Codec; - -/** Struct */ -export interface SignedBlock extends Struct { - /** Block */ - readonly block: Block; - /** Justification */ - readonly justification: Justification; -} - /** Struct */ export interface StorageChangeSet extends Struct { /** Hash */ diff --git a/packages/types/src/primitive/Header.spec.ts b/packages/types/src/interfaces/runtime/Header.spec.ts similarity index 66% rename from packages/types/src/primitive/Header.spec.ts rename to packages/types/src/interfaces/runtime/Header.spec.ts index 11e3450c78..4877b58b1a 100644 --- a/packages/types/src/primitive/Header.spec.ts +++ b/packages/types/src/interfaces/runtime/Header.spec.ts @@ -2,23 +2,23 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import '../injector'; +import '../../injector'; import BN from 'bn.js'; -import json1 from '../json/Header.001.json'; -import json2 from '../json/Header.002.json'; -import json3 from '../json/Header.003.json'; -import block00300 from '../json/SignedBlock.003.00.json'; -import block00301 from '../json/SignedBlock.003.01.json'; -import Header from './Header'; +import createType from '../../codec/createType'; +import json1 from '../../json/Header.001.json'; +import json2 from '../../json/Header.002.json'; +import json3 from '../../json/Header.003.json'; +import block00300 from '../../json/SignedBlock.003.00.json'; +import block00301 from '../../json/SignedBlock.003.01.json'; describe('Header', (): void => { it('decodes an actual JSON response', (): void => { - const header = new Header(json1.result); + const header = createType('Header', json1.result); expect( - header.blockNumber.toNumber() + header.number.toNumber() ).toEqual(0); expect( header.extrinsicsRoot.toString() @@ -35,7 +35,7 @@ describe('Header', (): void => { }); it('parses old-style JSON headers (deprecated)', (): void => { - const header = new Header(json2.result); + const header = createType('Header', json2.result); expect( header.digest.logs @@ -43,18 +43,18 @@ describe('Header', (): void => { }); it('creates a valid hash (incl. digest & compact)', (): void => { - const header = new Header(json3.result); + const header = createType('Header', json3.result); expect( header.hash.toHex() ).toEqual('0x464692ad0e225a74274a7ef411e045f1fc7c2639b5f780c7c18f91f4100f5e54'); expect( - header.blockNumber.eq(new BN(1650758)) + header.number.eq(new BN(1650758)) ).toBe(true); }); it('calculates correct hash, matching with parentHash', (): void => { - const blockHash = new Header(block00300.result.block.header).hash.toHex(); + const blockHash = createType('Header', block00300.result.block.header).hash.toHex(); expect(blockHash).toEqual(block00301.result.block.header.parentHash); }); diff --git a/packages/types/src/interfaces/rpc/SignedBlock.spec.ts b/packages/types/src/interfaces/runtime/SignedBlock.spec.ts similarity index 100% rename from packages/types/src/interfaces/rpc/SignedBlock.spec.ts rename to packages/types/src/interfaces/runtime/SignedBlock.spec.ts diff --git a/packages/types/src/interfaces/runtime/definitions.ts b/packages/types/src/interfaces/runtime/definitions.ts index a7406d8eeb..83a61efb80 100644 --- a/packages/types/src/interfaces/runtime/definitions.ts +++ b/packages/types/src/interfaces/runtime/definitions.ts @@ -8,8 +8,37 @@ export default { Balance: 'u128', BalanceOf: 'Balance', BlockNumber: 'u64', + + // block + // :: signature + Signature: 'H512', + Ed25519Signature: 'Signature', + Sr25519Signature: 'Signature', + // :: digest + ConsensusEngineId: 'GenericConsensusEngineId', + PreRuntime: '(ConsensusEngineId, Bytes)', + SealV0: '(u64, Signature)', + Seal: '(ConsensusEngineId, Bytes)', + Consensus: '(ConsensusEngineId, Bytes)', + DigestItem: 'GenericDigestItem', + Digest: 'GenericDigest', Hash: 'H256', + // :: parts + Header: { + parentHash: 'Hash', + number: 'Compact', + stateRoot: 'Hash', + extrinsicsRoot: 'Hash', + digest: 'Digest' + }, Justification: 'Bytes', + // :: definitions + Block: 'GenericBlock', + SignedBlock: { + block: 'Block', + justification: 'Justification' + }, + KeyTypeId: 'u32', LockIdentifier: '[u8; 8]', Index: 'u64', @@ -19,9 +48,6 @@ export default { SessionKeys: { ed25519: 'AccountId' }, - Signature: 'H512', - Ed25519Signature: 'Signature', - Sr25519Signature: 'Signature', StorageData: 'Bytes', KeyValue: '(StorageKey, StorageData)', ValidatorId: 'AccountId', diff --git a/packages/types/src/interfaces/runtime/types.ts b/packages/types/src/interfaces/runtime/types.ts index 30a2e0cfb0..e29377574b 100644 --- a/packages/types/src/interfaces/runtime/types.ts +++ b/packages/types/src/interfaces/runtime/types.ts @@ -1,8 +1,8 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Codec } from '../../types'; -import { Struct, Vec } from '../../codec'; -import { AccountId, Bytes, Fixed64, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive'; +import { Compact, Struct, Vec } from '../../codec'; +import { AccountId, Bytes, Fixed64, GenericBlock, GenericConsensusEngineId, GenericDigest, GenericDigestItem, H256, H512, Null, StorageKey, u128, u32, u64, u8 } from '../../primitive'; /** AccountId */ export type AccountIdOf = AccountId; @@ -13,15 +13,44 @@ export type Balance = u128; /** Balance */ export type BalanceOf = Balance; +/** GenericBlock */ +export type Block = GenericBlock; + /** u64 */ export type BlockNumber = u64; +/** [ConsensusEngineId, Bytes] & Codec */ +export type Consensus = [ConsensusEngineId, Bytes] & Codec; + +/** GenericConsensusEngineId */ +export type ConsensusEngineId = GenericConsensusEngineId; + +/** GenericDigest */ +export type Digest = GenericDigest; + +/** GenericDigestItem */ +export type DigestItem = GenericDigestItem; + /** Signature */ export type Ed25519Signature = Signature; /** H256 */ export type Hash = H256; +/** Struct */ +export interface Header extends Struct { + /** Hash */ + readonly parentHash: Hash; + /** Compact */ + readonly number: Compact; + /** Hash */ + readonly stateRoot: Hash; + /** Hash */ + readonly extrinsicsRoot: Hash; + /** Digest */ + readonly digest: Digest; +} + /** u64 */ export type Index = u64; @@ -46,6 +75,15 @@ export type Permill = u32; /** Null */ export type Phantom = Null; +/** [ConsensusEngineId, Bytes] & Codec */ +export type PreRuntime = [ConsensusEngineId, Bytes] & Codec; + +/** [ConsensusEngineId, Bytes] & Codec */ +export type Seal = [ConsensusEngineId, Bytes] & Codec; + +/** [u64, Signature] & Codec */ +export type SealV0 = [u64, Signature] & Codec; + /** Struct */ export interface SessionKeys extends Struct { /** AccountId */ @@ -55,6 +93,14 @@ export interface SessionKeys extends Struct { /** H512 */ export type Signature = H512; +/** Struct */ +export interface SignedBlock extends Struct { + /** Block */ + readonly block: Block; + /** Justification */ + readonly justification: Justification; +} + /** Signature */ export type Sr25519Signature = Signature; diff --git a/packages/types/src/interfaces/system/types.ts b/packages/types/src/interfaces/system/types.ts index 7a4a59f58c..c0e4e7a1ff 100644 --- a/packages/types/src/interfaces/system/types.ts +++ b/packages/types/src/interfaces/system/types.ts @@ -1,8 +1,8 @@ // Auto-generated via `yarn build:interfaces`, do not edit import { Enum, Struct, Vec } from '../../codec'; -import { Bytes, Digest, Event, u32, u8 } from '../../primitive'; -import { Hash } from '../runtime'; +import { Bytes, Event, u32, u8 } from '../../primitive'; +import { Digest, Hash } from '../runtime'; /** Digest */ export type DigestOf = Digest; diff --git a/packages/types/src/primitive/Block.spec.ts b/packages/types/src/primitive/Generic/Block.spec.ts similarity index 92% rename from packages/types/src/primitive/Block.spec.ts rename to packages/types/src/primitive/Generic/Block.spec.ts index e595178e6b..c272375fb3 100644 --- a/packages/types/src/primitive/Block.spec.ts +++ b/packages/types/src/primitive/Generic/Block.spec.ts @@ -2,12 +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 '../injector'; +import '../../injector'; import extrinsics from '@polkadot/api-metadata/extrinsics/static'; -import block00300 from '../json/SignedBlock.003.00.json'; -import Method from './Method'; +import block00300 from '../../json/SignedBlock.003.00.json'; +import Method from '../Method'; import Block from './Block'; describe('Block', (): void => { diff --git a/packages/types/src/primitive/Block.ts b/packages/types/src/primitive/Generic/Block.ts similarity index 69% rename from packages/types/src/primitive/Block.ts rename to packages/types/src/primitive/Generic/Block.ts index 092af48466..840d1b84e5 100644 --- a/packages/types/src/primitive/Block.ts +++ b/packages/types/src/primitive/Generic/Block.ts @@ -2,16 +2,23 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Hash } from '../interfaces/runtime'; -import { AnyU8a } from '../types'; +import { Digest, DigestItem, Hash, Header } from '../../interfaces/runtime'; +import { AnyNumber, AnyU8a } from '../../types'; import { blake2AsU8a } from '@polkadot/util-crypto'; -import createType, { ClassOf } from '../codec/createType'; -import Extrinsic from './Extrinsic/Extrinsic'; -import Struct from '../codec/Struct'; -import Vec from '../codec/Vec'; -import Header, { HeaderValue } from './Header'; +import createType, { ClassOf } from '../../codec/createType'; +import Extrinsic from '../Extrinsic/Extrinsic'; +import Struct from '../../codec/Struct'; +import Vec from '../../codec/Vec'; + +export interface HeaderValue { + digest?: Digest | { logs: DigestItem[] }; + extrinsicsRoot?: AnyU8a; + number?: AnyNumber; + parentHash?: AnyU8a; + stateRoot?: AnyU8a; +} export interface BlockValue { extrinsics?: AnyU8a[]; @@ -26,7 +33,7 @@ export interface BlockValue { export default class Block extends Struct { public constructor (value?: BlockValue | Uint8Array) { super({ - header: Header, + header: ClassOf('Header'), extrinsics: ClassOf('Vec') }, value); } diff --git a/packages/types/src/primitive/ConsensusEngineId.ts b/packages/types/src/primitive/Generic/ConsensusEngineId.ts similarity index 95% rename from packages/types/src/primitive/ConsensusEngineId.ts rename to packages/types/src/primitive/Generic/ConsensusEngineId.ts index 02feac9f65..aace6cdf2c 100644 --- a/packages/types/src/primitive/ConsensusEngineId.ts +++ b/packages/types/src/primitive/Generic/ConsensusEngineId.ts @@ -5,9 +5,9 @@ import BN from 'bn.js'; import { assert, bnToBn } from '@polkadot/util'; -import Bytes from './Bytes'; -import U32 from './U32'; -import U64 from './U64'; +import Bytes from '../Bytes'; +import U32 from '../U32'; +import U64 from '../U64'; const CID_AURA = 0x61727561; // 'aura' const CID_BABE = 0x65626162; // 'babe' diff --git a/packages/types/src/primitive/Digest.spec.ts b/packages/types/src/primitive/Generic/Digest.spec.ts similarity index 92% rename from packages/types/src/primitive/Digest.spec.ts rename to packages/types/src/primitive/Generic/Digest.spec.ts index 52998201de..263ab30975 100644 --- a/packages/types/src/primitive/Digest.spec.ts +++ b/packages/types/src/primitive/Generic/Digest.spec.ts @@ -2,10 +2,10 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import '../injector'; +import '../../injector'; -import json3 from '../json/Header.003.json'; -import block1 from '../json/SignedBlock.003.00.json'; +import json3 from '../../json/Header.003.json'; +import block1 from '../../json/SignedBlock.003.00.json'; import ConsensusEngineId, { CID_AURA, CID_BABE, CID_GRPA } from './ConsensusEngineId'; import Digest from './Digest'; diff --git a/packages/types/src/primitive/Digest.ts b/packages/types/src/primitive/Generic/Digest.ts similarity index 93% rename from packages/types/src/primitive/Digest.ts rename to packages/types/src/primitive/Generic/Digest.ts index 1581ef07a9..80621a0dd0 100644 --- a/packages/types/src/primitive/Digest.ts +++ b/packages/types/src/primitive/Generic/Digest.ts @@ -2,17 +2,16 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AuthorityId } from '../interfaces/consensus'; -import { Hash } from '../interfaces/runtime'; -import { Consensus, PreRuntime, Seal, SealV0 } from '../interfaces/rpc'; +import { AuthorityId } from '../../interfaces/consensus'; +import { Consensus, Hash, PreRuntime, Seal, SealV0 } from '../../interfaces/runtime'; import { assert } from '@polkadot/util'; -import { ClassOf } from '../codec/createType'; -import Enum from '../codec/Enum'; -import Struct from '../codec/Struct'; -import Vec from '../codec/Vec'; -import Bytes from './Bytes'; +import { ClassOf } from '../../codec/createType'; +import Enum from '../../codec/Enum'; +import Struct from '../../codec/Struct'; +import Vec from '../../codec/Vec'; +import Bytes from '../Bytes'; /** * @name DigestItem diff --git a/packages/types/src/primitive/Generic/index.ts b/packages/types/src/primitive/Generic/index.ts new file mode 100644 index 0000000000..827a8dc1d3 --- /dev/null +++ b/packages/types/src/primitive/Generic/index.ts @@ -0,0 +1,7 @@ +// Copyright 2017-2019 @polkadot/types 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 GenericBlock } from './Block'; +export { default as GenericConsensusEngineId } from './ConsensusEngineId'; +export { default as GenericDigest, DigestItem as GenericDigestItem } from './Digest'; diff --git a/packages/types/src/primitive/Header.ts b/packages/types/src/primitive/Header.ts deleted file mode 100644 index ba14b0d5bf..0000000000 --- a/packages/types/src/primitive/Header.ts +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright 2017-2019 @polkadot/types 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 { BlockNumber, Hash } from '../interfaces/runtime'; -import { AnyNumber, AnyU8a } from '../types'; - -import { ClassOf } from '../codec/createType'; -import Compact from '../codec/Compact'; -import Struct from '../codec/Struct'; -import Digest, { DigestItem } from './Digest'; - -export interface HeaderValue { - digest?: Digest | { logs: DigestItem[] }; - extrinsicsRoot?: AnyU8a; - number?: AnyNumber; - parentHash?: AnyU8a; - stateRoot?: AnyU8a; -} - -/** - * @name Header - * @description - * A [[Block]] header - */ -export default class Header extends Struct { - public constructor (value?: HeaderValue | Uint8Array | null) { - super({ - parentHash: ClassOf('Hash'), - number: ClassOf('Compact'), - stateRoot: ClassOf('Hash'), - extrinsicsRoot: ClassOf('Hash'), - digest: ClassOf('Digest') - }, value || {}); - } - - /** - * @description The wrapped [[BlockNumber]] - */ - public get blockNumber (): BlockNumber { - return (this.get('number') as Compact).unwrap(); - } - - /** - * @description The wrapped [[Digest]] - */ - public get digest (): Digest { - return this.get('digest') as Digest; - } - - /** - * @description The wrapped extrisics root as a [[Hash]] - */ - public get extrinsicsRoot (): Hash { - return this.get('extrinsicsRoot') as Hash; - } - - /** - * @description Alias for `blockNumber` (this is displayed in JSON) - */ - public get number (): BlockNumber { - return this.blockNumber; - } - - /** - * @description The wrapped parent as a [[Hash]] - */ - public get parentHash (): Hash { - return this.get('parentHash') as Hash; - } - - /** - * @description The wrapped state root as a [[Hash]] - */ - public get stateRoot (): Hash { - return this.get('stateRoot') as Hash; - } -} diff --git a/packages/types/src/primitive/index.ts b/packages/types/src/primitive/index.ts index 0a411e7633..23d8a2a165 100644 --- a/packages/types/src/primitive/index.ts +++ b/packages/types/src/primitive/index.ts @@ -14,17 +14,14 @@ export { default as AccountId } from './AccountId'; export { default as AccountIndex } from './AccountIndex'; export { default as Address } from './Address'; export { default as Event } from './Event'; -export { default as Block } from './Block'; export { default as Bool, default as bool } from './Bool'; export { default as Bytes } from './Bytes'; -export { default as ConsensusEngineId } from './ConsensusEngineId'; export { default as Data } from './Data'; -export { default as Digest, DigestItem } from './Digest'; export * from './Extrinsic'; +export * from './Generic'; export { default as H160 } from './H160'; export { default as H256 } from './H256'; export { default as H512 } from './H512'; -export { default as Header } from './Header'; export { default as i8, default as I8 } from './I8'; export { default as i16, default as I16 } from './I16'; export { default as i32, default as I32 } from './I32';