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