uint256: Remove unnecessary crypto/common.h use

This commit is contained in:
Karl-Johan Alm 2018-05-17 14:50:15 +09:00
parent cb25cd6aa1
commit bf2e010973
No known key found for this signature in database
GPG key ID: 57AF762DB3353322
5 changed files with 21 additions and 18 deletions

View file

@ -11,22 +11,22 @@
int CAddrInfo::GetTriedBucket(const uint256& nKey) const int CAddrInfo::GetTriedBucket(const uint256& nKey) const
{ {
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetHash().GetCheapHash(); uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetHash().GetCheapHash(); uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
return hash2 % ADDRMAN_TRIED_BUCKET_COUNT; return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
} }
int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src) const int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src) const
{ {
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup(); std::vector<unsigned char> vchSourceGroupKey = src.GetGroup();
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << vchSourceGroupKey).GetHash().GetCheapHash(); uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup() << vchSourceGroupKey).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetHash().GetCheapHash(); uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
return hash2 % ADDRMAN_NEW_BUCKET_COUNT; return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
} }
int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const int CAddrInfo::GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
{ {
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetHash().GetCheapHash(); uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? 'N' : 'K') << nBucket << GetKey()).GetCheapHash();
return hash1 % ADDRMAN_BUCKET_SIZE; return hash1 % ADDRMAN_BUCKET_SIZE;
} }

View file

@ -6,6 +6,7 @@
#ifndef BITCOIN_HASH_H #ifndef BITCOIN_HASH_H
#define BITCOIN_HASH_H #define BITCOIN_HASH_H
#include <crypto/common.h>
#include <crypto/ripemd160.h> #include <crypto/ripemd160.h>
#include <crypto/sha256.h> #include <crypto/sha256.h>
#include <prevector.h> #include <prevector.h>
@ -138,6 +139,15 @@ public:
return result; return result;
} }
/**
* Returns the first 64 bits from the resulting hash.
*/
inline uint64_t GetCheapHash() {
unsigned char result[CHash256::OUTPUT_SIZE];
ctx.Finalize(result);
return ReadLE64(result);
}
template<typename T> template<typename T>
CHashWriter& operator<<(const T& obj) { CHashWriter& operator<<(const T& obj) {
// Serialize to this stream // Serialize to this stream

View file

@ -34,7 +34,7 @@ public:
int RandomInt(int nMax) override int RandomInt(int nMax) override
{ {
state = (CHashWriter(SER_GETHASH, 0) << state).GetHash().GetCheapHash(); state = (CHashWriter(SER_GETHASH, 0) << state).GetCheapHash();
return (unsigned int)(state % nMax); return (unsigned int)(state % nMax);
} }

View file

@ -12,7 +12,6 @@
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include <vector> #include <vector>
#include <crypto/common.h>
/** Template base class for fixed-sized opaque blobs. */ /** Template base class for fixed-sized opaque blobs. */
template<unsigned int BITS> template<unsigned int BITS>
@ -123,16 +122,6 @@ class uint256 : public base_blob<256> {
public: public:
uint256() {} uint256() {}
explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {} explicit uint256(const std::vector<unsigned char>& 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);
}
}; };
/* uint256 from const char *. /* uint256 from const char *.

View file

@ -12,6 +12,7 @@
#include <amount.h> #include <amount.h>
#include <coins.h> #include <coins.h>
#include <crypto/common.h> // for ReadLE64
#include <fs.h> #include <fs.h>
#include <protocol.h> // For CMessageHeader::MessageStartChars #include <protocol.h> // For CMessageHeader::MessageStartChars
#include <policy/feerate.h> #include <policy/feerate.h>
@ -138,7 +139,10 @@ static const int DEFAULT_STOPATHEIGHT = 0;
struct BlockHasher 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()); }
}; };
extern CScript COINBASE_FLAGS; extern CScript COINBASE_FLAGS;