Refactoring

- Skip calling time(nullptr) twice
- Use getter method for m_api instead of exposing it
- Moved api_udate_*() back to private since they're not used anywhere else
This commit is contained in:
SChernykh 2021-10-04 10:28:56 +02:00
parent a0a31b0f3b
commit 52e5008bf4
4 changed files with 21 additions and 30 deletions

View file

@ -49,7 +49,7 @@ public:
SideChain& side_chain() { return *m_sideChain; }
const MinerData& miner_data() const { return m_minerData; }
p2pool_api* m_api;
p2pool_api* api() const { return m_api; }
RandomX_Hasher* hasher() const { return m_hasher; }
bool calculate_hash(const void* data, size_t size, const hash& seed, hash& result);
@ -76,10 +76,6 @@ public:
void api_update_block_found(const ChainMain* data);
void api_update_network_stats();
void api_update_pool_stats();
void api_update_stats_mod();
bool get_difficulty_at_height(uint64_t height, difficulty_type& diff);
private:
@ -96,6 +92,7 @@ private:
Params* m_params;
p2pool_api* m_api;
SideChain* m_sideChain;
RandomX_Hasher* m_hasher;
BlockTemplate* m_blockTemplate;
@ -126,6 +123,10 @@ private:
bool parse_block_header(const char* data, size_t size, ChainMain& result);
uint32_t parse_block_headers_range(const char* data, size_t size);
void api_update_network_stats();
void api_update_pool_stats();
void api_update_stats_mod();
struct FoundBlock
{
FORCEINLINE FoundBlock(time_t _t, uint64_t _h, const hash& _id, const difficulty_type& _block_diff, const difficulty_type& _total_hashes)

View file

@ -49,7 +49,7 @@ StratumServer::StratumServer(p2pool* pool)
, m_hashrateDataTail_24h(0)
, m_cumulativeFoundSharesDiff(0.0)
, m_totalFoundShares(0)
, m_apiLastUpdateTime(-1)
, m_apiLastUpdateTime(0)
{
m_hashrateData[0] = { time(nullptr), 0 };
@ -377,7 +377,7 @@ uint64_t StratumServer::get_random64()
void StratumServer::print_status()
{
update_hashrate_data(0);
update_hashrate_data(0, time(nullptr));
print_stratum_status();
}
@ -530,10 +530,8 @@ void StratumServer::on_blobs_ready()
LOGINFO(3, "sent new job to " << extra_nonce << '/' << numClientsProcessed << " clients");
}
void StratumServer::update_hashrate_data(uint64_t target)
void StratumServer::update_hashrate_data(uint64_t target, time_t timestamp)
{
const time_t timestamp = time(nullptr);
uint64_t rem;
const uint64_t hashes = (target > 1) ? udiv128(1, 0, target, &rem) : 0;
@ -646,8 +644,9 @@ void StratumServer::on_share_found(uv_work_t* req)
const uint64_t value = *reinterpret_cast<uint64_t*>(share->m_resultHash.h + HASH_SIZE - sizeof(uint64_t));
if (LIKELY(value < target)) {
server->update_hashrate_data(target);
server->api_update_local_stats();
const time_t timestamp = time(nullptr);
server->update_hashrate_data(target, timestamp);
server->api_update_local_stats(timestamp);
share->m_result = SubmittedShare::Result::OK;
}
else {
@ -917,27 +916,19 @@ bool StratumServer::StratumClient::process_submit(rapidjson::Document& doc, uint
return static_cast<StratumServer*>(m_owner)->on_submit(this, id, job_id.GetString(), nonce.GetString(), result.GetString());
}
void StratumServer::api_update_local_stats()
void StratumServer::api_update_local_stats(time_t timestamp)
{
if (!m_pool->m_api) {
if (!m_pool->api() || !m_pool->params().m_localStats) {
return;
}
if (!m_pool->params().m_localStats) {
return;
}
const time_t api_time_now = time(nullptr);
// Rate limit to no more than once in 60 seconds.
if (m_apiLastUpdateTime == -1) {
m_apiLastUpdateTime = api_time_now;
} else if (difftime(api_time_now, m_apiLastUpdateTime) < 60) {
if (timestamp < m_apiLastUpdateTime + 60) {
return;
} else {
m_apiLastUpdateTime = api_time_now;
}
m_apiLastUpdateTime = timestamp;
uint64_t hashes_15m, hashes_1h, hashes_24h, total_hashes;
int64_t dt_15m, dt_1h, dt_24h;
@ -981,7 +972,7 @@ void StratumServer::api_update_local_stats()
int connections = m_numConnections;
int incoming_connections = m_numIncomingConnections;
m_pool->m_api->set(p2pool_api::Category::LOCAL, "stats",
m_pool->api()->set(p2pool_api::Category::LOCAL, "stats",
[hashrate_15m, hashrate_1h, hashrate_24h, total_hashes, shares_found, average_effort, current_effort, connections, incoming_connections](log::Stream& s)
{
s << "{\"hashrate_15m\":" << hashrate_15m

View file

@ -154,9 +154,8 @@ private:
time_t m_apiLastUpdateTime;
void update_hashrate_data(uint64_t target);
void api_update_local_stats();
void update_hashrate_data(uint64_t target, time_t timestamp);
void api_update_local_stats(time_t timestamp);
};
} // namespace p2pool