Skip to content

Commit

Permalink
daemon: print 128bit diff properly /monero#5659
Browse files Browse the repository at this point in the history
  • Loading branch information
stoffu committed Jul 3, 2019
1 parent ea3d306 commit 66c7dd9
Showing 1 changed file with 39 additions and 16 deletions.
55 changes: 39 additions & 16 deletions src/daemon/rpc_command_executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "daemon/rpc_command_executor.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include "cryptonote_core/cryptonote_core.h"
#include "cryptonote_basic/difficulty.h"
#include "cryptonote_basic/hardfork.h"
#include <boost/format.hpp>
#include <ctime>
Expand Down Expand Up @@ -73,7 +74,7 @@ namespace {
<< "height: " << boost::lexical_cast<std::string>(header.height) << std::endl
<< "depth: " << boost::lexical_cast<std::string>(header.depth) << std::endl
<< "hash: " << header.hash << std::endl
<< "difficulty: " << boost::lexical_cast<std::string>(header.difficulty) << std::endl
<< "difficulty: " << header.wide_difficulty << std::endl
<< "POW hash: " << header.pow_hash << std::endl
<< "block size: " << header.block_size << std::endl
<< "long term size: " << header.long_term_size << std::endl
Expand Down Expand Up @@ -322,18 +323,40 @@ bool t_rpc_command_executor::show_difficulty() {

tools::success_msg_writer() << "BH: " << res.height
<< ", TH: " << res.top_block_hash
<< ", DIFF: " << res.difficulty
<< ", HR: " << res.difficulty / res.target << " H/s";
<< ", DIFF: " << res.wide_difficulty
<< ", HR: " << cryptonote::difficulty_type(res.wide_difficulty) / res.target << " H/s";

return true;
}

static std::string get_mining_speed(uint64_t hr)
static void get_metric_prefix(cryptonote::difficulty_type hr, double& hr_d, char& prefix)
{
if (hr>1e9) return (boost::format("%.2f GH/s") % (hr/1e9)).str();
if (hr>1e6) return (boost::format("%.2f MH/s") % (hr/1e6)).str();
if (hr>1e3) return (boost::format("%.2f kH/s") % (hr/1e3)).str();
return (boost::format("%.0f H/s") % hr).str();
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<double>() / 1000;
prefix = metric_prefixes[i];
return;
}
hr /= 1000;
}
prefix = 0;
}

static std::string get_mining_speed(cryptonote::difficulty_type hr)
{
double hr_d;
char prefix;
get_metric_prefix(hr, hr_d, prefix);
if (prefix == 0) return (boost::format("%.0f H/s") % hr).str();
return (boost::format("%.2f %cH/s") % hr_d % prefix).str();
}

