Extra block author for babe (#1227)

* Update examples for 0.90

* Fix number typo

* Adapth to use timestamp.now (blockPeriod now in all versions)

* Babe extract block author

* TODO for cleaner aura extraction

* Update Babe id

* Update & split tests
This commit is contained in:
Jaco Greeff
2019-07-29 21:23:34 +02:00
committed by GitHub
parent 29bda0b311
commit 0d68f98c75
7 changed files with 77 additions and 36 deletions
+8 -1
View File
@@ -8,7 +8,7 @@ import { Keys, SessionIndex } from './interfaces/session';
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 { BabeWeight } from './interfaces/babe';
import { BabeWeight, RawBabePreDigest, SlotNumber } from './interfaces/babe';
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';
@@ -300,6 +300,13 @@ export interface InterfaceRegistry {
'Compact<BabeWeight>': Compact<BabeWeight>;
'Option<BabeWeight>': Option<BabeWeight>;
'Vec<BabeWeight>': Vec<BabeWeight>;
SlotNumber: SlotNumber;
'Compact<SlotNumber>': Compact<SlotNumber>;
'Option<SlotNumber>': Option<SlotNumber>;
'Vec<SlotNumber>': Vec<SlotNumber>;
RawBabePreDigest: RawBabePreDigest;
'Option<RawBabePreDigest>': Option<RawBabePreDigest>;
'Vec<RawBabePreDigest>': Vec<RawBabePreDigest>;
VestingSchedule: VestingSchedule;
'Option<VestingSchedule>': Option<VestingSchedule>;
'Vec<VestingSchedule>': Vec<VestingSchedule>;
@@ -4,6 +4,13 @@
export default {
types: {
BabeWeight: 'u64'
BabeWeight: 'u64',
SlotNumber: 'u64',
RawBabePreDigest: {
slotNumber: 'SlotNumber',
authorityIndex: 'u64', // AuthorityIndex (also in aura, same size)
vrfOutput: 'H256', // should be '[u8; 32]' (generator support lacking here)
vrfProof: 'H256' // should be '[u8; 32]'
}
}
};
+17 -1
View File
@@ -1,6 +1,22 @@
// Auto-generated via `yarn build:interfaces`, do not edit
import { u64 } from '../../primitive';
import { Struct } from '../../codec';
import { H256, u64 } from '../../primitive';
/** u64 */
export type BabeWeight = u64;
/** Struct */
export interface RawBabePreDigest extends Struct {
/** SlotNumber */
readonly slotNumber: SlotNumber;
/** u64 */
readonly authorityIndex: u64;
/** H256 */
readonly vrfOutput: H256;
/** H256 */
readonly vrfProof: H256;
}
/** u64 */
export type SlotNumber = u64;
@@ -0,0 +1,18 @@
// 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 '../../injector';
import ConsensusEngineId, { CID_AURA, CID_BABE, CID_GRPA } from './ConsensusEngineId';
describe('ConsensusEngineId', (): void => {
it('creates a valid id for aura', (): void => {
expect(ConsensusEngineId.stringToId('FRNK')).toEqual(CID_GRPA);
expect(ConsensusEngineId.stringToId('aura')).toEqual(CID_AURA);
});
it('reverses an id to string for babe', (): void => {
expect(ConsensusEngineId.idToString(CID_BABE)).toEqual('BABE');
});
});
@@ -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 BN from 'bn.js';
import { assert, bnToBn } from '@polkadot/util';
import { AccountId } from '../../interfaces/runtime';
import BN from 'bn.js';
import { bnToBn } from '@polkadot/util';
import createType from '../../codec/createType';
import Bytes from '../Bytes';
import U32 from '../U32';
import U64 from '../U64';
const CID_AURA = 0x61727561; // 'aura'
const CID_BABE = 0x65626162; // 'babe'
const CID_BABE = 0x45424142; // 'BABE'
const CID_GRPA = 0x4b4e5246; // 'FRNK' (don't ask, used to be afg1)
export { CID_AURA, CID_BABE, CID_GRPA };
@@ -57,15 +60,21 @@ export default class ConsensusEngineId extends U32 {
}
/**
* @description From the input bytes, decode into an aura-tuple
* @description From the input bytes, decode into an author
*/
public extractSlot (bytes: Bytes): U64 {
assert(this.isAura, 'Invalid engine for asAura conversion');
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)
];
} else if (this.isBabe) {
return sessionValidators[
createType('RawBabePreDigest', bytes.toU8a(true)).authorityIndex.toNumber()
];
}
return new U64(
// no compact prefix, only use the correct number of supplied bytes
bytes.toU8a(true).subarray(0, 8)
);
throw new Error('Invalid engine for extractAuthor conversion');
}
/**
@@ -6,7 +6,6 @@ import '../../injector';
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';
describe('Digest', (): void => {
@@ -37,15 +36,4 @@ describe('Digest', (): void => {
expect(logs.length).toEqual(1);
expect(logs[0].type).toEqual('Seal');
});
describe('ConsensusId', (): void => {
it('creates a valid id for aura', (): void => {
expect(ConsensusEngineId.stringToId('FRNK')).toEqual(CID_GRPA);
expect(ConsensusEngineId.stringToId('aura')).toEqual(CID_AURA);
});
it('reverses an id to string for babe', (): void => {
expect(ConsensusEngineId.idToString(CID_BABE)).toEqual('babe');
});
});
});