Add derive.chain.{getHeader,subscribeNewHead} with author (#602)

* Add derive.chain.{getHeader,subscribeNewHead} with author

* tslint cast, ignore
This commit is contained in:
Jaco Greeff
2019-01-18 14:44:55 +01:00
committed by GitHub
parent 96e4f9b7b5
commit db832f0207
7 changed files with 110 additions and 6 deletions
+28
View File
@@ -9,6 +9,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto';
import Compact from './codec/Compact';
import Struct from './codec/Struct';
import AccountId from './AccountId';
import BlockNumber from './BlockNumber';
import Digest, { DigestItem } from './Digest';
import Hash from './Hash';
@@ -81,3 +82,30 @@ export default class Header extends Struct {
return this.get('stateRoot') as Hash;
}
}
/**
* @name HeaderExtended
* @description
* A [[Block]] header with an additional `author` field that indicates the block author
*/
export class HeaderExtended extends Header {
private _author?: AccountId;
constructor (header: Header, sessionValidators: Array<AccountId>) {
super(header);
const { digest: { logs } } = header;
const digestItem = logs.find(({ type }) => type === 'Seal');
this._author = digestItem && sessionValidators.length
? sessionValidators[digestItem.asSeal.slot.toNumber() % sessionValidators.length]
: undefined;
}
/**
* @description Convenience method, returns the author for the block
*/
get author (): AccountId | undefined {
return this._author;
}
}