From c2ceefbc22915feb77ea012cdbb057bea1ea863c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 6 Feb 2017 09:53:22 +0100 Subject: [PATCH] Fix wrong output format of peers (#4270) (#4442) * Fix wrong output format of peers * Add outPeer tests --- js/src/api/format/output.js | 21 ++++++++++- js/src/api/format/output.spec.js | 65 +++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/js/src/api/format/output.js b/js/src/api/format/output.js index 877728717..8c30ab1e1 100644 --- a/js/src/api/format/output.js +++ b/js/src/api/format/output.js @@ -129,12 +129,31 @@ export function outNumber (number) { return new BigNumber(number || 0); } +export function outPeer (peer) { + const protocols = Object.keys(peer.protocols) + .reduce((obj, key) => { + if (peer.protocols[key]) { + obj[key] = { + ...peer.protocols[key], + difficulty: outNumber(peer.protocols[key].difficulty) + }; + } + + return obj; + }, {}); + + return { + ...peer, + protocols + }; +} + export function outPeers (peers) { return { active: outNumber(peers.active), connected: outNumber(peers.connected), max: outNumber(peers.max), - peers: peers.peers.map(p => { Object.keys(p.protocols).forEach(k => { p.protocols[k].difficulty = outNumber(p.protocols[k].difficulty); }); return p; }) + peers: peers.peers.map((peer) => outPeer(peer)) }; } diff --git a/js/src/api/format/output.spec.js b/js/src/api/format/output.spec.js index cdf96601e..7b26e4c2e 100644 --- a/js/src/api/format/output.spec.js +++ b/js/src/api/format/output.spec.js @@ -16,7 +16,7 @@ import BigNumber from 'bignumber.js'; -import { outBlock, outAccountInfo, outAddress, outChainStatus, outDate, outHistogram, outNumber, outPeers, outReceipt, outSyncing, outTransaction, outTrace } from './output'; +import { outBlock, outAccountInfo, outAddress, outChainStatus, outDate, outHistogram, outNumber, outPeer, outPeers, outReceipt, outSyncing, outTransaction, outTrace } from './output'; import { isAddress, isBigNumber, isInstanceOf } from '../../../test/types'; describe('api/format/output', () => { @@ -165,6 +165,66 @@ describe('api/format/output', () => { }); }); + describe('outPeer', () => { + it('converts all internal numbers to BigNumbers', () => { + expect(outPeer({ + caps: ['par/1'], + id: '0x01', + name: 'Parity', + network: { + localAddress: '10.0.0.1', + remoteAddress: '10.0.0.1' + }, + protocols: { + par: { + difficulty: '0x0f', + head: '0x02', + version: 63 + } + } + })).to.deep.equal({ + caps: ['par/1'], + id: '0x01', + name: 'Parity', + network: { + localAddress: '10.0.0.1', + remoteAddress: '10.0.0.1' + }, + protocols: { + par: { + difficulty: new BigNumber(15), + head: '0x02', + version: 63 + } + } + }); + }); + + it('does not output null protocols', () => { + expect(outPeer({ + caps: ['par/1'], + id: '0x01', + name: 'Parity', + network: { + localAddress: '10.0.0.1', + remoteAddress: '10.0.0.1' + }, + protocols: { + les: null + } + })).to.deep.equal({ + caps: ['par/1'], + id: '0x01', + name: 'Parity', + network: { + localAddress: '10.0.0.1', + remoteAddress: '10.0.0.1' + }, + protocols: {} + }); + }); + }); + describe('outPeers', () => { it('converts all internal numbers to BigNumbers', () => { expect(outPeers({ @@ -185,7 +245,8 @@ describe('api/format/output', () => { difficulty: '0x0f', head: '0x02', version: 63 - } + }, + les: null } } ]