Merge #13258: uint256: Remove unnecessary crypto/common.h dependency

bf2e01097 uint256: Remove unnecessary crypto/common.h use (Karl-Johan Alm)

Pull request description:

  This is an alternative to #13242 which keeps the `ReadLE64` part, but moves the `crypto/common.h` dependency into `crypto/common.h` as a function outside of `uint256`.

  **Reason:** this change will remove dependencies for `uint256` to `crypto/common.h`, `compat/endian.h`, and `compat/byteswap.h`.

  This PR removes the need to update tests to be endian-aware/-independent, but keeps the (arguably dubious) `ReadLE64` part (which was only introduced to fix the tests, not for any functionality).

Tree-SHA512: 78b35123cdb185b3b3ec59aba5ca8a5db72624d147f2d6a5484ffa5ce626a72f782a01dc6893fc8f5619b03e2eae7b5a03b0df5d43460f3bda428e719e188aec
This commit is contained in:
Wladimir J. van der Laan 2018-11-30 18:21:03 +01:00 committed by Vijay Das Manikpuri
parent 1d457affaa
commit d0724b5ee1
No known key found for this signature in database
GPG Key ID: DB1D81B01DB7C46E
13 changed files with 50 additions and 49 deletions

View File

@ -12,8 +12,8 @@
int CAddrInfo::GetTriedBucket(const uint256& nKey, const std::vector<bool> &asmap) const
{
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetHash().GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetHash().GetCheapHash();
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
int tried_bucket = hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
uint32_t mapped_as = GetMappedAS(asmap);
LogPrint(BCLog::NET, "IP %s mapped to AS%i belongs to tried bucket %i\n", ToStringIP(), mapped_as, tried_bucket);
@ -23,8 +23,8 @@ int CAddrInfo::GetTriedBucket(const uint256& nKey, const std::vector<bool> &asma
int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std::vector<bool> &asmap) const
{
std::vector<unsigned char> vchSourceGroupKey = src.GetGroup(asmap);
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << vchSourceGroupKey).GetHash().GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetHash().GetCheapHash();
uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetGroup(asmap) << vchSourceGroupKey).GetCheapHash();
uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
int new_bucket = hash2 % ADDRMAN_NEW_BUCKET_COUNT;
uint32_t mapped_as = GetMappedAS(asmap);
LogPrint(BCLog::NET, "IP %s mapped to AS%i belongs to new bucket %i\n", ToStringIP(), mapped_as, new_bucket);
@ -33,7 +33,7 @@ int CAddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const std:
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;
}

View File

