Bitcoin Core  0.20.99
P2P Digital Currency
bloom.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 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);
65  CBloomFilter() : nHashFuncs(0), nTweak(0), nFlags(0) {}
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 
100 {
101 public:
102  CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
103 
104  void insert(const std::vector<unsigned char>& vKey);
105  void insert(const uint256& hash);
106  bool contains(const std::vector<unsigned char>& vKey) const;
107  bool contains(const uint256& hash) const;
108 
109  void reset();
110 
111 private:
115  std::vector<uint64_t> data;
116  unsigned int nTweak;
118 };
119 
120 #endif // BITCOIN_BLOOM_H
bloomflags
First two bits of nFlags control how much IsRelevantAndUpdate actually updates The remaining bits are...
Definition: bloom.h:24
unsigned int nTweak
Definition: bloom.h:49
unsigned char nFlags
Definition: bloom.h:50
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
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
unsigned int nTweak
Definition: bloom.h:116
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:99
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
std::vector< unsigned char > vData
Definition: bloom.h:47
unsigned int Hash(unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
Definition: bloom.cpp:40
unsigned int nHashFuncs
Definition: bloom.h:48
void insert(const std::vector< unsigned char > &vKey)
Definition: bloom.cpp:46
SERIALIZE_METHODS(CBloomFilter, obj)
Definition: bloom.h:67
CBloomFilter()
Definition: bloom.h:65
static const unsigned int MAX_HASH_FUNCS
Definition: bloom.h:18
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< uint64_t > data
Definition: bloom.h:115
256-bit opaque blob.
Definition: uint256.h:120
int nEntriesThisGeneration
Definition: bloom.h:113
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:251
#define READWRITE(...)
Definition: serialize.h:171
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
bool contains(const std::vector< unsigned char > &vKey) const
Definition: bloom.cpp:72
int nEntriesPerGeneration
Definition: bloom.h:112