Browse Source

Use SipHash-2-4 for address relay selection

0.13
Pieter Wuille 9 years ago
parent
commit
a68ec21f7e
  1. 20
      src/main.cpp

20
src/main.cpp

@ -4698,25 +4698,23 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
LOCK(cs_vNodes); LOCK(cs_vNodes);
// Use deterministic randomness to send to the same nodes for 24 hours // Use deterministic randomness to send to the same nodes for 24 hours
// at a time so the addrKnowns of the chosen nodes prevent repeats // at a time so the addrKnowns of the chosen nodes prevent repeats
static uint256 hashSalt; static uint64_t salt0 = 0, salt1 = 0;
if (hashSalt.IsNull()) while (salt0 == 0 && salt1 == 0) {
hashSalt = GetRandHash(); GetRandBytes((unsigned char*)&salt0, sizeof(salt0));
GetRandBytes((unsigned char*)&salt1, sizeof(salt1));
}
uint64_t hashAddr = addr.GetHash(); uint64_t hashAddr = addr.GetHash();
uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60))); multimap<uint64_t, CNode*> mapMix;
hashRand = Hash(BEGIN(hashRand), END(hashRand)); const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
multimap<uint256, CNode*> mapMix;
BOOST_FOREACH(CNode* pnode, vNodes) BOOST_FOREACH(CNode* pnode, vNodes)
{ {
if (pnode->nVersion < CADDR_TIME_VERSION) if (pnode->nVersion < CADDR_TIME_VERSION)
continue; continue;
unsigned int nPointer; uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
memcpy(&nPointer, &pnode, sizeof(nPointer));
uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
hashKey = Hash(BEGIN(hashKey), END(hashKey));
mapMix.insert(make_pair(hashKey, pnode)); mapMix.insert(make_pair(hashKey, pnode));
} }
int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s) int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi) for (multimap<uint64_t, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
((*mi).second)->PushAddress(addr); ((*mi).second)->PushAddress(addr);
} }
} }

Loading…
Cancel
Save