Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.17.4",
"@polkadot/dev": "^0.17.6",
"lerna": "^2.5.1"
}
}
2 changes: 1 addition & 1 deletion packages/api-format/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 3 additions & 1 deletion packages/api-provider/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
54 changes: 37 additions & 17 deletions packages/api-provider/src/mock/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};
8 changes: 6 additions & 2 deletions packages/api-provider/src/mock/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

import type { MockState } from './types';

module.exports = async function send (self: MockState, method: string, params: Array<mixed>): Promise<mixed> {
throw new Error('provider.send not implemented');
module.exports = async function send ({ requests, storage }: MockState, method: string, params: Array<mixed>): Promise<mixed> {
if (!requests[method]) {
throw new Error(`provider.send: Invalid method '${method}'`);
}

return requests[method](storage, params);
};
16 changes: 13 additions & 3 deletions packages/api-provider/src/mock/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -17,8 +17,16 @@ const METHODS = Array.prototype.concat.apply(
})
);

const REQUESTS = {
'state_getStorage': (storage: MockState$Storage, params: Array<mixed>): 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
Expand All @@ -29,6 +37,8 @@ module.exports = function state (): MockState {

return {
l,
requests: Object.assign({}, REQUESTS),
storage,
subscriptionId: 0,
subscriptionMap: {},
subscriptions
Expand Down
20 changes: 20 additions & 0 deletions packages/api-provider/src/mock/storageKey.js
Original file line number Diff line number Diff line change
@@ -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
)
)
);
};
10 changes: 10 additions & 0 deletions packages/api-provider/src/mock/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>) => mixed
};

export type MockState = {
l: Logger,
requests: MockState$Requests,
storage: MockState$Storage,
subscriptionId: number,
subscriptionMap: {
[number]: string
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
23 changes: 16 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down