Move Digest to interfaces (#2164)
* Move Digest to interfaces * Unconstructable typos
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
// Copyright 2017-2020 @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 { TypeRegistry } from '../create';
|
||||
import json3 from '../json/Header.003.json';
|
||||
import block1 from '../json/SignedBlock.003.00.json';
|
||||
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);
|
||||
|
||||
expect(digest.logs.length).toEqual(1);
|
||||
|
||||
const log = digest.logs[0];
|
||||
|
||||
expect(log.type).toBe('Consensus');
|
||||
expect(log.value.toJSON()).toEqual([
|
||||
1634891105,
|
||||
'0x69c97e0f00000000479c09f7ace12ed3e1af2dba6cca6831e257e80feef56c7474d6c69bae6f4e8a6e0045a87afe14296992c38b7d55abcbe617c441cd35e8667abbcb1678f31802'
|
||||
]);
|
||||
});
|
||||
|
||||
it('filters logs, excluding items', (): void => {
|
||||
const logs = new Digest(registry, block1.result.block.header.digest).logsWith('PreRuntime');
|
||||
|
||||
expect(logs.length).toEqual(1);
|
||||
expect(logs[0].type).toEqual('PreRuntime');
|
||||
});
|
||||
|
||||
it('filters logs, including items', (): void => {
|
||||
const logs = new Digest(registry, block1.result.block.header.digest).logsWithout('PreRuntime');
|
||||
|
||||
expect(logs.length).toEqual(1);
|
||||
expect(logs[0].type).toEqual('Seal');
|
||||
});
|
||||
});
|
||||
@@ -1,52 +0,0 @@
|
||||
// Copyright 2017-2020 @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 { DigestItem } from '../interfaces/runtime';
|
||||
import { AnyJson, Registry } from '../types';
|
||||
|
||||
import Struct from '../codec/Struct';
|
||||
import Vec from '../codec/Vec';
|
||||
|
||||
/**
|
||||
* @name Digest
|
||||
* @description
|
||||
* A [[Header]] Digest
|
||||
*/
|
||||
export default class Digest extends Struct {
|
||||
constructor (registry: Registry, value: any) {
|
||||
super(registry, {
|
||||
logs: 'Vec<DigestItem>'
|
||||
}, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[DigestItem]] logs
|
||||
*/
|
||||
public get logs (): Vec<DigestItem> {
|
||||
return this.get('logs') as Vec<DigestItem>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[DigestItem]] logs, filtered, filter items included. This is useful for derive functionality where only a certain type of log is to be returned.
|
||||
*/
|
||||
public logsWith (...include: string[]): Vec<DigestItem> {
|
||||
return this.logs.filter(({ type }): boolean => include.includes(type)) as Vec<DigestItem>;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The [[DigestItem]] logs, filtered, filter items exluded. This is useful for stripping headers for eg. WASM runtime execution.
|
||||
*/
|
||||
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 (): AnyJson {
|
||||
return {
|
||||
logs: this.logs.map((log): string => log.toHex())
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ 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 } from './Digest';
|
||||
export { default as GenericEvent } from './Event';
|
||||
export { default as GenericOrigin } from './Origin';
|
||||
export { default as GenericVote } from './Vote';
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
// Copyright 2017-2020 @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 { TypeRegistry } from '../../create';
|
||||
import json3 from '../../json/Header.003.json';
|
||||
|
||||
describe('Digest', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
it('has the correct JSON representation', (): void => {
|
||||
const digest = registry.createType('Digest', json3.result.digest);
|
||||
|
||||
expect(digest.logs.map((l) => l.toHex())).toEqual(json3.result.digest.logs);
|
||||
});
|
||||
|
||||
it('decodes logs with consensus', (): void => {
|
||||
const digest = registry.createType('Digest', json3.result.digest);
|
||||
|
||||
expect(digest.logs.length).toEqual(1);
|
||||
|
||||
const log = digest.logs[0];
|
||||
|
||||
expect(log.type).toBe('Consensus');
|
||||
expect(log.value.toJSON()).toEqual([
|
||||
1634891105,
|
||||
'0x69c97e0f00000000479c09f7ace12ed3e1af2dba6cca6831e257e80feef56c7474d6c69bae6f4e8a6e0045a87afe14296992c38b7d55abcbe617c441cd35e8667abbcb1678f31802'
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,9 @@ export default {
|
||||
digestLevels: 'u32'
|
||||
},
|
||||
ConsensusEngineId: 'GenericConsensusEngineId',
|
||||
Digest: 'GenericDigest',
|
||||
Digest: {
|
||||
logs: 'Vec<DigestItem>'
|
||||
},
|
||||
DigestItem: {
|
||||
_enum: {
|
||||
Other: 'Bytes', // 0
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import { ITuple } from '@polkadot/types/types';
|
||||
import { Compact, Enum, Int, Struct, U8aFixed, Vec } from '@polkadot/types/codec';
|
||||
import { GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericDigest, GenericOrigin } from '@polkadot/types/generic';
|
||||
import { GenericAccountId, GenericAccountIndex, GenericAddress, GenericBlock, GenericCall, GenericConsensusEngineId, GenericOrigin } from '@polkadot/types/generic';
|
||||
import { Bytes, Null, StorageKey, bool, u128, u32, u64, u8 } from '@polkadot/types/primitive';
|
||||
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
|
||||
import { Signature } from '@polkadot/types/interfaces/extrinsics';
|
||||
@@ -51,7 +51,9 @@ export interface Consensus extends ITuple<[ConsensusEngineId, Bytes]> {}
|
||||
export interface ConsensusEngineId extends GenericConsensusEngineId {}
|
||||
|
||||
/** @name Digest */
|
||||
export interface Digest extends GenericDigest {}
|
||||
export interface Digest extends Struct {
|
||||
readonly logs: Vec<DigestItem>;
|
||||
}
|
||||
|
||||
/** @name DigestItem */
|
||||
export interface DigestItem extends Enum {
|
||||
|
||||
@@ -9,7 +9,7 @@ import Null from './Null';
|
||||
/**
|
||||
* @name Unconstructable
|
||||
* @description
|
||||
* An unknown type theat fails on constrction with the type info
|
||||
* An unknown type that fails on construction with the type info
|
||||
*/
|
||||
export default class Unconstructable extends Null {
|
||||
constructor (registry: Registry, typeName: string) {
|
||||
|
||||
Reference in New Issue
Block a user