Bitcoin ABC  0.24.10
P2P Digital Currency
bloom.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2016 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 <cstdint>
11 #include <vector>
12 
13 class COutPoint;
14 class CTransaction;
15 class uint256;
16 
18 static const uint32_t MAX_BLOOM_FILTER_SIZE = 36000; // bytes
19 static const uint32_t MAX_HASH_FUNCS = 50;
20 
25 enum bloomflags {
28  // Only adds outpoints to the filter if the output is a
29  // pay-to-pubkey/pay-to-multisig script
32 };
33 
45 class CBloomFilter {
46 private:
47  std::vector<uint8_t> vData;
48  uint32_t nHashFuncs;
49  uint32_t nTweak;
50  uint8_t nFlags;
51 
52  uint32_t Hash(uint32_t nHashNum,
53  const std::vector<uint8_t> &vDataToHash) const;
54 
55 public:
67  CBloomFilter(const uint32_t nElements, const double nFPRate,
68  const uint32_t nTweak, uint8_t nFlagsIn);
70 
72  READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags);
73  }
74 
75  void insert(const std::vector<uint8_t> &vKey);
76  void insert(const COutPoint &outpoint);
77  void insert(const uint256 &hash);
78 
79  bool contains(const std::vector<uint8_t> &vKey) const;
80  bool contains(const COutPoint &outpoint) const;
81  bool contains(const uint256 &hash) const;
82 
86  bool IsWithinSizeConstraints() const;
87 
91  bool MatchAndInsertOutputs(const CTransaction &tx);
92 
95  bool MatchInputs(const CTransaction &tx);
96 
101  return MatchAndInsertOutputs(tx) || MatchInputs(tx);
102  }
103 };
104 
120 public:
121  CRollingBloomFilter(const uint32_t nElements, const double nFPRate);
122 
123  void insert(const std::vector<uint8_t> &vKey);
124  void insert(const uint256 &hash);
125  bool contains(const std::vector<uint8_t> &vKey) const;
126  bool contains(const uint256 &hash) const;
127 
128  void reset();
129 
130 private:
134  std::vector<uint64_t> data;
135  uint32_t nTweak;
137 };
138 
139 #endif // BITCOIN_BLOOM_H
CBloomFilter::nTweak
uint32_t nTweak
Definition: bloom.h:49
CRollingBloomFilter::nGeneration
int nGeneration
Definition: bloom.h:133
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:105
CBloomFilter
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:45
CBloomFilter::contains
bool contains(const std::vector< uint8_t > &vKey) const
Definition: bloom.cpp:78
BLOOM_UPDATE_P2PUBKEY_ONLY
@ BLOOM_UPDATE_P2PUBKEY_ONLY
Definition: bloom.h:30
CBloomFilter::insert
void insert(const std::vector< uint8_t > &vKey)
Definition: bloom.cpp:53
CBloomFilter::vData
std::vector< uint8_t > vData
Definition: bloom.h:47
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:194
CBloomFilter::nFlags
uint8_t nFlags
Definition: bloom.h:50
CBloomFilter::MatchAndInsertOutputs
bool MatchAndInsertOutputs(const CTransaction &tx)
Scans output scripts for matches and adds those outpoints to the filter for spend detection.
Definition: bloom.cpp:110
CRollingBloomFilter::nTweak
uint32_t nTweak
Definition: bloom.h:135
CBloomFilter::MatchInputs
bool MatchInputs(const CTransaction &tx)
Scan inputs to see if the spent outpoints are a match, or the input scripts contain matching elements...
Definition: bloom.cpp:160
CRollingBloomFilter::reset
void reset()
Definition: bloom.cpp:294
CBloomFilter::CBloomFilter
CBloomFilter()
Definition: bloom.h:69
uint256
256-bit opaque blob.
Definition: uint256.h:127
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
CBloomFilter::SERIALIZE_METHODS
SERIALIZE_METHODS(CBloomFilter, obj)
Definition: bloom.h:71
bloomflags
bloomflags
First two bits of nFlags control how much IsRelevantAndUpdate actually updates.
Definition: bloom.h:25
BLOOM_UPDATE_MASK
@ BLOOM_UPDATE_MASK
Definition: bloom.h:31
MAX_BLOOM_FILTER_SIZE
static const uint32_t MAX_BLOOM_FILTER_SIZE
20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
Definition: bloom.h:18
CRollingBloomFilter::nHashFuncs
int nHashFuncs
Definition: bloom.h:136
CRollingBloomFilter::nEntriesThisGeneration
int nEntriesThisGeneration
Definition: bloom.h:132
CRollingBloomFilter::data
std::vector< uint64_t > data
Definition: bloom.h:134
BLOOM_UPDATE_ALL
@ BLOOM_UPDATE_ALL
Definition: bloom.h:27
CRollingBloomFilter::insert
void insert(const std::vector< uint8_t > &vKey)
Definition: bloom.cpp:236
CBloomFilter::nHashFuncs
uint32_t nHashFuncs
Definition: bloom.h:48
serialize.h
CBloomFilter::Hash
uint32_t Hash(uint32_t nHashNum, const std::vector< uint8_t > &vDataToHash) const
Definition: bloom.cpp:45
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
CBloomFilter::IsRelevantAndUpdate
bool IsRelevantAndUpdate(const CTransaction &tx)
Check if the transaction is relevant for any reason.
Definition: bloom.h:100
CRollingBloomFilter::CRollingBloomFilter
CRollingBloomFilter(const uint32_t nElements, const double nFPRate)
Definition: bloom.cpp:185
CRollingBloomFilter
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:119
CRollingBloomFilter::contains
bool contains(const std::vector< uint8_t > &vKey) const
Definition: bloom.cpp:275
MAX_HASH_FUNCS
static const uint32_t MAX_HASH_FUNCS
Definition: bloom.h:19
BLOOM_UPDATE_NONE
@ BLOOM_UPDATE_NONE
Definition: bloom.h:26
CRollingBloomFilter::nEntriesPerGeneration
int nEntriesPerGeneration
Definition: bloom.h:131