]>
Commit | Line | Data |
---|---|---|
bd21612c MC |
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. | |
51ed9ec9 | 4 | |
bd21612c MC |
5 | #ifndef BITCOIN_BLOOM_H |
6 | #define BITCOIN_BLOOM_H | |
7 | ||
bd21612c MC |
8 | #include "serialize.h" |
9 | ||
51ed9ec9 BD |
10 | #include <vector> |
11 | ||
bd21612c MC |
12 | class COutPoint; |
13 | class CTransaction; | |
51ed9ec9 | 14 | class uint256; |
bd21612c MC |
15 | |
16 | // 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001% | |
17 | static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes | |
18 | static const unsigned int MAX_HASH_FUNCS = 50; | |
19 | ||
e1a4f377 MC |
20 | // First two bits of nFlags control how much IsRelevantAndUpdate actually updates |
21 | // The remaining bits are reserved | |
22 | enum bloomflags | |
23 | { | |
24 | BLOOM_UPDATE_NONE = 0, | |
25 | BLOOM_UPDATE_ALL = 1, | |
26 | // Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script | |
27 | BLOOM_UPDATE_P2PUBKEY_ONLY = 2, | |
28 | BLOOM_UPDATE_MASK = 3, | |
29 | }; | |
bd21612c MC |
30 | |
31 | /** | |
32 | * BloomFilter is a probabilistic filter which SPV clients provide | |
33 | * so that we can filter the transactions we sends them. | |
34 | * | |
35 | * This allows for significantly more efficient transaction and block downloads. | |
36 | * | |
37 | * Because bloom filters are probabilistic, an SPV node can increase the false- | |
38 | * positive rate, making us send them transactions which aren't actually theirs, | |
39 | * allowing clients to trade more bandwidth for more privacy by obfuscating which | |
40 | * keys are owned by them. | |
41 | */ | |
42 | class CBloomFilter | |
43 | { | |
44 | private: | |
45 | std::vector<unsigned char> vData; | |
37c6389c GM |
46 | bool isFull; |
47 | bool isEmpty; | |
bd21612c | 48 | unsigned int nHashFuncs; |
b1f99bed | 49 | unsigned int nTweak; |
e1a4f377 | 50 | unsigned char nFlags; |
bd21612c MC |
51 | |
52 | unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const; | |
53 | ||
54 | public: | |
55 | // Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements | |
56 | // Note that if the given parameters will result in a filter outside the bounds of the protocol limits, | |
57 | // the filter created will be as close to the given parameters as possible within the protocol limits. | |
58 | // This will apply if nFPRate is very low or nElements is unreasonably high. | |
b1f99bed MC |
59 | // nTweak is a constant which is added to the seed value passed to the hash function |
60 | // It should generally always be a random value (and is largely only exposed for unit testing) | |
e1a4f377 MC |
61 | // nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK) |
62 | CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn); | |
37c6389c | 63 | CBloomFilter() : isFull(true) {} |
bd21612c MC |
64 | |
65 | IMPLEMENT_SERIALIZE | |
66 | ( | |
67 | READWRITE(vData); | |
68 | READWRITE(nHashFuncs); | |
b1f99bed | 69 | READWRITE(nTweak); |
e1a4f377 | 70 | READWRITE(nFlags); |
bd21612c MC |
71 | ) |
72 | ||
73 | void insert(const std::vector<unsigned char>& vKey); | |
74 | void insert(const COutPoint& outpoint); | |
75 | void insert(const uint256& hash); | |
76 | ||
77 | bool contains(const std::vector<unsigned char>& vKey) const; | |
78 | bool contains(const COutPoint& outpoint) const; | |
79 | bool contains(const uint256& hash) const; | |
80 | ||
81 | // True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS | |
82 | // (catch a filter which was just deserialized which was too big) | |
83 | bool IsWithinSizeConstraints() const; | |
84 | ||
d3b26f70 MC |
85 | // Also adds any outputs which match the filter to the filter (to match their spending txes) |
86 | bool IsRelevantAndUpdate(const CTransaction& tx, const uint256& hash); | |
37c6389c GM |
87 | |
88 | // Checks for empty and full filters to avoid wasting cpu | |
89 | void UpdateEmptyFull(); | |
bd21612c MC |
90 | }; |
91 | ||
92 | #endif /* BITCOIN_BLOOM_H */ |