Page MenuHomePhabricator

D4277.diff
No OneTemporary

D4277.diff

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<uint8_t> 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 <blockstatus.h>
#include <blockvalidity.h>
#include <consensus/params.h>
+#include <crypto/common.h> // for ReadLE64
#include <flatfile.h>
#include <primitives/block.h>
#include <sync.h>
@@ -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<uint256, CBlockIndex *, BlockHasher> 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 <crypto/common.h>
#include <crypto/ripemd160.h>
#include <crypto/sha256.h>
#include <prevector.h>
@@ -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 <typename T> 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 <crypto/common.h>
-
#include <cassert>
#include <cstdint>
#include <cstring>
@@ -124,14 +122,6 @@
public:
uint256() {}
explicit uint256(const std::vector<uint8_t> &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); }
};
/**

File Metadata

Mime Type
text/plain
Expires
Sat, Mar 1, 11:57 (2 h, 58 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5187743
Default Alt Text
D4277.diff (3 KB)

Event Timeline