Expand Digest for asAura, use in HeaderExtended (#832)

This commit is contained in:
Jaco Greeff
2019-05-27 15:45:50 +12:00
committed by ian.he
parent e4371684b6
commit 73a184ec7d
3 changed files with 73 additions and 7 deletions
+33
View File
@@ -77,6 +77,39 @@ export class Consensus extends Tuple {
Bytes
}, value);
}
/**
* @description `true` if the engine matches aura
*/
get isAura (): boolean {
return this.engine.eq(0x61727561); // ['a', 'u', 'r', 'a']
}
/**
* @description The wrapped engine [[U32]]
*/
get engine (): U32 {
return this[0] as U32;
}
/**
* @description The wrapped [[Bytes]]
*/
get data (): Bytes {
return this[1] as Bytes;
}
/**
* @description The slot and signature extracted from the raw data (assuming Aura)
*/
get asAura (): [U64, Signature] {
const raw = this.data.toU8a(true);
return [
new U64(raw.subarray(0, 4)),
new Signature(raw.subarray(64))
];
}
}
/**
+27 -4
View File
@@ -8,6 +8,7 @@ import { blake2AsU8a } from '@plugnet/util-crypto';
import Compact from '../codec/Compact';
import Struct from '../codec/Struct';
import U64 from '../primitive/U64';
import AccountId from './AccountId';
import BlockNumber from './BlockNumber';
@@ -101,11 +102,33 @@ export class HeaderExtended extends Header {
constructor (header: Header | null = null, sessionValidators: Array<AccountId> = []) {
super(header);
const digestItem = header && header.digest && header.digest.logs.find(({ type }) => type === 'Seal');
if (!header || !header.digest || !sessionValidators.length) {
return;
}
this._author = digestItem && sessionValidators.length
? sessionValidators[digestItem.asSeal.slot.toNumber() % sessionValidators.length]
: undefined;
let item = header.digest.logs.find((log) => log.isConsensus);
let slot: U64 | undefined;
// extract author from the consensus (substrate 1.0, digest)
if (item) {
const consensus = item.asConsensus;
if (consensus.isAura) {
slot = consensus.asAura[0];
}
} else {
item = header.digest.logs.find((log) => log.isSeal);
// extract author from the seal (pre substrate 1.0, backwards compat)
if (item) {
slot = item.asSeal.slot;
}
}
// found a slot? Great, extract the validator
if (slot) {
this._author = sessionValidators[slot.toNumber() % sessionValidators.length];
}
}
/**