From d53a61ec8695f88fbae3a0f3fe94fcefa8ee0056 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 4 May 2021 12:27:01 +0300 Subject: [PATCH] ConsensusEngineId implements [u8; 4] (#3482) * ConsensusEngineId implements [u8; 4] * Adjust tests * isLE --- CHANGELOG.md | 1 + packages/types/src/augment/registry.ts | 1 - .../src/generic/ConsensusEngineId.spec.ts | 17 ++--- .../types/src/generic/ConsensusEngineId.ts | 73 ++++++++++--------- .../src/interfaces/runtime/Digest.spec.ts | 2 +- 5 files changed, 46 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 49c5b8f074..711bca66ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Changes: - Re-add isPedantic check on storage decoding - Adjust capability detection (only on full-known historic `AccountInfo`) +- `ConsensusEngineId` now implements `[u8;4]` (mapping closer to Rust source) - Additional Xcm types - Adjust parachain types - Remove error logging on createType (details passed in error) diff --git a/packages/types/src/augment/registry.ts b/packages/types/src/augment/registry.ts index c3839239f3..2133d41718 100644 --- a/packages/types/src/augment/registry.ts +++ b/packages/types/src/augment/registry.ts @@ -73,7 +73,6 @@ declare module '@polkadot/types/types/registry' { 'Compact': Compact; 'Compact': Compact; 'Compact': Compact; - 'Compact': Compact; 'Compact': Compact; 'Compact': Compact; 'Compact': Compact; diff --git a/packages/types/src/generic/ConsensusEngineId.spec.ts b/packages/types/src/generic/ConsensusEngineId.spec.ts index b1cf6dfd04..97d48b3921 100644 --- a/packages/types/src/generic/ConsensusEngineId.spec.ts +++ b/packages/types/src/generic/ConsensusEngineId.spec.ts @@ -1,22 +1,17 @@ // Copyright 2017-2021 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import { hexToBn } from '@polkadot/util'; - -import { CID_AURA, CID_BABE, CID_GRPA, GenericConsensusEngineId as ConsensusEngineId } from './ConsensusEngineId'; +import { TypeRegistry } from '../create'; +import { CID_AURA, GenericConsensusEngineId as ConsensusEngineId } from './ConsensusEngineId'; describe('ConsensusEngineId', (): void => { + const registry = new TypeRegistry(); + it('creates a valid id for aura', (): void => { - expect(ConsensusEngineId.stringToId('FRNK')).toEqual(CID_GRPA); - expect(ConsensusEngineId.stringToId('aura')).toEqual(CID_AURA); + expect(new ConsensusEngineId(registry, 'aura').toU8a()).toEqual(CID_AURA); }); it('reverses an id to string for babe', (): void => { - expect(ConsensusEngineId.idToString(CID_BABE)).toEqual('BABE'); - }); - - it('reverses a non-ASCII id to a hex string', (): void => { - expect(ConsensusEngineId.idToString(hexToBn('0x01020304', { isLe: true }))).toEqual('0x01020304'); - expect(ConsensusEngineId.idToString(0x04030201)).toEqual('0x01020304'); + expect(new ConsensusEngineId(registry, 'BABE').toString()).toEqual('BABE'); }); }); diff --git a/packages/types/src/generic/ConsensusEngineId.ts b/packages/types/src/generic/ConsensusEngineId.ts index 42a6fb2d6d..ff4b9764d6 100644 --- a/packages/types/src/generic/ConsensusEngineId.ts +++ b/packages/types/src/generic/ConsensusEngineId.ts @@ -2,40 +2,35 @@ // SPDX-License-Identifier: Apache-2.0 import type { AccountId } from '../interfaces/runtime'; +import type { AnyU8a, Registry } from '../types'; import BN from 'bn.js'; -import { bnToU8a, isAscii, u8aToHex, u8aToString } from '@polkadot/util'; +import { bnToU8a, isNumber, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util'; +import { U8aFixed } from '../codec/U8aFixed'; import { Bytes } from '../primitive/Bytes'; import { u32 } from '../primitive/U32'; -// there are all reversed since it is actually encoded as u32, LE, -// this means that FRNK has the bytes as KNRF -export const CID_AURA = 0x61727561; // 'aura' -export const CID_BABE = 0x45424142; // 'BABE' -export const CID_GRPA = 0x4b4e5246; // 'FRNK' (don't ask, used to be afg1) -export const CID_POW = 0x5f776f70; // 'pow_' +export const CID_AURA = stringToU8a('aura'); +export const CID_BABE = stringToU8a('BABE'); +export const CID_GRPA = stringToU8a('FRNK'); +export const CID_POW = stringToU8a('pow_'); /** * @name GenericConsensusEngineId * @description - * A 4-byte identifier (actually a [u8; 4]) identifying the engine, e.g. for Aura it would be [b'a', b'u', b'r', b'a'] + * A 4-byte identifier identifying the engine */ -export class GenericConsensusEngineId extends u32 { - public static idToString (input: number | BN): string { - const u8a = bnToU8a(input); - - return isAscii(u8a) - ? u8aToString(u8a) - : u8aToHex(u8a); - } - - public static stringToId (input: string): number { - return input - .split('') - .reverse() - .reduce((result, char): number => (result * 256) + char.charCodeAt(0), 0); +export class GenericConsensusEngineId extends U8aFixed { + constructor (registry: Registry, value?: AnyU8a) { + super( + registry, + isNumber(value) + ? bnToU8a(value, { isLe: false }) + : value, + 32 + ); } /** @@ -83,11 +78,7 @@ export class GenericConsensusEngineId extends u32 { ]; } - private _getPowAuthor (bytes: Bytes): AccountId { - return this.registry.createType('AccountId', bytes); - } - - private _getH160Author (bytes: Bytes): AccountId { + private _getBytesAsAuthor (bytes: Bytes): AccountId { return this.registry.createType('AccountId', bytes); } @@ -103,22 +94,34 @@ export class GenericConsensusEngineId extends u32 { } } - if (this.isPow) { - return this._getPowAuthor(bytes); - } - - // Moonbeam is neither Aura nor Babe nor Pow and uses h160 addresses - if (bytes.length === 20) { - return this._getH160Author(bytes); + // For pow & Moonbeam, the bytes are the actual author + if (this.isPow || bytes.length === 20) { + return this._getBytesAsAuthor(bytes); } return undefined; } + /** + * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information + */ + public toHuman (): string { + return this.toString(); + } + + /** + * @description Returns the base runtime type name for this instance + */ + public toRawType (): string { + return 'ConsensusEngineId'; + } + /** * @description Override the default toString to return a 4-byte string */ public toString (): string { - return GenericConsensusEngineId.idToString(this as BN); + return this.isAscii + ? u8aToString(this) + : u8aToHex(this); } } diff --git a/packages/types/src/interfaces/runtime/Digest.spec.ts b/packages/types/src/interfaces/runtime/Digest.spec.ts index 119753ead0..e9e67a0d54 100644 --- a/packages/types/src/interfaces/runtime/Digest.spec.ts +++ b/packages/types/src/interfaces/runtime/Digest.spec.ts @@ -24,7 +24,7 @@ describe('Digest', (): void => { expect(log.type).toBe('Consensus'); expect(log.value.toJSON()).toEqual([ - 1634891105, + '0x61757261', '0x69c97e0f00000000479c09f7ace12ed3e1af2dba6cca6831e257e80feef56c7474d6c69bae6f4e8a6e0045a87afe14296992c38b7d55abcbe617c441cd35e8667abbcb1678f31802' ]); });