Fabcoin Core  0.16.2
P2P Digital Currency
bloom.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2017 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 FABCOIN_BLOOM_H
6 #define FABCOIN_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  bool isFull;
49  bool isEmpty;
50  unsigned int nHashFuncs;
51  unsigned int nTweak;
52  unsigned char nFlags;
53 
54  unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
55 
56  // Private constructor for CRollingBloomFilter, no restrictions on size
57  CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak);
58  friend class CRollingBloomFilter;
59 
60 public:
70  CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak, unsigned char nFlagsIn);
71  CBloomFilter() : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {}
72 
74 
75  template <typename Stream, typename Operation>
76  inline void SerializationOp(Stream& s, Operation ser_action) {
77  READWRITE(vData);
78  READWRITE(nHashFuncs);
79  READWRITE(nTweak);
80  READWRITE(nFlags);
81  }
82 
83  void insert(const std::vector<unsigned char>& vKey);
84  void insert(const COutPoint& outpoint);
85  void insert(const uint256& hash);
86 
87  bool contains(const std::vector<unsigned char>& vKey) const;
88  bool contains(const COutPoint& outpoint) const;
89  bool contains(const uint256& hash) const;
90 
91  void clear();
92  void reset(const unsigned int nNewTweak);
93 
96  bool IsWithinSizeConstraints() const;
97 
99  bool IsRelevantAndUpdate(const CTransaction& tx);
100 
102  void UpdateEmptyFull();
103 };
104 
120 {
121 public:
122  // A random bloom filter calls GetRand() at creation time.
123  // Don't create global CRollingBloomFilter objects, as they may be
124  // constructed before the randomizer is properly initialized.
125  CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
126 
127  void insert(const std::vector<unsigned char>& vKey);
128  void insert(const uint256& hash);
129  bool contains(const std::vector<unsigned char>& vKey) const;
130  bool contains(const uint256& hash) const;
131 
132  void reset();
133 
134 private:
138  std::vector<uint64_t> data;
139  unsigned int nTweak;
141 };
142 
143 #endif // FABCOIN_BLOOM_H
bloomflags
First two bits of nFlags control how much IsRelevantAndUpdate actually updates The remaining bits are...
Definition: bloom.h:24
#define READWRITE(obj)
Definition: serialize.h:179
ADD_SERIALIZE_METHODS
Definition: bloom.h:73
unsigned int nTweak
Definition: bloom.h:51
unsigned char nFlags
Definition: bloom.h:52
bool IsRelevantAndUpdate(const CTransaction &tx)
Also adds any outputs which match the filter to the filter (to match their spending txes) ...
Definition: bloom.cpp:133
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
unsigned int Hash(unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
Definition: bloom.cpp:52
unsigned int nTweak
Definition: bloom.h:139
bool isFull
Definition: bloom.h:48
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:119
std::vector< unsigned char > vData
Definition: bloom.h:47
unsigned int nHashFuncs
Definition: bloom.h:50
void reset(const unsigned int nNewTweak)
Definition: bloom.cpp:122
void insert(const std::vector< unsigned char > &vKey)
Definition: bloom.cpp:58
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:128
bool contains(const std::vector< unsigned char > &vKey) const
Definition: bloom.cpp:85
void clear()
Definition: bloom.cpp:115
CBloomFilter()
Definition: bloom.h:71
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:138
256-bit opaque blob.
Definition: uint256.h:132
int nEntriesThisGeneration
Definition: bloom.h:136
friend class CRollingBloomFilter
Definition: bloom.h:58
void UpdateEmptyFull()
Checks for empty and full filters to avoid wasting cpu.
Definition: bloom.cpp:203
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:275
void SerializationOp(Stream &s, Operation ser_action)
Definition: bloom.h:76
bool isEmpty
Definition: bloom.h:49
int nEntriesPerGeneration
Definition: bloom.h:135