diff --git a/src/util/hasher.cpp b/src/util/hasher.cpp index 07d3e5431..7af45fb0a 100644 --- a/src/util/hasher.cpp +++ b/src/util/hasher.cpp @@ -1,16 +1,26 @@ // 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 #include #include SaltedUint256Hasher::SaltedUint256Hasher() : k0(GetRand(std::numeric_limits::max())), k1(GetRand(std::numeric_limits::max())) {} SaltedOutpointHasher::SaltedOutpointHasher() : k0(GetRand(std::numeric_limits::max())), k1(GetRand(std::numeric_limits::max())) {} + +SaltedSipHasher::SaltedSipHasher() + : m_k0(GetRand(std::numeric_limits::max())), + m_k1(GetRand(std::numeric_limits::max())) {} + +size_t SaltedSipHasher::operator()(const Span &script) const { + return CSipHasher(m_k0, m_k1) + .Write(script.data(), script.size()) + .Finalize(); +} diff --git a/src/util/hasher.h b/src/util/hasher.h index a614550b4..0da3b38af 100644 --- a/src/util/hasher.h +++ b/src/util/hasher.h @@ -1,101 +1,105 @@ // 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 #include #include #include #include class SaltedUint256Hasher { private: /** Salt */ const uint64_t k0, k1; public: SaltedUint256Hasher(); size_t hash(const uint256 &h) const { return SipHashUint256(k0, k1, h); } size_t operator()(const uint256 &h) const { return hash(h); } }; class SaltedTxIdHasher : private SaltedUint256Hasher { public: SaltedTxIdHasher() : SaltedUint256Hasher() {} size_t operator()(const TxId &txid) const { return hash(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). - * Note: This information above might be outdated as the unordered map - * container type has meanwhile been switched to the C++ standard library - * implementation. - * * 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 &outpoint) const noexcept { return SipHashUint256Extra(k0, k1, outpoint.GetTxId(), outpoint.GetN()); } }; 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 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; } }; /** * Maintain a map of CBlockIndex for all known headers. */ 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 BlockHash &hash) const { return ReadLE64(hash.begin()); } }; +class SaltedSipHasher { +private: + /** Salt */ + const uint64_t m_k0, m_k1; + +public: + SaltedSipHasher(); + + size_t operator()(const Span &script) const; +}; + #endif // BITCOIN_UTIL_HASHER_H diff --git a/src/wallet/scriptpubkeyman.h b/src/wallet/scriptpubkeyman.h index 5dd63a7dd..8a68cf0ff 100644 --- a/src/wallet/scriptpubkeyman.h +++ b/src/wallet/scriptpubkeyman.h @@ -1,789 +1,782 @@ // 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_WALLET_SCRIPTPUBKEYMAN_H #define BITCOIN_WALLET_SCRIPTPUBKEYMAN_H #include #include