Skip to content

Commit 8b28f5f

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#8020: Use SipHash-2-4 for various non-cryptographic hashes
a68ec21 Use SipHash-2-4 for address relay selection (Pieter Wuille) 8cc9cfe Switch CTxMempool::mapTx to use a hash index for txids (Pieter Wuille) 382c871 Use SipHash-2-4 for CCoinsCache index (Pieter Wuille) 0b1295b Add SipHash-2-4 primitives to hash (Pieter Wuille)
1 parent a0afc3e commit 8b28f5f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/net_processing.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -761,20 +761,18 @@ static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connma
761761
// Relay to a limited number of other nodes
762762
// Use deterministic randomness to send to the same nodes for 24 hours
763763
// at a time so the addrKnowns of the chosen nodes prevent repeats
764-
static uint256 hashSalt;
765-
if (hashSalt.IsNull())
766-
hashSalt = GetRandHash();
764+
static uint64_t salt0 = 0, salt1 = 0;
765+
while (salt0 == 0 && salt1 == 0) {
766+
GetRandBytes((unsigned char*)&salt0, sizeof(salt0));
767+
GetRandBytes((unsigned char*)&salt1, sizeof(salt1));
768+
}
767769
uint64_t hashAddr = addr.GetHash();
768-
uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)));
769-
hashRand = Hash(BEGIN(hashRand), END(hashRand));
770-
std::multimap<uint256, CNode*> mapMix;
770+
multimap<uint64_t, CNode*> mapMix;
771+
const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
771772

772-
auto sortfunc = [&mapMix, &hashRand](CNode* pnode) {
773+
auto sortfunc = [&mapMix, &hasher](CNode* pnode) {
773774
if (pnode->nVersion >= CADDR_TIME_VERSION) {
774-
unsigned int nPointer;
775-
memcpy(&nPointer, &pnode, sizeof(nPointer));
776-
uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
777-
hashKey = Hash(BEGIN(hashKey), END(hashKey));
775+
uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
778776
mapMix.emplace(hashKey, pnode);
779777
}
780778
};

src/txmempool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#undef foreach
2020
#include "boost/multi_index_container.hpp"
2121
#include "boost/multi_index/ordered_index.hpp"
22+
#include "boost/multi_index/hashed_index.hpp"
2223

2324
class CAutoFile;
2425
class CBlockIndex;
@@ -439,7 +440,7 @@ class CTxMemPool
439440
CTxMemPoolEntry,
440441
boost::multi_index::indexed_by<
441442
// sorted by txid
442-
boost::multi_index::ordered_unique<mempoolentry_txid>,
443+
boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
443444
// sorted by fee rate
444445
boost::multi_index::ordered_non_unique<
445446
boost::multi_index::tag<descendant_score>,

0 commit comments

Comments
 (0)