mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
Internal RNG for approximateBestSubset to prevent degenerate behavior.
This fixes test_bitcoin failures on openbsd reported by dhill on IRC. On some systems rand() is a simple LCG over 2^31 and so it produces an even-odd sequence. ApproximateBestSubset was only using the least significant bit and so every run of the iterative solver would be the same for some inputs, resulting in some pretty dumb decisions. Using something other than the least significant bit would paper over the issue but who knows what other way a system's rand() might get us here. Instead we use an internal RNG with a period of something like 2^60 which is well behaved. This also makes it possible to make the selection deterministic for the tests, if we wanted to implement that.
This commit is contained in:
26
src/util.cpp
26
src/util.cpp
@@ -1276,12 +1276,26 @@ void AddTimeData(const CNetAddr& ip, int64 nTime)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
uint32_t insecure_rand_Rz = 11;
|
||||
uint32_t insecure_rand_Rw = 11;
|
||||
void seed_insecure_rand(bool fDeterministic)
|
||||
{
|
||||
//The seed values have some unlikely fixed points which we avoid.
|
||||
if(fDeterministic)
|
||||
{
|
||||
insecure_rand_Rz = insecure_rand_Rw = 11;
|
||||
} else {
|
||||
uint32_t tmp;
|
||||
do{
|
||||
RAND_bytes((unsigned char*)&tmp,4);
|
||||
}while(tmp==0 || tmp==0x9068ffffU);
|
||||
insecure_rand_Rz=tmp;
|
||||
do{
|
||||
RAND_bytes((unsigned char*)&tmp,4);
|
||||
}while(tmp==0 || tmp==0x464fffffU);
|
||||
insecure_rand_Rw=tmp;
|
||||
}
|
||||
}
|
||||
|
||||
string FormatVersion(int nVersion)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user