Encode mock responses as JSON (#66)

* Encode mock responses as JSON

* Update primitives
This commit is contained in:
Jaco Greeff
2018-04-16 18:07:21 +02:00
committed by GitHub
parent 2e4a9e7793
commit d86cd86251
5 changed files with 73 additions and 79 deletions
+1 -1
View File
@@ -33,7 +33,7 @@
"@polkadot/api-jsonrpc": "^0.8.10"
},
"dependencies": {
"@polkadot/primitives-json": "^0.9.5",
"@polkadot/primitives-json": "^0.10.1",
"@polkadot/util": "^0.18.4",
"babel-runtime": "^6.26.0"
}
+3 -2
View File
@@ -8,6 +8,7 @@ import type { InterfaceOutputType } from '@polkadot/api-jsonrpc/types';
const bnDecode = require('@polkadot/primitives-json/bn/decode');
const bytesDecode = require('@polkadot/primitives-json/bytes/decode');
const headerDecode = require('@polkadot/primitives-json/header/decode');
const isNull = require('@polkadot/util/is/null');
const isUndefined = require('@polkadot/util/is/undefined');
const util = require('./util');
@@ -20,8 +21,8 @@ const formatters = {
};
module.exports = function formatOutput (output: InterfaceOutputType, value?: mixed): ?mixed {
if (isUndefined(value)) {
return void 0;
if (isUndefined(value) || isNull(value)) {
return value;
}
return util.format(formatters, output.type, value);
+7 -7
View File
@@ -11,13 +11,13 @@ describe('formatOutput', () => {
expect(
JSON.stringify(
formatOutput({ type: 'Header' }, {
parentHash: '0x1234',
number: '0x1234',
stateRoot: '0x5678',
transactionRoot: '0xabcd',
digest: {
logs: ['0x5678', '0x789a']
}
},
extrinsicsRoot: '0xabcd',
number: '0x1234',
parentHash: '0x1234',
stateRoot: '0x5678'
})
)
).toEqual(
@@ -28,10 +28,10 @@ describe('formatOutput', () => {
new Uint8Array([0x78, 0x9a])
]
},
extrinsicsRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd]),
number: new BN(0x1234),
parentHash: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34]),
stateRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x56, 0x78]),
transactionRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd])
stateRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x56, 0x78])
})
);
});
+10 -8
View File
@@ -5,27 +5,29 @@
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 randomAsHex = require('@polkadot/util-crypto/random/asHex');
const randomAsU8a = require('@polkadot/util-crypto/random/asU8a');
function makeBlockHeader (prevNumber: number) {
const blockNumber = prevNumber++;
function makeBlockHeader (prevNumber: BN) {
const blockNumber = prevNumber.addn(1);
return {
digest: {
logs: []
},
extrinsicsRoot: randomAsHex(),
extrinsicsRoot: randomAsU8a(),
number: blockNumber,
parentHash: prevNumber === -1
? '0x0000000000000000000000000000000000000000000000000000000000000000'
parentHash: prevNumber.eqn(-1)
? new Uint8Array(32)
: bnToU8a(prevNumber, 256),
stateRoot: bnToU8a(blockNumber, 256)
};
}
module.exports = function mocks ({ subscriptions }: MockState): void {
let newHead = makeBlockHeader(-1);
let newHead = makeBlockHeader(new BN(-1));
const updateSubs = (method, value) => {
subscriptions[method].lastValue = value;
@@ -45,6 +47,6 @@ module.exports = function mocks ({ subscriptions }: MockState): void {
setInterval(() => {
newHead = makeBlockHeader(newHead.number);
updateSubs('subscribe_newHead', newHead);
updateSubs('subscribe_newHead', headerEncode(newHead));
}, 5000);
};