From 475cb4fa692fd80d23446af1c21c4f163734d9fe Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 14 Apr 2020 12:09:36 +0200 Subject: [PATCH] Move Digest to interfaces (#2164) * Move Digest to interfaces * Unconstructable typos --- CHANGELOG.md | 1 + .../api-derive/src/type/HeaderExtended.ts | 4 +- packages/types/src/generic/Digest.spec.ts | 46 ---------------- packages/types/src/generic/Digest.ts | 52 ------------------- packages/types/src/generic/index.ts | 1 - .../src/interfaces/runtime/Digest.spec.ts | 30 +++++++++++ .../src/interfaces/runtime/definitions.ts | 4 +- .../types/src/interfaces/runtime/types.ts | 6 ++- .../types/src/primitive/Unconstructable.ts | 2 +- 9 files changed, 41 insertions(+), 105 deletions(-) delete mode 100644 packages/types/src/generic/Digest.spec.ts delete mode 100644 packages/types/src/generic/Digest.ts create mode 100644 packages/types/src/interfaces/runtime/Digest.spec.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index f448045cd5..a1697b97d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 1.11.0-beta.x +- `Digest` has been moved to definitions, removal of `GenericDigest` type - Fix entries decoding for `Option` # 1.10.1 Apr 13, 2020 diff --git a/packages/api-derive/src/type/HeaderExtended.ts b/packages/api-derive/src/type/HeaderExtended.ts index 4e34c2ed75..61cfbc508b 100644 --- a/packages/api-derive/src/type/HeaderExtended.ts +++ b/packages/api-derive/src/type/HeaderExtended.ts @@ -26,7 +26,7 @@ export default class HeaderExtended extends _Header { } private _extractAuthor (sessionValidators: AccountId[] = []): AccountId | undefined { - const [pitem] = this.digest.logsWith('PreRuntime'); + const [pitem] = this.digest.logs.filter(({ type }) => type === 'PreRuntime'); // extract from the substrate 2.0 PreRuntime digest if (pitem) { @@ -34,7 +34,7 @@ export default class HeaderExtended extends _Header { return engine.extractAuthor(data, sessionValidators); } else { - const [citem] = this.digest.logsWith('Consensus'); + const [citem] = this.digest.logs.filter(({ type }) => type === 'Consensus'); // extract author from the consensus (substrate 1.0, digest) if (citem) { diff --git a/packages/types/src/generic/Digest.spec.ts b/packages/types/src/generic/Digest.spec.ts deleted file mode 100644 index 53c156b775..0000000000 --- a/packages/types/src/generic/Digest.spec.ts +++ /dev/null @@ -1,46 +0,0 @@ -// Copyright 2017-2020 @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 { TypeRegistry } from '../create'; -import json3 from '../json/Header.003.json'; -import block1 from '../json/SignedBlock.003.00.json'; -import Digest from './Digest'; - -describe('Digest', (): void => { - const registry = new TypeRegistry(); - - it('has the correct JSON representation', (): void => { - const digest = new Digest(registry, json3.result.digest); - - expect(digest.toJSON()).toEqual(json3.result.digest); - }); - - it('decodes logs with consensus', (): void => { - const digest = new Digest(registry, json3.result.digest); - - expect(digest.logs.length).toEqual(1); - - const log = digest.logs[0]; - - expect(log.type).toBe('Consensus'); - expect(log.value.toJSON()).toEqual([ - 1634891105, - '0x69c97e0f00000000479c09f7ace12ed3e1af2dba6cca6831e257e80feef56c7474d6c69bae6f4e8a6e0045a87afe14296992c38b7d55abcbe617c441cd35e8667abbcb1678f31802' - ]); - }); - - it('filters logs, excluding items', (): void => { - const logs = new Digest(registry, block1.result.block.header.digest).logsWith('PreRuntime'); - - expect(logs.length).toEqual(1); - expect(logs[0].type).toEqual('PreRuntime'); - }); - - it('filters logs, including items', (): void => { - const logs = new Digest(registry, block1.result.block.header.digest).logsWithout('PreRuntime'); - - expect(logs.length).toEqual(1); - expect(logs[0].type).toEqual('Seal'); - }); -}); diff --git a/packages/types/src/generic/Digest.ts b/packages/types/src/generic/Digest.ts deleted file mode 100644 index 0b634ae84b..0000000000 --- a/packages/types/src/generic/Digest.ts +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2017-2020 @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 { DigestItem } from '../interfaces/runtime'; -import { AnyJson, Registry } from '../types'; - -import Struct from '../codec/Struct'; -import Vec from '../codec/Vec'; - -/** - * @name Digest - * @description - * A [[Header]] Digest - */ -export default class Digest extends Struct { - constructor (registry: Registry, value: any) { - super(registry, { - logs: 'Vec' - }, value); - } - - /** - * @description The [[DigestItem]] logs - */ - public get logs (): Vec { - return this.get('logs') as Vec; - } - - /** - * @description The [[DigestItem]] logs, filtered, filter items included. This is useful for derive functionality where only a certain type of log is to be returned. - */ - public logsWith (...include: string[]): Vec { - return this.logs.filter(({ type }): boolean => include.includes(type)) as Vec; - } - - /** - * @description The [[DigestItem]] logs, filtered, filter items exluded. This is useful for stripping headers for eg. WASM runtime execution. - */ - public logsWithout (...exclude: string[]): Vec { - return this.logs.filter(({ type }): boolean => !exclude.includes(type)) as Vec; - } - - /** - * @desrcript The JSON representation as it goes over RPC - */ - public toJSON (): AnyJson { - return { - logs: this.logs.map((log): string => log.toHex()) - }; - } -} diff --git a/packages/types/src/generic/index.ts b/packages/types/src/generic/index.ts index 34e12d3b5c..76428e65be 100644 --- a/packages/types/src/generic/index.ts +++ b/packages/types/src/generic/index.ts @@ -8,7 +8,6 @@ export { default as GenericAddress } from './Address'; export { default as GenericBlock } from './Block'; export { default as GenericCall } from './Call'; export { default as GenericConsensusEngineId } from './ConsensusEngineId'; -export { default as GenericDigest } from './Digest'; export { default as GenericEvent } from './Event'; export { default as GenericOrigin } from './Origin'; export { default as GenericVote } from './Vote'; diff --git a/packages/types/src/interfaces/runtime/Digest.spec.ts b/packages/types/src/interfaces/runtime/Digest.spec.ts new file mode 100644 index 0000000000..f85ade27d2 --- /dev/null +++ b/packages/types/src/interfaces/runtime/Digest.spec.ts @@ -0,0 +1,30 @@ +// Copyright 2017-2020 @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 { TypeRegistry } from '../../create'; +import json3 from '../../json/Header.003.json'; + +describe('Digest', (): void => { + const registry = new TypeRegistry(); + + it('has the correct JSON representation', (): void => { + const digest = registry.createType('Digest', json3.result.digest); + + expect(digest.logs.map((l) => l.toHex())).toEqual(json3.result.digest.logs); + }); + + it('decodes logs with consensus', (): void => { + const digest = registry.createType('Digest', json3.result.digest); + + expect(digest.logs.length).toEqual(1); + + const log = digest.logs[0]; + + expect(log.type).toBe('Consensus'); + expect(log.value.toJSON()).toEqual([ + 1634891105, + '0x69c97e0f00000000479c09f7ace12ed3e1af2dba6cca6831e257e80feef56c7474d6c69bae6f4e8a6e0045a87afe14296992c38b7d55abcbe617c441cd35e8667abbcb1678f31802' + ]); + }); +}); diff --git a/packages/types/src/interfaces/runtime/definitions.ts b/packages/types/src/interfaces/runtime/definitions.ts index 68d9bddfe8..1007e1e7cc 100644 --- a/packages/types/src/interfaces/runtime/definitions.ts +++ b/packages/types/src/interfaces/runtime/definitions.ts @@ -25,7 +25,9 @@ export default { digestLevels: 'u32' }, ConsensusEngineId: 'GenericConsensusEngineId', - Digest: 'GenericDigest', + Digest: { + logs: 'Vec' + }, DigestItem: { _enum: { Other: 'Bytes', // 0 diff --git a/packages/types/src/interfaces/runtime/types.ts b/packages/types/src/interfaces/runtime/types.ts index 815bc7bcdf..f9979edbfe 100644 --- a/packages/types/src/interfaces/runtime/types.ts +++ b/packages/types/src/interfaces/runtime/types.ts @@ -3,7 +3,7 @@ import { ITuple } from '@polkadot/types/types'; import { Compact, Enum, Int, Struct, U8aFixed, Vec } from '@polkadot/types/codec'; -import { GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericOrigin } from '@polkadot/types/generic'; +import { GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericOrigin } from '@polkadot/types/generic'; import { Bytes, Null, StorageKey, bool, u128, u32, u64, u8 } from '@polkadot/types/primitive'; import { AuthorityId } from '@polkadot/types/interfaces/consensus'; import { Signature } from '@polkadot/types/interfaces/extrinsics'; @@ -51,7 +51,9 @@ export interface Consensus extends ITuple<[ConsensusEngineId, Bytes]> {} export interface ConsensusEngineId extends GenericConsensusEngineId {} /** @name Digest */ -export interface Digest extends GenericDigest {} +export interface Digest extends Struct { + readonly logs: Vec; +} /** @name DigestItem */ export interface DigestItem extends Enum { diff --git a/packages/types/src/primitive/Unconstructable.ts b/packages/types/src/primitive/Unconstructable.ts index f61f02af17..8d56d0bb1c 100644 --- a/packages/types/src/primitive/Unconstructable.ts +++ b/packages/types/src/primitive/Unconstructable.ts @@ -9,7 +9,7 @@ import Null from './Null'; /** * @name Unconstructable * @description - * An unknown type theat fails on constrction with the type info + * An unknown type that fails on construction with the type info */ export default class Unconstructable extends Null { constructor (registry: Registry, typeName: string) {