Mock interfaces has isSubscription flag (#64)
* Mock interfaces has isSubscription flag * chain_newHead mock
This commit is contained in:
@@ -13,6 +13,7 @@ import type { InterfaceMethodDefinition } from '../types';
|
||||
Returns the best blockNumber available on the client
|
||||
*/
|
||||
module.exports = ({
|
||||
isSubscription: true,
|
||||
inputs: [],
|
||||
output: { type: 'BlockNumber' }
|
||||
}: InterfaceMethodDefinition);
|
||||
|
||||
@@ -13,6 +13,7 @@ import type { InterfaceMethodDefinition } from '../types';
|
||||
Returns the current time on the running node
|
||||
*/
|
||||
module.exports = ({
|
||||
isSubscription: true,
|
||||
inputs: [],
|
||||
output: { type: 'U64' }
|
||||
}: InterfaceMethodDefinition);
|
||||
|
||||
@@ -5,8 +5,27 @@
|
||||
|
||||
import type { MockState, MockState$Subscription$Callback } from './types';
|
||||
|
||||
const bnToU8a = require('@polkadot/util/bn/toU8a');
|
||||
const randomAsHex = require('@polkadot/util-crypto/random/asHex');
|
||||
|
||||
function makeBlockHeader (prevNumber: number) {
|
||||
const blockNumber = prevNumber++;
|
||||
|
||||
return {
|
||||
digest: {
|
||||
logs: []
|
||||
},
|
||||
extrinsicsRoot: randomAsHex(),
|
||||
number: blockNumber,
|
||||
parentHash: prevNumber === -1
|
||||
? '0x0000000000000000000000000000000000000000000000000000000000000000'
|
||||
: bnToU8a(prevNumber, 256),
|
||||
stateRoot: bnToU8a(blockNumber, 256)
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = function mocks ({ subscriptions }: MockState): void {
|
||||
let blockNumber = 0;
|
||||
let newHead = makeBlockHeader(-1);
|
||||
|
||||
const updateSubs = (method, value) => {
|
||||
subscriptions[method].lastValue = value;
|
||||
@@ -24,7 +43,10 @@ module.exports = function mocks ({ subscriptions }: MockState): void {
|
||||
};
|
||||
|
||||
setInterval(() => {
|
||||
updateSubs('extra_getBlockNumber', `0x${(++blockNumber).toString(16)}`);
|
||||
newHead = makeBlockHeader(newHead.number);
|
||||
|
||||
updateSubs('extra_getBlockNumber', `0x${(newHead.number).toString(16)}`);
|
||||
updateSubs('chain_newHead', newHead);
|
||||
}, 5000);
|
||||
|
||||
setInterval(() => {
|
||||
|
||||
@@ -12,6 +12,7 @@ const METHODS = Array.prototype.concat.apply(
|
||||
[], Object.keys(interfaces).map((section) => {
|
||||
return Object
|
||||
.keys(interfaces[section].methods)
|
||||
.filter((method) => interfaces[section].methods[method].isSubscription)
|
||||
.map((method) => `${section}_${method}`);
|
||||
})
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user