From f873852516763b0a1f9d60020f762b900d4fd0aa Mon Sep 17 00:00:00 2001 From: adaxi Date: Sun, 18 Feb 2018 21:51:01 +0100 Subject: [PATCH 1/2] Improve error checking in coin functions --- lib/coins/aeon.js | 50 ++++++++++++++++++++++------------------------- lib/coins/krb.js | 50 ++++++++++++++++++++++------------------------- lib/coins/xmr.js | 50 ++++++++++++++++++++++------------------------- 3 files changed, 69 insertions(+), 81 deletions(-) diff --git a/lib/coins/aeon.js b/lib/coins/aeon.js index c68082dc..16e683d1 100644 --- a/lib/coins/aeon.js +++ b/lib/coins/aeon.js @@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs'); let hexChars = new RegExp("[0-9a-f]+"); +function handleRpcError(err, body, callback) { + console.error(JSON.stringify(body)); + callback(new Error(`${err} RPC: ${JSON.stringify(body)}`)) +} + function Coin(data){ this.bestExchange = global.config.payout.bestExchange; this.data = data; @@ -28,37 +33,28 @@ function Coin(data){ this.niceHashDiff = 400000; - this.getBlockHeaderByID = function(blockId, callback){ - global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) { - if (body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getBlockHeaderByHeight = (height, callback) => { // unused + global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) => + (body && body,result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getblockheaderbyheight' to aeon daemon failed to return the block header.`, body, callback) + ); }; - this.getBlockHeaderByHash = function(blockHash, callback){ - global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) { - if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getBlockHeaderByHash = (hash, callback) => { + global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) => + (body && body,result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getblockheaderbyhash' to aeon daemon failed to return the block header.`, body, callback) + ); }; - this.getLastBlockHeader = function(callback){ - global.support.rpcDaemon('getlastblockheader', [], function (body) { - if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getLastBlockHeader = (callback) => { + global.support.rpcDaemon('getlastblockheader', {}, (body) => + (body && body.result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getlastblockheader' to aeon daemon failed to return the block header.`, body, callback) + ); }; this.getBlockTemplate = function(walletAddress, callback){ diff --git a/lib/coins/krb.js b/lib/coins/krb.js index 1494da8f..a88f5212 100644 --- a/lib/coins/krb.js +++ b/lib/coins/krb.js @@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs'); let hexChars = new RegExp("[0-9a-f]+"); +function handleRpcError(err, body, callback) { + console.error(JSON.stringify(body)); + callback(new Error(`${err} RPC: ${JSON.stringify(body)}`)) +} + function Coin(data){ this.bestExchange = global.config.payout.bestExchange; this.data = data; @@ -28,37 +33,28 @@ function Coin(data){ this.niceHashDiff = 200000; - this.getBlockHeaderByID = function(blockId, callback){ - global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) { - if (body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getBlockHeaderByHeight = (height, callback) => { // unused + global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) => + (body && body,result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getblockheaderbyheight' to krb daemon failed to return the block header.`, body, callback) + ); }; - this.getBlockHeaderByHash = function(blockHash, callback){ - global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) { - if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getBlockHeaderByHash = (hash, callback) => { + global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) => + (body && body,result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getblockheaderbyhash' to krb daemon failed to return the block header.`, body, callback) + ); }; - this.getLastBlockHeader = function(callback){ - global.support.rpcDaemon('getlastblockheader', [], function (body) { - if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getLastBlockHeader = (callback) => { + global.support.rpcDaemon('getlastblockheader', {}, (body) => + (body && body.result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getlastblockheader' to krb daemon failed to return the block header.`, body, callback) + ); }; this.getBlockTemplate = function(walletAddress, callback){ diff --git a/lib/coins/xmr.js b/lib/coins/xmr.js index a4521110..b5a2c78e 100644 --- a/lib/coins/xmr.js +++ b/lib/coins/xmr.js @@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs'); let hexChars = new RegExp("[0-9a-f]+"); +function handleRpcError(err, body, callback) { + console.error(JSON.stringify(body)); + callback(new Error(`${err} RPC: ${JSON.stringify(body)}`)) +} + function Coin(data){ this.bestExchange = global.config.payout.bestExchange; this.data = data; @@ -42,37 +47,28 @@ function Coin(data){ this.niceHashDiff = 400000; - this.getBlockHeaderByID = function(blockId, callback){ - global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) { - if (body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getBlockHeaderByHeight = (height, callback) => { // unused + global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) => + (body && body,result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getblockheaderbyheight' to monero daemon failed to return the block header.`, body, callback) + ); }; - this.getBlockHeaderByHash = function(blockHash, callback){ - global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) { - if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getBlockHeaderByHash = (hash, callback) => { + global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) => + (body && body,result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getblockheaderbyhash' to monero daemon failed to return the block header.`, body, callback) + ); }; - this.getLastBlockHeader = function(callback){ - global.support.rpcDaemon('getlastblockheader', [], function (body) { - if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(null, body.result.block_header); - } else { - console.error(JSON.stringify(body)); - return callback(true, body); - } - }); + this.getLastBlockHeader = (callback) => { + global.support.rpcDaemon('getlastblockheader', {}, (body) => + (body && body.result && body.result.status === 'OK' && body.result.block_header) + ? callback(null, body.result.block_header) + : handleRpcError(`RPC 'getlastblockheader' to monero daemon failed to return the block header.`, body, callback) + ); }; this.getBlockTemplate = function(walletAddress, callback){ From 1b5fe049ca2217732bd448727ecf48db08247ecc Mon Sep 17 00:00:00 2001 From: adaxi Date: Sun, 18 Feb 2018 21:56:52 +0100 Subject: [PATCH 2/2] Fix typo --- lib/coins/aeon.js | 4 ++-- lib/coins/krb.js | 4 ++-- lib/coins/xmr.js | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/coins/aeon.js b/lib/coins/aeon.js index 16e683d1..462ccd59 100644 --- a/lib/coins/aeon.js +++ b/lib/coins/aeon.js @@ -35,7 +35,7 @@ function Coin(data){ this.getBlockHeaderByHeight = (height, callback) => { // unused global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) => - (body && body,result && body.result.status === 'OK' && body.result.block_header) + (body && body.result && body.result.status === 'OK' && body.result.block_header) ? callback(null, body.result.block_header) : handleRpcError(`RPC 'getblockheaderbyheight' to aeon daemon failed to return the block header.`, body, callback) ); @@ -43,7 +43,7 @@ function Coin(data){ this.getBlockHeaderByHash = (hash, callback) => { global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) => - (body && body,result && body.result.status === 'OK' && body.result.block_header) + (body && body.result && body.result.status === 'OK' && body.result.block_header) ? callback(null, body.result.block_header) : handleRpcError(`RPC 'getblockheaderbyhash' to aeon daemon failed to return the block header.`, body, callback) ); diff --git a/lib/coins/krb.js b/lib/coins/krb.js index a88f5212..868bd898 100644 --- a/lib/coins/krb.js +++ b/lib/coins/krb.js @@ -35,7 +35,7 @@ function Coin(data){ this.getBlockHeaderByHeight = (height, callback) => { // unused global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) => - (body && body,result && body.result.status === 'OK' && body.result.block_header) + (body && body.result && body.result.status === 'OK' && body.result.block_header) ? callback(null, body.result.block_header) : handleRpcError(`RPC 'getblockheaderbyheight' to krb daemon failed to return the block header.`, body, callback) ); @@ -43,7 +43,7 @@ function Coin(data){ this.getBlockHeaderByHash = (hash, callback) => { global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) => - (body && body,result && body.result.status === 'OK' && body.result.block_header) + (body && body.result && body.result.status === 'OK' && body.result.block_header) ? callback(null, body.result.block_header) : handleRpcError(`RPC 'getblockheaderbyhash' to krb daemon failed to return the block header.`, body, callback) ); diff --git a/lib/coins/xmr.js b/lib/coins/xmr.js index b5a2c78e..07a8086f 100644 --- a/lib/coins/xmr.js +++ b/lib/coins/xmr.js @@ -49,7 +49,7 @@ function Coin(data){ this.getBlockHeaderByHeight = (height, callback) => { // unused global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) => - (body && body,result && body.result.status === 'OK' && body.result.block_header) + (body && body.result && body.result.status === 'OK' && body.result.block_header) ? callback(null, body.result.block_header) : handleRpcError(`RPC 'getblockheaderbyheight' to monero daemon failed to return the block header.`, body, callback) ); @@ -57,7 +57,7 @@ function Coin(data){ this.getBlockHeaderByHash = (hash, callback) => { global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) => - (body && body,result && body.result.status === 'OK' && body.result.block_header) + (body && body.result && body.result.status === 'OK' && body.result.block_header) ? callback(null, body.result.block_header) : handleRpcError(`RPC 'getblockheaderbyhash' to monero daemon failed to return the block header.`, body, callback) );