mirror of
https://github.com/SChernykh/p2pool.git
synced 2024-12-23 11:59:31 +00:00
Refactored RNG usage across the code
This commit is contained in:
parent
62b1690780
commit
c21d052d7a
9 changed files with 32 additions and 28 deletions
|
@ -54,6 +54,7 @@
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
#include "keccak.h"
|
#include "keccak.h"
|
||||||
#include "uv_util.h"
|
#include "uv_util.h"
|
||||||
#include <random>
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "crypto-ops.h"
|
#include "crypto-ops.h"
|
||||||
|
@ -32,7 +31,7 @@ namespace {
|
||||||
class RandomBytes
|
class RandomBytes
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RandomBytes() : rng(s), dist(0, 255)
|
RandomBytes() : rng(RandomDeviceSeed::instance), dist(0, 255)
|
||||||
{
|
{
|
||||||
uv_mutex_init_checked(&m);
|
uv_mutex_init_checked(&m);
|
||||||
|
|
||||||
|
@ -57,22 +56,6 @@ public:
|
||||||
private:
|
private:
|
||||||
uv_mutex_t m;
|
uv_mutex_t m;
|
||||||
|
|
||||||
// Fills the whole initial MT19937-64 state with non-deterministic random numbers
|
|
||||||
struct SeedSequence
|
|
||||||
{
|
|
||||||
using result_type = std::random_device::result_type;
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
static void generate(T begin, T end)
|
|
||||||
{
|
|
||||||
std::random_device rd;
|
|
||||||
for (T i = begin; i != end; ++i) {
|
|
||||||
*i = rd();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
SeedSequence s;
|
|
||||||
std::mt19937_64 rng;
|
std::mt19937_64 rng;
|
||||||
std::uniform_int_distribution<> dist;
|
std::uniform_int_distribution<> dist;
|
||||||
};
|
};
|
||||||
|
|
|
@ -47,15 +47,18 @@ P2PServer::P2PServer(p2pool* pool)
|
||||||
, m_cache(pool->params().m_blockCache ? new BlockCache() : nullptr)
|
, m_cache(pool->params().m_blockCache ? new BlockCache() : nullptr)
|
||||||
, m_cacheLoaded(false)
|
, m_cacheLoaded(false)
|
||||||
, m_initialPeerList(pool->params().m_p2pPeerList)
|
, m_initialPeerList(pool->params().m_p2pPeerList)
|
||||||
, m_rd{}
|
, m_rng(RandomDeviceSeed::instance)
|
||||||
, m_rng(m_rd())
|
|
||||||
, m_block(new PoolBlock())
|
, m_block(new PoolBlock())
|
||||||
, m_timer{}
|
, m_timer{}
|
||||||
, m_timerCounter(0)
|
, m_timerCounter(0)
|
||||||
, m_timerInterval(2)
|
, m_timerInterval(2)
|
||||||
, m_peerId(m_rng())
|
|
||||||
, m_peerListLastSaved(0)
|
, m_peerListLastSaved(0)
|
||||||
{
|
{
|
||||||
|
// Diffuse the initial state in case it has low quality
|
||||||
|
m_rng.discard(10000);
|
||||||
|
|
||||||
|
m_peerId = m_rng();
|
||||||
|
|
||||||
set_max_outgoing_peers(pool->params().m_maxOutgoingPeers);
|
set_max_outgoing_peers(pool->params().m_maxOutgoingPeers);
|
||||||
set_max_incoming_peers(pool->params().m_maxIncomingPeers);
|
set_max_incoming_peers(pool->params().m_maxIncomingPeers);
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "tcp_server.h"
|
#include "tcp_server.h"
|
||||||
#include <random>
|
|
||||||
|
|
||||||
namespace p2pool {
|
namespace p2pool {
|
||||||
|
|
||||||
|
@ -170,7 +169,6 @@ private:
|
||||||
void remove_peer_from_list(const raw_ip& ip);
|
void remove_peer_from_list(const raw_ip& ip);
|
||||||
|
|
||||||
uv_mutex_t m_rngLock;
|
uv_mutex_t m_rngLock;
|
||||||
std::random_device m_rd;
|
|
||||||
std::mt19937_64 m_rng;
|
std::mt19937_64 m_rng;
|
||||||
|
|
||||||
uv_mutex_t m_blockLock;
|
uv_mutex_t m_blockLock;
|
||||||
|
|
|
@ -39,8 +39,7 @@ StratumServer::StratumServer(p2pool* pool)
|
||||||
: TCPServer(StratumClient::allocate)
|
: TCPServer(StratumClient::allocate)
|
||||||
, m_pool(pool)
|
, m_pool(pool)
|
||||||
, m_extraNonce(0)
|
, m_extraNonce(0)
|
||||||
, m_rd{}
|
, m_rng(RandomDeviceSeed::instance)
|
||||||
, m_rng(m_rd())
|
|
||||||
, m_cumulativeHashes(0)
|
, m_cumulativeHashes(0)
|
||||||
, m_cumulativeHashesAtLastShare(0)
|
, m_cumulativeHashesAtLastShare(0)
|
||||||
, m_hashrateDataHead(0)
|
, m_hashrateDataHead(0)
|
||||||
|
@ -51,6 +50,9 @@ StratumServer::StratumServer(p2pool* pool)
|
||||||
, m_totalFoundShares(0)
|
, m_totalFoundShares(0)
|
||||||
, m_apiLastUpdateTime(0)
|
, m_apiLastUpdateTime(0)
|
||||||
{
|
{
|
||||||
|
// Diffuse the initial state in case it has low quality
|
||||||
|
m_rng.discard(10000);
|
||||||
|
|
||||||
m_hashrateData[0] = { time(nullptr), 0 };
|
m_hashrateData[0] = { time(nullptr), 0 };
|
||||||
|
|
||||||
uv_mutex_init_checked(&m_blobsQueueLock);
|
uv_mutex_init_checked(&m_blobsQueueLock);
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
|
|
||||||
#include "tcp_server.h"
|
#include "tcp_server.h"
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
#include <random>
|
|
||||||
|
|
||||||
namespace p2pool {
|
namespace p2pool {
|
||||||
|
|
||||||
|
@ -109,7 +108,6 @@ private:
|
||||||
std::atomic<uint32_t> m_extraNonce;
|
std::atomic<uint32_t> m_extraNonce;
|
||||||
|
|
||||||
uv_mutex_t m_rngLock;
|
uv_mutex_t m_rngLock;
|
||||||
std::random_device m_rd;
|
|
||||||
std::mt19937_64 m_rng;
|
std::mt19937_64 m_rng;
|
||||||
|
|
||||||
struct SubmittedShare
|
struct SubmittedShare
|
||||||
|
|
|
@ -382,4 +382,6 @@ bool resolve_host(std::string& host, bool& is_v6)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RandomDeviceSeed RandomDeviceSeed::instance;
|
||||||
|
|
||||||
} // namespace p2pool
|
} // namespace p2pool
|
||||||
|
|
18
src/util.h
18
src/util.h
|
@ -145,6 +145,24 @@ using unordered_map = robin_hood::detail::Table<false, 80, Key, T, robin_hood::h
|
||||||
template <typename Key>
|
template <typename Key>
|
||||||
using unordered_set = robin_hood::detail::Table<false, 80, Key, void, robin_hood::hash<Key>, std::equal_to<Key>>;
|
using unordered_set = robin_hood::detail::Table<false, 80, Key, void, robin_hood::hash<Key>, std::equal_to<Key>>;
|
||||||
|
|
||||||
|
// Fills the whole initial MT19937-64 state with non-deterministic random numbers
|
||||||
|
struct RandomDeviceSeed
|
||||||
|
{
|
||||||
|
using result_type = std::random_device::result_type;
|
||||||
|
static_assert(sizeof(result_type) >= 4, "result_type must have at least 32 bits");
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static void generate(T begin, T end)
|
||||||
|
{
|
||||||
|
std::random_device rd;
|
||||||
|
for (T i = begin; i != end; ++i) {
|
||||||
|
*i = rd();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static RandomDeviceSeed instance;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace p2pool
|
} // namespace p2pool
|
||||||
|
|
||||||
namespace robin_hood {
|
namespace robin_hood {
|
||||||
|
|
|
@ -19,7 +19,6 @@
|
||||||
#include "zmq_reader.h"
|
#include "zmq_reader.h"
|
||||||
#include "json_parsers.h"
|
#include "json_parsers.h"
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
#include <random>
|
|
||||||
|
|
||||||
static constexpr char log_category_prefix[] = "ZMQReader ";
|
static constexpr char log_category_prefix[] = "ZMQReader ";
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue