Add storage subscription API endpoints (#126)

* Add storage subscriptions

* StorageResult formatting

* Decoding WIP

* Storage subsciptions in place

* Allow null decodes, ref https://github.com/polkadot-js/apps/issues/215

* e2e tests for subscribeStorage

* Cache subscription results, return with changeset
This commit is contained in:
Jaco Greeff
2018-08-11 20:50:57 +02:00
committed by GitHub
parent 4d815ef726
commit 187c66f600
50 changed files with 463 additions and 278 deletions
@@ -71,9 +71,9 @@ describe('decode', () => {
).toEqual('{"length":15,"value":["69",[true,false,true]]}');
});
it('decodes KeyValueStorage arrays', () => {
it('decodes StorageKeyValue arrays', () => {
expect(
decode(['KeyValueStorage'], new Uint8Array([
decode(['StorageKeyValue'], new Uint8Array([
2, 0, 0, 0,
4, 0, 0, 0,
0x11, 0x22, 0x33, 0x44,
@@ -96,6 +96,6 @@ describe('decode', () => {
value: toU8a('0x9988')
}
]
})
});
});
});
+11 -8
View File
@@ -5,10 +5,11 @@
import { EncodingVersions, Param$Decoded, Param$Type$Array, Param$Types, Param$Value, Param$Value$Array } from '../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
import toU8a from '@polkadot/util/u8a/toU8a';
import decodeValue from './value';
function decodeTuple (type: Param$Type$Array, input: Uint8Array | null, version: EncodingVersions): Param$Decoded {
function decodeTuple (type: Param$Type$Array, input: Uint8Array | null, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (!input) {
return {
length: 0,
@@ -20,7 +21,7 @@ function decodeTuple (type: Param$Type$Array, input: Uint8Array | null, version:
let length = 0;
type.forEach((_type: Param$Types) => {
const decoded = decode(_type, input.subarray(length), version);
const decoded = decode(_type, input.subarray(length), version, isStorage);
// NOTE small TS hack, since we don't have recursive arrays, assume primitives to push
value.push(decoded.value as Param$Value);
@@ -33,7 +34,7 @@ function decodeTuple (type: Param$Type$Array, input: Uint8Array | null, version:
};
}
function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null, version: EncodingVersions): Param$Decoded {
function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (!input) {
return {
length: 0,
@@ -46,7 +47,7 @@ function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null, vers
let length = 4;
for (let index = 0; index < arrayLength; index++) {
const decoded = decode(type, input.subarray(length), version);
const decoded = decode(type, input.subarray(length), version, isStorage);
// NOTE small TS hack, since we don't have recursive arrays, assume primitives to push
value.push(decoded.value as Param$Value);
@@ -59,15 +60,17 @@ function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null, vers
};
}
export default function decode (type: Param$Types, input: Uint8Array, version: EncodingVersions): Param$Decoded {
export default function decode (type: Param$Types, _input: Uint8Array | string | null, version: EncodingVersions, isStorage: boolean = false): Param$Decoded {
const input = toU8a(_input);
if (Array.isArray(type)) {
// Arrays have single entries, Tuples will have multiple types
if (type.length === 1) {
return decodeArray(type, input, version);
return decodeArray(type, input, version, isStorage);
} else {
return decodeTuple(type, input, version);
return decodeTuple(type, input, version, isStorage);
}
}
return decodeValue(decode, type, input, version);
return decodeValue(decode, type, input, version, isStorage);
}
+1 -1
View File
@@ -4,4 +4,4 @@
import { EncodingVersions, Param$Types, Param$Decoded } from '../types';
export type Decoder = (type: Param$Types, input: Uint8Array, version: EncodingVersions) => Param$Decoded;
export type Decoder = (type: Param$Types, input: Uint8Array, version: EncodingVersions, isStorage: boolean) => Param$Decoded;
+8 -3
View File
@@ -8,12 +8,17 @@ import BN from 'bn.js';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function bn (input: Uint8Array | null, bitLength: 32 | 64 | 128): Param$Decoded {
if (input === null) {
return {
length: 0,
value: new BN(0)
};
}
const length = bitLength / 8;
return {
length,
value: input
? u8aToBn(input.subarray(0, length), true)
: new BN(0)
value: u8aToBn(input.subarray(0, length), true)
};
}
@@ -5,8 +5,15 @@
import { Param$Decoded } from '../../types';
export default function bool (input: Uint8Array | null): Param$Decoded {
if (input === null) {
return {
length: 0,
value: false
};
}
return {
length: 1,
value: !!(input && input[0] !== 0)
value: input[0] !== 0
};
}
@@ -5,10 +5,15 @@
import { Param$Decoded } from '../../types';
export default function byte (input: Uint8Array | null): Param$Decoded {
if (input === null) {
return {
length: 0,
value: 0
};
}
return {
length: 1,
value: input
? input[0]
: 0
value: input[0]
};
}
@@ -7,14 +7,17 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function bytes (input: Uint8Array | null): Param$Decoded {
const length = input
? u8aToBn(input.subarray(0, 4), true).toNumber()
: 0;
if (input === null) {
return {
length: 0,
value: new Uint8Array()
};
}
const length = u8aToBn(input.subarray(0, 4), true).toNumber();
return {
length: length + 4,
value: input
? input.subarray(4, length + 4)
: new Uint8Array()
value: input.subarray(4, length + 4)
};
}
@@ -7,12 +7,15 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function code (input: Uint8Array | null): Param$Decoded {
const length = input
? u8aToBn(input.subarray(0, 4), true).toNumber() + 4
: 0;
const value = input
? input.subarray(4, length)
: new Uint8Array();
if (input === null) {
return {
length: 0,
value: new Uint8Array()
};
}
const length = u8aToBn(input.subarray(0, 4), true).toNumber() + 4;
const value = input.subarray(4, length);
return {
length,
@@ -10,6 +10,12 @@ import u8aToBn from '@polkadot/util/u8a/toBn';
import bytes from './bytes';
export default function digest (input: Uint8Array | null): Param$Decoded {
if (input === null) {
return {
length: 0
} as Param$Decoded;
}
const logLength = input
? u8aToBn(input.subarray(0, 4), true).toNumber()
: 0;
@@ -20,12 +26,7 @@ export default function digest (input: Uint8Array | null): Param$Decoded {
let length = 4;
for (let index = 0; index < logLength; index++) {
const decoded = input
? bytes(input.subarray(length))
: {
length: 0,
value: new Uint8Array()
};
const decoded = bytes(input.subarray(length));
length += decoded.length;
value.logs.push((decoded.value as Uint8Array));
@@ -7,10 +7,14 @@ import { Param$Decoded } from '../../types';
import decodeHeader from '@polkadot/primitives/codec/header/decode';
export default function header (input: Uint8Array | null): Param$Decoded {
if (input === null) {
return {
length: 0
} as Param$Decoded;
}
// FIXME We don't have a way to determine the length atm
const length = input
? input.length
: 0;
const length = input.length;
const value = decodeHeader(input);
return {
+10 -4
View File
@@ -6,6 +6,7 @@ import { EncodingVersions, Param$Decoded, Param$Type } from '../../types';
import { Decoder } from '../types';
import sizes from '@polkadot/primitives/sizes';
import toU8a from '@polkadot/util/u8a/toU8a';
import bool from './bool';
import bn from './bn';
@@ -22,12 +23,14 @@ import string from './string';
import time from './time';
import u8a from './u8a';
export default function decodeValue (decode: Decoder, type: Param$Type, input: Uint8Array, version: EncodingVersions): Param$Decoded {
export default function decodeValue (decode: Decoder, type: Param$Type, _input: Uint8Array | string | null, version: EncodingVersions, isStorage: boolean): Param$Decoded {
const input: Uint8Array | null = toU8a(_input);
try {
switch (type) {
// TODO Pass back the actual address, not publicKey?
case 'AccountId':
return u8a(input, 256, version === 'poc-1' ? 0 : 1);
return u8a(input, 256, (isStorage || version === 'poc-1') ? 0 : 1);
case 'Balance':
return bn(input, sizes.Balance);
@@ -42,11 +45,12 @@ export default function decodeValue (decode: Decoder, type: Param$Type, input: U
return bool(input);
case 'Bytes':
case 'StorageKey':
return bytes(input);
case 'Call':
case 'Proposal':
return method(decode, input, type === 'Call', version);
return method(decode, input, type === 'Call', version, isStorage);
case 'Code':
return code(input);
@@ -67,7 +71,7 @@ export default function decodeValue (decode: Decoder, type: Param$Type, input: U
return bn(input, sizes.AccountIndex);
case 'KeyValue':
case 'KeyValueStorage':
case 'StorageKeyValue':
return keyValue(input);
case 'MisbehaviorReport':
@@ -95,6 +99,8 @@ export default function decodeValue (decode: Decoder, type: Param$Type, input: U
case 'VoteThreshold':
return byte(input);
case 'StorageResult':
case 'StorageResultSet':
default:
// tslint:disable-next-line
(type as never);
@@ -9,7 +9,7 @@ import decode from './index';
describe('keyValue', () => {
const nullDecoder = () => {};
it('encodes KeyValueStorage -> Uint8Array properly', () => {
it('encodes StorageKeyValue -> Uint8Array properly', () => {
expect(
decode(
nullDecoder,
@@ -28,7 +28,7 @@ describe('keyValue', () => {
key: toU8a('0x11223344'),
value: toU8a('0x998877665544332211')
}
})
});
});
it('encodes KeyValue -> Uint8Array properly', () => {
@@ -50,6 +50,6 @@ describe('keyValue', () => {
key: toU8a('0x11'),
value: toU8a('0x9988')
}
})
});
});
});
@@ -9,12 +9,8 @@ import u8aToBn from '@polkadot/util/u8a/toBn';
export default function bytes (input: Uint8Array | null): Param$Decoded {
if (!input) {
return {
length: 0,
value: {
key: new Uint8Array(),
value: new Uint8Array()
}
};
length: 0
} as Param$Decoded;
}
const keyLength = u8aToBn(input.subarray(0, 4), true).toNumber();
@@ -16,7 +16,13 @@ type Result = {
value: ExtrinsicDecoded
};
export default function call (decode: Decoder, input: Uint8Array, isPublic: boolean, version: EncodingVersions): Param$Decoded {
export default function call (decode: Decoder, input: Uint8Array | null, isPublic: boolean, version: EncodingVersions, isStorage: boolean): Param$Decoded {
if (input === null) {
return {
length: 0
} as Param$Decoded;
}
const section: Section<Extrinsics> | undefined = Object.values(extrinsics).find(({ index }) =>
index[0] === input[0]
);
@@ -42,7 +48,7 @@ export default function call (decode: Decoder, input: Uint8Array, isPublic: bool
};
return params.reduce(({ length, value }: Result, { type }: Param) => {
const decoded = decode(type, input.subarray(length), version);
const decoded = decode(type, input.subarray(length), version, isStorage);
value.params.push(decoded.value);
@@ -18,7 +18,13 @@ const B_SIG = B_HASH + 32;
const length = B_SIG + 64;
export default function misbehavior (input: Uint8Array): Param$Decoded {
export default function misbehavior (input: Uint8Array | null): Param$Decoded {
if (input === null) {
return {
length: 0
} as Param$Decoded;
}
return {
length,
value: ({
@@ -4,7 +4,13 @@
import { Param$Decoded } from '../../types';
export default function passThrough (input: Uint8Array): Param$Decoded {
export default function passThrough (input: Uint8Array | null): Param$Decoded {
if (input === null) {
return {
length: 0
} as Param$Decoded;
}
return {
length: input.length,
value: input
@@ -8,16 +8,19 @@ import u8aToBn from '@polkadot/util/u8a/toBn';
import u8aToUtf8 from '@polkadot/util/u8a/toUtf8';
export default function string (input: Uint8Array | null): Param$Decoded {
const length = input
? u8aToBn(input.subarray(0, 4), true).toNumber()
: 0;
if (input === null) {
return {
length: 0,
value: ''
};
}
const length = u8aToBn(input.subarray(0, 4), true).toNumber();
return {
length: length + 4,
value: input
? u8aToUtf8(
input.subarray(4, length + 4)
)
: ''
value: u8aToUtf8(
input.subarray(4, length + 4)
)
};
}
+12 -7
View File
@@ -7,14 +7,19 @@ import { Param$Decoded } from '../../types';
import u8aToBn from '@polkadot/util/u8a/toBn';
export default function time (input: Uint8Array | null): Param$Decoded {
if (input === null) {
return {
length: 0,
value: new Date(0)
};
}
return {
length: 8,
value: input
? new Date(
u8aToBn(input.subarray(0, 8), true)
.imuln(1000)
.toNumber()
)
: new Date(0)
value: new Date(
u8aToBn(input.subarray(0, 8), true)
.imuln(1000)
.toNumber()
)
};
}
+8 -3
View File
@@ -5,12 +5,17 @@
import { Param$Decoded } from '../../types';
export default function u8a (input: Uint8Array | null, bitLength: 256 | 512, offset: number): Param$Decoded {
if (input === null) {
return {
length: 0,
value: new Uint8Array()
};
}
const length = (bitLength / 8) + offset;
return {
length,
value: input
? input.subarray(offset, length)
: new Uint8Array()
value: input.subarray(offset, length)
};
}
@@ -14,6 +14,7 @@ import defaultSizes from '@polkadot/primitives/sizes';
import sizes from '../../sizes';
import keyValue from './keyValue';
import storageKey from './storageKey';
export default function encodeType (type: Param$Type, value: any, version: EncodingVersions): Uint8Array {
try {
@@ -60,7 +61,7 @@ export default function encodeType (type: Param$Type, value: any, version: Encod
return bnToU8a(value, sizes.AccountIndex.get(version) || defaultSizes.Balance, true);
case 'KeyValue':
case 'KeyValueStorage':
case 'StorageKeyValue':
return keyValue(value);
case 'ParachainId':
@@ -70,6 +71,9 @@ export default function encodeType (type: Param$Type, value: any, version: Encod
case 'u32':
return bnToU8a(value, 32, true);
case 'StorageKey':
return storageKey(value);
case 'String':
return (() => {
const u8a = u8aFromString(value);
@@ -7,9 +7,9 @@ import toU8a from '@polkadot/util/u8a/toU8a';
import encode from './index';
describe('keyValue', () => {
it('encodes KeyValueStorage -> Uint8Array properly', () => {
it('encodes StorageKeyValue -> Uint8Array properly', () => {
expect(
encode('KeyValueStorage', {
encode('StorageKeyValue', {
key: toU8a('0x11223344'),
value: toU8a('0x998877665544332211')
})
@@ -20,7 +20,7 @@ describe('keyValue', () => {
9, 0, 0, 0,
0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11
])
)
);
});
it('encodes KeyValue -> Uint8Array properly', () => {
@@ -36,6 +36,6 @@ describe('keyValue', () => {
2, 0, 0, 0,
0x99, 0x88
])
)
);
});
});
@@ -0,0 +1,21 @@
// Copyright 2017-2018 @polkadot/params authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import storage from '@polkadot/storage';
import toU8a from '@polkadot/util/u8a/toU8a';
import encode from './index';
describe('storageKey', () => {
it('encodes storageKey -> Uint8Array properly', () => {
expect(
encode('StorageKey', [
storage.staking.public.freeBalanceOf,
'5EhmTa7fL6SdjgKXo9g6hetR6nHnRAmrtisoGFWEESjzECtY'
])
).toEqual(
toU8a('0x11cf1094db4db43356b7787c3e59c39f')
);
});
});
@@ -0,0 +1,12 @@
// Copyright 2017-2018 @polkadot/params authors & contributors
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { SectionItem } from '@polkadot/params/types';
import { Storages } from '@polkadot/storage/types';
import createStorageKey from '@polkadot/storage/key';
export default function storageKey ([key, ...params]: [SectionItem<Storages>, any]): Uint8Array {
return createStorageKey(key).apply(null, params);
}
+1 -1
View File
@@ -14,7 +14,7 @@ export type EncodingVersions = 'poc-1' | 'latest';
// - @polkadot/storage/key/params.ts
// - decode/value/index.js
// - encode/type/index.js
export type Param$Type = 'AccountId' | 'AccountIndex' | 'Balance' | 'BlockNumber' | 'bool' | 'Bytes' | 'Call' | 'CandidateReceipt' | 'Code' | 'Digest' | 'Gas' | 'Hash' | 'Header' | 'KeyValue' | 'KeyValueStorage' | 'MisbehaviorReport' | 'ParachainId' | 'PropIndex' | 'Proposal' | 'ReferendumIndex' | 'SessionKey' | 'Signature' | 'String' | 'Timestamp' | 'u32' | 'u64' | 'u128' | 'VoteIndex' | 'VoteThreshold';
export type Param$Type = 'AccountId' | 'AccountIndex' | 'Balance' | 'BlockNumber' | 'bool' | 'Bytes' | 'Call' | 'CandidateReceipt' | 'Code' | 'Digest' | 'Gas' | 'Hash' | 'Header' | 'KeyValue' | 'MisbehaviorReport' | 'ParachainId' | 'PropIndex' | 'Proposal' | 'ReferendumIndex' | 'SessionKey' | 'Signature' | 'StorageKey' | 'StorageKeyValue' | 'StorageResult' | 'StorageResultSet' | 'String' | 'Timestamp' | 'u32' | 'u64' | 'u128' | 'VoteIndex' | 'VoteThreshold';
export type Param$Type$Array = Array<Param$Type | Array<Param$Type | Array<Param$Type>>>;