Skip to content

Commit bf2e010

Browse files
committed
uint256: Remove unnecessary crypto/common.h use
1 parent cb25cd6 commit bf2e010

File tree

5 files changed

+21
-18
lines changed

5 files changed

+21
-18
lines changed

src/addrman.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,22 @@
1111

1212
int CAddrInfo::GetTriedBucket(const uint256& nKey) const
1313
{
14-
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetHash().GetCheapHash();
15-
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetHash().GetCheapHash();
14+
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
15+
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
1616
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
1717
}
1818

1919
int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src) const
2020
{
2121
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup();
22-
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << vchSourceGroupKey).GetHash().GetCheapHash();
23-
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetHash().GetCheapHash();
22+
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << vchSourceGroupKey).GetCheapHash();
23+
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
2424
return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
2525
}
2626

2727
int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
2828
{
29-
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetHash().GetCheapHash();
29+
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash();
3030
return hash1 % ADDRMAN_BUCKET_SIZE;
3131
}
3232

src/hash.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_HASH_H
77
#define BITCOIN_HASH_H
88

9+
#include <crypto/common.h>
910
#include <crypto/ripemd160.h>
1011
#include <crypto/sha256.h>
1112
#include <prevector.h>
@@ -138,6 +139,15 @@ class CHashWriter
138139
return result;
139140
}
140141

142+
/**
143+
* Returns the first 64 bits from the resulting hash.
144+
*/
145+
inline uint64_t GetCheapHash() {
146+
unsigned char result[CHash256::OUTPUT_SIZE];
147+
ctx.Finalize(result);
148+
return ReadLE64(result);
149+
}
150+
141151
template<typename T>
142152
CHashWriter& operator<<(const T& obj) {
143153
// Serialize to this stream

src/test/addrman_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class CAddrManTest : public CAddrMan
3434

3535
int RandomInt(int nMax) override
3636
{
37-
state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash();
37+
state = (CHashWriter(SER_GETHASH, 0) << state).GetCheapHash();
3838
return (unsigned int)(state % nMax);
3939
}
4040

src/uint256.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <stdint.h>
1313
#include <string>
1414
#include <vector>
15-
#include <crypto/common.h>
1615

1716
/** Template base class for fixed-sized opaque blobs. */
1817
template<unsigned int BITS>
@@ -123,16 +122,6 @@ class uint256 : public base_blob<256> {
123122
public:
124123
uint256() {}
125124
explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {}
126-
127-
/** A cheap hash function that just returns 64 bits from the result, it can be
128-
* used when the contents are considered uniformly random. It is not appropriate
129-
* when the value can easily be influenced from outside as e.g. a network adversary could
130-
* provide values to trigger worst-case behavior.
131-
*/
132-
uint64_t GetCheapHash() const
133-
{
134-
return ReadLE64(data);
135-
}
136125
};
137126

138127
/* uint256 from const char *.

src/validation.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <amount.h>
1414
#include <coins.h>
15+
#include <crypto/common.h> // for ReadLE64
1516
#include <fs.h>
1617
#include <protocol.h> // For CMessageHeader::MessageStartChars
1718
#include <policy/feerate.h>
@@ -138,7 +139,10 @@ static const int DEFAULT_STOPATHEIGHT = 0;
138139

139140
struct BlockHasher
140141
{
141-
size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }
142+
// this used to call `GetCheapHash()` in uint256, which was later moved; the
143+
// cheap hash function simply calls ReadLE64() however, so the end result is
144+
// identical
145+
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
142146
};
143147

144148
extern CScript COINBASE_FLAGS;

0 commit comments

Comments
 (0)