static std::string get_fork_extra_info(uint64_t t, uint64_t now, uint64_t block_time)
Expand Down Expand Up @@ -449,7 +472,7 @@ bool t_rpc_command_executor::show_status() {
% (ires.testnet ? "testnet" : ires.stagenet ? "stagenet" : "mainnet")
% bootstrap_msg
% (!has_mining_info ? "mining info unavailable" : mining_busy ? "syncing" : mres.active ? ( ( mres.is_background_mining_enabled ? "smart " : "" ) + std::string("mining at ") + get_mining_speed(mres.speed) ) : "not mining")
% get_mining_speed(ires.difficulty / ires.target)
% get_mining_speed(cryptonote::difficulty_type(ires.wide_difficulty) / ires.target)
% (unsigned)hfres.version
% get_fork_extra_info(hfres.earliest_height, net_height, ires.target)
% (unsigned)ires.outgoing_connections_count
Expand Down Expand Up @@ -561,7 +584,7 @@ bool t_rpc_command_executor::print_blockchain_info(uint64_t start_block_index, u
<< ", size: " << header.block_size << ", transactions: " << header.num_txes << std::endl
<< "major version: " << (unsigned)header.major_version << ", minor version: " << (unsigned)header.minor_version << std::endl
<< "block id: " << header.hash << ", previous block id: " << header.prev_hash << std::endl
<< "difficulty: " << header.difficulty << ", nonce " << header.nonce << ", reward " << cryptonote::print_money(header.reward) << std::endl;
<< "difficulty: " << header.wide_difficulty << ", nonce " << header.nonce << ", reward " << cryptonote::print_money(header.reward) << std::endl;
first = false;
}

Expand Down Expand Up @@ -1672,7 +1695,7 @@ bool t_rpc_command_executor::alt_chain_info(const std::string &tip)
{
uint64_t start_height = (chain.height - chain.length + 1);
tools::msg_writer() << chain.length << " blocks long, from height " << start_height << " (" << (ires.height - start_height - 1)
<< " deep), diff " << chain.difficulty << ": " << chain.block_hash;
<< " deep), diff " << chain.wide_difficulty << ": " << chain.block_hash;
}
}
else
Expand All @@ -1684,7 +1707,7 @@ bool t_rpc_command_executor::alt_chain_info(const std::string &tip)
tools::success_msg_writer() << "Found alternate chain with tip " << tip;
uint64_t start_height = (chain.height - chain.length + 1);
tools::msg_writer() << chain.length << " blocks long, from height " << start_height << " (" << (ires.height - start_height - 1)
<< " deep), diff " << chain.difficulty << ":";
<< " deep), diff " << chain.wide_difficulty << ":";
for (const std::string &block_id: chain.block_hashes)
tools::msg_writer() << " " << block_id;
tools::msg_writer() << "Chain parent on main chain: " << chain.main_chain_parent_block;
Expand Down Expand Up @@ -1733,7 +1756,7 @@ bool t_rpc_command_executor::print_blockchain_dynamic_stats(uint64_t nblocks)
}
}

tools::msg_writer() << "Height: " << ires.height << ", diff " << ires.difficulty << ", cum. diff " << ires.cumulative_difficulty
tools::msg_writer() << "Height: " << ires.height << ", diff " << ires.wide_difficulty << ", cum. diff " << ires.wide_cumulative_difficulty
<< ", target " << ires.target << " sec" << ", dyn fee " << cryptonote::print_money(feres.fee) << "/kB";

if (nblocks > 0)
Expand All @@ -1759,7 +1782,7 @@ bool t_rpc_command_executor::print_blockchain_dynamic_stats(uint64_t nblocks)
}
}

double avgdiff = 0;
cryptonote::difficulty_type avgdiff = 0;
double avgnumtxes = 0;
double avgreward = 0;
std::vector<uint64_t> sizes;
Expand All @@ -1768,7 +1791,7 @@ bool t_rpc_command_executor::print_blockchain_dynamic_stats(uint64_t nblocks)
std::vector<unsigned> major_versions(256, 0), minor_versions(256, 0);
for (const auto &bhr: bhres.headers)
{
avgdiff += bhr.difficulty;
avgdiff += cryptonote::difficulty_type(bhr.wide_difficulty);
avgnumtxes += bhr.num_txes;
avgreward += bhr.reward;
sizes.push_back(bhr.block_size);
Expand All @@ -1783,7 +1806,7 @@ bool t_rpc_command_executor::print_blockchain_dynamic_stats(uint64_t nblocks)
avgnumtxes /= nblocks;
avgreward /= nblocks;
uint64_t median_block_size = epee::misc_utils::median(sizes);
tools::msg_writer() << "Last " << nblocks << ": avg. diff " << (uint64_t)avgdiff << ", " << (latest - earliest) / nblocks << " avg sec/block, avg num txes " << avgnumtxes
tools::msg_writer() << "Last " << nblocks << ": avg. diff " << avgdiff << ", " << (latest - earliest) / nblocks << " avg sec/block, avg num txes " << avgnumtxes
<< ", avg. reward " << cryptonote::print_money(avgreward) << ", median block size " << median_block_size;

unsigned int max_major = 256, max_minor = 256;
Expand Down

0 comments on commit 66c7dd9

Please sign in to comment.