diff --git a/src/actions/peers.js b/src/actions/peers.js index 74752fe3d..60131e2b5 100644 --- a/src/actions/peers.js +++ b/src/actions/peers.js @@ -1,5 +1,15 @@ import Lisk from 'lisk-js'; import actionTypes from '../constants/actions'; +import { getNethash } from './../utils/api/nethash'; + +const peerSet = (data, config) => ({ + data: Object.assign({ + passphrase: data.passphrase, + publicKey: data.publicKey, + activePeer: Lisk.api(config), + }), + type: actionTypes.activePeerSet, +}); /** * Returns required action object to set @@ -9,37 +19,34 @@ import actionTypes from '../constants/actions'; * @param {Object} data - Active peer data and the passphrase of account * @returns {Object} Action object */ -export const activePeerSet = (data) => { - const addHttp = (url) => { - const reg = /^(?:f|ht)tps?:\/\//i; - return reg.test(url) ? url : `http://${url}`; - }; +export const activePeerSet = data => + (dispatch) => { + const addHttp = (url) => { + const reg = /^(?:f|ht)tps?:\/\//i; + return reg.test(url) ? url : `http://${url}`; + }; + const config = data.network || {}; - const { network } = data; - let config = { }; - if (network) { - config = network; - if (network.address) { - const normalizedUrl = new URL(addHttp(network.address)); + if (config.address) { + const { hostname, port, protocol } = new URL(addHttp(config.address)); - config.node = normalizedUrl.hostname; - config.port = normalizedUrl.port; - config.ssl = normalizedUrl.protocol === 'https'; + config.node = hostname; + config.port = port; + config.ssl = protocol === 'https'; } if (config.testnet === undefined && config.port !== undefined) { config.testnet = config.port === '7000'; } - } - - return { - data: Object.assign({ - passphrase: data.passphrase, - publicKey: data.publicKey, - activePeer: Lisk.api(config), - }), - type: actionTypes.activePeerSet, + if (config.custom) { + getNethash(Lisk.api(config)).then((response) => { + config.nethash = response.nethash; + dispatch(peerSet(data, config)); + }); + } else { + dispatch(peerSet(data, config)); + } }; -}; + /** * Returns required action object to partially diff --git a/src/actions/peers.test.js b/src/actions/peers.test.js index b6580859a..d29afa900 100644 --- a/src/actions/peers.test.js +++ b/src/actions/peers.test.js @@ -1,12 +1,13 @@ import { expect } from 'chai'; -import { spy } from 'sinon'; -import Lisk from 'lisk-js'; +import { spy, stub, match } from 'sinon'; import actionTypes from '../constants/actions'; import { activePeerSet, activePeerUpdate } from './peers'; +import * as nethashApi from './../utils/api/nethash'; describe('actions: peers', () => { const passphrase = 'wagon stock borrow episode laundry kitten salute link globe zero feed marble'; + const nethash = '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d'; describe('activePeerUpdate', () => { it('should create an action to update the active peer', () => { @@ -23,7 +24,20 @@ describe('actions: peers', () => { }); describe('activePeerSet', () => { + let dispatch; + let getNetHash; + + beforeEach(() => { + dispatch = spy(); + getNetHash = stub(nethashApi, 'getNethash'); + }); + + afterEach(() => { + getNetHash.restore(); + }); + it('creates active peer config', () => { + getNetHash.returnsPromise(); const data = { passphrase, network: { @@ -31,57 +45,58 @@ describe('actions: peers', () => { custom: true, address: 'http://localhost:4000', testnet: true, - nethash: '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d', + nethash, }, }; - const actionSpy = spy(Lisk, 'api'); - activePeerSet(data); - expect(actionSpy).to.have.been.calledWith(data.network); - Lisk.api.restore(); + + activePeerSet(data)(dispatch); + getNetHash.resolves({ nethash }); + + expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.options', data.network)); }); it('dispatch activePeerSet action also when address http missing', () => { - const data = { - passphrase, - network: { - address: 'localhost:8000', - }, + const network = { address: 'localhost:8000' }; + + activePeerSet({ passphrase, network })(dispatch); + + expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.options.address', 'localhost:8000')); + }); + + it('dispatch activePeerSet with nethash from response when the network is a custom node', () => { + getNetHash.returnsPromise(); + const network = { + address: 'http://localhost:4000', + custom: true, }; - const actionSpy = spy(Lisk, 'api'); - activePeerSet(data); - expect(actionSpy).to.have.been.calledWith(); - Lisk.api.restore(); + + activePeerSet({ passphrase, network })(dispatch); + getNetHash.resolves({ nethash: 'nethash from response' }); + + expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.nethash.nethash', 'nethash from response')); }); it('dispatch activePeerSet action even if network is undefined', () => { - const data = { passphrase }; - const actionSpy = spy(Lisk, 'api'); - activePeerSet(data); - expect(actionSpy).to.have.been.calledWith(); - Lisk.api.restore(); + activePeerSet({ passphrase })(dispatch); + + expect(dispatch).to.have.been.calledWith(); }); it('dispatch activePeerSet action even if network.address is undefined', () => { - const data = { passphrase, network: {} }; - const actionSpy = spy(Lisk, 'api'); - activePeerSet(data); - expect(actionSpy).to.have.been.calledWith(); - Lisk.api.restore(); + activePeerSet({ passphrase, network: {} })(dispatch); + + expect(dispatch).to.have.been.calledWith(); }); it('should set to testnet if not defined in config but port is 7000', () => { - const network7000 = { - address: 'http://127.0.0.1:7000', - nethash: '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d', - }; - const network4000 = { - address: 'http://127.0.0.1:4000', - nethash: '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d', - }; - let actionObj = activePeerSet({ passphrase, network: network7000 }); - expect(actionObj.data.activePeer.testnet).to.be.equal(true); - actionObj = activePeerSet({ passphrase, network: network4000 }); - expect(actionObj.data.activePeer.testnet).to.be.equal(false); + const network7000 = { address: 'http://127.0.0.1:7000', nethash }; + const network4000 = { address: 'http://127.0.0.1:4000', nethash }; + + activePeerSet({ passphrase, network: network7000 })(dispatch); + expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.options.testnet', true)); + + activePeerSet({ passphrase, network: network4000 })(dispatch); + expect(dispatch).to.have.been.calledWith(match.hasNested('data.activePeer.options.testnet', false)); }); }); }); diff --git a/src/components/login/networks.js b/src/components/login/networks.js index 6f6263c1c..6b060d439 100644 --- a/src/components/login/networks.js +++ b/src/components/login/networks.js @@ -1,5 +1,4 @@ import i18next from 'i18next'; -import env from '../../constants/env'; export default () => ([ { @@ -15,9 +14,5 @@ export default () => ([ name: i18next.t('Custom Node'), custom: true, address: 'http://localhost:4000', - ...(env.production ? {} : { - testnet: true, - nethash: '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d', - }), }, ]); diff --git a/src/utils/api/nethash.js b/src/utils/api/nethash.js new file mode 100644 index 000000000..a1b4997a3 --- /dev/null +++ b/src/utils/api/nethash.js @@ -0,0 +1,4 @@ +import { requestToActivePeer } from './peers'; + +// eslint-disable-next-line import/prefer-default-export +export const getNethash = activePeer => (requestToActivePeer(activePeer, 'blocks/getNethash')); diff --git a/src/utils/api/nethash.test.js b/src/utils/api/nethash.test.js new file mode 100644 index 000000000..eebbf9e06 --- /dev/null +++ b/src/utils/api/nethash.test.js @@ -0,0 +1,29 @@ +import { expect } from 'chai'; +import { mock } from 'sinon'; +import * as peers from './peers'; +import { getNethash } from './nethash'; + + +describe('Utils: Nethash', () => { + let peersMock; + const activePeer = {}; + + beforeEach(() => { + peersMock = mock(peers); + }); + + afterEach(() => { + peersMock.restore(); + }); + + it('should return the result from requestToActivePeer call', () => { + const mockedReturns = 'requestToActivePeer returns something'; + + peersMock.expects('requestToActivePeer') + .withArgs(activePeer, 'blocks/getNethash') + .returns(mockedReturns); + + const returnedPromise = getNethash(activePeer); + expect(returnedPromise).to.equal(mockedReturns); + }); +}); diff --git a/src/utils/api/peers.js b/src/utils/api/peers.js index 76d32f0bb..837e93dac 100644 --- a/src/utils/api/peers.js +++ b/src/utils/api/peers.js @@ -1,6 +1,6 @@ import { loadingStarted, loadingFinished } from '../../utils/loading'; -/* eslint-disable */ +// eslint-disable-next-line import/prefer-default-export export const requestToActivePeer = (activePeer, path, urlParams) => new Promise((resolve, reject) => { loadingStarted(path); @@ -13,4 +13,3 @@ export const requestToActivePeer = (activePeer, path, urlParams) => loadingFinished(path); }); }); -/* eslint-enable */