Skip to content

Commit

Permalink
blockfilter: Use unordered_set instead of set in blockfilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimpo committed Nov 5, 2018
1 parent 4fb789e commit fef5adc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
5 changes: 3 additions & 2 deletions src/blockfilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#ifndef BITCOIN_BLOCKFILTER_H
#define BITCOIN_BLOCKFILTER_H

#include <set>
#include <stdint.h>
#include <unordered_set>
#include <vector>

#include <primitives/block.h>
#include <serialize.h>
#include <uint256.h>
#include <undo.h>
#include <util/bytevectorhash.h>

/**
* This implements a Golomb-coded set as defined in BIP 158. It is a
Expand All @@ -22,7 +23,7 @@ class GCSFilter
{
public:
typedef std::vector<unsigned char> Element;
typedef std::set<Element> ElementSet;
typedef std::unordered_set<Element, ByteVectorHash> ElementSet;

private:
uint64_t m_siphash_k0;
Expand Down
18 changes: 18 additions & 0 deletions src/util/bytevectorhash.cpp
Original file line number Diff line number Diff line change
@@ -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 <crypto/siphash.h>
#include <random.h>
#include <util/bytevectorhash.h>

ByteVectorHash::ByteVectorHash()
{
GetRandBytes(reinterpret_cast<unsigned char*>(&m_k0), sizeof(m_k0));
GetRandBytes(reinterpret_cast<unsigned char*>(&m_k1), sizeof(m_k1));
}

size_t ByteVectorHash::operator()(const std::vector<unsigned char>& input) const
{
return CSipHasher(m_k0, m_k1).Write(input.data(), input.size()).Finalize();
}
26 changes: 26 additions & 0 deletions src/util/bytevectorhash.h
Original file line number Diff line number Diff line change
@@ -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 <stdint.h>
#include <vector>

/**
* 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<unsigned char>& input) const;
};

#endif // BITCOIN_UTIL_BYTEVECTORHASH_H

0 comments on commit fef5adc

Please sign in to comment.