From f50c51d77cda780d822d012f84b967e69668a2b7 Mon Sep 17 00:00:00 2001 From: joeuhren <46763106+joeuhren@users.noreply.github.com> Date: Sat, 5 Dec 2020 12:39:36 -0700 Subject: [PATCH] Improved long index sync warning functionality --- lib/database.js | 4 ++++ lib/locale.js | 4 ++-- locale/en.json | 2 +- routes/index.js | 36 ++++++++++++++----------------- scripts/sync.js | 55 ++++++++++++++++++++++++++++++++++++++++++++---- views/index.pug | 10 +++------ views/layout.pug | 7 ++++++ 7 files changed, 84 insertions(+), 34 deletions(-) diff --git a/lib/database.js b/lib/database.js index 77aa2d6..f5df620 100644 --- a/lib/database.js +++ b/lib/database.js @@ -246,6 +246,10 @@ module.exports = { }); }, + check_show_sync_message: function() { + return fs.existsSync('./show_sync_message.tmp'); + }, + update_label: function(hash, message, cb){ find_address(hash, false, function(address){ if (address){ diff --git a/lib/locale.js b/lib/locale.js index 58ebad8..dd71613 100644 --- a/lib/locale.js +++ b/lib/locale.js @@ -21,7 +21,7 @@ exports.ex_search_title = "Search", exports.ex_search_button = "Search", exports.ex_search_message = "You may enter a block height, block hash ,tx hash or address.", exports.ex_error = "Error!", -exports.ex_warning = "Warning:", +exports.ex_warning = "Warning", exports.ex_search_error = "Search found no results.", exports.ex_latest_transactions = "Latest Transactions", exports.ex_summary = "Block Summary", @@ -57,7 +57,7 @@ exports.new_coins = "New Coins", exports.proof_of_stake = "PoS", exports.hidden_sender = "Hidden Sender", exports.hidden_recipient = "Hidden Recipient", -exports.initial_index_alert = "Indexing is currently incomplete, functionality is limited until index is up-to-date.", +exports.initial_index_alert = "Blockchain data is currently being synchronized. You may browse the site during this time, but keep in mind that data may not yet be fully accurate and some functionality may not work until synchronization is complete.", exports.a_menu_showing = "Showing", exports.a_menu_txs = "transactions", diff --git a/locale/en.json b/locale/en.json index a5cd556..464ab33 100644 --- a/locale/en.json +++ b/locale/en.json @@ -59,7 +59,7 @@ "proof_of_stake": "PoS", "hidden_sender": "Hidden Sender", "hidden_recipient": "Hidden Recipient", - "initial_index_alert": "Indexing is currently incomplete, functionality is limited until index is up-to-date.", + "initial_index_alert": "Blockchain data is currently being synchronized. You may browse the site during this time, but keep in mind that data may not yet be fully accurate and some functionality may not work until synchronization is complete.", //address menu "a_menu_showing": "Showing last", diff --git a/routes/index.js b/routes/index.js index 858cf4a..25e0c22 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,16 +10,16 @@ function route_get_block(res, blockhash) { lib.get_block(blockhash, function (block) { if (block != 'There was an error. Check your console.') { if (blockhash == settings.genesis_block) { - res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: 'GENESIS'}); + res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: 'GENESIS', showSync: db.check_show_sync_message()}); } else { db.get_txs(block, function(txs) { if (txs.length > 0) { - res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: txs}); + res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: txs, showSync: db.check_show_sync_message()}); } else { db.create_txs(block, function(){ db.get_txs(block, function(ntxs) { if (ntxs.length > 0) { - res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: ntxs}); + res.render('block', { active: 'block', block: block, confirmations: settings.confirmations, txs: ntxs, showSync: db.check_show_sync_message()}); } else { route_get_index(res, 'Block not found: ' + blockhash); } @@ -53,7 +53,7 @@ function route_get_tx(res, txid) { db.get_tx(txid, function(tx) { if (tx) { lib.get_blockcount(function(blockcount) { - res.render('tx', { active: 'tx', tx: tx, confirmations: settings.confirmations, blockcount: blockcount}); + res.render('tx', { active: 'tx', tx: tx, confirmations: settings.confirmations, blockcount: blockcount, showSync: db.check_show_sync_message()}); }); } else { @@ -72,7 +72,7 @@ function route_get_tx(res, txid) { blockhash: '-', blockindex: -1, }; - res.render('tx', { active: 'tx', tx: utx, confirmations: settings.confirmations, blockcount:-1}); + res.render('tx', { active: 'tx', tx: utx, confirmations: settings.confirmations, blockcount:-1, showSync: db.check_show_sync_message()}); } else { var utx = { txid: rtx.txid, @@ -84,7 +84,7 @@ function route_get_tx(res, txid) { blockindex: rtx.blockheight, }; lib.get_blockcount(function(blockcount) { - res.render('tx', { active: 'tx', tx: utx, confirmations: settings.confirmations, blockcount: blockcount}); + res.render('tx', { active: 'tx', tx: utx, confirmations: settings.confirmations, blockcount: blockcount, showSync: db.check_show_sync_message()}); }); } }); @@ -100,20 +100,14 @@ function route_get_tx(res, txid) { } function route_get_index(res, error) { - db.is_locked(function(locked) { - if (locked) { - res.render('index', { active: 'home', error: error, warning: locale.initial_index_alert}); - } else { - res.render('index', { active: 'home', error: error, warning: null}); - } - }); + res.render('index', { active: 'home', error: error, showSync: db.check_show_sync_message()}); } function route_get_address(res, hash, count) { db.get_address(hash, false, function(address) { if (address) { var txs = []; - res.render('address', { active: 'address', address: address, txs: txs}); + res.render('address', { active: 'address', address: address, txs: txs, showSync: db.check_show_sync_message()}); } else { route_get_index(res, hash + ' not found'); } @@ -123,7 +117,7 @@ function route_get_address(res, hash, count) { function route_get_claim_form(res, hash){ db.get_address(hash, false, function(address) { if (address) { - res.render("claim_address", { active: "address", address: address}); + res.render("claim_address", { active: "address", address: address, showSync: db.check_show_sync_message()}); } else { route_get_index(res, hash + ' not found'); } @@ -136,7 +130,7 @@ router.get('/', function(req, res) { }); router.get('/info', function(req, res) { - res.render('info', { active: 'info', address: settings.address, hashes: settings.api }); + res.render('info', { active: 'info', address: settings.address, hashes: settings.api, showSync: db.check_show_sync_message() }); }); router.get('/markets/:market', function(req, res) { @@ -152,7 +146,8 @@ router.get('/markets/:market', function(req, res) { exchange: settings.markets.exchange, data: data, }, - market: market + market: market, + showSync: db.check_show_sync_message() }); }); } else { @@ -181,6 +176,7 @@ router.get('/richlist', function(req, res) { show_dist: settings.richlist.distribution, show_received: settings.richlist.received, show_balance: settings.richlist.balance, + showSync: db.check_show_sync_message() }); }); } else { @@ -194,11 +190,11 @@ router.get('/richlist', function(req, res) { }); router.get('/movement', function(req, res) { - res.render('movement', {active: 'movement', flaga: settings.movement.low_flag, flagb: settings.movement.high_flag, min_amount:settings.movement.min_amount}); + res.render('movement', {active: 'movement', flaga: settings.movement.low_flag, flagb: settings.movement.high_flag, min_amount:settings.movement.min_amount, showSync: db.check_show_sync_message()}); }); router.get('/network', function(req, res) { - res.render('network', {active: 'network'}); + res.render('network', {active: 'network', showSync: db.check_show_sync_message()}); }); router.get('/reward', function(req, res){ @@ -217,7 +213,7 @@ router.get('/reward', function(req, res){ } }); - res.render('reward', { active: 'reward', stats: stats, heavy: heavy, votes: votes }); + res.render('reward', { active: 'reward', stats: stats, heavy: heavy, votes: votes, showSync: db.check_show_sync_message() }); }); }); }); diff --git a/scripts/sync.js b/scripts/sync.js index e796c77..cdd6032 100644 --- a/scripts/sync.js +++ b/scripts/sync.js @@ -269,10 +269,19 @@ if (database == 'peers') { }, function() { console.log('index cleared (reindex)'); }); + + // Check if there are more than 1000 blocks to index + var showSync = check_show_sync_message(stats.count); + db.update_tx_db(settings.coin, 1, stats.count, stats.txes, settings.update_timeout, function(){ db.update_richlist('received', function(){ db.update_richlist('balance', function(){ db.get_stats(settings.coin, function(nstats){ + // Check if the sync msg was showing + if (showSync) { + // Remove the sync msg + remove_sync_message(); + } console.log('reindex complete (block: %s)', nstats.last); exit(); }); @@ -293,17 +302,25 @@ if (database == 'peers') { }); } else if (mode == 'update') { // Lookup the last block index - Tx.findOne({}, {blockindex:1}).sort({blockindex:-1}).limit(1).exec(function(err, data){ - var nLast = stats.last; + Tx.findOne({}, {blockindex:1}).sort({blockindex:-1}).limit(1).exec(function(err, data) { + var last = stats.last; if (!err && data) { // start from the last block index - nLast = data.blockindex; + last = data.blockindex; } - db.update_tx_db(settings.coin, nLast, stats.count, stats.txes, settings.update_timeout, function(){ + // Check if there are more than 1000 blocks to index + var showSync = check_show_sync_message(stats.count - last); + + db.update_tx_db(settings.coin, last, stats.count, stats.txes, settings.update_timeout, function(){ db.update_richlist('received', function(){ db.update_richlist('balance', function(){ db.get_stats(settings.coin, function(nstats){ + // Check if the sync msg was showing + if (showSync) { + // Remove the sync msg + remove_sync_message(); + } console.log('update complete (block: %s)', nstats.last); exit(); }); @@ -381,6 +398,36 @@ if (database == 'peers') { }); } +function check_show_sync_message(blocks_to_sync) { + var retVal = false; + var filePath = './show_sync_message.tmp'; + // Check if there are more than 1000 blocks to index + if (blocks_to_sync > 1000) { + // Check if the show sync stub file already exists + if (!fs.existsSync(filePath)) { + // File doesn't exist, so create it now + fs.writeFileSync(filePath, ''); + } + + retVal = true; + } + + return retVal; +} + +function remove_sync_message() { + var filePath = './show_sync_message.tmp'; + // Check if the show sync stub file exists + if (fs.existsSync(filePath)) { + // File exists, so delete it now + try { + fs.unlinkSync(filePath); + } catch (err) { + console.log(err); + } + } +} + function get_last_usd_price() { // Get the last usd price for coinstats db.get_last_usd_price(function(retVal) { exit(); }); diff --git a/views/index.pug b/views/index.pug index 7eb367f..d710a5c 100644 --- a/views/index.pug +++ b/views/index.pug @@ -55,16 +55,12 @@ block content rtable.api().ajax.reload(null, false); }, 60000 ); }); - .row - .col-md-12 - if error !== null + if error !== null + .row + .col-md-12 .alert.alert-danger.alert-dismissable(role='alert') button.close(type='button', data-dismiss='alert') × strong #{settings.locale.ex_error} : #{error} - if warning !== null - .alert.alert-warning.alert-dismissable(role='alert') - button.close(type='button', data-dismiss='alert') × - strong #{settings.locale.ex_warning} #{warning} .col-md-12.cardSpacer .card.card-default.cardSpacer .card-header diff --git a/views/layout.pug b/views/layout.pug index 8264671..82f6fef 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -230,6 +230,13 @@ html a.nav-link(href='/info') span.fa.fa-info-circle span.menu-text #{settings.locale.menu_api} + if showSync != null && showSync == true + .col-lg-12 + .alert.alert-warning + .cardSpacer + span.fas.fa-exclamation-triangle(style='margin-right:5px') + strong #{settings.locale.ex_warning} + div #{settings.locale.initial_index_alert} .col-lg-12 .row.text-center.d-flex.justify-content-center div(class="col-lg-2 col-lg-offset-" + settings.paneloffset)