mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-12 12:53:31 +02:00
refactor: make GetRand a template, remove GetRandInt
This commit is contained in:
parent
505ba39665
commit
ab1ea29ba1
@ -49,8 +49,7 @@ template <typename Data>
|
|||||||
bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data, int version)
|
bool SerializeFileDB(const std::string& prefix, const fs::path& path, const Data& data, int version)
|
||||||
{
|
{
|
||||||
// Generate random temporary filename
|
// Generate random temporary filename
|
||||||
uint16_t randv = 0;
|
const uint16_t randv{GetRand<uint16_t>()};
|
||||||
GetRandBytes({(unsigned char*)&randv, sizeof(randv)});
|
|
||||||
std::string tmpfn = strprintf("%s.%04x", prefix, randv);
|
std::string tmpfn = strprintf("%s.%04x", prefix, randv);
|
||||||
|
|
||||||
// open temp output file, and associate with CAutoFile
|
// open temp output file, and associate with CAutoFile
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock& block, bool fUseWTXID) :
|
CBlockHeaderAndShortTxIDs::CBlockHeaderAndShortTxIDs(const CBlock& block, bool fUseWTXID) :
|
||||||
nonce(GetRand(std::numeric_limits<uint64_t>::max())),
|
nonce(GetRand<uint64_t>()),
|
||||||
shorttxids(block.vtx.size() - 1), prefilledtxn(1), header(block) {
|
shorttxids(block.vtx.size() - 1), prefilledtxn(1), header(block) {
|
||||||
FillShortTxIDSelector();
|
FillShortTxIDSelector();
|
||||||
//TODO: Use our mempool prior to block acceptance to predictively fill more than just the coinbase
|
//TODO: Use our mempool prior to block acceptance to predictively fill more than just the coinbase
|
||||||
|
@ -239,7 +239,7 @@ bool CRollingBloomFilter::contains(Span<const unsigned char> vKey) const
|
|||||||
|
|
||||||
void CRollingBloomFilter::reset()
|
void CRollingBloomFilter::reset()
|
||||||
{
|
{
|
||||||
nTweak = GetRand(std::numeric_limits<unsigned int>::max());
|
nTweak = GetRand<unsigned int>();
|
||||||
nEntriesThisGeneration = 0;
|
nEntriesThisGeneration = 0;
|
||||||
nGeneration = 1;
|
nGeneration = 1;
|
||||||
std::fill(data.begin(), data.end(), 0);
|
std::fill(data.begin(), data.end(), 0);
|
||||||
|
@ -1268,8 +1268,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
|||||||
assert(!node.banman);
|
assert(!node.banman);
|
||||||
node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
|
node.banman = std::make_unique<BanMan>(gArgs.GetDataDirNet() / "banlist", &uiInterface, args.GetIntArg("-bantime", DEFAULT_MISBEHAVING_BANTIME));
|
||||||
assert(!node.connman);
|
assert(!node.connman);
|
||||||
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()),
|
node.connman = std::make_unique<CConnman>(GetRand<uint64_t>(),
|
||||||
GetRand(std::numeric_limits<uint64_t>::max()),
|
GetRand<uint64_t>(),
|
||||||
*node.addrman, *node.netgroupman, args.GetBoolArg("-networkactive", true));
|
*node.addrman, *node.netgroupman, args.GetBoolArg("-networkactive", true));
|
||||||
|
|
||||||
assert(!node.fee_estimator);
|
assert(!node.fee_estimator);
|
||||||
|
@ -2154,7 +2154,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
|
|||||||
|
|
||||||
if (fFeeler) {
|
if (fFeeler) {
|
||||||
// Add small amount of random noise before connection to avoid synchronization.
|
// Add small amount of random noise before connection to avoid synchronization.
|
||||||
int randsleep = GetRandInt(FEELER_SLEEP_WINDOW * 1000);
|
int randsleep = GetRand<int>(FEELER_SLEEP_WINDOW * 1000);
|
||||||
if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
|
if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
|
||||||
return;
|
return;
|
||||||
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
|
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
|
||||||
|
@ -4470,10 +4470,10 @@ void PeerManagerImpl::MaybeSendPing(CNode& node_to, Peer& peer, std::chrono::mic
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pingSend) {
|
if (pingSend) {
|
||||||
uint64_t nonce = 0;
|
uint64_t nonce;
|
||||||
while (nonce == 0) {
|
do {
|
||||||
GetRandBytes({(unsigned char*)&nonce, sizeof(nonce)});
|
nonce = GetRand<uint64_t>();
|
||||||
}
|
} while (nonce == 0);
|
||||||
peer.m_ping_queued = false;
|
peer.m_ping_queued = false;
|
||||||
peer.m_ping_start = now;
|
peer.m_ping_start = now;
|
||||||
if (node_to.GetCommonVersion() > BIP0031_VERSION) {
|
if (node_to.GetCommonVersion() > BIP0031_VERSION) {
|
||||||
|
@ -556,8 +556,8 @@ class CServiceHash
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
CServiceHash()
|
CServiceHash()
|
||||||
: m_salt_k0{GetRand(std::numeric_limits<uint64_t>::max())},
|
: m_salt_k0{GetRand<uint64_t>()},
|
||||||
m_salt_k1{GetRand(std::numeric_limits<uint64_t>::max())}
|
m_salt_k1{GetRand<uint64_t>()}
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -586,16 +586,11 @@ void RandAddEvent(const uint32_t event_info) noexcept { GetRNGState().AddEvent(e
|
|||||||
|
|
||||||
bool g_mock_deterministic_tests{false};
|
bool g_mock_deterministic_tests{false};
|
||||||
|
|
||||||
uint64_t GetRand(uint64_t nMax) noexcept
|
uint64_t GetRandInternal(uint64_t nMax) noexcept
|
||||||
{
|
{
|
||||||
return FastRandomContext(g_mock_deterministic_tests).randrange(nMax);
|
return FastRandomContext(g_mock_deterministic_tests).randrange(nMax);
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetRandInt(int nMax) noexcept
|
|
||||||
{
|
|
||||||
return GetRand(nMax);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint256 GetRandHash() noexcept
|
uint256 GetRandHash() noexcept
|
||||||
{
|
{
|
||||||
uint256 hash;
|
uint256 hash;
|
||||||
|
13
src/random.h
13
src/random.h
@ -69,7 +69,17 @@
|
|||||||
*/
|
*/
|
||||||
void GetRandBytes(Span<unsigned char> bytes) noexcept;
|
void GetRandBytes(Span<unsigned char> bytes) noexcept;
|
||||||
/** Generate a uniform random integer in the range [0..range). Precondition: range > 0 */
|
/** Generate a uniform random integer in the range [0..range). Precondition: range > 0 */
|
||||||
uint64_t GetRand(uint64_t nMax) noexcept;
|
uint64_t GetRandInternal(uint64_t nMax) noexcept;
|
||||||
|
/** Generate a uniform random integer of type T in the range [0..nMax)
|
||||||
|
* nMax defaults to std::numeric_limits<T>::max()
|
||||||
|
* Precondition: nMax > 0, T is an integral type, no larger than uint64_t
|
||||||
|
*/
|
||||||
|
template<typename T>
|
||||||
|
T GetRand(T nMax=std::numeric_limits<T>::max()) noexcept {
|
||||||
|
static_assert(std::is_integral<T>(), "T must be integral");
|
||||||
|
static_assert(std::numeric_limits<T>::max() <= std::numeric_limits<uint64_t>::max(), "GetRand only supports up to uint64_t");
|
||||||
|
return T(GetRandInternal(nMax));
|
||||||
|
}
|
||||||
/** Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */
|
/** Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */
|
||||||
template <typename D>
|
template <typename D>
|
||||||
D GetRandomDuration(typename std::common_type<D>::type max) noexcept
|
D GetRandomDuration(typename std::common_type<D>::type max) noexcept
|
||||||
@ -95,7 +105,6 @@ constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
|
|||||||
* */
|
* */
|
||||||
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval);
|
std::chrono::microseconds GetExponentialRand(std::chrono::microseconds now, std::chrono::seconds average_interval);
|
||||||
|
|
||||||
int GetRandInt(int nMax) noexcept;
|
|
||||||
uint256 GetRandHash() noexcept;
|
uint256 GetRandHash() noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,8 +26,8 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
|
|||||||
FastRandomContext ctx2(true);
|
FastRandomContext ctx2(true);
|
||||||
|
|
||||||
for (int i = 10; i > 0; --i) {
|
for (int i = 10; i > 0; --i) {
|
||||||
BOOST_CHECK_EQUAL(GetRand(std::numeric_limits<uint64_t>::max()), uint64_t{10393729187455219830U});
|
BOOST_CHECK_EQUAL(GetRand<uint64_t>(), uint64_t{10393729187455219830U});
|
||||||
BOOST_CHECK_EQUAL(GetRandInt(std::numeric_limits<int>::max()), int{769702006});
|
BOOST_CHECK_EQUAL(GetRand<int>(), int{769702006});
|
||||||
BOOST_CHECK_EQUAL(GetRandMicros(std::chrono::hours{1}).count(), 2917185654);
|
BOOST_CHECK_EQUAL(GetRandMicros(std::chrono::hours{1}).count(), 2917185654);
|
||||||
BOOST_CHECK_EQUAL(GetRandMillis(std::chrono::hours{1}).count(), 2144374);
|
BOOST_CHECK_EQUAL(GetRandMillis(std::chrono::hours{1}).count(), 2144374);
|
||||||
}
|
}
|
||||||
@ -47,8 +47,8 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
|
|||||||
// Check that a nondeterministic ones are not
|
// Check that a nondeterministic ones are not
|
||||||
g_mock_deterministic_tests = false;
|
g_mock_deterministic_tests = false;
|
||||||
for (int i = 10; i > 0; --i) {
|
for (int i = 10; i > 0; --i) {
|
||||||
BOOST_CHECK(GetRand(std::numeric_limits<uint64_t>::max()) != uint64_t{10393729187455219830U});
|
BOOST_CHECK(GetRand<uint64_t>() != uint64_t{10393729187455219830U});
|
||||||
BOOST_CHECK(GetRandInt(std::numeric_limits<int>::max()) != int{769702006});
|
BOOST_CHECK(GetRand<int>() != int{769702006});
|
||||||
BOOST_CHECK(GetRandMicros(std::chrono::hours{1}) != std::chrono::microseconds{2917185654});
|
BOOST_CHECK(GetRandMicros(std::chrono::hours{1}) != std::chrono::microseconds{2917185654});
|
||||||
BOOST_CHECK(GetRandMillis(std::chrono::hours{1}) != std::chrono::milliseconds{2144374});
|
BOOST_CHECK(GetRandMillis(std::chrono::hours{1}) != std::chrono::milliseconds{2144374});
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,10 @@
|
|||||||
#include <random.h>
|
#include <random.h>
|
||||||
#include <util/bytevectorhash.h>
|
#include <util/bytevectorhash.h>
|
||||||
|
|
||||||
ByteVectorHash::ByteVectorHash()
|
ByteVectorHash::ByteVectorHash() :
|
||||||
|
m_k0(GetRand<uint64_t>()),
|
||||||
|
m_k1(GetRand<uint64_t>())
|
||||||
{
|
{
|
||||||
GetRandBytes({reinterpret_cast<unsigned char*>(&m_k0), sizeof(m_k0)});
|
|
||||||
GetRandBytes({reinterpret_cast<unsigned char*>(&m_k1), sizeof(m_k1)});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ByteVectorHash::operator()(const std::vector<unsigned char>& input) const
|
size_t ByteVectorHash::operator()(const std::vector<unsigned char>& input) const
|
||||||
|
@ -7,11 +7,11 @@
|
|||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand<uint64_t>()), k1(GetRand<uint64_t>()) {}
|
||||||
|
|
||||||
SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand<uint64_t>()), k1(GetRand<uint64_t>()) {}
|
||||||
|
|
||||||
SaltedSipHasher::SaltedSipHasher() : m_k0(GetRand(std::numeric_limits<uint64_t>::max())), m_k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
SaltedSipHasher::SaltedSipHasher() : m_k0(GetRand<uint64_t>()), m_k1(GetRand<uint64_t>()) {}
|
||||||
|
|
||||||
size_t SaltedSipHasher::operator()(const Span<const unsigned char>& script) const
|
size_t SaltedSipHasher::operator()(const Span<const unsigned char>& script) const
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user