@ -6,6 +6,7 @@
#define BITCOIN_EVO_DETERMINISTICMNS_H
#include <arith_uint256.h>
#include <crypto/common.h>
#include <evo/evodb.h>
#include <evo/providertx.h>
#include <saltedhasher.h>
@ -300,10 +301,16 @@ inline void SerReadWrite(Stream& s, immer::map<K, T, Hash, Equal>& obj, CSerActi
class CDeterministicMNList
{
private:
struct ImmerHasher
{
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
};
public:
using MnMap = immer::map<uint256, CDeterministicMNCPtr>;
using MnMap = immer::map<uint256, CDeterministicMNCPtr, ImmerHasher>;
using MnInternalIdMap = immer::map<uint64_t, uint256>;
using MnUniquePropertyMap = immer::map<uint256, std::pair<uint256, uint32_t> >;
using MnUniquePropertyMap = immer::map<uint256, std::pair<uint256, uint32_t>, ImmerHasher>;
private:
uint256 blockHash;

View File

@ -7,6 +7,7 @@
#ifndef BITCOIN_HASH_H
#define BITCOIN_HASH_H
#include <crypto/common.h>
#include <crypto/ripemd160.h>
#include <crypto/sha256.h>
#include <prevector.h>
@ -208,6 +209,15 @@ public:
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>
CHashWriter& operator<<(const T& obj) {
// Serialize to this stream

View File

@ -9,6 +9,7 @@
#include <llmq/signing.h>
#include <chainparams.h>
#include <crypto/common.h>
#include <net.h>
#include <saltedhasher.h>
#include <streams.h>
@ -70,9 +71,13 @@ private:
uint256 lastSignedMsgHash GUARDED_BY(cs);
// We keep track of txids from recently received blocks so that we can check if all TXs got islocked
using BlockTxs = std::unordered_map<uint256, std::shared_ptr<std::unordered_set<uint256, StaticSaltedHasher>>>;
struct BlockHasher
{
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
};
using BlockTxs = std::unordered_map<uint256, std::shared_ptr<std::unordered_set<uint256, StaticSaltedHasher>>, BlockHasher>;
BlockTxs blockTxs GUARDED_BY(cs);
std::unordered_map<uint256, int64_t> txFirstSeenTime GUARDED_BY(cs);
std::unordered_map<uint256, int64_t, StaticSaltedHasher> txFirstSeenTime GUARDED_BY(cs);
std::map<uint256, int64_t> seenChainLocks GUARDED_BY(cs);

View File

@ -169,7 +169,7 @@ void CInstantSendDb::WriteInstantSendLockArchived(CDBBatch& batch, const uint256
batch.Write(std::make_tuple(DB_ARCHIVED_BY_HASH, hash), true);
}
std::unordered_map<uint256, CInstantSendLockPtr> CInstantSendDb::RemoveConfirmedInstantSendLocks(int nUntilHeight)
std::unordered_map<uint256, CInstantSendLockPtr, StaticSaltedHasher> CInstantSendDb::RemoveConfirmedInstantSendLocks(int nUntilHeight)
{
LOCK(cs_db);
if (nUntilHeight <= best_confirmed_height) {
@ -186,7 +186,7 @@ std::unordered_map<uint256, CInstantSendLockPtr> CInstantSendDb::RemoveConfirmed
it->Seek(firstKey);
CDBBatch batch(*db);
std::unordered_map<uint256, CInstantSendLockPtr> ret;
std::unordered_map<uint256, CInstantSendLockPtr, StaticSaltedHasher> ret;
while (it->Valid()) {
decltype(firstKey) curKey;
if (!it->GetKey(curKey) || std::get<0>(curKey) != DB_MINED_BY_HEIGHT_AND_HASH) {
@ -900,12 +900,12 @@ bool CInstantSendManager::ProcessPendingInstantSendLocks()
return fMoreWork;
}
std::unordered_set<uint256> CInstantSendManager::ProcessPendingInstantSendLocks(int signOffset, const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban)
std::unordered_set<uint256, StaticSaltedHasher> CInstantSendManager::ProcessPendingInstantSendLocks(int signOffset, const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban)
{
auto llmqType = Params().GetConsensus().llmqTypeInstantSend;
CBLSBatchVerifier<NodeId, uint256> batchVerifier(false, true, 8);
std::unordered_map<uint256, CRecoveredSig> recSigs;
std::unordered_map<uint256, CRecoveredSig, StaticSaltedHasher> recSigs;
size_t verifyCount = 0;
size_t alreadyVerified = 0;
@ -977,7 +977,7 @@ std::unordered_set<uint256> CInstantSendManager::ProcessPendingInstantSendLocks(
LogPrint(BCLog::INSTANTSEND, "CInstantSendManager::%s -- verified locks. count=%d, alreadyVerified=%d, vt=%d, nodes=%d\n", __func__,
verifyCount, alreadyVerified, verifyTimer.count(), batchVerifier.GetUniqueSourceCount());
std::unordered_set<uint256> badISLocks;
std::unordered_set<uint256, StaticSaltedHasher> badISLocks;
if (ban && !batchVerifier.badSources.empty()) {
LOCK(cs_main);
@ -1335,7 +1335,7 @@ void CInstantSendManager::HandleFullyConfirmedBlock(const CBlockIndex* pindex)
void CInstantSendManager::RemoveMempoolConflictsForLock(const uint256& hash, const CInstantSendLock& islock)
{
std::unordered_map<uint256, CTransactionRef> toDelete;
std::unordered_map<uint256, CTransactionRef, StaticSaltedHasher> toDelete;
{
LOCK(mempool.cs);

View File

@ -117,7 +117,7 @@ public:
* @param nUntilHeight Removes all IS Locks confirmed up until nUntilHeight
* @return returns an unordered_map of the hash of the IS Locks and a pointer object to the IS Locks for all IS Locks which were removed
*/
std::unordered_map<uint256, CInstantSendLockPtr> RemoveConfirmedInstantSendLocks(int nUntilHeight);
std::unordered_map<uint256, CInstantSendLockPtr, StaticSaltedHasher> RemoveConfirmedInstantSendLocks(int nUntilHeight);
/**
* Removes IS Locks from the archive if the tx was confirmed 100 blocks before nUntilHeight
* @param nUntilHeight the height from which to base the remove of archive IS Locks
@ -230,7 +230,7 @@ private:
void ProcessMessageInstantSendLock(const CNode* pfrom, const CInstantSendLockPtr& islock) LOCKS_EXCLUDED(cs);
static bool PreVerifyInstantSendLock(const CInstantSendLock& islock);
bool ProcessPendingInstantSendLocks();
std::unordered_set<uint256> ProcessPendingInstantSendLocks(int signOffset, const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban);
std::unordered_set<uint256, StaticSaltedHasher> ProcessPendingInstantSendLocks(int signOffset, const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban);
void ProcessInstantSendLock(NodeId from, const uint256& hash, const CInstantSendLockPtr& islock);
void AddNonLockedTx(const CTransactionRef& tx, const CBlockIndex* pindexMined);

View File

@ -991,7 +991,7 @@ void CSigSharesManager::CollectSigSharesToSendConcentrated(std::unordered_map<No
{
AssertLockHeld(cs);
std::unordered_map<uint256, CNode*> proTxToNode;
std::unordered_map<uint256, CNode*, StaticSaltedHasher> proTxToNode;
for (const auto& pnode : vNodes) {
auto verifiedProRegTxHash = pnode->GetVerifiedProRegTxHash();
if (verifiedProRegTxHash.IsNull()) {

View File

@ -7,6 +7,7 @@
#include <hash.h>
#include <net.h>
#include <saltedhasher.h>
#include <util/strencodings.h>
#include <key.h>
@ -162,7 +163,7 @@ private:
mutable std::unordered_map<const SporkId, bool> mapSporksCachedActive GUARDED_BY(cs);
mutable std::unordered_map<SporkId, int64_t> mapSporksCachedValues GUARDED_BY(cs);
std::unordered_map<uint256, CSporkMessage> mapSporksByHash GUARDED_BY(cs);
std::unordered_map<uint256, CSporkMessage, StaticSaltedHasher> mapSporksByHash GUARDED_BY(cs);
std::unordered_map<SporkId, std::map<CKeyID, CSporkMessage> > mapSporksActive GUARDED_BY(cs);
std::set<CKeyID> setSporkPubKeyIDs GUARDED_BY(cs);

View File

@ -139,7 +139,7 @@ size_t CCoinsViewDB::EstimateSize() const
return db.EstimateSize(DB_COIN, (char)(DB_COIN+1));
}
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(gArgs.IsArgSet("-blocksdir") ? GetDataDir() / "blocks" / "index" : GetBlocksDir() / "index", nCacheSize, fMemory, fWipe), mapHasTxIndexCache(10000, 20000) {
CBlockTreeDB::CBlockTreeDB(size_t nCacheSize, bool fMemory, bool fWipe) : CDBWrapper(gArgs.IsArgSet("-blocksdir") ? GetDataDir() / "blocks" / "index" : GetBlocksDir() / "index", nCacheSize, fMemory, fWipe) {
}
bool CBlockTreeDB::ReadBlockFileInfo(int nFile, CBlockFileInfo &info) {

View File

@ -9,10 +9,8 @@
#include <coins.h>
#include <dbwrapper.h>
#include <chain.h>
#include <limitedmap.h>
#include <primitives/block.h>
#include <spentindex.h>
#include <sync.h>
#include <map>
#include <memory>
@ -91,10 +89,6 @@ private:
/** Access to the block database (blocks/index/) */
class CBlockTreeDB : public CDBWrapper
{
private:
CCriticalSection cs;
unordered_limitedmap<uint256, bool> mapHasTxIndexCache;
public:
explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);

View File

@ -13,7 +13,6 @@
#include <stdint.h>
#include <string>
#include <vector>
#include <crypto/common.h>
/** Template base class for fixed-sized opaque blobs. */
template<unsigned int BITS>
@ -127,16 +126,6 @@ class uint256 : public base_blob<256> {
public:
uint256() {}
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(m_data);
}
};
/* uint256 from const char *.
@ -175,15 +164,5 @@ public:
}
};
namespace std {
template <>
struct hash<uint256>
{
std::size_t operator()(const uint256& k) const
{
return (std::size_t)k.GetCheapHash();
}
};
}
#endif // BITCOIN_UINT256_H

View File

@ -13,6 +13,7 @@
#include <amount.h>
#include <coins.h>
#include <crypto/common.h> // for ReadLE64
#include <fs.h>
#include <protocol.h> // For CMessageHeader::MessageStartChars
#include <policy/feerate.h>
@ -114,7 +115,10 @@ static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 945 * 1024 * 1024;
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 CCriticalSection cs_main;

View File

@ -10,6 +10,7 @@
#include <wallet/coinselection.h>
#include <consensus/consensus.h>
#include <consensus/validation.h>
#include <crypto/common.h>
#include <fs.h>
#include <key.h>
#include <key_io.h>
@ -1643,7 +1644,7 @@ bool CWallet::IsFullyMixed(const COutPoint& outpoint) const
ss << outpoint << nCoinJoinSalt;
uint256 nHash;
CSHA256().Write((const unsigned char*)ss.data(), ss.size()).Finalize(nHash.begin());
if (nHash.GetCheapHash() % 2 == 0) {
if (ReadLE64(nHash.begin()) % 2 == 0) {
return false;
}
}