Skip to content

Commit f873852

Browse files
committed
Improve error checking in coin functions
1 parent 43ed584 commit f873852

3 files changed

Lines changed: 69 additions & 81 deletions

File tree

lib/coins/aeon.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs');
77

88
let hexChars = new RegExp("[0-9a-f]+");
99

10+
function handleRpcError(err, body, callback) {
11+
console.error(JSON.stringify(body));
12+
callback(new Error(`${err} RPC: ${JSON.stringify(body)}`))
13+
}
14+
1015
function Coin(data){
1116
this.bestExchange = global.config.payout.bestExchange;
1217
this.data = data;
@@ -28,37 +33,28 @@ function Coin(data){
2833

2934
this.niceHashDiff = 400000;
3035

31-
this.getBlockHeaderByID = function(blockId, callback){
32-
global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) {
33-
if (body.hasOwnProperty('result')){
34-
return callback(null, body.result.block_header);
35-
} else {
36-
console.error(JSON.stringify(body));
37-
return callback(true, body);
38-
}
39-
});
36+
this.getBlockHeaderByHeight = (height, callback) => { // unused
37+
global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) =>
38+
(body && body,result && body.result.status === 'OK' && body.result.block_header)
39+
? callback(null, body.result.block_header)
40+
: handleRpcError(`RPC 'getblockheaderbyheight' to aeon daemon failed to return the block header.`, body, callback)
41+
);
4042
};
4143

42-
this.getBlockHeaderByHash = function(blockHash, callback){
43-
global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) {
44-
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
45-
return callback(null, body.result.block_header);
46-
} else {
47-
console.error(JSON.stringify(body));
48-
return callback(true, body);
49-
}
50-
});
44+
this.getBlockHeaderByHash = (hash, callback) => {
45+
global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) =>
46+
(body && body,result && body.result.status === 'OK' && body.result.block_header)
47+
? callback(null, body.result.block_header)
48+
: handleRpcError(`RPC 'getblockheaderbyhash' to aeon daemon failed to return the block header.`, body, callback)
49+
);
5150
};
5251

53-
this.getLastBlockHeader = function(callback){
54-
global.support.rpcDaemon('getlastblockheader', [], function (body) {
55-
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
56-
return callback(null, body.result.block_header);
57-
} else {
58-
console.error(JSON.stringify(body));
59-
return callback(true, body);
60-
}
61-
});
52+
this.getLastBlockHeader = (callback) => {
53+
global.support.rpcDaemon('getlastblockheader', {}, (body) =>
54+
(body && body.result && body.result.status === 'OK' && body.result.block_header)
55+
? callback(null, body.result.block_header)
56+
: handleRpcError(`RPC 'getlastblockheader' to aeon daemon failed to return the block header.`, body, callback)
57+
);
6258
};
6359

6460
this.getBlockTemplate = function(walletAddress, callback){

lib/coins/krb.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs');
77

88
let hexChars = new RegExp("[0-9a-f]+");
99

10+
function handleRpcError(err, body, callback) {
11+
console.error(JSON.stringify(body));
12+
callback(new Error(`${err} RPC: ${JSON.stringify(body)}`))
13+
}
14+
1015
function Coin(data){
1116
this.bestExchange = global.config.payout.bestExchange;
1217
this.data = data;
@@ -28,37 +33,28 @@ function Coin(data){
2833

2934
this.niceHashDiff = 200000;
3035

31-
this.getBlockHeaderByID = function(blockId, callback){
32-
global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) {
33-
if (body.hasOwnProperty('result')){
34-
return callback(null, body.result.block_header);
35-
} else {
36-
console.error(JSON.stringify(body));
37-
return callback(true, body);
38-
}
39-
});
36+
this.getBlockHeaderByHeight = (height, callback) => { // unused
37+
global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) =>
38+
(body && body,result && body.result.status === 'OK' && body.result.block_header)
39+
? callback(null, body.result.block_header)
40+
: handleRpcError(`RPC 'getblockheaderbyheight' to krb daemon failed to return the block header.`, body, callback)
41+
);
4042
};
4143

