diff --git a/lib/blockManager.js b/lib/blockManager.js index 02a286a..bea07e2 100644 --- a/lib/blockManager.js +++ b/lib/blockManager.js @@ -421,22 +421,29 @@ function blockPayments(block) { switch (block.poolType) { case global.protos.POOLTYPE.PPS: // PPS is paid out per share find per block, so this is handled in the main block-find loop. + global.database.unlockBlock(block.hash); break; case global.protos.POOLTYPE.PPLNS: - global.coinFuncs.getBlockHeaderByHash(block.hash, function (header) { - calculatePPLNSPayments(header); + global.coinFuncs.getBlockHeaderByHash(block.hash, function (err, header) { + if (err === null){ + calculatePPLNSPayments(header); + global.database.unlockBlock(block.hash); + } }); break; case global.protos.POOLTYPE.SOLO: - global.coinFuncs.getBlockHeaderByHash(block.hash, function (header) { - calculateSoloPayments(header); + global.coinFuncs.getBlockHeaderByHash(block.hash, function (err, header) { + if (err === null){ + calculateSoloPayments(header); + global.database.unlockBlock(block.hash); + } }); break; default: console.log("Unknown payment type. FREAKOUT"); + global.database.unlockBlock(block.hash); break; } - global.database.unlockBlock(block.hash); } function blockScanner() { @@ -446,27 +453,33 @@ function blockScanner() { return; } scanInProgress = true; - global.coinFuncs.getLastBlockHeader(function (blockHeader) { - if (lastBlock === blockHeader.height) { - debug("No new work to be performed, block header matches last block"); - scanInProgress = false; - return; - } - debug("Parsing data for new blocks"); - lastBlock = blockHeader.height; - range.range(0, (blockHeader.height - Math.floor(global.config.payout.blocksRequired/2))).forEach(function (blockID) { - if (!blockIDCache.hasOwnProperty(blockID)) { - inc_check += 1; - blockQueue.push({blockID: blockID}, function (err) { - debug("Completed block scan on " + blockID); - if (err) { - console.error("Error processing " + blockID); - } - }); + global.coinFuncs.getLastBlockHeader(function (err, blockHeader) { + if (err === null){ + if (lastBlock === blockHeader.height) { + debug("No new work to be performed, block header matches last block"); + scanInProgress = false; + return; } - }); - if (inc_check === 0) { - debug("No new work to be performed, initial scan complete"); + debug("Parsing data for new blocks"); + lastBlock = blockHeader.height; + range.range(0, (blockHeader.height - Math.floor(global.config.payout.blocksRequired/2))).forEach(function (blockID) { + if (!blockIDCache.hasOwnProperty(blockID)) { + inc_check += 1; + blockQueue.push({blockID: blockID}, function (err) { + debug("Completed block scan on " + blockID); + if (err) { + console.error("Error processing " + blockID); + } + }); + } + }); + if (inc_check === 0) { + debug("No new work to be performed, initial scan complete"); + scanInProgress = false; + blockScannerTask = setInterval(blockScanner, 1000); + } + } else { + console.error(`Upstream error from the block daemon. Resetting scanner due to: ${JSON.stringify(blockHeader)}`); scanInProgress = false; blockScannerTask = setInterval(blockScanner, 1000); } diff --git a/lib/coins/aeon.js b/lib/coins/aeon.js index d58e877..4d0f934 100644 --- a/lib/coins/aeon.js +++ b/lib/coins/aeon.js @@ -31,9 +31,10 @@ function Coin(data){ this.getBlockHeaderByID = function(blockId, callback){ global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) { if (body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); + callback(true, body); } }); }; @@ -41,10 +42,10 @@ function Coin(data){ this.getBlockHeaderByHash = function(blockHash, callback){ global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) { if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); - return callback(false); + return callback(true, body); } }); }; @@ -52,9 +53,10 @@ function Coin(data){ this.getLastBlockHeader = function(callback){ global.support.rpcDaemon('getlastblockheader', [], function (body) { if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); + return callback(true, body); } }); }; diff --git a/lib/coins/krb.js b/lib/coins/krb.js index e5491ad..1494da8 100644 --- a/lib/coins/krb.js +++ b/lib/coins/krb.js @@ -31,9 +31,10 @@ function Coin(data){ this.getBlockHeaderByID = function(blockId, callback){ global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) { if (body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); + return callback(true, body); } }); }; @@ -41,10 +42,10 @@ function Coin(data){ this.getBlockHeaderByHash = function(blockHash, callback){ global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) { if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); - return callback(false); + return callback(true, body); } }); }; @@ -52,9 +53,10 @@ function Coin(data){ this.getLastBlockHeader = function(callback){ global.support.rpcDaemon('getlastblockheader', [], function (body) { if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); + return callback(true, body); } }); }; diff --git a/lib/coins/xmr.js b/lib/coins/xmr.js index 7ccdc47..ac97e8b 100644 --- a/lib/coins/xmr.js +++ b/lib/coins/xmr.js @@ -45,9 +45,10 @@ function Coin(data){ this.getBlockHeaderByID = function(blockId, callback){ global.support.rpcDaemon('getblockheaderbyheight', {"height": blockId}, function (body) { if (body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); + return callback(true, body); } }); }; @@ -55,10 +56,10 @@ function Coin(data){ this.getBlockHeaderByHash = function(blockHash, callback){ global.support.rpcDaemon('getblockheaderbyhash', {"hash": blockHash}, function (body) { if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); - return callback(false); + return callback(true, body); } }); }; @@ -66,9 +67,10 @@ function Coin(data){ this.getLastBlockHeader = function(callback){ global.support.rpcDaemon('getlastblockheader', [], function (body) { if (typeof(body) !== 'undefined' && body.hasOwnProperty('result')){ - return callback(body.result.block_header); + return callback(null, body.result.block_header); } else { console.error(JSON.stringify(body)); + return callback(true, body); } }); }; @@ -78,7 +80,7 @@ function Coin(data){ reserve_size: 17, wallet_address: walletAddress }, function(body){ - return callback(body); + return callback(err, body); }); }; diff --git a/lib/local_comms.js b/lib/local_comms.js index cb996e4..a6e39b6 100644 --- a/lib/local_comms.js +++ b/lib/local_comms.js @@ -375,8 +375,8 @@ function Database(){ this.refreshEnv(); try{ let blockDataDecoded = global.protos.Block.decode(blockData); - global.coinFuncs.getBlockHeaderByHash(blockDataDecoded.hash, function(header){ - if (typeof(header) === 'undefined' || !header){ + global.coinFuncs.getBlockHeaderByHash(blockDataDecoded.hash, function(err, header){ + if (err || typeof(header) === 'undefined' || !header){ return callback(false); } blockDataDecoded.value = header.reward; @@ -409,12 +409,14 @@ function Database(){ let blockData = txn.getBinary(global.database.blockDB, blockId); txn.abort(); let blockDataDecoded = global.protos.Block.decode(blockData); - global.coinFuncs.getBlockHeaderByHash(blockDataDecoded.hash, function(header){ - blockDataDecoded.value = header.reward; - blockData = global.database.calculateShares(blockDataDecoded, blockId); - let txn = global.database.env.beginTxn(); - txn.putBinary(global.database.blockDB, blockId, blockData); - txn.commit(); + global.coinFuncs.getBlockHeaderByHash(blockDataDecoded.hash, function(err, header){ + if (err === null) { + blockDataDecoded.value = header.reward; + blockData = global.database.calculateShares(blockDataDecoded, blockId); + let txn = global.database.env.beginTxn(); + txn.putBinary(global.database.blockDB, blockId, blockData); + txn.commit(); + } }); }; diff --git a/lib/pool.js b/lib/pool.js index 41f470d..23d402b 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -155,8 +155,8 @@ function checkAliveMiners() { } function templateUpdate(repeating) { - global.coinFuncs.getBlockTemplate(global.config.pool.address, function (rpcResponse) { - if (rpcResponse) { + global.coinFuncs.getBlockTemplate(global.config.pool.address, function (err, rpcResponse) { + if (rpcResponse && typeof rpcResponse.result !== 'undefined') { rpcResponse = rpcResponse.result; let buffer = new Buffer(rpcResponse.blocktemplate_blob, 'hex'); let new_hash = new Buffer(32); @@ -174,6 +174,8 @@ function templateUpdate(repeating) { if (repeating !== false) { setTimeout(templateUpdate, 300); } + } else { + setTimeout(templateUpdate, 300); } }); } diff --git a/lib/worker.js b/lib/worker.js index 941c347..8589dcb 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -16,7 +16,10 @@ function updateShareStats() { let activeAddresses = []; async.waterfall([ function (callback) { - global.coinFuncs.getLastBlockHeader(function (body) { + global.coinFuncs.getLastBlockHeader(function (err, body) { + if (err !== null){ + return callback(err, "Invalid block header"); + } callback(null, body.height + 1); }); }, @@ -493,8 +496,12 @@ function updateWalletStats() { } function monitorNodes() { - global.mysql.query("SELECT blockID FROM pools WHERE last_checkin > date_sub(now(), interval 30 minute)").then(function (rows) { - global.coinFuncs.getLastBlockHeader(function (block) { + global.mysql.query("SELECT blockID, hostname, ip FROM pools WHERE last_checkin > date_sub(now(), interval 30 minute)").then(function (rows) { + global.coinFuncs.getLastBlockHeader(function (err, block) { + if (err !== null){ + console.error("Issue in getting block header. Skipping node monitor"); + return; + } rows.forEach(function (row) { if (row.blockID < block.height - 3) { global.support.sendEmail(global.config.general.adminEmail, "Pool server behind in blocks", "The pool server: "+row.hostname+" with IP: "+row.ip+" is "+block.height - row.blockID+ " blocks behind");