Skip to content

Commit a68ec21

Browse files
committed
Use SipHash-2-4 for address relay selection
1 parent 8cc9cfe commit a68ec21

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/main.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4698,25 +4698,23 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
46984698
LOCK(cs_vNodes);
46994699
// Use deterministic randomness to send to the same nodes for 24 hours
47004700
// at a time so the addrKnowns of the chosen nodes prevent repeats
4701-
static uint256 hashSalt;
4702-
if (hashSalt.IsNull())
4703-
hashSalt = GetRandHash();
4701+
static uint64_t salt0 = 0, salt1 = 0;
4702+
while (salt0 == 0 && salt1 == 0) {
4703+
GetRandBytes((unsigned char*)&salt0, sizeof(salt0));
4704+
GetRandBytes((unsigned char*)&salt1, sizeof(salt1));
4705+
}
47044706
uint64_t hashAddr = addr.GetHash();
4705-
uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)));
4706-
hashRand = Hash(BEGIN(hashRand), END(hashRand));
4707-
multimap<uint256, CNode*> mapMix;
4707+
multimap<uint64_t, CNode*> mapMix;
4708+
const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
47084709
BOOST_FOREACH(CNode* pnode, vNodes)
47094710
{
47104711
if (pnode->nVersion < CADDR_TIME_VERSION)
47114712
continue;
4712-
unsigned int nPointer;
4713-
memcpy(&nPointer, &pnode, sizeof(nPointer));
4714-
uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
4715-
hashKey = Hash(BEGIN(hashKey), END(hashKey));
4713+
uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
47164714
mapMix.insert(make_pair(hashKey, pnode));
47174715
}
47184716
int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
4719-
for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
4717+
for (multimap<uint64_t, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
47204718
((*mi).second)->PushAddress(addr);
47214719
}
47224720
}

0 commit comments

Comments
 (0)