Convert DigestItem to definition (#1886)

* Convert DigestItem to definition

* Remove GenericDigestItem export

* Update CHANGELOG.md
This commit is contained in:
Jaco Greeff
2020-02-18 13:20:29 +01:00
committed by GitHub
parent 1e5191d695
commit a5c8e3c4e1
6 changed files with 50 additions and 158 deletions
@@ -42,7 +42,17 @@ export default {
},
ConsensusEngineId: 'GenericConsensusEngineId',
Digest: 'GenericDigest',
DigestItem: 'GenericDigestItem',
DigestItem: {
_enum: {
Other: 'Bytes', // 0
AuthoritiesChange: 'Vec<AuthorityId>', // 1
ChangesTrieRoot: 'Hash', // 2
SealV0: 'SealV0', // 3
Consensus: 'Consensus', // 4
Seal: 'Seal', // 5
PreRuntime: 'PreRuntime' // 6
}
},
DispatchClass: {
_enum: ['Normal', 'Operational']
},
+19 -3
View File
@@ -2,8 +2,9 @@
/* eslint-disable @typescript-eslint/no-empty-interface */
import { Codec, ITuple } from '@polkadot/types/types';
import { Compact, Enum, Struct } from '@polkadot/types/codec';
import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericDigestItem, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericExtrinsicPayloadUnknown, GenericExtrinsicPayloadV1, GenericExtrinsicPayloadV2, GenericExtrinsicPayloadV3, GenericExtrinsicPayloadV4, GenericExtrinsicSignatureV1, GenericExtrinsicSignatureV2, GenericExtrinsicSignatureV3, GenericExtrinsicSignatureV4, GenericExtrinsicUnknown, GenericExtrinsicV1, GenericExtrinsicV2, GenericExtrinsicV3, GenericExtrinsicV4, GenericImmortalEra, GenericMortalEra, GenericOrigin, GenericSignerPayload, H256, H512, Null, StorageData, StorageKey, bool, u128, u32, u64, u8 } from '@polkadot/types/primitive';
import { Compact, Enum, Struct, Vec } from '@polkadot/types/codec';
import { Bytes, Fixed64, GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericExtrinsic, GenericExtrinsicEra, GenericExtrinsicPayload, GenericExtrinsicPayloadUnknown, GenericExtrinsicPayloadV1, GenericExtrinsicPayloadV2, GenericExtrinsicPayloadV3, GenericExtrinsicPayloadV4, GenericExtrinsicSignatureV1, GenericExtrinsicSignatureV2, GenericExtrinsicSignatureV3, GenericExtrinsicSignatureV4, GenericExtrinsicUnknown, GenericExtrinsicV1, GenericExtrinsicV2, GenericExtrinsicV3, GenericExtrinsicV4, GenericImmortalEra, GenericMortalEra, GenericOrigin, GenericSignerPayload, H256, H512, Null, StorageData, StorageKey, bool, u128, u32, u64, u8 } from '@polkadot/types/primitive';
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
/** @name AccountId */
export interface AccountId extends GenericAccountId {}
@@ -51,7 +52,22 @@ export interface ConsensusEngineId extends GenericConsensusEngineId {}
export interface Digest extends GenericDigest {}
/** @name DigestItem */
export interface DigestItem extends GenericDigestItem {}
export interface DigestItem extends Enum {
readonly isOther: boolean;
readonly asOther: Bytes;
readonly isAuthoritiesChange: boolean;
readonly asAuthoritiesChange: Vec<AuthorityId>;
readonly isChangesTrieRoot: boolean;
readonly asChangesTrieRoot: Hash;
readonly isSealV0: boolean;
readonly asSealV0: SealV0;
readonly isConsensus: boolean;
readonly asConsensus: Consensus;
readonly isSeal: boolean;
readonly asSeal: Seal;
readonly isPreRuntime: boolean;
readonly asPreRuntime: PreRuntime;
}
/** @name DispatchClass */
export interface DispatchClass extends Enum {
@@ -10,6 +10,12 @@ import Digest from './Digest';
describe('Digest', (): void => {
const registry = new TypeRegistry();
it('has the correct JSON representation', (): void => {
const digest = new Digest(registry, json3.result.digest);
expect(digest.toJSON()).toEqual(json3.result.digest);
});
it('decodes logs with consensus', (): void => {
const digest = new Digest(registry, json3.result.digest);
+12 -153
View File
@@ -2,161 +2,11 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AuthorityId } from '../../interfaces/consensus';
import { Consensus, Hash, PreRuntime, Seal, SealV0 } from '../../interfaces/runtime';
import { Registry } from '../../types';
import { DigestItem } from '../../interfaces/runtime';
import { Registry, AnyJsonObject } from '../../types';
import { assert } from '@polkadot/util';
import Enum from '../../codec/Enum';
import Struct from '../../codec/Struct';
import Vec from '../../codec/Vec';
import Bytes from '../Bytes';
/**
* @name DigestItem
* @description
* A [[Enum]] the specifies the specific item in the logs of a [[Digest]]
*/
export class DigestItem extends Enum {
constructor (registry: Registry, value: any) {
super(registry, {
Other: 'Bytes', // 0
AuthoritiesChange: 'Vec<AuthorityId>', // 1
ChangesTrieRoot: 'Hash', // 2
SealV0: 'SealV0', // 3
Consensus: 'Consensus', // 4
Seal: 'Seal', // 5
PreRuntime: 'PreRuntime' // 6
}, value);
}
/**
* @description Returns the item as a [[Vec<AuthorityId>]]
*/
public get asAuthoritiesChange (): Vec<AuthorityId> {
assert(this.isAuthoritiesChange, `Cannot convert '${this.type}' via asAuthoritiesChange`);
return this.value as Vec<AuthorityId>;
}
/**
* @description Returns the item as a [[ChangesTrieRoot]]
*/
public get asChangesTrieRoot (): Hash {
assert(this.isChangesTrieRoot, `Cannot convert '${this.type}' via asChangesTrieRoot`);
return this.value as Hash;
}
/**
* @desciption Retuns the item as a [[Consensus]]
*/
public get asConsensus (): Consensus {
assert(this.isConsensus, `Cannot convert '${this.type}' via asConsensus`);
return this.value as Consensus;
}
/**
* @description Returns the item as a [[Bytes]]
*/
public get asOther (): Bytes {
assert(this.isOther, `Cannot convert '${this.type}' via asOther`);
return this.value as Bytes;
}
/**
* @description Returns the item as a [[PreRuntime]]
*/
public get asPreRuntime (): PreRuntime {
assert(this.isPreRuntime, `Cannot convert '${this.type}' via asPreRuntime`);
return this.value as PreRuntime;
}
/**
* @description Returns the item as a [[Seal]]
*/
public get asSeal (): Seal {
assert(this.isSeal, `Cannot convert '${this.type}' via asSeal`);
return this.value as Seal;
}
/**
* @description Returns the item as a [[SealV0]]
*/
public get asSealV0 (): SealV0 {
assert(this.isSealV0, `Cannot convert '${this.type}' via asSealV0`);
return this.value as SealV0;
}
/**
* @description Returns true on [[AuthoritiesChange]]
*/
public get isAuthoritiesChange (): boolean {
return this.type === 'AuthoritiesChange';
}
/**
* @description Returns true on [[ChangesTrieRoot]]
*/
public get isChangesTrieRoot (): boolean {
return this.type === 'ChangesTrieRoot';
}
/**
* @description Returns true on [[Consensus]]
*/
public get isConsensus (): boolean {
return this.type === 'Consensus';
}
/**
* @description Returns true on [[Other]]
*/
public get isOther (): boolean {
return this.type === 'Other';
}
/**
* @description Returns true on [[PreRuntime]]
*/
public get isPreRuntime (): boolean {
return this.type === 'PreRuntime';
}
/**
* @description Returns true on [[Seal]]
*/
public get isSeal (): boolean {
return this.type === 'Seal';
}
/**
* @description Returns true on [[SealV0]]
*/
public get isSealV0 (): boolean {
return this.type === 'SealV0';
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers. For logs, we overrides to produce the hex version (sligning with substrate gives in actual JSON responses)
*/
public toJSON (): string {
return this.toHex();
}
/**
* @description Returns the type of engine
*/
public get type (): string {
return super.type;
}
}
/**
* @name Digest
@@ -166,7 +16,7 @@ export class DigestItem extends Enum {
export default class Digest extends Struct {
constructor (registry: Registry, value: any) {
super(registry, {
logs: Vec.with(DigestItem)
logs: 'Vec<DigestItem>'
}, value);
}
@@ -190,4 +40,13 @@ export default class Digest extends Struct {
public logsWithout (...exclude: string[]): Vec<DigestItem> {
return this.logs.filter(({ type }): boolean => !exclude.includes(type)) as Vec<DigestItem>;
}
/**
* @desrcript The JSON representation as it goes over RPC
*/
public toJSON (): AnyJsonObject {
return {
logs: this.logs.map((log): string => log.toHex())
};
}
}
@@ -8,7 +8,7 @@ export { default as GenericAddress } from './Address';
export { default as GenericBlock } from './Block';
export { default as GenericCall } from './Call';
export { default as GenericConsensusEngineId } from './ConsensusEngineId';
export { default as GenericDigest, DigestItem as GenericDigestItem } from './Digest';
export { default as GenericDigest } from './Digest';
export { default as GenericEvent } from './Event';
export { default as GenericOrigin } from './Origin';
export { default as GenericVote } from './Vote';