Raven Core  3.0.0
P2P Digital Currency
bloom.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_BLOOM_H
7 #define RAVEN_BLOOM_H
8 
9 #include "serialize.h"
10 
11 #include <vector>
12 
13 class COutPoint;
14 class CTransaction;
15 class uint256;
16 
18 static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes
19 static const unsigned int MAX_HASH_FUNCS = 50;
20 
26 {
29  // Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script
32 };
33 
46 {
47 private:
48  std::vector<unsigned char> vData;
49  bool isFull;
50  bool isEmpty;
51  unsigned int nHashFuncs;
52  unsigned int nTweak;
53  unsigned char nFlags;
54 
55  unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
56 
57  // Private constructor for CRollingBloomFilter, no restrictions on size
58  CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak);
59  friend class CRollingBloomFilter;
60 
61 public:
71  CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak, unsigned char nFlagsIn);
72  CBloomFilter() : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {}
73 
75 
76  template <typename Stream, typename Operation>
77  inline void SerializationOp(Stream& s, Operation ser_action) {
78  READWRITE(vData);
79  READWRITE(nHashFuncs);
80  READWRITE(nTweak);
81  READWRITE(nFlags);
82  }
83 
84  void insert(const std::vector<unsigned char>& vKey);
85  void insert(const COutPoint& outpoint);
86  void insert(const uint256& hash);
87 
88  bool contains(const std::vector<unsigned char>& vKey) const;
89  bool contains(const COutPoint& outpoint) const;
90  bool contains(const uint256& hash) const;
91 
92  void clear();
93  void reset(const unsigned int nNewTweak);
94 
97  bool IsWithinSizeConstraints() const;
98 
100  bool IsRelevantAndUpdate(const CTransaction& tx);
101 
103  void UpdateEmptyFull();
104 };
105 
121 {
122 public:
123  // A random bloom filter calls GetRand() at creation time.
124  // Don't create global CRollingBloomFilter objects, as they may be
125  // constructed before the randomizer is properly initialized.
126  CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
127 
128  void insert(const std::vector<unsigned char>& vKey);
129  void insert(const uint256& hash);
130  bool contains(const std::vector<unsigned char>& vKey) const;
131  bool contains(const uint256& hash) const;
132 
133  void reset();
134 
135 private:
139  std::vector<uint64_t> data;
140  unsigned int nTweak;
142 };
143 
144 #endif // RAVEN_BLOOM_H
bloomflags
First two bits of nFlags control how much IsRelevantAndUpdate actually updates The remaining bits are...
Definition: bloom.h:25
#define READWRITE(obj)
Definition: serialize.h:163
ADD_SERIALIZE_METHODS
Definition: bloom.h:74
unsigned int nTweak
Definition: bloom.h:52
unsigned char nFlags
Definition: bloom.h:53
bool IsRelevantAndUpdate(const CTransaction &tx)
Also adds any outputs which match the filter to the filter (to match their spending txes) ...
Definition: bloom.cpp:134
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
unsigned int nTweak
Definition: bloom.h:140
bool isFull
Definition: bloom.h:49
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. ...
Definition: bloom.h:120
std::vector< unsigned char > vData
Definition: bloom.h:48
unsigned int Hash(unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
Definition: bloom.cpp:53
unsigned int nHashFuncs
Definition: bloom.h:51
void reset(const unsigned int nNewTweak)
Definition: bloom.cpp:123
void insert(const std::vector< unsigned char > &vKey)
Definition: bloom.cpp:59
void clear()
Definition: bloom.cpp:116
CBloomFilter()
Definition: bloom.h:72
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
std::vector< uint64_t > data
Definition: bloom.h:139
256-bit opaque blob.
Definition: uint256.h:123
int nEntriesThisGeneration
Definition: bloom.h:137
friend class CRollingBloomFilter
Definition: bloom.h:59
void UpdateEmptyFull()
Checks for empty and full filters to avoid wasting cpu.
Definition: bloom.cpp:204
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
void SerializationOp(Stream &s, Operation ser_action)
Definition: bloom.h:77
bool isEmpty
Definition: bloom.h:50
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:129
bool contains(const std::vector< unsigned char > &vKey) const
Definition: bloom.cpp:86
int nEntriesPerGeneration
Definition: bloom.h:136