From fef5adcc331c4d7b92b71e03fc8a73343a865599 Mon Sep 17 00:00:00 2001 From: Jim Posen Date: Mon, 20 Aug 2018 23:35:29 -0700 Subject: [PATCH] blockfilter: Use unordered_set instead of set in blockfilter. --- src/Makefile.am | 2 ++ src/blockfilter.h | 5 +++-- src/util/bytevectorhash.cpp | 18 ++++++++++++++++++ src/util/bytevectorhash.h | 26 ++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/util/bytevectorhash.cpp create mode 100644 src/util/bytevectorhash.h diff --git a/src/Makefile.am b/src/Makefile.am index 662c8bb4d8..6852ef408a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -183,6 +183,7 @@ BITCOIN_CORE_H = \ txmempool.h \ ui_interface.h \ undo.h \ + util/bytevectorhash.h \ util/system.h \ util/memory.h \ util/moneystr.h \ @@ -429,6 +430,7 @@ libbitcoin_util_a_SOURCES = \ support/cleanse.cpp \ sync.cpp \ threadinterrupt.cpp \ + util/bytevectorhash.cpp \ util/system.cpp \ util/moneystr.cpp \ util/strencodings.cpp \ diff --git a/src/blockfilter.h b/src/blockfilter.h index 46833ac0be..871be11769 100644 --- a/src/blockfilter.h +++ b/src/blockfilter.h @@ -5,14 +5,15 @@ #ifndef BITCOIN_BLOCKFILTER_H #define BITCOIN_BLOCKFILTER_H -#include #include +#include #include #include #include #include #include +#include /** * This implements a Golomb-coded set as defined in BIP 158. It is a @@ -22,7 +23,7 @@ class GCSFilter { public: typedef std::vector Element; - typedef std::set ElementSet; + typedef std::unordered_set ElementSet; private: uint64_t m_siphash_k0; diff --git a/src/util/bytevectorhash.cpp b/src/util/bytevectorhash.cpp new file mode 100644 index 0000000000..f87d0e04b3 --- /dev/null +++ b/src/util/bytevectorhash.cpp @@ -0,0 +1,18 @@ +// Copyright (c) 2018 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 + +ByteVectorHash::ByteVectorHash() +{ + GetRandBytes(reinterpret_cast(&m_k0), sizeof(m_k0)); + GetRandBytes(reinterpret_cast(&m_k1), sizeof(m_k1)); +} + +size_t ByteVectorHash::operator()(const std::vector& input) const +{ + return CSipHasher(m_k0, m_k1).Write(input.data(), input.size()).Finalize(); +} diff --git a/src/util/bytevectorhash.h b/src/util/bytevectorhash.h new file mode 100644 index 0000000000..b88c17460b --- /dev/null +++ b/src/util/bytevectorhash.h @@ -0,0 +1,26 @@ +// Copyright (c) 2018 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_BYTEVECTORHASH_H +#define BITCOIN_UTIL_BYTEVECTORHASH_H + +#include +#include + +/** + * Implementation of Hash named requirement for types that internally store a byte array. This may + * be used as the hash function in std::unordered_set or std::unordered_map over such types. + * Internally, this uses a random instance of SipHash-2-4. + */ +class ByteVectorHash final +{ +private: + uint64_t m_k0, m_k1; + +public: + ByteVectorHash(); + size_t operator()(const std::vector& input) const; +}; + +#endif // BITCOIN_UTIL_BYTEVECTORHASH_H