From fa1a5d2100d6a1e1ac3c03c95ea8759b1dab5707 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Thu, 24 Aug 2023 00:25:19 +0530 Subject: [PATCH] merge bitcoin#19935: Move SaltedHashers to separate file and add some new ones --- src/Makefile.am | 2 + src/coins.cpp | 2 - src/coins.h | 29 +---------- src/index/blockfilterindex.h | 6 +-- src/script/sigcache.h | 22 +------- src/txmempool.cpp | 2 - src/txmempool.h | 16 +----- src/util/hasher.cpp | 19 +++++++ src/util/hasher.h | 99 ++++++++++++++++++++++++++++++++++++ src/validation.h | 9 +--- 10 files changed, 125 insertions(+), 81 deletions(-) create mode 100644 src/util/hasher.cpp create mode 100644 src/util/hasher.h diff --git a/src/Makefile.am b/src/Makefile.am index a3cd678806..56c1226e59 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -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 \ diff --git a/src/coins.cpp b/src/coins.cpp index c938354d5c..4d50a0cc5b 100644 --- a/src/coins.cpp +++ b/src/coins.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::max())), k1(GetRand(std::numeric_limits::max())) {} - CCoinsViewCache::CCoinsViewCache(CCoinsView *baseIn) : CCoinsViewBacked(baseIn), cachedCoinsUsage(0) {} size_t CCoinsViewCache::DynamicMemoryUsage() const { diff --git a/src/coins.h b/src/coins.h index 625047a228..816b4864a3 100644 --- a/src/coins.h +++ b/src/coins.h @@ -8,11 +8,11 @@ #include #include -#include #include #include #include #include +#include #include #include @@ -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. * diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h index 317f8c0e40..bb75eee960 100644 --- a/src/index/blockfilterindex.h +++ b/src/index/blockfilterindex.h @@ -9,15 +9,11 @@ #include #include #include +#include /** 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 diff --git a/src/script/sigcache.h b/src/script/sigcache.h index bd153a0dec..062de0e556 100644 --- a/src/script/sigcache.h +++ b/src/script/sigcache.h @@ -7,6 +7,7 @@ #define BITCOIN_SCRIPT_SIGCACHE_H #include