Fix wrong output format of peers (#4270) (#4442)

* Fix wrong output format of peers

* Add outPeer tests
This commit is contained in:
Jaco Greeff 2017-02-06 09:53:22 +01:00 committed by GitHub
parent ab2d02bbb6
commit c2ceefbc22
2 changed files with 83 additions and 3 deletions

View File

@ -129,12 +129,31 @@ export function outNumber (number) {
return new BigNumber(number || 0); 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) { export function outPeers (peers) {
return { return {
active: outNumber(peers.active), active: outNumber(peers.active),
connected: outNumber(peers.connected), connected: outNumber(peers.connected),
max: outNumber(peers.max), 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))
}; };
} }

View File

@ -16,7 +16,7 @@
import BigNumber from 'bignumber.js'; 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'; import { isAddress, isBigNumber, isInstanceOf } from '../../../test/types';
describe('api/format/output', () => { 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', () => { describe('outPeers', () => {
it('converts all internal numbers to BigNumbers', () => { it('converts all internal numbers to BigNumbers', () => {
expect(outPeers({ expect(outPeers({
@ -185,7 +245,8 @@ describe('api/format/output', () => {
difficulty: '0x0f', difficulty: '0x0f',
head: '0x02', head: '0x02',
version: 63 version: 63
} },
les: null
} }
} }
] ]