diff --git a/src/addrman.cpp b/src/addrman.cpp --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -13,12 +13,10 @@ int CAddrInfo::GetTriedBucket(const uint256 &nKey) const { uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()) - .GetHash() .GetCheapHash(); uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)) - .GetHash() .GetCheapHash(); return hash2 % ADDRMAN_TRIED_BUCKET_COUNT; } @@ -27,12 +25,10 @@ std::vector vchSourceGroupKey = src.GetGroup(); uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << vchSourceGroupKey) - .GetHash() .GetCheapHash(); uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)) - .GetHash() .GetCheapHash(); return hash2 % ADDRMAN_NEW_BUCKET_COUNT; } @@ -41,7 +37,6 @@ int nBucket) const { uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()) - .GetHash() .GetCheapHash(); return hash1 % ADDRMAN_BUCKET_SIZE; } diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -10,6 +10,7 @@ #include #include #include +#include // for ReadLE64 #include #include #include @@ -243,7 +244,12 @@ * Maintain a map of CBlockIndex for all known headers. */ struct BlockHasher { - size_t operator()(const uint256 &hash) const { return hash.GetCheapHash(); } + // this used to call `GetCheapHash()` in uint256, which was later moved; the + // cheap hash function simply calls ReadLE64() however, so the end result is + // identical + size_t operator()(const uint256 &hash) const { + return ReadLE64(hash.begin()); + } }; typedef std::unordered_map BlockMap; diff --git a/src/hash.h b/src/hash.h --- a/src/hash.h +++ b/src/hash.h @@ -6,6 +6,7 @@ #ifndef BITCOIN_HASH_H #define BITCOIN_HASH_H +#include #include #include #include @@ -141,6 +142,15 @@ return result; } + /** + * Returns the first 64 bits from the resulting hash. + */ + inline uint64_t GetCheapHash() { + uint8_t result[CHash256::OUTPUT_SIZE]; + ctx.Finalize(result); + return ReadLE64(result); + } + template CHashWriter &operator<<(const T &obj) { // Serialize to this stream ::Serialize(*this, obj); diff --git a/src/uint256.h b/src/uint256.h --- a/src/uint256.h +++ b/src/uint256.h @@ -6,8 +6,6 @@ #ifndef BITCOIN_UINT256_H #define BITCOIN_UINT256_H -#include - #include #include #include @@ -124,14 +122,6 @@ public: uint256() {} explicit uint256(const std::vector &vch) : base_blob<256>(vch) {} - - /** - * A cheap hash function that just returns 64 bits from the result, it can - * be used when the contents are considered uniformly random. It is not - * appropriate when the value can easily be influenced from outside as e.g. - * a network adversary could provide values to trigger worst-case behavior. - */ - uint64_t GetCheapHash() const { return ReadLE64(data); } }; /**