Fix wrong random using (#955)

Fixes #855
This commit is contained in:
Sergey Kuznetsov
2023-10-30 16:40:16 +00:00
committed by GitHub
parent 200d97f0de
commit b363cc93af
5 changed files with 89 additions and 16 deletions

View File

@@ -137,6 +137,7 @@ target_sources (clio PRIVATE
## Util
src/util/config/Config.cpp
src/util/log/Logger.cpp
src/util/Random.cpp
src/util/Taggable.cpp)
# Clio server

View File

@@ -24,6 +24,7 @@
#include <etl/Source.h>
#include <rpc/RPCHelpers.h>
#include <util/Profiler.h>
#include <util/Random.h>
#include <util/log/Logger.h>
#include <ripple/beast/net/IPEndpoint.h>
@@ -184,8 +185,10 @@ LoadBalancer::forwardToRippled(
boost::asio::yield_context yield
) const
{
srand(static_cast<unsigned>(time(0)));
auto sourceIdx = rand() % sources_.size();
std::size_t sourceIdx = 0;
if (!sources_.empty())
sourceIdx = util::Random::uniform(0ul, sources_.size());
auto numAttempts = 0u;
while (numAttempts < sources_.size()) {
@@ -228,8 +231,10 @@ template <class Func>
bool
LoadBalancer::execute(Func f, uint32_t ledgerSequence)
{
srand(static_cast<unsigned>(time(0)));
auto sourceIdx = rand() % sources_.size();
std::size_t sourceIdx = 0;
if (!sources_.empty())
sourceIdx = util::Random::uniform(0ul, sources_.size());
auto numAttempts = 0;
while (true) {

27
src/util/Random.cpp Normal file
View File

@@ -0,0 +1,27 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#include <util/Random.h>
#include <chrono>
namespace util {
std::mt19937_64 Random::generator_{std::chrono::system_clock::now().time_since_epoch().count()};
} // namespace util

44
src/util/Random.h Normal file
View File

@@ -0,0 +1,44 @@
//------------------------------------------------------------------------------
/*
This file is part of clio: https://github.com/XRPLF/clio
Copyright (c) 2023, the clio developers.
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#pragma once
#include <random>
namespace util {
class Random {
public:
template <typename T>
static T
uniform(T min, T max)
{
assert(min <= max);
if constexpr (std::is_floating_point_v<T>) {
std::uniform_real_distribution<T> distribution(min, max);
return distribution(generator_);
}
std::uniform_int_distribution<T> distribution(min, max);
return distribution(generator_);
}
private:
static std::mt19937_64 generator_;
};
} // namespace util

View File

@@ -23,6 +23,7 @@
#include <data/CassandraBackend.h>
#include <etl/NFTHelpers.h>
#include <rpc/RPCHelpers.h>
#include <util/Random.h>
#include <util/config/Config.h>
#include <boost/json/parse.hpp>
@@ -73,6 +74,8 @@ protected:
EXPECT_TRUE(handle.connect());
handle.execute("DROP KEYSPACE " + std::string{keyspace});
}
std::default_random_engine randomEngine{0};
};
TEST_F(BackendCassandraTest, Basic)
@@ -437,8 +440,6 @@ TEST_F(BackendCassandraTest, Basic)
EXPECT_FALSE(obj);
}
// obtain a time-based seed:
auto const seed = std::chrono::system_clock::now().time_since_epoch().count();
std::string accountBlobOld = accountBlob;
{
lgrInfoNext.seq = lgrInfoNext.seq + 1;
@@ -448,7 +449,7 @@ TEST_F(BackendCassandraTest, Basic)
lgrInfoNext.accountHash = ~(lgrInfoNext.accountHash ^ lgrInfoNext.txHash);
backend->writeLedger(lgrInfoNext, ledgerInfoToBinaryString(lgrInfoNext));
std::shuffle(accountBlob.begin(), accountBlob.end(), std::default_random_engine(seed));
std::shuffle(accountBlob.begin(), accountBlob.end(), randomEngine);
backend->writeLedgerObject(std::string{accountIndexBlob}, lgrInfoNext.seq, std::string{accountBlob});
ASSERT_TRUE(backend->finishWrites(lgrInfoNext.seq));
@@ -560,7 +561,6 @@ TEST_F(BackendCassandraTest, Basic)
auto generateAccountTx = [&](uint32_t ledgerSequence, auto txns) {
std::vector<AccountTransactionsData> ret;
auto accounts = generateAccounts(ledgerSequence, 10);
std::srand(std::time(nullptr));
uint32_t idx = 0;
for (auto& [hash, txn, meta] : txns) {
AccountTransactionsData data;
@@ -568,17 +568,16 @@ TEST_F(BackendCassandraTest, Basic)
data.transactionIndex = idx;
data.txHash = hash;
for (size_t i = 0; i < 3; ++i) {
data.accounts.insert(accounts[std::rand() % accounts.size()]);
data.accounts.insert(accounts[util::Random::uniform(0ul, accounts.size() - 1)]);
}
++idx;
ret.push_back(data);
}
return ret;
};
auto generateNextLedger = [seed](auto lgrInfo) {
auto generateNextLedger = [this](auto lgrInfo) {
++lgrInfo.seq;
lgrInfo.parentHash = lgrInfo.hash;
static auto randomEngine = std::default_random_engine(seed);
std::shuffle(lgrInfo.txHash.begin(), lgrInfo.txHash.end(), randomEngine);
std::shuffle(lgrInfo.accountHash.begin(), lgrInfo.accountHash.end(), randomEngine);
std::shuffle(lgrInfo.hash.begin(), lgrInfo.hash.end(), randomEngine);
@@ -967,8 +966,6 @@ TEST_F(BackendCassandraTest, CacheIntegration)
obj = backend->fetchLedgerObject(key256, lgrInfoOld.seq - 1, yield);
EXPECT_FALSE(obj);
}
// obtain a time-based seed:
unsigned const seed = std::chrono::system_clock::now().time_since_epoch().count();
std::string accountBlobOld = accountBlob;
{
backend->startWrites();
@@ -979,7 +976,7 @@ TEST_F(BackendCassandraTest, CacheIntegration)
lgrInfoNext.accountHash = ~(lgrInfoNext.accountHash ^ lgrInfoNext.txHash);
backend->writeLedger(lgrInfoNext, ledgerInfoToBinaryString(lgrInfoNext));
std::shuffle(accountBlob.begin(), accountBlob.end(), std::default_random_engine(seed));
std::shuffle(accountBlob.begin(), accountBlob.end(), randomEngine);
auto key = ripple::uint256::fromVoidChecked(accountIndexBlob);
backend->cache().update({{*key, {accountBlob.begin(), accountBlob.end()}}}, lgrInfoNext.seq);
backend->writeLedgerObject(std::string{accountIndexBlob}, lgrInfoNext.seq, std::string{accountBlob});
@@ -1065,10 +1062,9 @@ TEST_F(BackendCassandraTest, CacheIntegration)
return objs;
};
auto generateNextLedger = [seed](auto lgrInfo) {
auto generateNextLedger = [this](auto lgrInfo) {
++lgrInfo.seq;
lgrInfo.parentHash = lgrInfo.hash;
static auto randomEngine = std::default_random_engine(seed);
std::shuffle(lgrInfo.txHash.begin(), lgrInfo.txHash.end(), randomEngine);
std::shuffle(lgrInfo.accountHash.begin(), lgrInfo.accountHash.end(), randomEngine);
std::shuffle(lgrInfo.hash.begin(), lgrInfo.hash.end(), randomEngine);