Bitcoin Core  22.99.0
P2P Digital Currency
bloom.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 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_BLOOM_H
6 #define BITCOIN_BLOOM_H
7 
8 #include <serialize.h>
9 
10 #include <vector>
11 
12 class COutPoint;
13 class CTransaction;
14 class uint256;
15 
17 static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes
18 static const unsigned int MAX_HASH_FUNCS = 50;
19 
25 {
28  // Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script
31 };
32 
45 {
46 private:
47  std::vector<unsigned char> vData;
48  unsigned int nHashFuncs;
49  unsigned int nTweak;
50  unsigned char nFlags;
51 
52  unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
53 
54 public:
64  CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak, unsigned char nFlagsIn);
66 
67  SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); }
68 
69  void insert(const std::vector<unsigned char>& vKey);
70  void insert(const COutPoint& outpoint);
71  void insert(const uint256& hash);
72 
73  bool contains(const std::vector<unsigned char>& vKey) const;
74  bool contains(const COutPoint& outpoint) const;
75  bool contains(const uint256& hash) const;
76 
79  bool IsWithinSizeConstraints() const;
80 
82  bool IsRelevantAndUpdate(const CTransaction& tx);
83 };
84 
111 {
112 public:
113  CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
114 
115  void insert(const std::vector<unsigned char>& vKey);
116  void insert(const uint256& hash);
117  bool contains(const std::vector<unsigned char>& vKey) const;
118  bool contains(const uint256& hash) const;
119 
120  void reset();
121 
122 private:
126  std::vector<uint64_t> data;
127  unsigned int nTweak;
129 };
130 
131 #endif // BITCOIN_BLOOM_H
CBloomFilter::contains
bool contains(const std::vector< unsigned char > &vKey) const
Definition: bloom.cpp:72
CRollingBloomFilter::nGeneration
int nGeneration
Definition: bloom.h:125
CRollingBloomFilter::insert
void insert(const std::vector< unsigned char > &vKey)
Definition: bloom.cpp:213
CBloomFilter::IsWithinSizeConstraints
bool IsWithinSizeConstraints() const
True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS (c...
Definition: bloom.cpp:100
CBloomFilter
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
CBloomFilter::nTweak
unsigned int nTweak
Definition: bloom.h:49
CRollingBloomFilter::CRollingBloomFilter
CRollingBloomFilter(const unsigned int nElements, const double nFPRate)
Definition: bloom.cpp:173
CRollingBloomFilter::nTweak
unsigned int nTweak
Definition: bloom.h:127
MAX_HASH_FUNCS
static const unsigned int MAX_HASH_FUNCS
Definition: bloom.h:18
BLOOM_UPDATE_P2PUBKEY_ONLY
@ BLOOM_UPDATE_P2PUBKEY_ONLY
Definition: bloom.h:29
CBloomFilter::Hash
unsigned int Hash(unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
Definition: bloom.cpp:40
CBloomFilter::vData
std::vector< unsigned char > vData
Definition: bloom.h:47
CRollingBloomFilter::contains
bool contains(const std::vector< unsigned char > &vKey) const
Definition: bloom.cpp:250
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
MAX_BLOOM_FILTER_SIZE
static const unsigned int MAX_BLOOM_FILTER_SIZE
20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
Definition: bloom.h:17
CBloomFilter::insert
void insert(const std::vector< unsigned char > &vKey)
Definition: bloom.cpp:46
CRollingBloomFilter::reset
void reset()
Definition: bloom.cpp:270
CBloomFilter::CBloomFilter
CBloomFilter()
Definition: bloom.h:65
uint256
256-bit opaque blob.
Definition: uint256.h:124
READWRITE
#define READWRITE(...)
Definition: serialize.h:147
CBloomFilter::SERIALIZE_METHODS
SERIALIZE_METHODS(CBloomFilter, obj)
Definition: bloom.h:67
bloomflags
bloomflags
First two bits of nFlags control how much IsRelevantAndUpdate actually updates The remaining bits are...
Definition: bloom.h:24
BLOOM_UPDATE_MASK
@ BLOOM_UPDATE_MASK
Definition: bloom.h:30
CRollingBloomFilter::nHashFuncs
int nHashFuncs
Definition: bloom.h:128
CRollingBloomFilter::nEntriesThisGeneration
int nEntriesThisGeneration
Definition: bloom.h:124
CRollingBloomFilter::data
std::vector< uint64_t > data
Definition: bloom.h:126
CBloomFilter::nFlags
unsigned char nFlags
Definition: bloom.h:50
BLOOM_UPDATE_ALL
@ BLOOM_UPDATE_ALL
Definition: bloom.h:27
CBloomFilter::nHashFuncs
unsigned int nHashFuncs
Definition: bloom.h:48
serialize.h
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
CBloomFilter::IsRelevantAndUpdate
bool IsRelevantAndUpdate(const CTransaction &tx)
Also adds any outputs which match the filter to the filter (to match their spending txes)
Definition: bloom.cpp:105
CRollingBloomFilter
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:110
BLOOM_UPDATE_NONE
@ BLOOM_UPDATE_NONE
Definition: bloom.h:26
CRollingBloomFilter::nEntriesPerGeneration
int nEntriesPerGeneration
Definition: bloom.h:123