daemon: new mining_status command

This commit is contained in:
moneromooo-monero 2019-02-22 20:17:45 +00:00
parent 31bdf7bd11
commit 59478c80dd
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
9 changed files with 134 additions and 7 deletions

View file

@ -115,7 +115,8 @@ namespace cryptonote
m_min_idle_seconds(BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS), m_min_idle_seconds(BACKGROUND_MINING_DEFAULT_MIN_IDLE_INTERVAL_IN_SECONDS),
m_idle_threshold(BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE), m_idle_threshold(BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE),
m_mining_target(BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE), m_mining_target(BACKGROUND_MINING_DEFAULT_MINING_TARGET_PERCENTAGE),
m_miner_extra_sleep(BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS) m_miner_extra_sleep(BACKGROUND_MINING_DEFAULT_MINER_EXTRA_SLEEP_MILLIS),
m_block_reward(0)
{ {
} }
@ -126,12 +127,13 @@ namespace cryptonote
catch (...) { /* ignore */ } catch (...) { /* ignore */ }
} }
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
bool miner::set_block_template(const block& bl, const difficulty_type& di, uint64_t height) bool miner::set_block_template(const block& bl, const difficulty_type& di, uint64_t height, uint64_t block_reward)
{ {
CRITICAL_REGION_LOCAL(m_template_lock); CRITICAL_REGION_LOCAL(m_template_lock);
m_template = bl; m_template = bl;
m_diffic = di; m_diffic = di;
m_height = height; m_height = height;
m_block_reward = block_reward;
++m_template_no; ++m_template_no;
m_starter_nonce = crypto::rand<uint32_t>(); m_starter_nonce = crypto::rand<uint32_t>();
return true; return true;
@ -163,7 +165,7 @@ namespace cryptonote
LOG_ERROR("Failed to get_block_template(), stopping mining"); LOG_ERROR("Failed to get_block_template(), stopping mining");
return false; return false;
} }
set_block_template(bl, di, height); set_block_template(bl, di, height, expected_reward);
return true; return true;
} }
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
@ -295,6 +297,7 @@ namespace cryptonote
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
bool miner::start(const account_public_address& adr, size_t threads_count, const boost::thread::attributes& attrs, bool do_background, bool ignore_battery) bool miner::start(const account_public_address& adr, size_t threads_count, const boost::thread::attributes& attrs, bool do_background, bool ignore_battery)
{ {
m_block_reward = 0;
m_mine_address = adr; m_mine_address = adr;
m_threads_total = static_cast<uint32_t>(threads_count); m_threads_total = static_cast<uint32_t>(threads_count);
m_starter_nonce = crypto::rand<uint32_t>(); m_starter_nonce = crypto::rand<uint32_t>();

View file

@ -61,7 +61,7 @@ namespace cryptonote
~miner(); ~miner();
bool init(const boost::program_options::variables_map& vm, network_type nettype); bool init(const boost::program_options::variables_map& vm, network_type nettype);
static void init_options(boost::program_options::options_description& desc); static void init_options(boost::program_options::options_description& desc);
bool set_block_template(const block& bl, const difficulty_type& diffic, uint64_t height); bool set_block_template(const block& bl, const difficulty_type& diffic, uint64_t height, uint64_t block_reward);
bool on_block_chain_update(); bool on_block_chain_update();
bool start(const account_public_address& adr, size_t threads_count, const boost::thread::attributes& attrs, bool do_background = false, bool ignore_battery = false); bool start(const account_public_address& adr, size_t threads_count, const boost::thread::attributes& attrs, bool do_background = false, bool ignore_battery = false);
uint64_t get_speed() const; uint64_t get_speed() const;
@ -85,6 +85,7 @@ namespace cryptonote
bool set_idle_threshold(uint8_t idle_threshold); bool set_idle_threshold(uint8_t idle_threshold);
uint8_t get_mining_target() const; uint8_t get_mining_target() const;
bool set_mining_target(uint8_t mining_target); bool set_mining_target(uint8_t mining_target);
uint64_t get_block_reward() const { return m_block_reward; }
static constexpr uint8_t BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE = 90; static constexpr uint8_t BACKGROUND_MINING_DEFAULT_IDLE_THRESHOLD_PERCENTAGE = 90;
static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE = 50; static constexpr uint8_t BACKGROUND_MINING_MIN_IDLE_THRESHOLD_PERCENTAGE = 50;
@ -164,5 +165,6 @@ namespace cryptonote
static bool get_process_time(uint64_t& total_time); static bool get_process_time(uint64_t& total_time);
static uint8_t get_percent_of_total(uint64_t some_time, uint64_t total_time); static uint8_t get_percent_of_total(uint64_t some_time, uint64_t total_time);
static boost::logic::tribool on_battery_power(); static boost::logic::tribool on_battery_power();
std::atomic<uint64_t> m_block_reward;
}; };
} }