42-
this.getBlockHeaderByHash = function(blockHash, callback){
43-
global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) {
44-
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
45-
return callback(null, body.result.block_header);
46-
} else {
47-
console.error(JSON.stringify(body));
48-
return callback(true, body);
49-
}
50-
});
44+
this.getBlockHeaderByHash = (hash, callback) => {
45+
global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) =>
46+
(body && body,result && body.result.status === 'OK' && body.result.block_header)
47+
? callback(null, body.result.block_header)
48+
: handleRpcError(`RPC 'getblockheaderbyhash' to krb daemon failed to return the block header.`, body, callback)
49+
);
5150
};
5251

53-
this.getLastBlockHeader = function(callback){
54-
global.support.rpcDaemon('getlastblockheader', [], function (body) {
55-
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
56-
return callback(null, body.result.block_header);
57-
} else {
58-
console.error(JSON.stringify(body));
59-
return callback(true, body);
60-
}
61-
});
52+
this.getLastBlockHeader = (callback) => {
53+
global.support.rpcDaemon('getlastblockheader', {}, (body) =>
54+
(body && body.result && body.result.status === 'OK' && body.result.block_header)
55+
? callback(null, body.result.block_header)
56+
: handleRpcError(`RPC 'getlastblockheader' to krb daemon failed to return the block header.`, body, callback)
57+
);
6258
};
6359

6460
this.getBlockTemplate = function(walletAddress, callback){

lib/coins/xmr.js

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const debug = require('debug')('coinFuncs');
77

88
let hexChars = new RegExp("[0-9a-f]+");
99

10+
function handleRpcError(err, body, callback) {
11+
console.error(JSON.stringify(body));
12+
callback(new Error(`${err} RPC: ${JSON.stringify(body)}`))
13+
}
14+
1015
function Coin(data){
1116
this.bestExchange = global.config.payout.bestExchange;
1217
this.data = data;
@@ -42,37 +47,28 @@ function Coin(data){
4247

4348
this.niceHashDiff = 400000;
4449

45-
this.getBlockHeaderByID = function(blockId, callback){
46-
global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) {
47-
if (body.hasOwnProperty('result')){
48-
return callback(null, body.result.block_header);
49-
} else {
50-
console.error(JSON.stringify(body));
51-
return callback(true, body);
52-
}
53-
});
50+
this.getBlockHeaderByHeight = (height, callback) => { // unused
51+
global.support.rpcDaemon('getblockheaderbyheight', { height }, (body) =>
52+
(body && body,result && body.result.status === 'OK' && body.result.block_header)
53+
? callback(null, body.result.block_header)
54+
: handleRpcError(`RPC 'getblockheaderbyheight' to monero daemon failed to return the block header.`, body, callback)
55+
);
5456
};
5557

56-
this.getBlockHeaderByHash = function(blockHash, callback){
57-
global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) {
58-
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
59-
return callback(null, body.result.block_header);
60-
} else {
61-
console.error(JSON.stringify(body));
62-
return callback(true, body);
63-
}
64-
});
58+
this.getBlockHeaderByHash = (hash, callback) => {
59+
global.support.rpcDaemon('getblockheaderbyhash', { hash }, (body) =>
60+
(body && body,result && body.result.status === 'OK' && body.result.block_header)
61+
? callback(null, body.result.block_header)
62+
: handleRpcError(`RPC 'getblockheaderbyhash' to monero daemon failed to return the block header.`, body, callback)
63+
);
6564
};
6665

67-
this.getLastBlockHeader = function(callback){
68-
global.support.rpcDaemon('getlastblockheader', [], function (body) {
69-
if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){
70-
return callback(null, body.result.block_header);
71-
} else {
72-
console.error(JSON.stringify(body));
73-
return callback(true, body);
74-
}
75-
});
66+
this.getLastBlockHeader = (callback) => {
67+
global.support.rpcDaemon('getlastblockheader', {}, (body) =>
68+
(body && body.result && body.result.status === 'OK' && body.result.block_header)
69+
? callback(null, body.result.block_header)
70+
: handleRpcError(`RPC 'getlastblockheader' to monero daemon failed to return the block header.`, body, callback)
71+
);
7672
};
7773

7874
this.getBlockTemplate = function(walletAddress, callback){

0 commit comments

Comments
 (0)