Update osx build instructions to ensure users link to the correct version of OpenSSL
[bitcoinplatinum.git] / src / bloom.h
blob389ae748e6238046cabd45dd1643e65e4e209e78
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
7 #include <vector>
9 #include "uint256.h"
10 #include "serialize.h"
12 class COutPoint;
13 class CTransaction;
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;
19 // First two bits of nFlags control how much IsRelevantAndUpdate actually updates
20 // The remaining bits are reserved
21 enum bloomflags
23 BLOOM_UPDATE_NONE = 0,
24 BLOOM_UPDATE_ALL = 1,
25 // Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script
26 BLOOM_UPDATE_P2PUBKEY_ONLY = 2,
27 BLOOM_UPDATE_MASK = 3,
30 /**
31 * BloomFilter is a probabilistic filter which SPV clients provide
32 * so that we can filter the transactions we sends them.
34 * This allows for significantly more efficient transaction and block downloads.
36 * Because bloom filters are probabilistic, an SPV node can increase the false-
37 * positive rate, making us send them transactions which aren't actually theirs,
38 * allowing clients to trade more bandwidth for more privacy by obfuscating which
39 * keys are owned by them.
41 class CBloomFilter
43 private:
44 std::vector<unsigned char> vData;
45 unsigned int nHashFuncs;
46 unsigned int nTweak;
47 unsigned char nFlags;
49 unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
51 public:
52 // Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements
53 // Note that if the given parameters will result in a filter outside the bounds of the protocol limits,
54 // the filter created will be as close to the given parameters as possible within the protocol limits.
55 // This will apply if nFPRate is very low or nElements is unreasonably high.
56 // nTweak is a constant which is added to the seed value passed to the hash function
57 // It should generally always be a random value (and is largely only exposed for unit testing)
58 // nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK)
59 CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn);
60 // Using a filter initialized with this results in undefined behavior
61 // Should only be used for deserialization
62 CBloomFilter() {}
64 IMPLEMENT_SERIALIZE
66 READWRITE(vData);
67 READWRITE(nHashFuncs);
68 READWRITE(nTweak);
69 READWRITE(nFlags);
72 void insert(const std::vector<unsigned char>& vKey);
73 void insert(const COutPoint& outpoint);
74 void insert(const uint256& hash);
76 bool contains(const std::vector<unsigned char>& vKey) const;
77 bool contains(const COutPoint& outpoint) const;
78 bool contains(const uint256& hash) const;
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;
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);
88 #endif /* BITCOIN_BLOOM_H */