mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 03:52:49 +01:00
merge bitcoin#19935: Move SaltedHashers to separate file and add some new ones
This commit is contained in:
parent
e5445e5444
commit
fa1a5d2100
@ -313,6 +313,7 @@ BITCOIN_CORE_H = \
|
||||
util/fastrange.h \
|
||||
util/fees.h \
|
||||
util/golombrice.h \
|
||||
util/hasher.h \
|
||||
util/hash_type.h \
|
||||
util/irange.h \
|
||||
util/spanparsing.h \
|
||||
@ -740,6 +741,7 @@ libbitcoin_util_a_SOURCES = \
|
||||
util/check.cpp \
|
||||
util/error.cpp \
|
||||
util/fees.cpp \
|
||||
util/hasher.cpp \
|
||||
util/getuniquepath.cpp \
|
||||
util/sock.cpp \
|
||||
util/system.cpp \
|
||||
|
@ -31,8 +31,6 @@ bool CCoinsViewBacked::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
|
||||
CCoinsViewCursor *CCoinsViewBacked::Cursor() const { return base->Cursor(); }
|
||||
size_t CCoinsViewBacked::EstimateSize() const { return base->EstimateSize(); }
|
||||
|
||||
SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
||||
|
||||
CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) {}
|
||||
|
||||
size_t CCoinsViewCache::DynamicMemoryUsage() const {
|
||||
|
29
src/coins.h
29
src/coins.h
@ -8,11 +8,11 @@
|
||||
|
||||
#include <compressor.h>
|
||||
#include <core_memusage.h>
|
||||
#include <crypto/siphash.h>
|
||||
#include <memusage.h>
|
||||
#include <primitives/transaction.h>
|
||||
#include <serialize.h>
|
||||
#include <uint256.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
@ -85,33 +85,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class SaltedOutpointHasher
|
||||
{
|
||||
private:
|
||||
/** Salt */
|
||||
const uint64_t k0, k1;
|
||||
|
||||
public:
|
||||
SaltedOutpointHasher();
|
||||
|
||||
/**
|
||||
* This *must* return size_t. With Boost 1.46 on 32-bit systems the
|
||||
* unordered_map will behave unpredictably if the custom hasher returns a
|
||||
* uint64_t, resulting in failures when syncing the chain (#4634).
|
||||
*
|
||||
* Having the hash noexcept allows libstdc++'s unordered_map to recalculate
|
||||
* the hash during rehash, so it does not have to cache the value. This
|
||||
* reduces node's memory by sizeof(size_t). The required recalculation has
|
||||
* a slight performance penalty (around 1.6%), but this is compensated by
|
||||
* memory savings of about 9% which allow for a larger dbcache setting.
|
||||
*
|
||||
* @see https://gcc.gnu.org/onlinedocs/gcc-9.2.0/libstdc++/manual/manual/unordered_associative.html
|
||||
*/
|
||||
size_t operator()(const COutPoint& id) const noexcept {
|
||||
return SipHashUint256Extra(k0, k1, id.hash, id.n);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* A Coin in one level of the coins database caching hierarchy.
|
||||
*
|
||||
|
@ -9,15 +9,11 @@
|
||||
#include <chain.h>
|
||||
#include <flatfile.h>
|
||||
#include <index/base.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
/** Interval between compact filter checkpoints. See BIP 157. */
|
||||
static constexpr int CFCHECKPT_INTERVAL = 1000;
|
||||
|
||||
struct FilterHeaderHasher
|
||||
{
|
||||
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
|
||||
};
|
||||
|
||||
/**
|
||||
* BlockFilterIndex is used to store and retrieve block filters, hashes, and headers for a range of
|
||||
* blocks by height. An index is constructed for each supported filter type with its own database
|
||||
|
@ -7,6 +7,7 @@
|
||||
#define BITCOIN_SCRIPT_SIGCACHE_H
|
||||
|
||||
#include <script/interpreter.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
@ -19,27 +20,6 @@ static const int64_t MAX_MAX_SIG_CACHE_SIZE = 16384;
|
||||
|
||||
class CPubKey;
|
||||
|
||||
/**
|
||||
* We're hashing a nonce into the entries themselves, so we don't need extra
|
||||
* blinding in the set hash computation.
|
||||
*
|
||||
* This may exhibit platform endian dependent behavior but because these are
|
||||
* nonced hashes (random) and this state is only ever used locally it is safe.
|
||||
* All that matters is local consistency.
|
||||
*/
|
||||
class SignatureCacheHasher
|
||||
{
|
||||
public:
|
||||
template <uint8_t hash_select>
|
||||
uint32_t operator()(const uint256& key) const
|
||||
{
|
||||
static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
|
||||
uint32_t u;
|
||||
std::memcpy(&u, key.begin()+4*hash_select, 4);
|
||||
return u;
|
||||
}
|
||||
};
|
||||
|
||||
class CachingTransactionSignatureChecker : public TransactionSignatureChecker
|
||||
{
|
||||
private:
|
||||
|
@ -1682,5 +1682,3 @@ void CTxMemPool::SetIsLoaded(bool loaded)
|
||||
LOCK(cs);
|
||||
m_is_loaded = loaded;
|
||||
}
|
||||
|
||||
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
||||
|
@ -18,7 +18,6 @@
|
||||
#include <spentindex.h>
|
||||
#include <amount.h>
|
||||
#include <coins.h>
|
||||
#include <crypto/siphash.h>
|
||||
#include <indirectmap.h>
|
||||
#include <policy/feerate.h>
|
||||
#include <primitives/transaction.h>
|
||||
@ -26,6 +25,7 @@
|
||||
#include <random.h>
|
||||
#include <netaddress.h>
|
||||
#include <pubkey.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/multi_index_container.hpp>
|
||||
@ -368,20 +368,6 @@ enum class MemPoolRemovalReason {
|
||||
MANUAL //!< Removed manually
|
||||
};
|
||||
|
||||
class SaltedTxidHasher
|
||||
{
|
||||
private:
|
||||
/** Salt */
|
||||
const uint64_t k0, k1;
|
||||
|
||||
public:
|
||||
SaltedTxidHasher();
|
||||
|
||||
size_t operator()(const uint256& txid) const {
|
||||
return SipHashUint256(k0, k1, txid);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* CTxMemPool stores valid-according-to-the-current-best-chain transactions
|
||||
* that may be included in the next block.
|
||||
|
19
src/util/hasher.cpp
Normal file
19
src/util/hasher.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2019 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <random.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
#include <limits>
|
||||
|
||||
SaltedTxidHasher::SaltedTxidHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
||||
|
||||
SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits<uint64_t>::max())), k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
||||
|
||||
SaltedSipHasher::SaltedSipHasher() : m_k0(GetRand(std::numeric_limits<uint64_t>::max())), m_k1(GetRand(std::numeric_limits<uint64_t>::max())) {}
|
||||
|
||||
size_t SaltedSipHasher::operator()(const Span<const unsigned char>& script) const
|
||||
{
|
||||
return CSipHasher(m_k0, m_k1).Write(script.data(), script.size()).Finalize();
|
||||
}
|
99
src/util/hasher.h
Normal file
99
src/util/hasher.h
Normal file
@ -0,0 +1,99 @@
|
||||
// Copyright (c) 2019 The Bitcoin Core developers
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#ifndef BITCOIN_UTIL_HASHER_H
|
||||
#define BITCOIN_UTIL_HASHER_H
|
||||
|
||||
#include <crypto/siphash.h>
|
||||
#include <primitives/transaction.h>
|
||||
#include <uint256.h>
|
||||
|
||||
class SaltedTxidHasher
|
||||
{
|
||||
private:
|
||||
/** Salt */
|
||||
const uint64_t k0, k1;
|
||||
|
||||
public:
|
||||
SaltedTxidHasher();
|
||||
|
||||
size_t operator()(const uint256& txid) const {
|
||||
return SipHashUint256(k0, k1, txid);
|
||||
}
|
||||
};
|
||||
|
||||
class SaltedOutpointHasher
|
||||
{
|
||||
private:
|
||||
/** Salt */
|
||||
const uint64_t k0, k1;
|
||||
|
||||
public:
|
||||
SaltedOutpointHasher();
|
||||
|
||||
/**
|
||||
* This *must* return size_t. With Boost 1.46 on 32-bit systems the
|
||||
* unordered_map will behave unpredictably if the custom hasher returns a
|
||||
* uint64_t, resulting in failures when syncing the chain (#4634).
|
||||
*
|
||||
* Having the hash noexcept allows libstdc++'s unordered_map to recalculate
|
||||
* the hash during rehash, so it does not have to cache the value. This
|
||||
* reduces node's memory by sizeof(size_t). The required recalculation has
|
||||
* a slight performance penalty (around 1.6%), but this is compensated by
|
||||
* memory savings of about 9% which allow for a larger dbcache setting.
|
||||
*
|
||||
* @see https://gcc.gnu.org/onlinedocs/gcc-9.2.0/libstdc++/manual/manual/unordered_associative.html
|
||||
*/
|
||||
size_t operator()(const COutPoint& id) const noexcept {
|
||||
return SipHashUint256Extra(k0, k1, id.hash, id.n);
|
||||
}
|
||||
};
|
||||
|
||||
struct FilterHeaderHasher
|
||||
{
|
||||
size_t operator()(const uint256& hash) const { return ReadLE64(hash.begin()); }
|
||||
};
|
||||
|
||||
/**
|
||||
* We're hashing a nonce into the entries themselves, so we don't need extra
|
||||
* blinding in the set hash computation.
|
||||
*
|
||||
* This may exhibit platform endian dependent behavior but because these are
|
||||
* nonced hashes (random) and this state is only ever used locally it is safe.
|
||||
* All that matters is local consistency.
|
||||
*/
|
||||
class SignatureCacheHasher
|
||||
{
|
||||
public:
|
||||
template <uint8_t hash_select>
|
||||
uint32_t operator()(const uint256& key) const
|
||||
{
|
||||
static_assert(hash_select <8, "SignatureCacheHasher only has 8 hashes available.");
|
||||
uint32_t u;
|
||||
std::memcpy(&u, key.begin()+4*hash_select, 4);
|
||||
return u;
|
||||
}
|
||||
};
|
||||
|
||||
struct BlockHasher
|
||||
{
|
||||
// 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()); }
|
||||
};
|
||||
|
||||
class SaltedSipHasher
|
||||
{
|
||||
private:
|
||||
/** Salt */
|
||||
const uint64_t m_k0, m_k1;
|
||||
|
||||
public:
|
||||
SaltedSipHasher();
|
||||
|
||||
size_t operator()(const Span<const unsigned char>& script) const;
|
||||
};
|
||||
|
||||
#endif // BITCOIN_UTIL_HASHER_H
|
@ -26,6 +26,7 @@
|
||||
#include <versionbits.h>
|
||||
#include <serialize.h>
|
||||
#include <spentindex.h>
|
||||
#include <util/hasher.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <map>
|
||||
@ -126,14 +127,6 @@ static const unsigned int DEFAULT_CHECKLEVEL = 3;
|
||||
// Setting the target to > than 945 MiB will make it likely we can respect the target.
|
||||
static const uint64_t MIN_DISK_SPACE_FOR_BLOCK_FILES = 945 * 1024 * 1024;
|
||||
|
||||
struct BlockHasher
|
||||
{
|
||||
// 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 RecursiveMutex cs_main;
|
||||
typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
|
||||
typedef std::unordered_multimap<uint256, CBlockIndex*, BlockHasher> PrevBlockMap;
|
||||
|
Loading…
Reference in New Issue
Block a user