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
This commit is contained in:
Joe Uhren
2022-06-24 19:43:02 -06:00
parent 9df2bf4024
commit 57b6dd72a9
5 changed files with 159 additions and 43 deletions
+68 -16
View File
@@ -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();
});
});
});
});