Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Merge pull request #858 from LiskHQ/830-nethash-from-api
Browse files Browse the repository at this point in the history
Get Blockchain Nethash from API - Closes #830
  • Loading branch information
gina contrino authored Oct 13, 2017
2 parents 59e6ef9 + 9bbfdbd commit 40eac16
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 69 deletions.
55 changes: 31 additions & 24 deletions src/actions/peers.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
91 changes: 53 additions & 38 deletions src/actions/peers.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -23,65 +24,79 @@ 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: {
name: 'Custom Node',
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));
});
});
});
5 changes: 0 additions & 5 deletions src/components/login/networks.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import i18next from 'i18next';
import env from '../../constants/env';

export default () => ([
{
Expand All @@ -15,9 +14,5 @@ export default () => ([
name: i18next.t('Custom Node'),
custom: true,
address: 'http://localhost:4000',
...(env.production ? {} : {
testnet: true,
nethash: '198f2b61a8eb95fbeed58b8216780b68f697f26b849acf00c8c93bb9b24f783d',
}),
},
]);
4 changes: 4 additions & 0 deletions src/utils/api/nethash.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { requestToActivePeer } from './peers';

// eslint-disable-next-line import/prefer-default-export
export const getNethash = activePeer => (requestToActivePeer(activePeer, 'blocks/getNethash'));
29 changes: 29 additions & 0 deletions src/utils/api/nethash.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 1 addition & 2 deletions src/utils/api/peers.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -13,4 +13,3 @@ export const requestToActivePeer = (activePeer, path, urlParams) =>
loadingFinished(path);
});
});
/* eslint-enable */

0 comments on commit 40eac16

Please sign in to comment.