Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better error checks on RPC response #321

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions lib/coins/aeon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand Down
50 changes: 23 additions & 27 deletions lib/coins/krb.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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){
Expand Down
50 changes: 23 additions & 27 deletions lib/coins/xmr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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){
Expand Down