Skip to content

Commit fef5adc

Browse files
committed
blockfilter: Use unordered_set instead of set in blockfilter.
1 parent 4fb789e commit fef5adc

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ BITCOIN_CORE_H = \
183183
txmempool.h \
184184
ui_interface.h \
185185
undo.h \
186+
util/bytevectorhash.h \
186187
util/system.h \
187188
util/memory.h \
188189
util/moneystr.h \
@@ -429,6 +430,7 @@ libbitcoin_util_a_SOURCES = \
429430
support/cleanse.cpp \
430431
sync.cpp \
431432
threadinterrupt.cpp \
433+
util/bytevectorhash.cpp \
432434
util/system.cpp \
433435
util/moneystr.cpp \
434436
util/strencodings.cpp \

src/blockfilter.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
#ifndef BITCOIN_BLOCKFILTER_H
66
#define BITCOIN_BLOCKFILTER_H
77

8-
#include <set>
98
#include <stdint.h>
9+
#include <unordered_set>
1010
#include <vector>
1111

1212
#include <primitives/block.h>
1313
#include <serialize.h>
1414
#include <uint256.h>
1515
#include <undo.h>
16+
#include <util/bytevectorhash.h>
1617

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

2728
private:
2829
uint64_t m_siphash_k0;

src/util/bytevectorhash.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <crypto/siphash.h>
6+
#include <random.h>
7+
#include <util/bytevectorhash.h>
8+
9+
ByteVectorHash::ByteVectorHash()
10+
{
11+
GetRandBytes(reinterpret_cast<unsigned char*>(&m_k0), sizeof(m_k0));
12+
GetRandBytes(reinterpret_cast<unsigned char*>(&m_k1), sizeof(m_k1));
13+
}
14+
15+
size_t ByteVectorHash::operator()(const std::vector<unsigned char>& input) const
16+
{
17+
return CSipHasher(m_k0, m_k1).Write(input.data(), input.size()).Finalize();
18+
}

src/util/bytevectorhash.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_BYTEVECTORHASH_H
6+
#define BITCOIN_UTIL_BYTEVECTORHASH_H
7+
8+
#include <stdint.h>
9+
#include <vector>
10+
11+
/**
12+
* Implementation of Hash named requirement for types that internally store a byte array. This may
13+
* be used as the hash function in std::unordered_set or std::unordered_map over such types.
14+
* Internally, this uses a random instance of SipHash-2-4.
15+
*/
16+
class ByteVectorHash final
17+
{
18+
private:
19+
uint64_t m_k0, m_k1;
20+
21+
public:
22+
ByteVectorHash();
23+
size_t operator()(const std::vector<unsigned char>& input) const;
24+
};
25+
26+
#endif // BITCOIN_UTIL_BYTEVECTORHASH_H

0 commit comments

Comments
 (0)