]> Git Repo - VerusCoin.git/blob - src/bloom.cpp
Get rid of C99 PRI?64 usage in source files
[VerusCoin.git] / src / bloom.cpp
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
5 #include "bloom.h"
6
7 #include "core.h"
8 #include "script.h"
9
10 #include <math.h>
11 #include <stdlib.h>
12
13 #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
14 #define LN2 0.6931471805599453094172321214581765680755001343602552
15
16 using namespace std;
17
18 static const unsigned char bit_mask[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
19
20 CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn, unsigned char nFlagsIn) :
21 // The ideal size for a bloom filter with a given number of elements and false positive rate is:
22 // - nElements * log(fp rate) / ln(2)^2
23 // We ignore filter parameters which will create a bloom filter larger than the protocol limits
24 vData(min((unsigned int)(-1  / LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
25 // The ideal number of hash functions is filter size * ln(2) / number of elements
26 // Again, we ignore filter parameters which will create a bloom filter with more hash functions than the protocol limits
27 // See http://en.wikipedia.org/wiki/Bloom_filter for an explanation of these formulas
28 isFull(false),
29 isEmpty(false),
30 nHashFuncs(min((unsigned int)(vData.size() * 8 / nElements * LN2), MAX_HASH_FUNCS)),
31 nTweak(nTweakIn),
32 nFlags(nFlagsIn)
33 {
34 }
35
36 inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const
37 {
38     // 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
39     return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
40 }
41
42 void CBloomFilter::insert(const vector<unsigned char>& vKey)
43 {
44     if (isFull)
45         return;
46     for (unsigned int i = 0; i < nHashFuncs; i++)
47     {
48         unsigned int nIndex = Hash(i, vKey);
49         // Sets bit nIndex of vData
50         vData[nIndex >> 3] |= bit_mask[7 & nIndex];
51     }
52     isEmpty = false;
53 }
54
55 void CBloomFilter::insert(const COutPoint& outpoint)
56 {
57     CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
58     stream << outpoint;
59     vector<unsigned char> data(stream.begin(), stream.end());
60     insert(data);
61 }
62
63 void CBloomFilter::insert(const uint256& hash)
64 {
65     vector<unsigned char> data(hash.begin(), hash.end());
66     insert(data);
67 }
68
69 bool CBloomFilter::contains(const vector<unsigned char>& vKey) const
70 {
71     if (isFull)
72         return true;
73     if (isEmpty)
74         return false;
75     for (unsigned int i = 0; i < nHashFuncs; i++)
76     {
77         unsigned int nIndex = Hash(i, vKey);
78         // Checks bit nIndex of vData
79         if (!(vData[nIndex >> 3] & bit_mask[7 & nIndex]))
80             return false;
81     }
82     return true;
83 }
84
85 bool CBloomFilter::contains(const COutPoint& outpoint) const
86 {
87     CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
88     stream << outpoint;
89     vector<unsigned char> data(stream.begin(), stream.end());
90     return contains(data);
91 }
92
93 bool CBloomFilter::contains(const uint256& hash) const
94 {
95     vector<unsigned char> data(hash.begin(), hash.end());
96     return contains(data);
97 }
98
99 bool CBloomFilter::IsWithinSizeConstraints() const
100 {
101     return vData.size() <= MAX_BLOOM_FILTER_SIZE && nHashFuncs <= MAX_HASH_FUNCS;
102 }
103
104 bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx, const uint256& hash)
105 {
106     bool fFound = false;
107     // Match if the filter contains the hash of tx
108     //  for finding tx when they appear in a block
109     if (isFull)
110         return true;
111     if (isEmpty)
112         return false;
113     if (contains(hash))
114         fFound = true;
115
116     for (unsigned int i = 0; i < tx.vout.size(); i++)
117     {
118         const CTxOut& txout = tx.vout[i];
119         // Match if the filter contains any arbitrary script data element in any scriptPubKey in tx
120         // If this matches, also add the specific output that was matched.
121         // This means clients don't have to update the filter themselves when a new relevant tx 
122         // is discovered in order to find spending transactions, which avoids round-tripping and race conditions.
123         CScript::const_iterator pc = txout.scriptPubKey.begin();
124         vector<unsigned char> data;
125         while (pc < txout.scriptPubKey.end())
126         {
127             opcodetype opcode;
128             if (!txout.scriptPubKey.GetOp(pc, opcode, data))
129                 break;
130             if (data.size() != 0 && contains(data))
131             {
132                 fFound = true;
133                 if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
134                     insert(COutPoint(hash, i));
135                 else if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_P2PUBKEY_ONLY)
136                 {
137                     txnouttype type;
138                     vector<vector<unsigned char> > vSolutions;
139                     if (Solver(txout.scriptPubKey, type, vSolutions) &&
140                             (type == TX_PUBKEY || type == TX_MULTISIG))
141                         insert(COutPoint(hash, i));
142                 }
143                 break;
144             }
145         }
146     }
147
148     if (fFound)
149         return true;
150
151     BOOST_FOREACH(const CTxIn& txin, tx.vin)
152     {
153         // Match if the filter contains an outpoint tx spends
154         if (contains(txin.prevout))
155             return true;
156
157         // Match if the filter contains any arbitrary script data element in any scriptSig in tx
158         CScript::const_iterator pc = txin.scriptSig.begin();
159         vector<unsigned char> data;
160         while (pc < txin.scriptSig.end())
161         {
162             opcodetype opcode;
163             if (!txin.scriptSig.GetOp(pc, opcode, data))
164                 break;
165             if (data.size() != 0 && contains(data))
166                 return true;
167         }
168     }
169
170     return false;
171 }
172
173 void CBloomFilter::UpdateEmptyFull()
174 {
175     bool full = true;
176     bool empty = true;
177     for (unsigned int i = 0; i < vData.size(); i++)
178     {
179         full &= vData[i] == 0xff;
180         empty &= vData[i] == 0;
181     }
182     isFull = full;
183     isEmpty = empty;
184 }
This page took 0.0349 seconds and 4 git commands to generate.