Cleanup jsonrpc, refer to api-codec (#174)

* Cleanup jsonrpc, refer to api-codec

* Move storagekey legacy into type-params (legacy)

* Remove misplaced debug log

* Remove header encoding, use api-codec/Header

* Remove legacy types

* Add metadat array (hack around json import)

* Move json metadata to ts
This commit is contained in:
Jaco Greeff
2018-09-28 18:35:39 +02:00
committed by GitHub
parent 62ed0a7b3f
commit b3a95b58ed
29 changed files with 282 additions and 263 deletions
+12 -21
View File
@@ -4,26 +4,20 @@
// FIXME: This file is way too long and way too messy
import { Storages } from '@polkadot/storage/types';
import { SectionItem } from '@polkadot/params/types';
import { StorageFunction } from '@polkadot/api-codec/StorageKey';
import { KeyringPair } from '@polkadot/util-keyring/types';
import { ProviderInterface$Emitted } from '../types';
import { MockState, MockState$Storage, MockState$Subscriptions } from './types';
import { MockState, MockState$Db, MockState$Subscriptions } from './types';
import BN from 'bn.js';
import headerEncode from '@polkadot/primitives/json/header/encode';
import createKey from '@polkadot/storage/key';
import state from '@polkadot/storage/index';
import Header from '@polkadot/api-codec/Header';
import storage from '@polkadot/storage/testing';
import bnToU8a from '@polkadot/util/bn/toU8a';
import u8aToHex from '@polkadot/util/u8a/toHex';
import randomAsU8a from '@polkadot/util-crypto/random/asU8a';
import testKeyring from '@polkadot/util-keyring/testing';
const keyring = testKeyring();
// FIXME No more .public
const stateStaking = state.staking.public;
const stateSystem = state.staking.public;
const stateTimestamp = state.timestamp.public;
const emitEvents: Array<ProviderInterface$Emitted> = ['connected', 'disconnected'];
let emitIndex = 0;
@@ -58,15 +52,15 @@ function updateSubs (subscriptions: MockState$Subscriptions, method: string, val
});
}
function setStorageBn (storage: MockState$Storage, key: SectionItem<Storages>, value: BN | number, ...keyParams: Array<Uint8Array>): void {
function setStorageBn (db: MockState$Db, createKey: StorageFunction, value: BN | number, ...keyParams: Array<Uint8Array>): void {
const keyValue = u8aToHex(
createKey(key).apply(null, keyParams)
createKey(...keyParams)
);
storage[keyValue] = bnToU8a(value, 64, true);
db[keyValue] = bnToU8a(value, 64, true);
}
export default function mocks ({ emitter, storage, subscriptions }: MockState): void {
export default function mocks ({ emitter, db, subscriptions }: MockState): void {
let newHead = makeBlockHeader(new BN(-1));
setInterval(() => {
@@ -79,15 +73,12 @@ export default function mocks ({ emitter, storage, subscriptions }: MockState):
newHead = makeBlockHeader(newHead.number);
keyring.getPairs().forEach(({ publicKey }: KeyringPair, index: number) => {
// @ts-ignore FIXME This does not exist anymore
setStorageBn(storage, stateStaking.freeBalanceOf, newHead.number.muln(3).iaddn(index), publicKey());
// @ts-ignore FIXME This does not exist anymore
setStorageBn(storage, stateSystem.accountIndexOf, newHead.number.addn(index), publicKey());
setStorageBn(db, storage.balances.freeBalance, newHead.number.muln(3).iaddn(index), publicKey());
setStorageBn(db, storage.system.accountNonce, newHead.number.addn(index), publicKey());
});
// @ts-ignore FIXME This does not exist anymore
setStorageBn(storage, stateTimestamp.current, Math.floor(Date.now() / 1000));
setStorageBn(db, storage.timestamp.now, Math.floor(Date.now() / 1000));
updateSubs(subscriptions, 'chain_newHead', headerEncode(newHead));
updateSubs(subscriptions, 'chain_newHead', new Header(newHead).toJSON());
}, 5000);
}
+2 -2
View File
@@ -4,10 +4,10 @@
import { MockState } from './types';
export default async function send ({ requests, storage }: MockState, method: string, params: Array<any>): Promise<any> {
export default async function send ({ requests, db }: MockState, method: string, params: Array<any>): Promise<any> {
if (!requests[method]) {
throw new Error(`provider.send: Invalid method '${method}'`);
}
return requests[method](storage, params);
return requests[method](db, params);
}
+5 -5
View File
@@ -2,7 +2,7 @@
// This software may be modified and distributed under the terms
// of the ISC license. See the LICENSE file for details.
import { MockState, MockState$Storage, MockState$Subscriptions } from './types';
import { MockState, MockState$Db, MockState$Subscriptions } from './types';
import E3 from 'eventemitter3';
import interfaces from '@polkadot/jsonrpc/index';
@@ -14,7 +14,7 @@ const l = logger('api-mock');
const SUBSCRIPTIONS: string[] = Array.prototype.concat.apply(
[], Object.values(interfaces).map((area) =>
Object
.values(area.public)
.values(area.methods)
.filter((method) =>
method.isSubscription
)
@@ -25,7 +25,7 @@ const SUBSCRIPTIONS: string[] = Array.prototype.concat.apply(
);
const REQUESTS = {
'state_getStorage': (storage: MockState$Storage, params: Array<any>): string => {
'state_getStorage': (storage: MockState$Db, params: Array<any>): string => {
return u8aToHex(
storage[(params[0] as string)]
);
@@ -36,7 +36,7 @@ const REQUESTS = {
};
export default function state (): MockState {
const storage = {};
const db = {};
const subscriptions: MockState$Subscriptions = SUBSCRIPTIONS.reduce((subs, name) => {
subs[name] = {
callbacks: {},
@@ -50,7 +50,7 @@ export default function state (): MockState {
emitter: new E3.EventEmitter(),
l,
requests: Object.assign({}, REQUESTS),
storage,
db,
subscriptionId: 0,
subscriptionMap: {},
subscriptions
+3 -3
View File
@@ -16,19 +16,19 @@ export type MockState$Subscriptions = {
}
};
export type MockState$Storage = {
export type MockState$Db = {
[index: string]: Uint8Array
};
export type MockState$Requests = {
[index: string]: (storage: MockState$Storage, params: Array<any>) => string
[index: string]: (db: MockState$Db, params: Array<any>) => string
};
export type MockState = {
emitter: EventEmitter,
l: Logger,
requests: MockState$Requests,
storage: MockState$Storage,
db: MockState$Db,
subscriptionId: number,
subscriptionMap: {
[index: number]: string