diff --git a/packages/api-derive/src/type/HeaderExtended.ts b/packages/api-derive/src/type/HeaderExtended.ts index a9de5a7af7..894942ca3e 100644 --- a/packages/api-derive/src/type/HeaderExtended.ts +++ b/packages/api-derive/src/type/HeaderExtended.ts @@ -32,7 +32,7 @@ export default class HeaderExtended extends _Header { if (pitem) { const [engine, data] = pitem.asPreRuntime; - if (engine.isBabe || engine.isAura) { + if (engine.isAbrs || engine.isBabe || engine.isAura) { this._author = engine.extractAuthor(data, sessionValidators); } } else { diff --git a/packages/types/src/interfaceRegistry.ts b/packages/types/src/interfaceRegistry.ts index 5064bd2c0c..c6b915c01e 100644 --- a/packages/types/src/interfaceRegistry.ts +++ b/packages/types/src/interfaceRegistry.ts @@ -8,7 +8,9 @@ import { Keys, SessionIndex, SessionKeysPolkadot, SessionKeysSubstrate } from '. import { AuthorityWeight, NextAuthority, PendingPause, PendingResume, StoredPendingChange, StoredState } from './interfaces/grandpa'; import { ApprovalFlag, SetIndex, Vote, VoteIndex, VoteThreshold, VoterInfo } from './interfaces/elections'; import { InclusionHeight, Uncle, UncleEntryItem } from './interfaces/authorship'; +import { RawAuraPreDigest } from './interfaces/aura'; import { BabeWeight, RawBabePreDigest, SlotNumber } from './interfaces/babe'; +import { RawAuraBorosPreDigest } from './interfaces/abrs'; import { BalanceLock, VestingSchedule, WithdrawReasons } from './interfaces/balances'; import { MemberCount, ProposalIndex, Votes } from './interfaces/collective'; import { AliveContractInfo, CodeHash, ContractInfo, ContractStorageKey, Gas, PrefabWasmModule, PrefabWasmModuleReserved, Schedule, SeedOf, TombstoneContractInfo, TrieId } from './interfaces/contracts'; @@ -311,6 +313,9 @@ export interface InterfaceRegistry { UncleEntryItem: UncleEntryItem; 'Option': Option; 'Vec': Vec; + RawAuraPreDigest: RawAuraPreDigest; + 'Option': Option; + 'Vec': Vec; BabeWeight: BabeWeight; 'Compact': Compact; 'Option': Option; @@ -322,6 +327,9 @@ export interface InterfaceRegistry { RawBabePreDigest: RawBabePreDigest; 'Option': Option; 'Vec': Vec; + RawAuraBorosPreDigest: RawAuraBorosPreDigest; + 'Option': Option; + 'Vec': Vec; VestingSchedule: VestingSchedule; 'Option': Option; 'Vec': Vec; diff --git a/packages/types/src/interfaces/abrs/definitions.ts b/packages/types/src/interfaces/abrs/definitions.ts new file mode 100644 index 0000000000..c16e95a602 --- /dev/null +++ b/packages/types/src/interfaces/abrs/definitions.ts @@ -0,0 +1,14 @@ +// 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 { + types: { + RawAuraBorosPreDigest: { + _enum: { + Aura: 'RawAuraPreDigest', + Babe: 'RawBabePreDigest' + } + } + } +}; diff --git a/packages/types/src/interfaces/abrs/index.ts b/packages/types/src/interfaces/abrs/index.ts new file mode 100644 index 0000000000..f94c8a0ae8 --- /dev/null +++ b/packages/types/src/interfaces/abrs/index.ts @@ -0,0 +1,3 @@ +// Auto-generated via `yarn build:interfaces`, do not edit + +export * from './types'; diff --git a/packages/types/src/interfaces/abrs/types.ts b/packages/types/src/interfaces/abrs/types.ts new file mode 100644 index 0000000000..87415dc704 --- /dev/null +++ b/packages/types/src/interfaces/abrs/types.ts @@ -0,0 +1,17 @@ +// Auto-generated via `yarn build:interfaces`, do not edit + +import { Enum } from '../../codec'; +import { RawAuraPreDigest } from '../aura'; +import { RawBabePreDigest } from '../babe'; + +/** Enum */ +export interface RawAuraBorosPreDigest extends Enum { + /** 0:: Aura(RawAuraPreDigest) */ + readonly isAura: boolean; + /** RawAuraPreDigest */ + readonly asAura: RawAuraPreDigest; + /** 1:: Babe(RawBabePreDigest) */ + readonly isBabe: boolean; + /** RawBabePreDigest */ + readonly asBabe: RawBabePreDigest; +} diff --git a/packages/types/src/interfaces/aura/definitions.ts b/packages/types/src/interfaces/aura/definitions.ts new file mode 100644 index 0000000000..c6114c8bd9 --- /dev/null +++ b/packages/types/src/interfaces/aura/definitions.ts @@ -0,0 +1,11 @@ +// 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 { + types: { + RawAuraPreDigest: { + slotNumber: 'u64' + } + } +}; diff --git a/packages/types/src/interfaces/aura/index.ts b/packages/types/src/interfaces/aura/index.ts new file mode 100644 index 0000000000..f94c8a0ae8 --- /dev/null +++ b/packages/types/src/interfaces/aura/index.ts @@ -0,0 +1,3 @@ +// Auto-generated via `yarn build:interfaces`, do not edit + +export * from './types'; diff --git a/packages/types/src/interfaces/aura/types.ts b/packages/types/src/interfaces/aura/types.ts new file mode 100644 index 0000000000..deeff7e43b --- /dev/null +++ b/packages/types/src/interfaces/aura/types.ts @@ -0,0 +1,10 @@ +// Auto-generated via `yarn build:interfaces`, do not edit + +import { Struct } from '../../codec'; +import { u64 } from '../../primitive'; + +/** Struct */ +export interface RawAuraPreDigest extends Struct { + /** u64 */ + readonly slotNumber: u64; +} diff --git a/packages/types/src/interfaces/definitions.ts b/packages/types/src/interfaces/definitions.ts index c5829e4fed..ff3868ac7d 100644 --- a/packages/types/src/interfaces/definitions.ts +++ b/packages/types/src/interfaces/definitions.ts @@ -21,9 +21,13 @@ export { default as grandpa } from './grandpa/definitions'; // elections before democracy export { default as elections } from './elections/definitions'; -// remaining, alphabetical odering +// authoring, abra uses babe & aura export { default as authorship } from './authorship/definitions'; +export { default as aura } from './aura/definitions'; export { default as babe } from './babe/definitions'; +export { default as abrs } from './abrs/definitions'; + +// remaining, alphabetical odering export { default as balances } from './balances/definitions'; export { default as collective } from './collective/definitions'; export { default as contracts } from './contracts/definitions'; diff --git a/packages/types/src/interfaces/types.ts b/packages/types/src/interfaces/types.ts index 0641821293..0b3c623272 100644 --- a/packages/types/src/interfaces/types.ts +++ b/packages/types/src/interfaces/types.ts @@ -6,7 +6,9 @@ export * from './session/types'; export * from './grandpa/types'; export * from './elections/types'; export * from './authorship/types'; +export * from './aura/types'; export * from './babe/types'; +export * from './abrs/types'; export * from './balances/types'; export * from './collective/types'; export * from './contracts/types'; diff --git a/packages/types/src/primitive/Generic/ConsensusEngineId.ts b/packages/types/src/primitive/Generic/ConsensusEngineId.ts index a797b37572..6f61ae4d34 100644 --- a/packages/types/src/primitive/Generic/ConsensusEngineId.ts +++ b/packages/types/src/primitive/Generic/ConsensusEngineId.ts @@ -10,13 +10,13 @@ import { bnToBn } from '@polkadot/util'; import createType from '../../codec/createType'; import Bytes from '../Bytes'; import U32 from '../U32'; -import U64 from '../U64'; +const CID_ABRS = 0x53524241; // 'ABRS' const CID_AURA = 0x61727561; // 'aura' const CID_BABE = 0x45424142; // 'BABE' const CID_GRPA = 0x4b4e5246; // 'FRNK' (don't ask, used to be afg1) -export { CID_AURA, CID_BABE, CID_GRPA }; +export { CID_ABRS, CID_AURA, CID_BABE, CID_GRPA }; /** * @name ConsensusEngineId @@ -38,6 +38,13 @@ export default class ConsensusEngineId extends U32 { .reduce((result, char): number => (result * 256) + char.charCodeAt(0), 0); } + /** + * @description `true` if the engine matches abrs + */ + public get isAbrs (): boolean { + return this.eq(CID_ABRS); + } + /** * @description `true` if the engine matches aura */ @@ -59,19 +66,42 @@ export default class ConsensusEngineId extends U32 { return this.eq(CID_GRPA); } + private getAbrsAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId { + const pre = createType('RawAuraBorosPreDigest', bytes.toU8a(true)); + + return sessionValidators[ + pre.isBabe + ? pre.asBabe.authorityIndex.toNumber() + : pre.asAura.slotNumber.modn(sessionValidators.length) + ]; + } + + private getAuraAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId { + return sessionValidators[ + createType('RawAuraPreDigest', bytes.toU8a(true)) + .slotNumber + .modn(sessionValidators.length) + ]; + } + + private getBabeAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId { + return sessionValidators[ + createType('RawBabePreDigest', bytes.toU8a(true)) + .authorityIndex + .toNumber() + ]; + } + /** * @description From the input bytes, decode into an author */ public extractAuthor (bytes: Bytes, sessionValidators: AccountId[]): AccountId { - if (this.isAura) { - // TODO We really want proper decoding of the digest as below (i.e. via type) - return sessionValidators[ - new U64(bytes.toU8a(true).subarray(0, 8)).modn(sessionValidators.length) - ]; + if (this.isAbrs) { + return this.getAbrsAuthor(bytes, sessionValidators); + } else if (this.isAura) { + return this.getAuraAuthor(bytes, sessionValidators); } else if (this.isBabe) { - return sessionValidators[ - createType('RawBabePreDigest', bytes.toU8a(true)).authorityIndex.toNumber() - ]; + return this.getBabeAuthor(bytes, sessionValidators); } throw new Error('Invalid engine for extractAuthor conversion');