diff --git a/src/common.h b/src/common.h index 2f37724..6b95ccc 100644 --- a/src/common.h +++ b/src/common.h @@ -54,6 +54,7 @@ #include #include #include +#include #include diff --git a/src/crypto.cpp b/src/crypto.cpp index 239ddef..55a60cc 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -19,7 +19,6 @@ #include "crypto.h" #include "keccak.h" #include "uv_util.h" -#include extern "C" { #include "crypto-ops.h" @@ -32,7 +31,7 @@ namespace { class RandomBytes { public: - RandomBytes() : rng(s), dist(0, 255) + RandomBytes() : rng(RandomDeviceSeed::instance), dist(0, 255) { uv_mutex_init_checked(&m); @@ -57,22 +56,6 @@ public: private: 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 - 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::uniform_int_distribution<> dist; }; diff --git a/src/p2p_server.cpp b/src/p2p_server.cpp index 8cecb0b..6b7ed8c 100644 --- a/src/p2p_server.cpp +++ b/src/p2p_server.cpp @@ -47,15 +47,18 @@ P2PServer::P2PServer(p2pool* pool) , m_cache(pool->params().m_blockCache ? new BlockCache() : nullptr) , m_cacheLoaded(false) , m_initialPeerList(pool->params().m_p2pPeerList) - , m_rd{} - , m_rng(m_rd()) + , m_rng(RandomDeviceSeed::instance) , m_block(new PoolBlock()) , m_timer{} , m_timerCounter(0) , m_timerInterval(2) - , m_peerId(m_rng()) , 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_incoming_peers(pool->params().m_maxIncomingPeers); diff --git a/src/p2p_server.h b/src/p2p_server.h index 1fc1a50..0bc1492 100644 --- a/src/p2p_server.h +++ b/src/p2p_server.h @@ -18,7 +18,6 @@ #pragma once #include "tcp_server.h" -#include namespace p2pool { @@ -170,7 +169,6 @@ private: void remove_peer_from_list(const raw_ip& ip); uv_mutex_t m_rngLock; - std::random_device m_rd; std::mt19937_64 m_rng; uv_mutex_t m_blockLock; diff --git a/src/stratum_server.cpp b/src/stratum_server.cpp index e920be4..65a00ab 100644 --- a/src/stratum_server.cpp +++ b/src/stratum_server.cpp @@ -39,8 +39,7 @@ StratumServer::StratumServer(p2pool* pool) : TCPServer(StratumClient::allocate) , m_pool(pool) , m_extraNonce(0) - , m_rd{} - , m_rng(m_rd()) + , m_rng(RandomDeviceSeed::instance) , m_cumulativeHashes(0) , m_cumulativeHashesAtLastShare(0) , m_hashrateDataHead(0) @@ -51,6 +50,9 @@ StratumServer::StratumServer(p2pool* pool) , m_totalFoundShares(0) , m_apiLastUpdateTime(0) { + // Diffuse the initial state in case it has low quality + m_rng.discard(10000); + m_hashrateData[0] = { time(nullptr), 0 }; uv_mutex_init_checked(&m_blobsQueueLock); diff --git a/src/stratum_server.h b/src/stratum_server.h index 3bb6535..02e2bfe 100644 --- a/src/stratum_server.h +++ b/src/stratum_server.h @@ -19,7 +19,6 @@ #include "tcp_server.h" #include -#include namespace p2pool { @@ -109,7 +108,6 @@ private: std::atomic m_extraNonce; uv_mutex_t m_rngLock; - std::random_device m_rd; std::mt19937_64 m_rng; struct SubmittedShare diff --git a/src/util.cpp b/src/util.cpp index 3498aa7..2b342e8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -382,4 +382,6 @@ bool resolve_host(std::string& host, bool& is_v6) return true; } +RandomDeviceSeed RandomDeviceSeed::instance; + } // namespace p2pool diff --git a/src/util.h b/src/util.h index 74f2f2a..c421ef4 100644 --- a/src/util.h +++ b/src/util.h @@ -145,6 +145,24 @@ using unordered_map = robin_hood::detail::Table using unordered_set = robin_hood::detail::Table, std::equal_to>; +// 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 + 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 robin_hood { diff --git a/src/zmq_reader.cpp b/src/zmq_reader.cpp index c912dfc..cd0125d 100644 --- a/src/zmq_reader.cpp +++ b/src/zmq_reader.cpp @@ -19,7 +19,6 @@ #include "zmq_reader.h" #include "json_parsers.h" #include -#include static constexpr char log_category_prefix[] = "ZMQReader ";