Mock tim:val for queries (#82)

* Mock tim:val for queries

* Update for failing types

* Use @polkadot/storage

* Upgrades
This commit is contained in:
Jaco Greeff
2018-05-01 12:40:14 +02:00
committed by GitHub
parent 07697a385c
commit 233a47b626
5 changed files with 37 additions and 33 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
"test": "jest --coverage"
},
"devDependencies": {
"@polkadot/dev": "^0.17.12",
"@polkadot/dev": "^0.17.13",
"lerna": "^2.5.1"
}
}
+1
View File
@@ -34,6 +34,7 @@
"nock": "^9.1.0"
},
"dependencies": {
"@polkadot/storage-substrate": "^0.10.2",
"@polkadot/util": "^0.19.4",
"@polkadot/util-crypto": "^0.19.4",
"@polkadot/util-keyring": "^0.19.4",
+13 -5
View File
@@ -5,18 +5,20 @@
// FIME: This file is way too long and way too messy
import type { StorageDef$Key } from '@polkadot/storage/types';
import type { KeyringPair } from '@polkadot/util-keyring/types';
import type { ProviderInterface$Emitted } from '../types';
import type { MockState, MockState$Subscription$Callback } from './types';
const BN = require('bn.js');
const headerEncode = require('@polkadot/primitives-json/header/encode');
const createKey = require('@polkadot/storage/key');
const keys = require('@polkadot/storage-substrate/keys');
const bnToU8a = require('@polkadot/util/bn/toU8a');
const u8aToHex = require('@polkadot/util/u8a/toHex');
const randomAsU8a = require('@polkadot/util-crypto/random/asU8a');
const testKeyring = require('@polkadot/util-keyring/testing');
const storageKey = require('./storageKey');
const keyring = testKeyring();
const emitEvents: Array<ProviderInterface$Emitted> = ['connected', 'disconnected'];
@@ -53,8 +55,12 @@ function updateSubs (subscriptions, method, value) {
});
}
function setStorageBn (storage, prefix: string, key: Uint8Array, value: BN): void {
storage[storageKey(prefix, key)] = bnToU8a(value, 64, true);
function setStorageBn (storage, key: StorageDef$Key, value: BN | number, ...keyParams: Array<Uint8Array>): void {
const keyValue = u8aToHex(
createKey(key).apply(null, keyParams)
);
storage[keyValue] = bnToU8a(value, 64, true);
}
module.exports = function mocks ({ emitter, storage, subscriptions }: MockState): void {
@@ -70,9 +76,11 @@ module.exports = function mocks ({ emitter, storage, subscriptions }: MockState)
newHead = makeBlockHeader(newHead.number);
keyring.getPairs().forEach(({ publicKey }: KeyringPair, index: number) => {
setStorageBn(storage, 'sta:bal:', publicKey(), newHead.number.muln(3).iaddn(index));
setStorageBn(storage, keys.staking.balanceOf, newHead.number.muln(3).iaddn(index), publicKey());
});
setStorageBn(storage, keys.timestamp.current, Date.now());
updateSubs(subscriptions, 'subscribe_newHead', headerEncode(newHead));
}, 5000);
};
@@ -1,20 +0,0 @@
// 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
)
)
);
};
+22 -7
View File
@@ -79,9 +79,9 @@
lodash "^4.2.0"
to-fast-properties "^2.0.0"
"@polkadot/dev@^0.17.12":
version "0.17.12"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.12.tgz#90d0d302681c5eae4683eea75949548a99e52831"
"@polkadot/dev@^0.17.13":
version "0.17.13"
resolved "https://registry.yarnpkg.com/@polkadot/dev/-/dev-0.17.13.tgz#cd5690d4c45abcc1e4b2624944c79229a82930e2"
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.70.0"
flow-bin "^0.71.0"
flow-copy-source "^1.3.0"
jest "^22.4.2"
makeshift "^1.1.0"
@@ -166,6 +166,21 @@
"@polkadot/util" "^0.19.4"
babel-runtime "^6.26.0"
"@polkadot/storage-substrate@^0.10.2":
version "0.10.2"
resolved "https://registry.yarnpkg.com/@polkadot/storage-substrate/-/storage-substrate-0.10.2.tgz#2f431d812d57f01fbc2a36c87ba495c5917443ee"
dependencies:
"@polkadot/storage" "^0.10.2"
"@polkadot/util" "^0.19.4"
babel-runtime "^6.26.0"
"@polkadot/storage@^0.10.2":
version "0.10.2"
resolved "https://registry.yarnpkg.com/@polkadot/storage/-/storage-0.10.2.tgz#e01a53123e5c5de421117685c0a390d3beefbbde"
dependencies:
"@polkadot/util" "^0.19.4"
babel-runtime "^6.26.0"
"@polkadot/util-crypto@^0.19.4":
version "0.19.4"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.19.4.tgz#d6e33197125e9adaf50410478a67e440630e7368"
@@ -2468,9 +2483,9 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"
flow-bin@^0.70.0:
version "0.70.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.70.0.tgz#080ae83a997f2b4ddb3dc2649bf13336825292b5"
flow-bin@^0.71.0:
version "0.71.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.71.0.tgz#fd1b27a6458c3ebaa5cb811853182ed631918b70"
flow-copy-source@^1.3.0:
version "1.3.0"