View file

@ -404,6 +404,11 @@ bool t_command_parser_executor::stop_mining(const std::vector<std::string>& args
return m_executor.stop_mining(); return m_executor.stop_mining();
} }
bool t_command_parser_executor::mining_status(const std::vector<std::string>& args)
{
return m_executor.mining_status();
}
bool t_command_parser_executor::stop_daemon(const std::vector<std::string>& args) bool t_command_parser_executor::stop_daemon(const std::vector<std::string>& args)
{ {
if (!args.empty()) return false; if (!args.empty()) return false;

View file

@ -97,6 +97,8 @@ public:
bool stop_mining(const std::vector<std::string>& args); bool stop_mining(const std::vector<std::string>& args);
bool mining_status(const std::vector<std::string>& args);
bool stop_daemon(const std::vector<std::string>& args); bool stop_daemon(const std::vector<std::string>& args);
bool print_status(const std::vector<std::string>& args); bool print_status(const std::vector<std::string>& args);

View file

@ -112,6 +112,11 @@ t_command_server::t_command_server(
, std::bind(&t_command_parser_executor::stop_mining, &m_parser, p::_1) , std::bind(&t_command_parser_executor::stop_mining, &m_parser, p::_1)
, "Stop mining." , "Stop mining."
); );
m_command_lookup.set_handler(
"mining_status"
, std::bind(&t_command_parser_executor::mining_status, &m_parser, p::_1)
, "Show current mining status."
);
m_command_lookup.set_handler( m_command_lookup.set_handler(
"print_pool" "print_pool"
, std::bind(&t_command_parser_executor::print_transaction_pool_long, &m_parser, p::_1) , std::bind(&t_command_parser_executor::print_transaction_pool_long, &m_parser, p::_1)

View file

@ -461,7 +461,7 @@ bool t_rpc_command_executor::show_status() {
% get_sync_percentage(ires) % get_sync_percentage(ires)
% (ires.testnet ? "testnet" : ires.stagenet ? "stagenet" : "mainnet") % (ires.testnet ? "testnet" : ires.stagenet ? "stagenet" : "mainnet")
% bootstrap_msg % 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) + std::string(" to ") + mres.address ) : "not mining") % (!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(ires.difficulty / ires.target)
% (unsigned)hfres.version % (unsigned)hfres.version
% get_fork_extra_info(hfres.earliest_height, net_height, ires.target) % get_fork_extra_info(hfres.earliest_height, net_height, ires.target)
@ -486,6 +486,81 @@ bool t_rpc_command_executor::show_status() {
return true; return true;
} }
bool t_rpc_command_executor::mining_status() {
cryptonote::COMMAND_RPC_MINING_STATUS::request mreq;
cryptonote::COMMAND_RPC_MINING_STATUS::response mres;
epee::json_rpc::error error_resp;
bool has_mining_info = true;
std::string fail_message = "Problem fetching info";
bool mining_busy = false;
if (m_is_rpc)
{
// mining info is only available non unrestricted RPC mode
has_mining_info = m_rpc_client->rpc_request(mreq, mres, "/mining_status", fail_message.c_str());
}
else
{
if (!m_rpc_server->on_mining_status(mreq, mres))
{
tools::fail_msg_writer() << fail_message.c_str();
return true;
}
if (mres.status == CORE_RPC_STATUS_BUSY)
{
mining_busy = true;
}
else if (mres.status != CORE_RPC_STATUS_OK)
{
tools::fail_msg_writer() << make_error(fail_message, mres.status);
return true;
}
}
if (!has_mining_info)
{
tools::fail_msg_writer() << "Mining info unavailable";
return true;
}
if (mining_busy || !mres.active)
{
tools::msg_writer() << "Not currently mining";
}
else
{
tools::msg_writer() << "Mining at " << get_mining_speed(mres.speed) << " with " << mres.threads_count << " threads";
}
if (mres.active || mres.is_background_mining_enabled)
{
tools::msg_writer() << "PoW algorithm: " << mres.pow_algorithm;
tools::msg_writer() << "Mining address: " << mres.address;
}
if (mres.is_background_mining_enabled)
{
tools::msg_writer() << "Smart mining enabled:";
tools::msg_writer() << " Target: " << (unsigned)mres.bg_target << "% CPU";
tools::msg_writer() << " Idle threshold: " << (unsigned)mres.bg_idle_threshold << "% CPU";
tools::msg_writer() << " Min idle time: " << (unsigned)mres.bg_min_idle_seconds << " seconds";
tools::msg_writer() << " Ignore battery: " << (mres.bg_ignore_battery ? "yes" : "no");
}
if (!mining_busy && mres.active)
{
uint64_t daily = 86400ull / mres.block_target * mres.block_reward;
uint64_t monthly = 86400ull / mres.block_target * 30.5 * mres.block_reward;
uint64_t yearly = 86400ull / mres.block_target * 356 * mres.block_reward;
tools::msg_writer() << "Expected: " << cryptonote::print_money(daily) << " monero daily, "
<< cryptonote::print_money(monthly) << " monero monthly, " << cryptonote::print_money(yearly) << " yearly";
}
return true;
}
bool t_rpc_command_executor::print_connections() { bool t_rpc_command_executor::print_connections() {
cryptonote::COMMAND_RPC_GET_CONNECTIONS::request req; cryptonote::COMMAND_RPC_GET_CONNECTIONS::request req;
cryptonote::COMMAND_RPC_GET_CONNECTIONS::response res; cryptonote::COMMAND_RPC_GET_CONNECTIONS::response res;

View file

@ -109,6 +109,8 @@ public:
bool stop_mining(); bool stop_mining();
bool mining_status();
bool stop_daemon(); bool stop_daemon();
bool print_status(); bool print_status();

View file

@ -875,11 +875,30 @@ namespace cryptonote
res.active = lMiner.is_mining(); res.active = lMiner.is_mining();
res.is_background_mining_enabled = lMiner.get_is_background_mining_enabled(); res.is_background_mining_enabled = lMiner.get_is_background_mining_enabled();
res.block_target = m_core.get_blockchain_storage().get_current_hard_fork_version() < 2 ? DIFFICULTY_TARGET_V1 : DIFFICULTY_TARGET_V2;
if ( lMiner.is_mining() ) { if ( lMiner.is_mining() ) {
res.speed = lMiner.get_speed(); res.speed = lMiner.get_speed();
res.threads_count = lMiner.get_threads_count(); res.threads_count = lMiner.get_threads_count();
const account_public_address& lMiningAdr = lMiner.get_mining_address(); res.block_reward = lMiner.get_block_reward();
res.address = get_account_address_as_str(nettype(), false, lMiningAdr); }
const account_public_address& lMiningAdr = lMiner.get_mining_address();
res.address = get_account_address_as_str(nettype(), false, lMiningAdr);
const uint8_t major_version = m_core.get_blockchain_storage().get_current_hard_fork_version();
const unsigned variant = major_version >= 7 ? major_version - 6 : 0;
switch (variant)
{
case 0: res.pow_algorithm = "Cryptonight"; break;
case 1: res.pow_algorithm = "CNv1 (Cryptonight variant 1)"; break;
case 2: case 3: res.pow_algorithm = "CNv2 (Cryptonight variant 2)"; break;
case 4: case 5: res.pow_algorithm = "CNv4 (Cryptonight variant 4)"; break;
default: res.pow_algorithm = "I'm not sure actually"; break;
}
if (res.is_background_mining_enabled)
{
res.bg_idle_threshold = lMiner.get_idle_threshold();
res.bg_min_idle_seconds = lMiner.get_min_idle_seconds();
res.bg_ignore_battery = lMiner.get_ignore_battery();
res.bg_target = lMiner.get_mining_target();
} }
res.status = CORE_RPC_STATUS_OK; res.status = CORE_RPC_STATUS_OK;

View file

@ -1016,7 +1016,14 @@ namespace cryptonote
uint64_t speed; uint64_t speed;
uint32_t threads_count; uint32_t threads_count;
std::string address; std::string address;
std::string pow_algorithm;
bool is_background_mining_enabled; bool is_background_mining_enabled;
uint8_t bg_idle_threshold;
uint8_t bg_min_idle_seconds;
bool bg_ignore_battery;
uint8_t bg_target;
uint32_t block_target;
uint64_t block_reward;
BEGIN_KV_SERIALIZE_MAP() BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(status) KV_SERIALIZE(status)
@ -1024,7 +1031,14 @@ namespace cryptonote
KV_SERIALIZE(speed) KV_SERIALIZE(speed)
KV_SERIALIZE(threads_count) KV_SERIALIZE(threads_count)
KV_SERIALIZE(address) KV_SERIALIZE(address)
KV_SERIALIZE(pow_algorithm)
KV_SERIALIZE(is_background_mining_enabled) KV_SERIALIZE(is_background_mining_enabled)
KV_SERIALIZE(bg_idle_threshold)
KV_SERIALIZE(bg_min_idle_seconds)
KV_SERIALIZE(bg_ignore_battery)
KV_SERIALIZE(bg_target)
KV_SERIALIZE(block_target)
KV_SERIALIZE(block_reward)
END_KV_SERIALIZE_MAP() END_KV_SERIALIZE_MAP()
}; };
}; };