From d89ee5432e0d5825606436ad91b6fb5c286b5113 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 20 Dec 2016 12:12:39 +0100 Subject: [PATCH] JS API interface for parity_chainStatus --- js/src/api/format/output.js | 15 +++++++++++++++ js/src/api/format/output.spec.js | 14 +++++++++++++- js/src/api/rpc/parity/parity.js | 8 +++++++- js/src/api/rpc/parity/parity.spec.js | 17 +++++++++++++++++ js/src/jsonrpc/interfaces/parity.js | 16 ++++++++++++++++ 5 files changed, 68 insertions(+), 2 deletions(-) diff --git a/js/src/api/format/output.js b/js/src/api/format/output.js index ca1d54ede..e1887582a 100644 --- a/js/src/api/format/output.js +++ b/js/src/api/format/output.js @@ -66,6 +66,20 @@ export function outBlock (block) { return block; } +export function outChainStatus (status) { + if (status) { + Object.keys(status).forEach((key) => { + switch (key) { + case 'blockGap': + status[key] = status[key].map(outNumber); + break; + } + }); + } + + return status; +} + export function outDate (date) { return new Date(outNumber(date).toNumber() * 1000); } @@ -77,6 +91,7 @@ export function outHistogram (histogram) { case 'bucketBounds': case 'counts': histogram[key] = histogram[key].map(outNumber); + break; } }); } diff --git a/js/src/api/format/output.spec.js b/js/src/api/format/output.spec.js index 1958b57d8..ce50c69c1 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, outDate, outHistogram, outNumber, outPeers, outReceipt, outSyncing, outTransaction, outTrace } from './output'; +import { outBlock, outAccountInfo, outAddress, outChainStatus, outDate, outHistogram, outNumber, outPeers, outReceipt, outSyncing, outTransaction, outTrace } from './output'; import { isAddress, isBigNumber, isInstanceOf } from '../../../test/types'; describe('api/format/output', () => { @@ -114,6 +114,18 @@ describe('api/format/output', () => { }); }); + describe('outChainStatus', () => { + it('formats blockGap values', () => { + const status = { + blockGap: [0x1234, '0x5678'] + }; + + expect(outChainStatus(status)).to.deep.equal({ + blockGap: [new BigNumber(0x1234), new BigNumber(0x5678)] + }); + }); + }); + describe('outDate', () => { it('converts a second date in unix timestamp', () => { expect(outDate(0x57513668)).to.deep.equal(new Date('2016-06-03T07:48:56.000Z')); diff --git a/js/src/api/rpc/parity/parity.js b/js/src/api/rpc/parity/parity.js index 2605e41e5..816081643 100644 --- a/js/src/api/rpc/parity/parity.js +++ b/js/src/api/rpc/parity/parity.js @@ -15,7 +15,7 @@ // along with Parity. If not, see . import { inAddress, inData, inHex, inNumber16, inOptions } from '../../format/input'; -import { outAccountInfo, outAddress, outHistogram, outNumber, outPeers, outTransaction } from '../../format/output'; +import { outAccountInfo, outAddress, outChainStatus, outHistogram, outNumber, outPeers, outTransaction } from '../../format/output'; export default class Parity { constructor (transport) { @@ -44,6 +44,12 @@ export default class Parity { .execute('parity_addReservedPeer', encode); } + chainStatus () { + return this._transport + .execute('parity_chainStatus') + .then(outChainStatus); + } + changePassword (account, password, newPassword) { return this._transport .execute('parity_changePassword', inAddress(account), password, newPassword); diff --git a/js/src/api/rpc/parity/parity.spec.js b/js/src/api/rpc/parity/parity.spec.js index b58c8f85c..6b15a8fe1 100644 --- a/js/src/api/rpc/parity/parity.spec.js +++ b/js/src/api/rpc/parity/parity.spec.js @@ -14,6 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . +import BigNumber from 'bignumber.js'; import { TEST_HTTP_URL, mockHttp } from '../../../../test/mockRpc'; import { isBigNumber } from '../../../../test/types'; @@ -45,6 +46,22 @@ describe('api/rpc/parity', () => { }); }); + describe('chainStatus', () => { + it('retrieves the chain status', () => { + mockHttp([{ method: 'parity_chainStatus', reply: { + result: { + 'blockGap': [0x123, 0x456] + } + } }]); + + return instance.chainStatus().then((result) => { + expect(result).to.deep.equal({ + 'blockGap': [new BigNumber(0x123), new BigNumber(0x456)] + }); + }); + }); + }); + describe('gasFloorTarget', () => { it('returns the gasfloor, formatted', () => { mockHttp([{ method: 'parity_gasFloorTarget', reply: { result: '0x123456' } }]); diff --git a/js/src/jsonrpc/interfaces/parity.js b/js/src/jsonrpc/interfaces/parity.js index 3f36a93b3..db194a913 100644 --- a/js/src/jsonrpc/interfaces/parity.js +++ b/js/src/jsonrpc/interfaces/parity.js @@ -86,6 +86,22 @@ export default { } }, + chainStatus: { + desc: 'Returns the information on warp sync blocks', + params: [], + returns: { + type: Object, + desc: 'The status object', + details: { + blockGap: { + type: Array, + desc: 'Describes the gap in the blockchain, if there is one: (first, last)', + optional: true + } + } + } + }, + checkRequest: { desc: 'Returns the transactionhash of the requestId (received from parity_postTransaction) if the request was confirmed', params: [