From 533cc202d28c8a70dd01b5e06064bd8f7199cea2 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 1 Sep 2021 16:26:56 +0200 Subject: [PATCH] Added pool stats --- src/console_commands.cpp | 26 ++++++++++++++++++-------- src/p2pool.cpp | 23 +++++++++++++++++++++++ src/p2pool.h | 1 + src/p2pool_api.cpp | 36 ++++++++++++++++++++++-------------- src/p2pool_api.h | 4 ++++ src/side_chain.cpp | 14 +++++++++++--- src/side_chain.h | 3 +++ 7 files changed, 82 insertions(+), 25 deletions(-) diff --git a/src/console_commands.cpp b/src/console_commands.cpp index 740a2d8..7e256d1 100644 --- a/src/console_commands.cpp +++ b/src/console_commands.cpp @@ -146,22 +146,32 @@ void ConsoleCommands::run() do { std::getline(std::cin, command); - if (stopped || std::cin.eof()) { + + if (std::cin.eof()) { + LOGINFO(1, "EOF, stopping"); return; } - int i; - for (i=0; cmds[i].name.len; i++) { - if (!strncmp(command.c_str(), cmds[i].name.str, cmds[i].name.len)) { - const char *args = command.c_str() + cmds[i].name.len + 1; - int rc = cmds[i].func(m_pool, args); - if ( rc ) + if (stopped) { + LOGINFO(1, "stopping"); + return; + } + + cmd* c = cmds; + for (; c->name.len; ++c) { + if (!strncmp(command.c_str(), c->name.str, c->name.len)) { + const char *args = command.c_str() + c->name.len + 1; + if (c->func(m_pool, args)) { + LOGINFO(1, "exit requested, stopping"); return; + } break; } } - if (!cmds[i].name.len) + + if (!c->name.len) { LOGWARN(0, "Unknown command " << command); + } } while (true); } diff --git a/src/p2pool.cpp b/src/p2pool.cpp index 4481754..d7f6032 100644 --- a/src/p2pool.cpp +++ b/src/p2pool.cpp @@ -438,6 +438,7 @@ void p2pool::update_block_template() } m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet); stratum_on_block(); + api_update_pool_stats(); } void p2pool::download_block_headers(uint64_t current_height) @@ -778,6 +779,28 @@ void p2pool::api_update_network_stats() }); } +void p2pool::api_update_pool_stats() +{ + if (!m_api) { + return; + } + + uint64_t t; + const difficulty_type diff = m_sideChain->difficulty(); + const uint64_t hashrate = udiv128(diff.hi, diff.lo, m_sideChain->block_time(), &t); + const uint64_t miners = m_sideChain->miner_count(); + const difficulty_type total_hashes = m_sideChain->total_hashes(); + + m_api->set(p2pool_api::Category::POOL, "stats", + [hashrate, miners, &total_hashes](log::Stream& s) + { + s << "{\"pool_list\":[\"pplns\"],\"pool_statistics\":{\"hashRate\":" << hashrate + << ",\"miners\":" << miners + << ",\"totalHashes\":" << total_hashes + << ",\"lastBlockFoundTime\":0,\"lastBlockFound\":0}}"; + }); +} + static void on_signal(uv_signal_t* handle, int signum) { p2pool* pool = reinterpret_cast(handle->data); diff --git a/src/p2pool.h b/src/p2pool.h index b1bbef0..252a0d8 100644 --- a/src/p2pool.h +++ b/src/p2pool.h @@ -114,6 +114,7 @@ private: uint32_t parse_block_headers_range(const char* data, size_t size); void api_update_network_stats(); + void api_update_pool_stats(); std::atomic m_serversStarted{ 0 }; StratumServer* m_stratumServer = nullptr; diff --git a/src/p2pool_api.cpp b/src/p2pool_api.cpp index 6f1fb21..44d5c72 100644 --- a/src/p2pool_api.cpp +++ b/src/p2pool_api.cpp @@ -59,20 +59,10 @@ p2pool_api::p2pool_api(const std::string& api_path) : m_apiPath(api_path) uv_mutex_init_checked(&m_dumpDataLock); m_networkPath = m_apiPath + "network/"; + m_poolPath = m_apiPath + "pool/"; -#ifdef _MSC_VER - result = _mkdir(m_networkPath.c_str()); -#else - result = mkdir(m_networkPath.c_str(), 0775); -#endif - - if (result < 0) { - result = errno; - if (result != EEXIST) { - LOGERR(1, "mkdir(" << m_networkPath << ") failed, error " << result); - panic(); - } - } + create_dir(m_networkPath); + create_dir(m_poolPath); } p2pool_api::~p2pool_api() @@ -80,6 +70,23 @@ p2pool_api::~p2pool_api() uv_mutex_destroy(&m_dumpDataLock); } +void p2pool_api::create_dir(const std::string& path) +{ +#ifdef _MSC_VER + int result = _mkdir(path.c_str()); +#else + int result = mkdir(path.c_str(), 0775); +#endif + + if (result < 0) { + result = errno; + if (result != EEXIST) { + LOGERR(1, "mkdir(" << path << ") failed, error " << result); + panic(); + } + } +} + void p2pool_api::on_stop() { uv_close(reinterpret_cast(&m_dumpToFileAsync), nullptr); @@ -95,7 +102,8 @@ void p2pool_api::dump_to_file_async_internal(const Category& category, const cha std::string path; switch (category) { - case Category::NETWORK: path = m_networkPath + filename; + case Category::NETWORK: path = m_networkPath + filename; break; + case Category::POOL: path = m_poolPath + filename; break; } { diff --git a/src/p2pool_api.h b/src/p2pool_api.h index 3f5f4f6..ef65547 100644 --- a/src/p2pool_api.h +++ b/src/p2pool_api.h @@ -30,6 +30,7 @@ public: enum class Category { NETWORK, + POOL, }; void on_stop(); @@ -38,6 +39,8 @@ public: void set(const Category& category, const char* filename, T&& callback) { dump_to_file_async_internal(category, filename, DumpFileCallback(std::move(callback))); } private: + void create_dir(const std::string& path); + static void on_dump_to_file(uv_async_t* async) { reinterpret_cast(async->data)->dump_to_file(); } struct DumpFileWork { @@ -75,6 +78,7 @@ private: std::string m_apiPath; std::string m_networkPath; + std::string m_poolPath; uv_mutex_t m_dumpDataLock; std::unordered_map> m_dumpData; diff --git a/src/side_chain.cpp b/src/side_chain.cpp index 4be1c64..669b259 100644 --- a/src/side_chain.cpp +++ b/src/side_chain.cpp @@ -663,6 +663,16 @@ void SideChain::print_status() ); } +difficulty_type SideChain::total_hashes() const +{ + return m_chainTip ? m_chainTip->m_cumulativeDifficulty : difficulty_type(); +} + +uint64_t SideChain::miner_count() const +{ + return m_chainTip ? m_chainTip->m_outputs.size() : 0; +} + bool SideChain::split_reward(uint64_t reward, const std::vector& shares, std::vector& rewards) { const size_t num_shares = shares.size(); @@ -1448,9 +1458,7 @@ void SideChain::prune_old_blocks() }), v.end()); if (v.empty()) { - auto old_it = it; - ++it; - m_blocksByHeight.erase(old_it); + it = m_blocksByHeight.erase(it); } else { ++it; diff --git a/src/side_chain.h b/src/side_chain.h index f098a13..adc6686 100644 --- a/src/side_chain.h +++ b/src/side_chain.h @@ -64,6 +64,9 @@ public: uint64_t chain_window_size() const { return m_chainWindowSize; } NetworkType network_type() const { return m_networkType; } const difficulty_type& difficulty() const { return m_curDifficulty; } + difficulty_type total_hashes() const; + uint64_t block_time() const { return m_targetBlockTime; } + uint64_t miner_count() const; static bool split_reward(uint64_t reward, const std::vector& shares, std::vector& rewards);