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