Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
bloom.h
Go to the documentation of this file.
1 // Copyright (c) 2012 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef BITCOIN_BLOOM_H
5 #define BITCOIN_BLOOM_H
6 
7 #include <vector>
8 
9 #include "uint256.h"
10 #include "serialize.h"
11 
12 class COutPoint;
13 class CTransaction;
14 
15 // 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
16 static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes
17 static const unsigned int MAX_HASH_FUNCS = 50;
18 
19 // First two bits of nFlags control how much IsRelevantAndUpdate actually updates
20 // The remaining bits are reserved
22 {
25  // Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script
28 };
29 
42 {
43 private:
44  std::vector<unsigned char> vData;
45  bool isFull;
46  bool isEmpty;
47  unsigned int nHashFuncs;
48  unsigned int nTweak;
49  unsigned char nFlags;
50 
51  unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
52 
53 public:
54  // Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements
55  // Note that if the given parameters will result in a filter outside the bounds of the protocol limits,
56  // the filter created will be as close to the given parameters as possible within the protocol limits.
57  // This will apply if nFPRate is very low or nElements is unreasonably high.
58  // nTweak is a constant which is added to the seed value passed to the hash function
59  // It should generally always be a random value (and is largely only exposed for unit testing)
60  // nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK)
61  CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn);
62  CBloomFilter() : isFull(true) {}
63 
65  (
66  READWRITE(vData);
67  READWRITE(nHashFuncs);
68  READWRITE(nTweak);
69  READWRITE(nFlags);
70  )
71 
72  void insert(const std::vector<unsigned char>& vKey);
73  void insert(const COutPoint& outpoint);
74  void insert(const uint256& hash);
75 
76  bool contains(const std::vector<unsigned char>& vKey) const;
77  bool contains(const COutPoint& outpoint) const;
78  bool contains(const uint256& hash) const;
79 
80  // True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS
81  // (catch a filter which was just deserialized which was too big)
82  bool IsWithinSizeConstraints() const;
83 
84  // Also adds any outputs which match the filter to the filter (to match their spending txes)
85  bool IsRelevantAndUpdate(const CTransaction& tx, const uint256& hash);
86 
87  // Checks for empty and full filters to avoid wasting cpu
88  void UpdateEmptyFull();
89 };
90 
91 #endif /* BITCOIN_BLOOM_H */
bloomflags
Definition: bloom.h:21
#define READWRITE(obj)
Definition: serialize.h:93
unsigned int nTweak
Definition: bloom.h:48
unsigned char nFlags
Definition: bloom.h:49
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:41
unsigned int Hash(unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
Definition: bloom.cpp:34
bool isFull
Definition: bloom.h:45
std::vector< unsigned char > vData
Definition: bloom.h:44
#define IMPLEMENT_SERIALIZE(statements)
Definition: serialize.h:55
unsigned int nHashFuncs
Definition: bloom.h:47
void insert(const uint256 &hash)
Definition: bloom.cpp:40
bool IsWithinSizeConstraints() const
Definition: bloom.cpp:97
bool contains(const std::vector< unsigned char > &vKey) const
Definition: bloom.cpp:67
CBloomFilter()
Definition: bloom.h:62
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: main.h:278
256-bit unsigned integer
Definition: uint256.h:537
bool IsRelevantAndUpdate(const CTransaction &tx, const uint256 &hash)
Definition: bloom.cpp:102
void UpdateEmptyFull()
Definition: bloom.cpp:171
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: main.h:477
uint32_t hash
Definition: cache.cc:34
bool isEmpty
Definition: bloom.h:46