From 23649b47127d5a3d2e41f820a7171311195d6f15 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 17 Apr 2018 12:09:25 +0200 Subject: [PATCH] state_getStorage mock --- package.json | 2 +- packages/api-format/package.json | 2 +- packages/api-provider/package.json | 4 +- packages/api-provider/src/mock/mocks.js | 54 ++++++++++++++------ packages/api-provider/src/mock/send.js | 8 ++- packages/api-provider/src/mock/state.js | 16 ++++-- packages/api-provider/src/mock/storageKey.js | 20 ++++++++ packages/api-provider/src/mock/types.js | 10 ++++ packages/api/package.json | 2 +- yarn.lock | 23 ++++++--- 10 files changed, 108 insertions(+), 33 deletions(-) create mode 100644 packages/api-provider/src/mock/storageKey.js diff --git a/package.json b/package.json index 9227134a2076..c6cbac07de9f 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "test": "jest --coverage" }, "devDependencies": { - "@polkadot/dev": "^0.17.4", + "@polkadot/dev": "^0.17.6", "lerna": "^2.5.1" } } diff --git a/packages/api-format/package.json b/packages/api-format/package.json index e99aa25d3cec..c34cc7ae55e9 100644 --- a/packages/api-format/package.json +++ b/packages/api-format/package.json @@ -34,7 +34,7 @@ }, "dependencies": { "@polkadot/primitives-json": "^0.10.1", - "@polkadot/util": "^0.18.4", + "@polkadot/util": "^0.18.5", "babel-runtime": "^6.26.0" } } diff --git a/packages/api-provider/package.json b/packages/api-provider/package.json index 18219a8d3a2d..92cfc29d5e76 100644 --- a/packages/api-provider/package.json +++ b/packages/api-provider/package.json @@ -34,7 +34,9 @@ "nock": "^9.1.0" }, "dependencies": { - "@polkadot/util": "^0.18.4", + "@polkadot/util": "^0.18.5", + "@polkadot/util-crypto": "^0.18.5", + "@polkadot/util-keyring": "^0.18.5", "babel-runtime": "^6.26.0", "isomorphic-fetch": "^2.2.1", "websocket": "^1.0.25" diff --git a/packages/api-provider/src/mock/mocks.js b/packages/api-provider/src/mock/mocks.js index 4145fcf1768a..5ab845533019 100644 --- a/packages/api-provider/src/mock/mocks.js +++ b/packages/api-provider/src/mock/mocks.js @@ -3,12 +3,20 @@ // of the ISC license. See the LICENSE file for details. // @flow +// FIME: This file is way too long and way too messy + +import type { KeyringPair } from '@polkadot/util-keyring/types'; import type { MockState, MockState$Subscription$Callback } from './types'; const BN = require('bn.js'); const headerEncode = require('@polkadot/primitives-json/header/encode'); const bnToU8a = require('@polkadot/util/bn/toU8a'); const randomAsU8a = require('@polkadot/util-crypto/random/asU8a'); +const testKeyring = require('@polkadot/util-keyring/testing'); + +const storageKey = require('./storageKey'); + +const keyring = testKeyring(); function makeBlockHeader (prevNumber: BN) { const blockNumber = prevNumber.addn(1); @@ -26,27 +34,39 @@ function makeBlockHeader (prevNumber: BN) { }; } -module.exports = function mocks ({ subscriptions }: MockState): void { - let newHead = makeBlockHeader(new BN(-1)); +function updateSubs (subscriptions, method, value) { + subscriptions[method].lastValue = value; - const updateSubs = (method, value) => { - subscriptions[method].lastValue = value; - - Object - .values(subscriptions[method].callbacks) - .forEach((cb) => { - try { - // flowlint-next-line unclear-type:off - ((cb: any): MockState$Subscription$Callback)(null, value); - } catch (error) { - console.error(`Error on '${method}' subscription`, error); - } - }); - }; + Object + .values(subscriptions[method].callbacks) + .forEach((cb) => { + try { + // flowlint-next-line unclear-type:off + ((cb: any): MockState$Subscription$Callback)(null, value); + } catch (error) { + console.error(`Error on '${method}' subscription`, error); + } + }); +} + +function setStorageBn (storage, prefix: string, key: Uint8Array, value: BN): void { + storage[storageKey(prefix, key)] = bnToU8a(value, 64, true); +} + +module.exports = function mocks ({ storage, subscriptions }: MockState): void { + let newHead = makeBlockHeader(new BN(-1)); setInterval(() => { newHead = makeBlockHeader(newHead.number); - updateSubs('subscribe_newHead', headerEncode(newHead)); + Object.values(keyring).forEach((pair, index) => { + // flowlint-next-line unclear-type:off + const publicKey = ((pair: any): KeyringPair).publicKey(); + const balance = newHead.number.muln(3).iaddn(index); + + setStorageBn(storage, 'sta:bal:', publicKey, balance); + }); + + updateSubs(subscriptions, 'subscribe_newHead', headerEncode(newHead)); }, 5000); }; diff --git a/packages/api-provider/src/mock/send.js b/packages/api-provider/src/mock/send.js index 25619b0c258c..4b9ea5757782 100644 --- a/packages/api-provider/src/mock/send.js +++ b/packages/api-provider/src/mock/send.js @@ -5,6 +5,10 @@ import type { MockState } from './types'; -module.exports = async function send (self: MockState, method: string, params: Array): Promise { - throw new Error('provider.send not implemented'); +module.exports = async function send ({ requests, storage }: MockState, method: string, params: Array): Promise { + if (!requests[method]) { + throw new Error(`provider.send: Invalid method '${method}'`); + } + + return requests[method](storage, params); }; diff --git a/packages/api-provider/src/mock/state.js b/packages/api-provider/src/mock/state.js index aa58ecf98ded..7dd7349df8b5 100644 --- a/packages/api-provider/src/mock/state.js +++ b/packages/api-provider/src/mock/state.js @@ -3,12 +3,12 @@ // of the ISC license. See the LICENSE file for details. // @flow -import type { MockState } from './types'; +import type { MockState, MockState$Storage } from './types'; const interfaces = require('@polkadot/api-jsonrpc'); const l = require('@polkadot/util/logger')('api-mock'); -const METHODS = Array.prototype.concat.apply( +const SUBSCRIPTIONS = Array.prototype.concat.apply( [], Object.keys(interfaces).map((section) => { return Object .keys(interfaces[section].methods) @@ -17,8 +17,16 @@ const METHODS = Array.prototype.concat.apply( }) ); +const REQUESTS = { + 'state_getStorage': (storage: MockState$Storage, params: Array): Uint8Array => { + // flowlint-next-line unclear-type:off + return storage[((params[0]: any): string)]; + } +}; + module.exports = function state (): MockState { - const subscriptions = METHODS.reduce((subscriptions, name) => { + const storage = {}; + const subscriptions = SUBSCRIPTIONS.reduce((subscriptions, name) => { subscriptions[name] = { callbacks: {}, lastValue: null @@ -29,6 +37,8 @@ module.exports = function state (): MockState { return { l, + requests: Object.assign({}, REQUESTS), + storage, subscriptionId: 0, subscriptionMap: {}, subscriptions diff --git a/packages/api-provider/src/mock/storageKey.js b/packages/api-provider/src/mock/storageKey.js new file mode 100644 index 000000000000..97f461d85cf5 --- /dev/null +++ b/packages/api-provider/src/mock/storageKey.js @@ -0,0 +1,20 @@ +// Copyright 2017-2018 Jaco Greeff +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. +// @flow + +const u8aConcat = require('@polkadot/util/u8a/concat'); +const u8aFromString = require('@polkadot/util/u8a/fromString'); +const u8aToHex = require('@polkadot/util/u8a/toHex'); +const xxhash = require('@polkadot/util-crypto/xxhash/asU8a128'); + +module.exports = function storageKey (prefix: string, key: Uint8Array | string): string { + return u8aToHex( + xxhash( + u8aConcat( + u8aFromString(prefix), + key + ) + ) + ); +}; diff --git a/packages/api-provider/src/mock/types.js b/packages/api-provider/src/mock/types.js index 6462c54ec431..187b36a78de8 100644 --- a/packages/api-provider/src/mock/types.js +++ b/packages/api-provider/src/mock/types.js @@ -16,8 +16,18 @@ export type MockState$Subscriptions = { } }; +export type MockState$Storage = { + [string]: Uint8Array +}; + +export type MockState$Requests = { + [string]: (storage: MockState$Storage, params: Array) => mixed +}; + export type MockState = { l: Logger, + requests: MockState$Requests, + storage: MockState$Storage, subscriptionId: number, subscriptionMap: { [number]: string diff --git a/packages/api/package.json b/packages/api/package.json index 37e9db050298..9eee9bda08df 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -33,7 +33,7 @@ "@polkadot/api-format": "^0.8.11", "@polkadot/api-jsonrpc": "^0.8.11", "@polkadot/api-provider": "^0.8.11", - "@polkadot/util": "^0.18.4", + "@polkadot/util": "^0.18.5", "babel-runtime": "^6.26.0" } } diff --git a/yarn.lock b/yarn.lock index b6331df46731..544f1b26acd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -79,9 +79,9 @@ lodash "^4.2.0" to-fast-properties "^2.0.0" -"@polkadot/dev@^0.17.4": - version "0.17.4" - resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.4.tgz#d5bcbbb0a6531ebbce222d946be9ae4a52eea1e4" +"@polkadot/dev@^0.17.6": + version "0.17.6" + resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.6.tgz#8935c83ee71d32bf343e83fe537deb20b5764be0" dependencies: babel-cli "^6.26.0" babel-core "^6.26.0" @@ -106,7 +106,7 @@ eslint-plugin-promise "^3.6.0" eslint-plugin-react "^7.7.0" eslint-plugin-standard "^3.0.1" - flow-bin "^0.69.0" + flow-bin "^0.70.0" flow-copy-source "^1.3.0" jest "^22.4.2" makeshift "^1.1.0" @@ -209,6 +209,15 @@ deasync "^0.1.12" ip-regex "^2.1.0" +"@polkadot/util@^0.18.5": + version "0.18.5" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.18.5.tgz#98c04608fceadaaace765eb44253912edc007651" + dependencies: + babel-runtime "^6.26.0" + bn.js "^4.11.8" + deasync "^0.1.12" + ip-regex "^2.1.0" + "@types/node@*": version "8.5.2" resolved "https://registry.yarnpkg.com/@types/node/-/node-8.5.2.tgz#83b8103fa9a2c2e83d78f701a9aa7c9539739aa5" @@ -2462,9 +2471,9 @@ flat-cache@^1.2.1: graceful-fs "^4.1.2" write "^0.2.1" -flow-bin@^0.69.0: - version "0.69.0" - resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.69.0.tgz#053159a684a6051fcbf0b71a2eb19a9679082da6" +flow-bin@^0.70.0: + version "0.70.0" + resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.70.0.tgz#080ae83a997f2b4ddb3dc2649bf13336825292b5" flow-copy-source@^1.3.0: version "1.3.0"