From be346609b299ec3abdd32aec37c49e53d417c4f2 Mon Sep 17 00:00:00 2001 From: Alexander Blair Date: Mon, 13 Feb 2017 14:55:31 -0800 Subject: [PATCH] blockManager fix for payout verification to properly flag blocks as orphans. --- lib/blockManager.js | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/lib/blockManager.js b/lib/blockManager.js index de4c660..e67c18a 100644 --- a/lib/blockManager.js +++ b/lib/blockManager.js @@ -390,27 +390,6 @@ function calculateSoloPayments(blockHeader) { console.log("Solo payout cycle complete on block: " + blockHeader.height + " Block Value: " + global.support.coinToDecimal(blockHeader.reward) + " Block Payouts: " + global.support.coinToDecimal(totalPayments) + " Payout Percentage: " + (totalPayments / blockHeader.reward) * 100 + "%"); } -function blockInvalidator() { - if (scanInProgress) { - debug("Skipping block invalidator run as there's a scan in progress"); - return; - } - debug("Running block invalidator"); - global.mysql.query("SELECT id, hex FROM block_log ORDER BY find_time DESC LIMIT ?", [global.config.payout.blocksRequired]).then(function (rows) { - rows.forEach(function (row) { - global.support.rpcDaemon('getblockheaderbyheight', {"height": row.id}, function (body) { - // DO NOT TRUST THIS. GET THE BLOCK HEADER BY HEIGHT AND VERIFY THAT THE HASHES MATCH. - if (body.result.block_header.hash !== row.hex) { - global.database.invalidateBlock(row.id); - global.mysql.query("UPDATE block_log SET orphan = true WHERE hex = ?", [row.hex]); - blockIDCache.splice(blockIDCache.indexOf(body.result.block_header.height)); - console.log("Invalidating block " + body.result.block_header.height + " due to being an orphan block"); - } - }); - }); - }); -} - function blockUnlocker() { if (scanInProgress) { debug("Skipping block unlocker run as there's a scan in progress"); @@ -421,9 +400,19 @@ function blockUnlocker() { global.support.rpcDaemon('getlastblockheader', [], function (body) { let blockHeight = body.result.block_header.height; blockList.forEach(function (row) { - if (blockHeight - row.height > global.config.payout.blocksRequired) { - blockPayments(row); - } + global.support.rpcDaemon('getblockheaderbyheight', {"height": row.height}, function (body) { + if (body.result.block_header.hash !== row.hash) { + global.database.invalidateBlock(row.height); + global.mysql.query("UPDATE block_log SET orphan = true WHERE hex = ?", [row.hash]); + blockIDCache.splice(blockIDCache.indexOf(body.result.block_header.height)); + console.log("Invalidating block " + body.result.block_header.height + " due to being an orphan block"); + } else { + if (blockHeight - row.height > global.config.payout.blocksRequired) { + blockPayments(row); + } + } + }); + }); }); } @@ -497,9 +486,7 @@ function initial_sync() { // Enable block scanning for 1 seconds to update the block log. blockScanner(); // Scan every 120 seconds for invalidated blocks - setInterval(blockInvalidator, 120000); setInterval(blockUnlocker, 120000); - blockInvalidator(); blockUnlocker(); debug("Blocks loaded from SQL: " + blockIDCache.length); console.log("Boot-sync from SQL complete. Pending completion of queued jobs to get back to work.");