Browse Source

Refactor: GetRandHash() method for util

0.8
Gavin Andresen 13 years ago
parent
commit
f718aedd9f
  1. 8
      src/main.cpp
  2. 12
      src/test/DoS_tests.cpp
  3. 6
      src/util.cpp
  4. 1
      src/util.h

8
src/main.cpp

@ -201,9 +201,7 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
while (mapOrphanTransactions.size() > nMaxOrphans) while (mapOrphanTransactions.size() > nMaxOrphans)
{ {
// Evict a random orphan: // Evict a random orphan:
std::vector<unsigned char> randbytes(32); uint256 randomhash = GetRandHash();
RAND_bytes(&randbytes[0], 32);
uint256 randomhash(randbytes);
map<uint256, CDataStream*>::iterator it = mapOrphanTransactions.lower_bound(randomhash); map<uint256, CDataStream*>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
if (it == mapOrphanTransactions.end()) if (it == mapOrphanTransactions.end())
it = mapOrphanTransactions.begin(); it = mapOrphanTransactions.begin();
@ -2354,7 +2352,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
// at a time so the setAddrKnowns of the chosen nodes prevent repeats // at a time so the setAddrKnowns of the chosen nodes prevent repeats
static uint256 hashSalt; static uint256 hashSalt;
if (hashSalt == 0) if (hashSalt == 0)
RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt)); hashSalt = GetRandHash();
int64 hashAddr = addr.GetHash(); int64 hashAddr = addr.GetHash();
uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)); uint256 hashRand = hashSalt ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60));
hashRand = Hash(BEGIN(hashRand), END(hashRand)); hashRand = Hash(BEGIN(hashRand), END(hashRand));
@ -2945,7 +2943,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
// 1/4 of tx invs blast to all immediately // 1/4 of tx invs blast to all immediately
static uint256 hashSalt; static uint256 hashSalt;
if (hashSalt == 0) if (hashSalt == 0)
RAND_bytes((unsigned char*)&hashSalt, sizeof(hashSalt)); hashSalt = GetRandHash();
uint256 hashRand = inv.hash ^ hashSalt; uint256 hashRand = inv.hash ^ hashSalt;
hashRand = Hash(BEGIN(hashRand), END(hashRand)); hashRand = Hash(BEGIN(hashRand), END(hashRand));
bool fTrickleWait = ((hashRand & 3) != 0); bool fTrickleWait = ((hashRand & 3) != 0);

12
src/test/DoS_tests.cpp

@ -129,18 +129,10 @@ BOOST_AUTO_TEST_CASE(DoS_checknbits)
} }
static uint256 RandomHash()
{
std::vector<unsigned char> randbytes(32);
RAND_bytes(&randbytes[0], 32);
uint256 randomhash(randbytes);
return randomhash;
}
CTransaction RandomOrphan() CTransaction RandomOrphan()
{ {
std::map<uint256, CDataStream*>::iterator it; std::map<uint256, CDataStream*>::iterator it;
it = mapOrphanTransactions.lower_bound(RandomHash()); it = mapOrphanTransactions.lower_bound(GetRandHash());
if (it == mapOrphanTransactions.end()) if (it == mapOrphanTransactions.end())
it = mapOrphanTransactions.begin(); it = mapOrphanTransactions.begin();
const CDataStream* pvMsg = it->second; const CDataStream* pvMsg = it->second;
@ -162,7 +154,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
CTransaction tx; CTransaction tx;
tx.vin.resize(1); tx.vin.resize(1);
tx.vin[0].prevout.n = 0; tx.vin[0].prevout.n = 0;
tx.vin[0].prevout.hash = RandomHash(); tx.vin[0].prevout.hash = GetRandHash();
tx.vin[0].scriptSig << OP_1; tx.vin[0].scriptSig << OP_1;
tx.vout.resize(1); tx.vout.resize(1);
tx.vout[0].nValue = 1*CENT; tx.vout[0].nValue = 1*CENT;

6
src/util.cpp

@ -174,6 +174,12 @@ int GetRandInt(int nMax)
return GetRand(nMax); return GetRand(nMax);
} }
uint256 GetRandHash()
{
uint256 hash;
RAND_bytes((unsigned char*)&hash, sizeof(hash));
return hash;
}

1
src/util.h

@ -168,6 +168,7 @@ bool SetStartOnSystemStartup(bool fAutoStart);
void ShrinkDebugFile(); void ShrinkDebugFile();
int GetRandInt(int nMax); int GetRandInt(int nMax);
uint64 GetRand(uint64 nMax); uint64 GetRand(uint64 nMax);
uint256 GetRandHash();
int64 GetTime(); int64 GetTime();
void SetMockTime(int64 nMockTimeIn); void SetMockTime(int64 nMockTimeIn);
int64 GetAdjustedTime(); int64 GetAdjustedTime();

Loading…
Cancel
Save