Event definitions (#350)
* Event definitions * Compact things a bit * Cleanup AccountIndex decoding, align Address * Remove stand-alone MethodIndex (declutter) * Inject metadata into Event from api * Update API TODOs to reflect current state * Update & expand comments * EventData only takes Uint8Array inputs (via EventRecord) * Remove duplicated toHex * Add FIXME with link to base issue
This commit is contained in:
committed by
Amaury Martiny
parent
06079c424e
commit
4997d8fb06
@@ -13,6 +13,7 @@ import storageFromMeta from '@polkadot/storage/fromMetadata';
|
||||
import storageStatic from '@polkadot/storage/static';
|
||||
import { Vector } from '@polkadot/types/codec';
|
||||
import { Hash, Method } from '@polkadot/types/index';
|
||||
import Event from '@polkadot/types/Event';
|
||||
import { StorageFunction } from '@polkadot/types/StorageKey';
|
||||
import { assert, isUndefined } from '@polkadot/util';
|
||||
|
||||
@@ -61,6 +62,7 @@ export default class ApiBase {
|
||||
ApiBase.extrinsics = extrinsicsFromMeta(meta);
|
||||
ApiBase.storage = storageFromMeta(meta);
|
||||
|
||||
Event.injectMetadata(meta);
|
||||
Method.injectExtrinsics(ApiBase.extrinsics);
|
||||
|
||||
if (!isReady) {
|
||||
|
||||
@@ -12,6 +12,7 @@ import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata';
|
||||
import { Storage } from '@polkadot/storage/types';
|
||||
import storageFromMeta from '@polkadot/storage/fromMetadata';
|
||||
import { Hash, Method, RuntimeVersion } from '@polkadot/types/index';
|
||||
import Event from '@polkadot/types/Event';
|
||||
import RuntimeMetadata from '@polkadot/types/Metadata';
|
||||
import { assert, isUndefined, logger } from '@polkadot/util';
|
||||
|
||||
@@ -188,8 +189,6 @@ export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S,
|
||||
|
||||
// TODO When re-connected (i.e. disconnected and then connected), we want to do a couple of things
|
||||
// - refresh metadata as needed, decorating again
|
||||
// - re-create storage subscriptions for those we already have
|
||||
// - re-watch extrinsics where we have subscriptions already
|
||||
// - need to refresh genesisHash, extrinsic resub only when it matches
|
||||
if (isReady) {
|
||||
return;
|
||||
@@ -217,6 +216,7 @@ export default abstract class ApiBase<R, S, E> implements ApiBaseInterface<R, S,
|
||||
this._extrinsics = this.decorateExtrinsics(extrinsics);
|
||||
this._query = this.decorateStorage(storage);
|
||||
|
||||
Event.injectMetadata(this.runtimeMetadata);
|
||||
Method.injectExtrinsics(extrinsics);
|
||||
|
||||
return true;
|
||||
|
||||
@@ -29,8 +29,6 @@ export default function createDescriptor (
|
||||
throw new Error(`Extrinsic ${section}.${method} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`);
|
||||
}
|
||||
|
||||
console.error(`${section}.${method}`, args);
|
||||
|
||||
return new Extrinsic({
|
||||
method: new Method({
|
||||
args,
|
||||
|
||||
@@ -8,7 +8,7 @@ import AccountIndex from './AccountIndex';
|
||||
describe('AccountIndex', () => {
|
||||
it('creates a BN representation', () => {
|
||||
expect(
|
||||
new AccountIndex([0xfd, 17, 18, 19, 20]).toBn().toString()
|
||||
new AccountIndex([17, 18, 19, 20]).toBn().toString()
|
||||
).toEqual('336794129');
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { decodeAddress, encodeAddress } from '@polkadot/keyring';
|
||||
import { bnToBn, bnToU8a, hexToU8a, isBn, isNumber, isU8a, isHex, u8aConcat, u8aToBn } from '@polkadot/util';
|
||||
import { bnToBn, bnToU8a, hexToU8a, isBn, isNumber, isU8a, isHex, u8aToBn } from '@polkadot/util';
|
||||
|
||||
import { AnyU8a } from './types';
|
||||
import U8a from './codec/U8a';
|
||||
@@ -23,25 +23,29 @@ const MAX_4BYTE = new BN(1).shln(32);
|
||||
// for an Account. We extends from U8a which is basically
|
||||
// just a Uint8Array wrapper.
|
||||
export default class AccountIndex extends U8a {
|
||||
constructor (value: BN | number | AnyU8a = new Uint8Array()) {
|
||||
// The maxLength refers to the maximum input. Effectively when returned as a storage value
|
||||
// (see EventRecord) the actual encoded length is 4 bytes. Set that as the default,
|
||||
// however pass it through as a computed value from the Address constructors
|
||||
//
|
||||
// FIXME Encoding is not handled 100% correctly with the maxLength. Change the base to be actually
|
||||
// compilant with the Rust implementation, https://github.com/polkadot-js/api/issues/354
|
||||
constructor (value: BN | number | AnyU8a = new Uint8Array(), maxLength: number = 4) {
|
||||
super(
|
||||
AccountIndex.decodeAccountIndex(value)
|
||||
AccountIndex.decodeAccountIndex(value, maxLength)
|
||||
);
|
||||
}
|
||||
|
||||
static decodeAccountIndex (value: BN | number | AnyU8a): Uint8Array {
|
||||
static decodeAccountIndex (value: BN | number | AnyU8a, maxLength: number): Uint8Array {
|
||||
if (value instanceof U8a) {
|
||||
return value.raw;
|
||||
} else if (Array.isArray(value)) {
|
||||
return AccountIndex.decodeAccountIndex(Uint8Array.from(value));
|
||||
return AccountIndex.decodeAccountIndex(Uint8Array.from(value), maxLength);
|
||||
} else if (isU8a(value)) {
|
||||
if (!value.length) {
|
||||
return value;
|
||||
}
|
||||
|
||||
const [offset, length] = AccountIndex.readLength(value);
|
||||
|
||||
return value.subarray(offset, offset + length);
|
||||
return value.subarray(0, Math.min(maxLength, value.length));
|
||||
} else if (isNumber(value) || isBn(value)) {
|
||||
const bitLength = 8 * AccountIndex.calcLength(value);
|
||||
|
||||
@@ -70,9 +74,7 @@ export default class AccountIndex extends U8a {
|
||||
static readLength (input: Uint8Array): [number, number] {
|
||||
const first = input[0];
|
||||
|
||||
if (first <= PREFIX_1BYTE) {
|
||||
return [0, 1];
|
||||
} else if (first === PREFIX_2BYTE) {
|
||||
if (first === PREFIX_2BYTE) {
|
||||
return [1, 2];
|
||||
} else if (first === PREFIX_4BYTE) {
|
||||
return [1, 4];
|
||||
@@ -80,7 +82,7 @@ export default class AccountIndex extends U8a {
|
||||
return [1, 8];
|
||||
}
|
||||
|
||||
throw new Error(`Invalid account index byte, 0x${first.toString(16)}`);
|
||||
return [0, 1];
|
||||
}
|
||||
|
||||
static writeLength (input: Uint8Array): Uint8Array {
|
||||
@@ -111,10 +113,7 @@ export default class AccountIndex extends U8a {
|
||||
// work in the way it was intended
|
||||
return isBare
|
||||
? bnToU8a(this.toBn().div(ENUMSET_SIZE), 32, true)
|
||||
: u8aConcat(
|
||||
AccountIndex.writeLength(this.raw),
|
||||
this.raw
|
||||
);
|
||||
: this.raw;
|
||||
}
|
||||
|
||||
toString (): string {
|
||||
|
||||
@@ -3,13 +3,13 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { decodeAddress } from '@polkadot/keyring';
|
||||
import { isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
import { hexToU8a, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import AccountId from './AccountId';
|
||||
import AccountIndex from './AccountIndex';
|
||||
import Base from './codec/Base';
|
||||
|
||||
const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
|
||||
export const ACCOUNT_ID_PREFIX = new Uint8Array([0xff]);
|
||||
|
||||
// A wrapper around an AccountId and/or AccountIndex that is encoded with a prefix.
|
||||
// Since we are dealing with underlying publicKeys (or shorter encoded addresses),
|
||||
@@ -31,37 +31,36 @@ export default class Address extends Base<AccountId | AccountIndex> {
|
||||
} else if (Array.isArray(value)) {
|
||||
return Address.decodeAddress(u8aToU8a(value));
|
||||
} else if (isU8a(value)) {
|
||||
if (value[0] === 0xff) {
|
||||
// This allows us to instantiate an address with a raw publicKey. Do this first before
|
||||
// we checking the first byte, otherwise we may split an already-existent valid address
|
||||
if (value.length === 32) {
|
||||
return new AccountId(value);
|
||||
} else if (value[0] === 0xff) {
|
||||
return new AccountId(value.subarray(1));
|
||||
}
|
||||
return value.length === 32
|
||||
? new AccountId(value)
|
||||
: new AccountIndex(value);
|
||||
|
||||
const [offset, length] = AccountIndex.readLength(value);
|
||||
|
||||
return new AccountIndex(value.subarray(offset, offset + length), length);
|
||||
} else if (isHex(value)) {
|
||||
return value.length === 66
|
||||
? new AccountId(value)
|
||||
: new AccountIndex(value);
|
||||
return Address.decodeAddress(hexToU8a(value));
|
||||
}
|
||||
|
||||
const decoded = decodeAddress(value);
|
||||
|
||||
// NOTE For AccountIndex this is really not the most efficient, however in the case
|
||||
// of u8a it expects the index to be in frony of the data. So, please it and add it.
|
||||
return decoded.length === 32
|
||||
? new AccountId(decoded)
|
||||
: new AccountIndex(
|
||||
u8aConcat(
|
||||
AccountIndex.writeLength(decoded),
|
||||
decoded
|
||||
)
|
||||
);
|
||||
: new AccountIndex(decoded, decoded.length);
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
return this.raw.encodedLength + (
|
||||
this.raw instanceof AccountIndex
|
||||
? 0
|
||||
: 1
|
||||
const dataLength = this.raw.encodedLength;
|
||||
|
||||
return dataLength + (
|
||||
// for 1 byte AccountIndexes, we are not adding a specific prefix
|
||||
dataLength > 1
|
||||
? 1
|
||||
: 0
|
||||
);
|
||||
}
|
||||
|
||||
@@ -78,11 +77,15 @@ export default class Address extends Base<AccountId | AccountIndex> {
|
||||
}
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
return isBare || this.raw instanceof AccountIndex
|
||||
? this.raw.toU8a(isBare)
|
||||
const encoded = this.raw.toU8a(isBare);
|
||||
|
||||
return isBare
|
||||
? encoded
|
||||
: u8aConcat(
|
||||
ACCOUNT_ID_PREFIX,
|
||||
this.raw.toU8a(isBare)
|
||||
this.raw instanceof AccountIndex
|
||||
? AccountIndex.writeLength(encoded)
|
||||
: ACCOUNT_ID_PREFIX,
|
||||
encoded
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// 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 { Constructor, ConstructorDef } from './types';
|
||||
|
||||
import { isUndefined, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import Struct from './codec/Struct';
|
||||
import Tuple from './codec/Tuple';
|
||||
import U8aFixed from './codec/U8aFixed';
|
||||
import { TypeDef, getTypeClass, getTypeDef } from './codec/createType';
|
||||
import Metadata from './Metadata';
|
||||
|
||||
const EventTypes: { [index: string]: Constructor<EventData> } = {};
|
||||
|
||||
class EventData extends Tuple {
|
||||
private _typeDef: Array<TypeDef>;
|
||||
|
||||
constructor (Types: ConstructorDef, value: Uint8Array, typeDef: Array<TypeDef>) {
|
||||
super(Types, value);
|
||||
|
||||
this._typeDef = typeDef;
|
||||
}
|
||||
|
||||
get typeDef (): Array<TypeDef> {
|
||||
return this._typeDef;
|
||||
}
|
||||
}
|
||||
|
||||
// like methods, we have the [sectionIndex, methodIndex] pairing
|
||||
class EventIndex extends U8aFixed {
|
||||
constructor (value?: any) {
|
||||
super(value, 16);
|
||||
}
|
||||
}
|
||||
|
||||
export default class Event extends Struct {
|
||||
// Currently we _only_ decode from Uint8Array, since we expect it to
|
||||
// be used via EventRecord
|
||||
constructor (_value: Uint8Array) {
|
||||
const { DataType, value } = Event.decodeEvent(_value);
|
||||
|
||||
super({
|
||||
index: EventIndex,
|
||||
data: DataType
|
||||
}, value);
|
||||
}
|
||||
|
||||
static decodeEvent (_value: Uint8Array) {
|
||||
const index = _value.subarray(0, 2);
|
||||
const DataType = EventTypes[index.toString()];
|
||||
|
||||
if (isUndefined(DataType)) {
|
||||
throw new Error(`Unable to decode event for index ${u8aToHex(index)}`);
|
||||
}
|
||||
|
||||
return {
|
||||
DataType,
|
||||
value: {
|
||||
index,
|
||||
data: _value.subarray(2)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// This is called/injected by the API on init, allowing a snapshot of
|
||||
// the available system events to be used in lookups
|
||||
static injectMetadata (metadata: Metadata): void {
|
||||
metadata.events.forEach((section, sectionIndex) => {
|
||||
section.events.forEach((event, methodIndex) => {
|
||||
const eventIndex = new Uint8Array([sectionIndex, methodIndex]);
|
||||
const typeDef = event.arguments.map((arg) => getTypeDef(arg));
|
||||
const Types = typeDef.reduce((result, def, index) => {
|
||||
result[index] = getTypeClass(def);
|
||||
|
||||
return result;
|
||||
}, {} as { [index: string]: Constructor });
|
||||
|
||||
EventTypes[eventIndex.toString()] = class extends EventData {
|
||||
constructor (value: Uint8Array) {
|
||||
super(Types, value, typeDef);
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
get data (): EventData {
|
||||
return this.get('data') as EventData;
|
||||
}
|
||||
|
||||
get index (): EventIndex {
|
||||
return this.get('index') as EventIndex;
|
||||
}
|
||||
}
|
||||
@@ -4,14 +4,44 @@
|
||||
|
||||
import createType from './codec/createType';
|
||||
import Vector from './codec/Vector';
|
||||
import json from './json/EventRecord.001.json';
|
||||
import { EventRecord } from './index';
|
||||
import json1 from './json/EventRecord.001.json';
|
||||
import json2 from './json/EventRecord.002.json';
|
||||
import AccountId from './AccountId';
|
||||
import Event from './Event';
|
||||
import EventRecord from './EventRecord';
|
||||
import Metadata from './Metadata';
|
||||
import metadataRpc from './/Metadata.rpc';
|
||||
|
||||
describe('EventRecord', () => {
|
||||
beforeEach(() => {
|
||||
Event.injectMetadata(
|
||||
new Metadata(metadataRpc)
|
||||
);
|
||||
});
|
||||
|
||||
it('decodes correctly', () => {
|
||||
const records: Vector<EventRecord> = createType('Vec<EventRecord>', json.params.result.changes[0][1]) as any;
|
||||
const records: Vector<EventRecord> = createType('Vec<EventRecord>', json1.params.result.changes[0][1]) as any;
|
||||
const er = records.get(0);
|
||||
|
||||
expect(er.phase.type).toEqual('ApplyExtrinsic');
|
||||
});
|
||||
|
||||
it('decodes more complex events', () => {
|
||||
const records: Vector<EventRecord> = createType('Vec<EventRecord>', json2.params.result.changes[0][1]) as any;
|
||||
|
||||
expect(records).toHaveLength(4);
|
||||
|
||||
const er = records.get(2);
|
||||
|
||||
expect(
|
||||
er.event.data.toArray().map((v) => v.toString())
|
||||
).toEqual(
|
||||
[
|
||||
new AccountId('0xd2de7394ae047a5502ad9adb9cc69ff6fe484033bfce874d775da947487cd832').toString(),
|
||||
new AccountId('0x37e027d776cd005c12bcf6722421374a9037167a0ceaf918f341c4ad68d54e59').toString(),
|
||||
'1000',
|
||||
'0'
|
||||
]
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
import EnumType from './codec/EnumType';
|
||||
import Struct from './codec/Struct';
|
||||
import Event from './Event';
|
||||
import Null from './Null';
|
||||
import U32 from './U32';
|
||||
|
||||
@@ -22,10 +23,6 @@ class Phase extends EnumType<ApplyExtrinsic | Finalization> {
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME Once we have actual data from Events, have a look on how to decode them
|
||||
class Event extends Null {
|
||||
}
|
||||
|
||||
export default class EventRecord extends Struct {
|
||||
constructor (value: any) {
|
||||
super({
|
||||
|
||||
@@ -57,6 +57,7 @@ export default class ExtrinsicSignature extends Struct {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
return value as any;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,11 @@ import { ExtrinsicFunction, Extrinsics } from '@polkadot/extrinsics/types';
|
||||
import { assert, isHex, isObject, isU8a } from '@polkadot/util';
|
||||
|
||||
import Base from './codec/Base';
|
||||
import { Constructor } from './types';
|
||||
import { AnyU8a, Constructor } from './types';
|
||||
import { FunctionMetadata, FunctionArgumentMetadata } from './Metadata';
|
||||
import { getTypeDef, getTypeClass } from './codec/createType';
|
||||
import MethodIndex from './MethodIndex';
|
||||
import Struct from './codec/Struct';
|
||||
import U8aFixed from './codec/U8aFixed';
|
||||
|
||||
const FN_UNKNOWN = {
|
||||
method: 'unknown',
|
||||
@@ -33,6 +33,12 @@ interface DecodedMethod extends DecodeMethodInput {
|
||||
|
||||
const extrinsicFns: { [index: string]: ExtrinsicFunction } = {};
|
||||
|
||||
class MethodIndex extends U8aFixed {
|
||||
constructor (value?: AnyU8a) {
|
||||
super(value, 16);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extrinsic function descriptor, as defined in
|
||||
* {@link https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node}.
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
// 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 MethodIndex from './MethodIndex';
|
||||
|
||||
describe('MethodIndex', () => {
|
||||
it('handles u8a encoding correctly', () => {
|
||||
expect(
|
||||
new MethodIndex([1, 2]).toU8a()
|
||||
).toEqual(new Uint8Array([1, 2]));
|
||||
});
|
||||
|
||||
it('provides access to section/method indexes', () => {
|
||||
const ci = new MethodIndex([1, 2]);
|
||||
|
||||
expect(ci.methodIndex).toEqual(2);
|
||||
expect(ci.sectionIndex).toEqual(1);
|
||||
});
|
||||
});
|
||||
@@ -1,21 +0,0 @@
|
||||
// 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 { AnyU8a } from './types';
|
||||
|
||||
import U8aFixed from './codec/U8aFixed';
|
||||
|
||||
export default class MethodIndex extends U8aFixed {
|
||||
constructor (value?: AnyU8a) {
|
||||
super(value, 16);
|
||||
}
|
||||
|
||||
get methodIndex (): number {
|
||||
return this.raw[1];
|
||||
}
|
||||
|
||||
get sectionIndex (): number {
|
||||
return this.raw[0];
|
||||
}
|
||||
}
|
||||
@@ -125,13 +125,17 @@ export default class Struct<
|
||||
}
|
||||
|
||||
get encodedLength (): number {
|
||||
return [...this.values()].reduce((length, entry) => {
|
||||
return this.toArray().reduce((length, entry) => {
|
||||
return length += entry.encodedLength;
|
||||
}, 0);
|
||||
}
|
||||
|
||||
getAtIndex (index: number): Base {
|
||||
return [...this.values()][index];
|
||||
return this.toArray()[index];
|
||||
}
|
||||
|
||||
toArray (): Array<Base> {
|
||||
return [...this.values()];
|
||||
}
|
||||
|
||||
toHex () {
|
||||
@@ -155,7 +159,7 @@ export default class Struct<
|
||||
|
||||
toU8a (isBare?: boolean): Uint8Array {
|
||||
return u8aConcat(
|
||||
...[...this.values()].map((entry) =>
|
||||
...this.toArray().map((entry) =>
|
||||
entry.toU8a(isBare)
|
||||
)
|
||||
);
|
||||
|
||||
@@ -25,8 +25,10 @@ export { default as Compact64 } from './Compact64';
|
||||
export { default as Compact128 } from './Compact128';
|
||||
export { default as Compact256 } from './Compact256';
|
||||
export { default as Data } from './Data';
|
||||
// NOTE Only used internally,
|
||||
// NOTE Only used internally via Header
|
||||
// export { default as Digest } from './Digest';
|
||||
// NOTE Only used via EventRecord at this point
|
||||
// export { default as Event } from './Event';
|
||||
export { default as EventRecord } from './EventRecord';
|
||||
export { default as Extrinsic } from './Extrinsic';
|
||||
export { default as ExtrinsicEra } from './ExtrinsicEra';
|
||||
@@ -43,7 +45,6 @@ export { default as Header } from './Header';
|
||||
export { default as KeyValue } from './KeyValue';
|
||||
export { default as Metadata } from './Metadata';
|
||||
export { default as Method } from './Method';
|
||||
export { default as MethodIndex } from './MethodIndex';
|
||||
export { default as MisbehaviorReport } from './MisbehaviorReport';
|
||||
export { default as Moment } from './Moment';
|
||||
export { default as NewAccountOutcome } from './NewAccountOutcome';
|
||||
|
||||
Reference in New Issue
Block a user