Added pool stats

pull/11/head
SChernykh 2021-09-01 16:26:56 +02:00
parent 295cbda449
commit 533cc202d2
7 changed files with 82 additions and 25 deletions

View File

@ -146,22 +146,32 @@ void ConsoleCommands::run()
do { do {
std::getline(std::cin, command); std::getline(std::cin, command);
if (stopped || std::cin.eof()) {
if (std::cin.eof()) {
LOGINFO(1, "EOF, stopping");
return; return;
} }
int i; if (stopped) {
for (i=0; cmds[i].name.len; i++) { LOGINFO(1, "stopping");
if (!strncmp(command.c_str(), cmds[i].name.str, cmds[i].name.len)) { return;
const char *args = command.c_str() + cmds[i].name.len + 1; }
int rc = cmds[i].func(m_pool, args);
if ( rc ) 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; return;
}
break; break;
} }
} }
if (!cmds[i].name.len)
if (!c->name.len) {
LOGWARN(0, "Unknown command " << command); LOGWARN(0, "Unknown command " << command);
}
} while (true); } while (true);
} }

View File

@ -438,6 +438,7 @@ void p2pool::update_block_template()
} }
m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet); m_blockTemplate->update(m_minerData, *m_mempool, &m_params->m_wallet);
stratum_on_block(); stratum_on_block();
api_update_pool_stats();
} }
void p2pool::download_block_headers(uint64_t current_height) 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) static void on_signal(uv_signal_t* handle, int signum)
{ {
p2pool* pool = reinterpret_cast<p2pool*>(handle->data); p2pool* pool = reinterpret_cast<p2pool*>(handle->data);

View File

@ -114,6 +114,7 @@ private:
uint32_t parse_block_headers_range(const char* data, size_t size); uint32_t parse_block_headers_range(const char* data, size_t size);
void api_update_network_stats(); void api_update_network_stats();
void api_update_pool_stats();
std::atomic<uint32_t> m_serversStarted{ 0 }; std::atomic<uint32_t> m_serversStarted{ 0 };
StratumServer* m_stratumServer = nullptr; StratumServer* m_stratumServer = nullptr;

View File

@ -59,20 +59,10 @@ p2pool_api::p2pool_api(const std::string& api_path) : m_apiPath(api_path)
uv_mutex_init_checked(&m_dumpDataLock); uv_mutex_init_checked(&m_dumpDataLock);
m_networkPath = m_apiPath + "network/"; m_networkPath = m_apiPath + "network/";
m_poolPath = m_apiPath + "pool/";
#ifdef _MSC_VER create_dir(m_networkPath);
result = _mkdir(m_networkPath.c_str()); create_dir(m_poolPath);
#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();
}
}
} }
p2pool_api::~p2pool_api() p2pool_api::~p2pool_api()
@ -80,6 +70,23 @@ p2pool_api::~p2pool_api()
uv_mutex_destroy(&m_dumpDataLock); 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() void p2pool_api::on_stop()
{ {
uv_close(reinterpret_cast<uv_handle_t*>(&m_dumpToFileAsync), nullptr); uv_close(reinterpret_cast<uv_handle_t*>(&m_dumpToFileAsync), nullptr);
@ -95,7 +102,8 @@ void p2pool_api::dump_to_file_async_internal(const Category& category, const cha
std::string path; std::string path;
switch (category) { 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;
} }
{ {

View File

@ -30,6 +30,7 @@ public:
enum class Category { enum class Category {
NETWORK, NETWORK,
POOL,
}; };
void on_stop(); 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<T>(std::move(callback))); } void set(const Category& category, const char* filename, T&& callback) { dump_to_file_async_internal(category, filename, DumpFileCallback<T>(std::move(callback))); }
private: private:
void create_dir(const std::string& path);
static void on_dump_to_file(uv_async_t* async) { reinterpret_cast<p2pool_api*>(async->data)->dump_to_file(); } static void on_dump_to_file(uv_async_t* async) { reinterpret_cast<p2pool_api*>(async->data)->dump_to_file(); }
struct DumpFileWork { struct DumpFileWork {
@ -75,6 +78,7 @@ private:
std::string m_apiPath; std::string m_apiPath;
std::string m_networkPath; std::string m_networkPath;
std::string m_poolPath;
uv_mutex_t m_dumpDataLock; uv_mutex_t m_dumpDataLock;
std::unordered_map<std::string, std::vector<char>> m_dumpData; std::unordered_map<std::string, std::vector<char>> m_dumpData;

View File

@ -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<MinerShare>& shares, std::vector<uint64_t>& rewards) bool SideChain::split_reward(uint64_t reward, const std::vector<MinerShare>& shares, std::vector<uint64_t>& rewards)
{ {
const size_t num_shares = shares.size(); const size_t num_shares = shares.size();
@ -1448,9 +1458,7 @@ void SideChain::prune_old_blocks()
}), v.end()); }), v.end());
if (v.empty()) { if (v.empty()) {
auto old_it = it; it = m_blocksByHeight.erase(it);
++it;
m_blocksByHeight.erase(old_it);
} }
else { else {
++it; ++it;

View File

@ -64,6 +64,9 @@ public:
uint64_t chain_window_size() const { return m_chainWindowSize; } uint64_t chain_window_size() const { return m_chainWindowSize; }
NetworkType network_type() const { return m_networkType; } NetworkType network_type() const { return m_networkType; }
const difficulty_type& difficulty() const { return m_curDifficulty; } 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<MinerShare>& shares, std::vector<uint64_t>& rewards); static bool split_reward(uint64_t reward, const std::vector<MinerShare>& shares, std::vector<uint64_t>& rewards);