From 2b5646d87c900ed01642444dab82f5d4f926a393 Mon Sep 17 00:00:00 2001 From: stoffu Date: Mon, 17 Jun 2019 12:55:18 +0900 Subject: [PATCH] Display 128bit network difficulty properly --- src/MempoolStatus.cpp | 6 +++++- src/MempoolStatus.h | 3 +++ src/page.h | 21 +++++++++++---------- src/templates/block.html | 2 +- src/tools.cpp | 27 +++++++++++++++++++++++++++ src/tools.h | 6 ++++++ 6 files changed, 53 insertions(+), 12 deletions(-) diff --git a/src/MempoolStatus.cpp b/src/MempoolStatus.cpp index 4c18fca..90e9fe0 100644 --- a/src/MempoolStatus.cpp +++ b/src/MempoolStatus.cpp @@ -275,8 +275,11 @@ MempoolStatus::read_network_info() local_copy.height = rpc_network_info.height; local_copy.target_height = rpc_network_info.target_height; local_copy.difficulty = rpc_network_info.difficulty; + local_copy.difficulty_top64 = rpc_network_info.difficulty_top64; local_copy.target = rpc_network_info.target; - local_copy.hash_rate = (rpc_network_info.difficulty/rpc_network_info.target); + cryptonote::difficulty_type hash_rate = cryptonote::difficulty_type(rpc_network_info.wide_difficulty) / rpc_network_info.target; + local_copy.hash_rate = (hash_rate & 0xFFFFFFFFFFFFFFFF).convert_to(); + local_copy.hash_rate_top64 = ((hash_rate >> 64) & 0xFFFFFFFFFFFFFFFF).convert_to(); local_copy.tx_count = rpc_network_info.tx_count; local_copy.tx_pool_size = rpc_network_info.tx_pool_size; local_copy.alt_blocks_count = rpc_network_info.alt_blocks_count; @@ -286,6 +289,7 @@ MempoolStatus::read_network_info() local_copy.nettype = rpc_network_info.testnet ? cryptonote::network_type::TESTNET : rpc_network_info.stagenet ? cryptonote::network_type::STAGENET : cryptonote::network_type::MAINNET; local_copy.cumulative_difficulty = rpc_network_info.cumulative_difficulty; + local_copy.cumulative_difficulty_top64 = rpc_network_info.cumulative_difficulty_top64; local_copy.block_size_limit = rpc_network_info.block_size_limit; local_copy.block_size_median = rpc_network_info.block_size_median; local_copy.block_weight_limit = rpc_network_info.block_weight_limit; diff --git a/src/MempoolStatus.h b/src/MempoolStatus.h index 06e02f9..0bd10e0 100644 --- a/src/MempoolStatus.h +++ b/src/MempoolStatus.h @@ -61,6 +61,7 @@ struct MempoolStatus uint64_t height {0}; uint64_t target_height {0}; uint64_t difficulty {0}; + uint64_t difficulty_top64 {0}; uint64_t target {0}; uint64_t tx_count {0}; uint64_t tx_pool_size {0}; @@ -72,6 +73,7 @@ struct MempoolStatus cryptonote::network_type nettype {cryptonote::network_type::MAINNET}; crypto::hash top_block_hash; uint64_t cumulative_difficulty {0}; + uint64_t cumulative_difficulty_top64 {0}; uint64_t block_size_limit {0}; uint64_t block_size_median {0}; uint64_t block_weight_limit {0}; @@ -81,6 +83,7 @@ struct MempoolStatus uint64_t current_hf_version {0}; uint64_t hash_rate {0}; + uint64_t hash_rate_top64 {0}; uint64_t fee_per_kb {0}; uint64_t info_timestamp {0}; diff --git a/src/page.h b/src/page.h index a148821..df7f245 100644 --- a/src/page.h +++ b/src/page.h @@ -929,13 +929,15 @@ index2(uint64_t page_no = 0, bool refresh_page = false) // perapre network info mstch::map for the front page string hash_rate; + double hr_d; + char metric_prefix; + cryptonote::difficulty_type hr = make_difficulty(current_network_info.hash_rate, current_network_info.hash_rate_top64); + get_metric_prefix(hr, hr_d, metric_prefix); - if (current_network_info.hash_rate > 1e6) - hash_rate = fmt::format("{:0.3f} MH/s", current_network_info.hash_rate/1.0e6); - else if (current_network_info.hash_rate > 1e3) - hash_rate = fmt::format("{:0.3f} kH/s", current_network_info.hash_rate/1.0e3); + if (metric_prefix != 0) + hash_rate = fmt::format("{:0.3f} {:c}H/s", hr_d, metric_prefix); else - hash_rate = fmt::format("{:d} H/s", current_network_info.hash_rate); + hash_rate = fmt::format("{:s} H/s", hr.str()); pair network_info_age = get_age(local_copy_server_timestamp, current_network_info.info_timestamp); @@ -948,7 +950,7 @@ index2(uint64_t page_no = 0, bool refresh_page = false) } context["network_info"] = mstch::map { - {"difficulty" , current_network_info.difficulty}, + {"difficulty" , make_difficulty(current_network_info.difficulty, current_network_info.difficulty_top64).str()}, {"hash_rate" , hash_rate}, {"fee_per_kb" , print_money(current_network_info.fee_per_kb)}, {"alt_blocks_no" , current_network_info.alt_blocks_count}, @@ -1312,8 +1314,7 @@ show_block(uint64_t _blk_height) {"delta_time" , delta_time}, {"blk_nonce" , blk.nonce}, {"blk_pow_hash" , blk_pow_hash_str}, - {"blk_difficulty_lo" , (blk_difficulty << 64 >> 64).convert_to()}, - {"blk_difficulty_hi" , (blk_difficulty >> 64).convert_to()}, + {"blk_difficulty" , blk_difficulty.str()}, {"age_format" , age.second}, {"major_ver" , std::to_string(blk.major_version)}, {"minor_ver" , std::to_string(blk.minor_version)}, @@ -6988,7 +6989,7 @@ get_monero_network_info(json& j_info) {"current" , local_copy_network_info.current}, {"height" , local_copy_network_info.height}, {"target_height" , local_copy_network_info.target_height}, - {"difficulty" , local_copy_network_info.difficulty}, + {"difficulty" , make_difficulty(local_copy_network_info.difficulty, local_copy_network_info.difficulty_top64).str()}, {"target" , local_copy_network_info.target}, {"hash_rate" , local_copy_network_info.hash_rate}, {"tx_count" , local_copy_network_info.tx_count}, @@ -7001,7 +7002,7 @@ get_monero_network_info(json& j_info) {"testnet" , local_copy_network_info.nettype == cryptonote::network_type::TESTNET}, {"stagenet" , local_copy_network_info.nettype == cryptonote::network_type::STAGENET}, {"top_block_hash" , pod_to_hex(local_copy_network_info.top_block_hash)}, - {"cumulative_difficulty" , local_copy_network_info.cumulative_difficulty}, + {"cumulative_difficulty" , make_difficulty(local_copy_network_info.cumulative_difficulty, local_copy_network_info.cumulative_difficulty_top64).str()}, {"block_size_limit" , local_copy_network_info.block_size_limit}, {"block_size_median" , local_copy_network_info.block_size_median}, {"start_time" , local_copy_network_info.start_time}, diff --git a/src/templates/block.html b/src/templates/block.html index 844eef5..680199f 100644 --- a/src/templates/block.html +++ b/src/templates/block.html @@ -32,7 +32,7 @@ PoW hash:{{blk_pow_hash}} - Difficulty (hi64, lo64):{{blk_difficulty_hi}}, {{blk_difficulty_lo}} + Difficulty:{{blk_difficulty}} diff --git a/src/tools.cpp b/src/tools.cpp index d3b1513..ed65f34 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -1272,4 +1272,31 @@ tx_to_hex(transaction const& tx) return epee::string_tools::buff_to_hex_nodelimer(t_serializable_object_to_blob(tx)); } +void get_metric_prefix(cryptonote::difficulty_type hr, double& hr_d, char& prefix) +{ + if (hr < 1000) + { + prefix = 0; + return; + } + static const char metric_prefixes[4] = { 'k', 'M', 'G', 'T' }; + for (size_t i = 0; i < sizeof(metric_prefixes); ++i) + { + if (hr < 1000000) + { + hr_d = hr.convert_to() / 1000; + prefix = metric_prefixes[i]; + return; + } + hr /= 1000; + } + prefix = 0; +} + +cryptonote::difficulty_type +make_difficulty(uint64_t low, uint64_t high) +{ + return (cryptonote::difficulty_type(high) << 64) + low; +} + } diff --git a/src/tools.h b/src/tools.h index 10df3d8..d511e1b 100644 --- a/src/tools.h +++ b/src/tools.h @@ -373,6 +373,12 @@ pause_execution(uint64_t no_seconds, const string& text = "now"); string tx_to_hex(transaction const& tx); +void +get_metric_prefix(cryptonote::difficulty_type hr, double& hr_d, char& prefix); + +cryptonote::difficulty_type +make_difficulty(uint64_t low, uint64_t high); + } #endif //XMREG01_TOOLS_H