From 57b6dd72a93f82655baf62886a92d0ad4b1dc7b8 Mon Sep 17 00:00:00 2001 From: Joe Uhren Date: Fri, 24 Jun 2022 19:43:02 -0600 Subject: [PATCH] Fix network difficulty chart for hybrid coins -Difficulty chart would fail to populate if the shared_pages.difficulty setting was 'Hybrid' and is now handled properly. The chart now supports 2 datasets: POW and POS -NetworkHistory.difficulty field was renamed to difficulty_pow and a new difficulty_pos field was added -The shared_pages.page_header.network_charts.difficulty_chart.line_color setting was renamed to pow_line_color -The shared_pages.page_header.network_charts.difficulty_chart.fill_color setting was renamed to pow_fill_color NOTE: If you have existing NetworkHistory data from before this update, it will be automatically assumed to be POW. If it was actualy POS, you will need to either manually fix the data in mongo or simply wait a couple hours while syncing to eventually push the old data out of the chart --- lib/database.js | 84 ++++++++++++++++++++++++++++++++-------- lib/settings.js | 26 +++++++++---- models/networkhistory.js | 3 +- settings.json.template | 24 ++++++++---- views/layout.pug | 65 ++++++++++++++++++++++++++----- 5 files changed, 159 insertions(+), 43 deletions(-) diff --git a/lib/database.js b/lib/database.js index 3af786a..5ccd6e8 100644 --- a/lib/database.js +++ b/lib/database.js @@ -205,6 +205,22 @@ function check_add_db_field(model_obj, field_name, default_value, cb) { }); } +function check_rename_db_field(model_obj, old_field_name, new_field_name, cb) { + // determine if a particular field exists in a db collection + model_obj.findOne({[old_field_name]: {$exists: false}}, function(err, model_data) { + // check if old field exists + if (model_data) { + // rename field + model_obj.updateMany({}, { + $rename: { [old_field_name]: new_field_name } + }, { multi: true, strict: false }, function() { + return cb(true); + }); + } else + return cb(false); + }); +} + function hex_to_ascii(hex) { var str = ''; for (var i = 0; i < hex.length; i += 2) @@ -935,11 +951,28 @@ module.exports = { lib.get_hashrate(function(hashrate) { // lookup network difficulty lib.get_difficulty(function(difficulty) { + var difficultyPOW = 0; + var difficultyPOS = 0; + + if (difficulty && difficulty['proof-of-work']) { + if (settings.shared_pages.difficulty == 'Hybrid') { + difficultyPOS = difficulty['proof-of-stake']; + difficultyPOW = difficulty['proof-of-work']; + } else if (settings.shared_pages.difficulty == 'POW') + difficultyPOW = difficulty['proof-of-work']; + else + difficultyPOS = difficulty['proof-of-stake']; + } else if (settings.shared_pages.difficulty == 'POW') + difficultyPOW = difficulty; + else + difficultyPOS = difficulty; + // create a new network history record var newNetworkHistory = new NetworkHistory({ blockindex: height, nethash: (hashrate == null || hashrate == '-' ? 0 : hashrate), - difficulty: difficulty + difficulty_pow: difficultyPOW, + difficulty_pos: difficultyPOS, }); // save the new network history record @@ -1541,6 +1574,22 @@ module.exports = { }); }, + check_networkhistory: function(cb) { + NetworkHistory.findOne({}, function(err, networkhistory) { + if (networkhistory) { + // collection has data + // determine if the difficulty field exists + check_rename_db_field(NetworkHistory, 'difficulty', 'difficulty_pow', function(renamed) { + // determine if difficulty_pos field exists + check_add_db_field(NetworkHistory, 'difficulty_pos', 0, function(exists) { + return cb(true); + }); + }); + } else + return cb(false); + }); + }, + initialize_data_startup: function(cb) { console.log('Initializing database.. Please wait...'); @@ -1562,23 +1611,26 @@ module.exports = { module.exports.check_txes(function(txes_exists) { // add new field(s) to masternode collection if missing module.exports.check_masternodes(function(masternodes_exists) { - // check if richlist collection is initialized - module.exports.check_richlist(settings.coin.name, function(richlist_exists) { - skip = true; + // add new field(s) and/or rename old field(s) in networkhistory collection if applicable + module.exports.check_networkhistory(function(networkhistory_exists) { + // check if richlist collection is initialized + module.exports.check_richlist(settings.coin.name, function(richlist_exists) { + skip = true; - // determine if richlist collection already exists - if (richlist_exists == false) { - console.log('No richlist entry found. Creating new entry now..'); - skip = false; - } + // determine if richlist collection already exists + if (richlist_exists == false) { + console.log('No richlist entry found. Creating new entry now..'); + skip = false; + } - // initialize the richlist collection - module.exports.create_richlist(settings.coin.name, skip, function() { - // check and initialize the heavycoin collection - init_heavy(function() { - // finished initializing startup data - console.log('Database initialization complete'); - return cb(); + // initialize the richlist collection + module.exports.create_richlist(settings.coin.name, skip, function() { + // check and initialize the heavycoin collection + init_heavy(function() { + // finished initializing startup data + console.log('Database initialization complete'); + return cb(); + }); }); }); }); diff --git a/lib/settings.js b/lib/settings.js index 1f277a1..2e0b881 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -311,14 +311,22 @@ exports.shared_pages = { // Set this to any valid html color // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" "bgcolor": "#ffffff", - // line_color: Change the line color of the network difficulty chart - // Set this to any valid html color - // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" - "line_color": "rgba(255, 99, 132, 1)", - // fill_color: Change the fill color of the network difficulty chart - // Set this to any valid html color - // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" - "fill_color": "rgba(255, 99, 132, 0.2)", + // pow_line_color: Change the line color of the network difficulty chart for POW coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pow_line_color": "rgba(255, 99, 132, 1)", + // pow_fill_color: Change the fill color of the network difficulty chart for POW coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pow_fill_color": "rgba(255, 99, 132, 0.2)", + // pos_line_color: Change the line color of the network difficulty chart for POS coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pos_line_color": "rgba(255, 161, 0, 1)", + // pos_fill_color: Change the fill color of the network difficulty chart for POS coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pos_fill_color": "rgba(255, 161, 0, 0.2)", // crosshair_color: Change the vertical crosshair line color of the network difficulty chart // Set this to any valid html color // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" @@ -1727,6 +1735,8 @@ exports.loadSettings = function loadSettings() { // delete old setting delete json_settings.shared_pages.favicon; } + json_settings = fix_deprecated_setting(json_settings, 'shared_pages.page_header.network_charts.difficulty_chart.line_color', 'shared_pages.page_header.network_charts.difficulty_chart.pow_line_color'); + json_settings = fix_deprecated_setting(json_settings, 'shared_pages.page_header.network_charts.difficulty_chart.fill_color', 'shared_pages.page_header.network_charts.difficulty_chart.pow_fill_color'); // loop through all settings from the settings.json file for (var current_setting in json_settings) { diff --git a/models/networkhistory.js b/models/networkhistory.js index fc99549..889491a 100644 --- a/models/networkhistory.js +++ b/models/networkhistory.js @@ -4,7 +4,8 @@ var mongoose = require('mongoose'), var NetworkHistorySchema = new Schema({ blockindex: {type: Number, default: 0, index: true}, nethash: { type: Number, default: 0 }, - difficulty: { type: Number, default: 0 } + difficulty_pow: { type: Number, default: 0 }, + difficulty_pos: { type: Number, default: 0 } }, {id: false}); module.exports = mongoose.model('NetworkHistory', NetworkHistorySchema); \ No newline at end of file diff --git a/settings.json.template b/settings.json.template index 8441613..e91b9b0 100644 --- a/settings.json.template +++ b/settings.json.template @@ -310,14 +310,22 @@ // Set this to any valid html color // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" "bgcolor": "#ffffff", - // line_color: Change the line color of the network difficulty chart - // Set this to any valid html color - // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" - "line_color": "rgba(255, 99, 132, 1)", - // fill_color: Change the fill color of the network difficulty chart - // Set this to any valid html color - // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" - "fill_color": "rgba(255, 99, 132, 0.2)", + // pow_line_color: Change the line color of the network difficulty chart for POW coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pow_line_color": "rgba(255, 99, 132, 1)", + // pow_fill_color: Change the fill color of the network difficulty chart for POW coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pow_fill_color": "rgba(255, 99, 132, 0.2)", + // pos_line_color: Change the line color of the network difficulty chart for POS coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pos_line_color": "rgba(255, 161, 0, 1)", + // pos_fill_color: Change the fill color of the network difficulty chart for POS coins + // Set this to any valid html color + // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" + "pos_fill_color": "rgba(255, 161, 0, 0.2)", // crosshair_color: Change the vertical crosshair line color of the network difficulty chart // Set this to any valid html color // Ex: "#ffffff" or "rgba(255, 255, 255, 1)" or "white" diff --git a/views/layout.pug b/views/layout.pug index 6460f17..72bae33 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -434,20 +434,43 @@ html(lang='en') if (#{settings.network_history.enabled} == true && #{showDifficultyChart} == true && #{settings.shared_pages.page_header.network_charts.difficulty_chart.enabled} == true) { const ctxDifficulty = document.getElementById('difficultyChart').getContext('2d'); + var diffDataSets = []; + var showPOW = false; + var showPOS = false; + + for (var i = 0; i < result.length; i++) { + if (result[i].difficulty_pow != 0) + showPOW = true; + if (result[i].difficulty_pos != 0) + showPOS = true; + } + + if (showPOS) { + diffDataSets.push({ + label: 'POS Difficulty', + data: result.map(function(a) {return a.difficulty_pos;}), + backgroundColor: ['#{settings.shared_pages.page_header.network_charts.difficulty_chart.pos_fill_color}'], + borderColor: ['#{settings.shared_pages.page_header.network_charts.difficulty_chart.pos_line_color}'], + fill: 'start' + }); + } + + if (showPOW || !showPOS) { + diffDataSets.push({ + label: 'POW Difficulty', + data: result.map(function(a) {return a.difficulty_pow;}), + backgroundColor: ['#{settings.shared_pages.page_header.network_charts.difficulty_chart.pow_fill_color}'], + borderColor: ['#{settings.shared_pages.page_header.network_charts.difficulty_chart.pow_line_color}'], + fill: 'start' + }); + } + if (difficultyChart == null) { difficultyChart = new Chart(ctxDifficulty, { type: 'line', data: { labels: result.map(function(a) {return a.blockindex;}), - datasets: [ - { - label: 'Difficulty', - data: result.map(function(a) {return a.difficulty;}), - backgroundColor: ['#{settings.shared_pages.page_header.network_charts.difficulty_chart.fill_color}'], - borderColor: ['#{settings.shared_pages.page_header.network_charts.difficulty_chart.line_color}'], - fill: 'start' - } - ] + datasets: diffDataSets }, options: { maintainAspectRatio: false, @@ -509,7 +532,29 @@ html(lang='en') $('#difficultyChartParent').fadeIn(); } else { difficultyChart.data.labels = result.map(function(a) {return a.blockindex;}); - difficultyChart.data.datasets[0].data = result.map(function(a) {return a.difficulty;}); + + if (difficultyChart.data.datasets.length != diffDataSets.length) + difficultyChart.data.datasets = diffDataSets; + else { + if ( + ( + difficultyChart.data.datasets[0].label.indexOf('POW') > -1 && + diffDataSets[0].label.indexOf('POW') > -1 + ) + || + ( + difficultyChart.data.datasets[0].label.indexOf('POS') > -1 && + diffDataSets[0].label.indexOf('POS') > -1 + ) + ) { + difficultyChart.data.datasets[0].data = diffDataSets[0].data; + + if (difficultyChart.data.datasets.length == 2) + difficultyChart.data.datasets[1].data = diffDataSets[1].data; + } else + difficultyChart.data.datasets = diffDataSets; + } + difficultyChart.update(); } }