Decode logs, without events (#328)
* Decode logs (without events) * Put digestItem in digest * Use spec.ts for signedBlock * Fix bug type * Add header * Remove comment * Add comment * Fix bug order
This commit is contained in:
committed by
Jaco Greeff
parent
e09dd254e5
commit
1635638f50
@@ -0,0 +1,42 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import AuthorityId from './AuthorityId';
|
||||
import EnumType from './codec/EnumType';
|
||||
import Hash from './Hash';
|
||||
import Null from './Null';
|
||||
import Struct from './codec/Struct';
|
||||
import Vector from './codec/Vector';
|
||||
|
||||
export class AuthoritiesChange extends Vector.with(AuthorityId) {
|
||||
}
|
||||
|
||||
export class ChangesTrieRoot extends Hash {
|
||||
}
|
||||
|
||||
// FIXME Once we have Events, have a look on how to decode them
|
||||
export class Other extends Null {
|
||||
}
|
||||
|
||||
export class DigestItem extends EnumType<AuthoritiesChange | ChangesTrieRoot | Other> {
|
||||
constructor (value: any) {
|
||||
super({
|
||||
0: Other,
|
||||
1: AuthoritiesChange,
|
||||
2: ChangesTrieRoot
|
||||
}, value);
|
||||
}
|
||||
}
|
||||
|
||||
export default class Digest extends Struct {
|
||||
constructor (value: any) {
|
||||
super({
|
||||
logs: Vector.with(DigestItem)
|
||||
}, value);
|
||||
}
|
||||
|
||||
get logs (): Vector<DigestItem> {
|
||||
return this.get('logs') as Vector<DigestItem>;
|
||||
}
|
||||
}
|
||||
@@ -7,39 +7,19 @@ import { AnyNumber, AnyU8a } from './types';
|
||||
import { blake2AsU8a } from '@polkadot/util-crypto';
|
||||
|
||||
import Struct from './codec/Struct';
|
||||
import Vector from './codec/Vector';
|
||||
|
||||
import BlockNumber from './BlockNumber';
|
||||
import Bytes from './Bytes';
|
||||
import Digest, { DigestItem } from './Digest';
|
||||
import Hash from './Hash';
|
||||
import Null from './Null';
|
||||
|
||||
type DigestValue = {
|
||||
logs?: Array<AnyU8a>
|
||||
};
|
||||
|
||||
export type HeaderValue = {
|
||||
digest?: DigestValue,
|
||||
digest?: Digest | { logs: DigestItem[] },
|
||||
extrinsicsRoot?: AnyU8a,
|
||||
number?: AnyNumber,
|
||||
parentHash?: AnyU8a,
|
||||
stateRoot?: AnyU8a
|
||||
};
|
||||
|
||||
// A block header digest.
|
||||
export class Digest extends Struct {
|
||||
constructor (value?: DigestValue) {
|
||||
super({
|
||||
// FIXME decode properly
|
||||
logs: Vector.with(Null)
|
||||
}, value);
|
||||
}
|
||||
|
||||
get logs (): Vector<Bytes> {
|
||||
return this.get('logs') as Vector<Bytes>;
|
||||
}
|
||||
}
|
||||
|
||||
// A block header.
|
||||
export default class Header extends Struct {
|
||||
constructor (value?: HeaderValue) {
|
||||
|
||||
@@ -4,13 +4,14 @@
|
||||
|
||||
import extrinsics from '@polkadot/extrinsics/static';
|
||||
|
||||
import Extrinsics from './Extrinsics';
|
||||
import Method from './Method';
|
||||
import rpc from './json/SignedBlock.001.json';
|
||||
import events from './json/SignedBlock.002.json';
|
||||
import SignedBlock from './SignedBlock';
|
||||
|
||||
describe('SignedBlock', () => {
|
||||
let block;
|
||||
let block: SignedBlock;
|
||||
|
||||
beforeEach(() => {
|
||||
Method.injectExtrinsics(extrinsics);
|
||||
@@ -30,14 +31,24 @@ describe('SignedBlock', () => {
|
||||
).toEqual('0xfab4bb3a8b0a072d3d09858dc865ad2750b2e708536b6dacc89fad369eba781a');
|
||||
});
|
||||
|
||||
it('decodes a block with events', () => {
|
||||
expect(
|
||||
new SignedBlock(events.result)
|
||||
).toBeDefined();
|
||||
it('decodes a block with logs', () => {
|
||||
const s = new SignedBlock(events.result);
|
||||
|
||||
expect(s.block.header.digest.logs.length).toBe(1);
|
||||
expect(s.block.header.digest.logs.get(0).type).toBe('AuthoritiesChange');
|
||||
expect(s.block.header.digest.logs.get(0).value.toJSON()).toEqual([
|
||||
'5EyrU8k6worwXWfW2LajRvS7XF6eta286tMpHeHUwMPpzQ9F',
|
||||
'5Dpq7rU6KAZVdRtcegvpCoeJtZzFPzieRkRz4xajRAiMRkCf',
|
||||
'5CCtQPAYtuDXt6H4WMrn5nFNk4gemkVRD3k1r1mA7YiZLPBq',
|
||||
'5F2ABwFm3fUhWMZT7Zeu7UjCr1dndjJD7qBeHChJhiJC949V',
|
||||
'5CA1n5DSpE46Q9Y7NaqD39T9X7S2dRtscsPvrv2dWuYhFtWr',
|
||||
'5CrxmsTCLbPRzHnFPs8Q6cx8jzHweegMSQYEJBGXLjDZW4NR',
|
||||
'5EQ4ZxYew2CXPkjGCBuPdTVg7Hp3bmBXiXBwn1iDmvj6CgaF'
|
||||
]);
|
||||
});
|
||||
|
||||
describe('extrinsics', () => {
|
||||
let extrinsics;
|
||||
let extrinsics: Extrinsics;
|
||||
|
||||
beforeEach(() => {
|
||||
extrinsics = block.block.extrinsics;
|
||||
@@ -24,6 +24,8 @@ export { default as Compact32 } from './Compact32';
|
||||
export { default as Compact64 } from './Compact64';
|
||||
export { default as Compact128 } from './Compact128';
|
||||
export { default as Compact256 } from './Compact256';
|
||||
// NOTE Only used internally,
|
||||
// export { default as Digest } from './Digest';
|
||||
export { default as Extrinsic } from './Extrinsic';
|
||||
export { default as ExtrinsicEra } from './ExtrinsicEra';
|
||||
export { default as ExtrinsicSignature } from './ExtrinsicSignature';
|
||||
|
||||
Vendored
+10
@@ -0,0 +1,10 @@
|
||||
// Copyright 2017-2018 @polkadot/types authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
// Be able to import json in TS
|
||||
// https://stackoverflow.com/questions/49996456/importing-json-file-in-typescript
|
||||
declare module '*.json' {
|
||||
const value: any;
|
||||
export default value;
|
||||
}
|
||||
Reference in New Issue
Block a user