1 // Copyright (c) 2012-2014 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #include "primitives/transaction.h"
9 #include "script/script.h"
10 #include "script/standard.h"
16 #include <boost/foreach.hpp>
18 #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
19 #define LN2 0.6931471805599453094172321214581765680755001343602552
23 CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn, unsigned char nFlagsIn) :
25 * The ideal size for a bloom filter with a given number of elements and false positive rate is:
26 * - nElements * log(fp rate) / ln(2)^2
27 * We ignore filter parameters which will create a bloom filter larger than the protocol limits
29 vData(min((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
31 * The ideal number of hash functions is filter size * ln(2) / number of elements
32 * Again, we ignore filter parameters which will create a bloom filter with more hash functions than the protocol limits
33 * See https://en.wikipedia.org/wiki/Bloom_filter for an explanation of these formulas
37 nHashFuncs(min((unsigned int)(vData.size() * 8 / nElements * LN2), MAX_HASH_FUNCS)),
43 // Private constructor used by CRollingBloomFilter
44 CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn) :
45 vData((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)) / 8),
48 nHashFuncs((unsigned int)(vData.size() * 8 / nElements * LN2)),
50 nFlags(BLOOM_UPDATE_NONE)
54 inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const
56 // 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
57 return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
60 void CBloomFilter::insert(const vector<unsigned char>& vKey)
64 for (unsigned int i = 0; i < nHashFuncs; i++)
66 unsigned int nIndex = Hash(i, vKey);
67 // Sets bit nIndex of vData
68 vData[nIndex >> 3] |= (1 << (7 & nIndex));
73 void CBloomFilter::insert(const COutPoint& outpoint)
75 CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
77 vector<unsigned char> data(stream.begin(), stream.end());
81 void CBloomFilter::insert(const uint256& hash)
83 vector<unsigned char> data(hash.begin(), hash.end());
87 bool CBloomFilter::contains(const vector<unsigned char>& vKey) const
93 for (unsigned int i = 0; i < nHashFuncs; i++)
95 unsigned int nIndex = Hash(i, vKey);
96 // Checks bit nIndex of vData
97 if (!(vData[nIndex >> 3] & (1 << (7 & nIndex))))
103 bool CBloomFilter::contains(const COutPoint& outpoint) const
105 CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
107 vector<unsigned char> data(stream.begin(), stream.end());
108 return contains(data);
111 bool CBloomFilter::contains(const uint256& hash) const
113 vector<unsigned char> data(hash.begin(), hash.end());
114 return contains(data);
117 void CBloomFilter::clear()
119 vData.assign(vData.size(),0);
124 bool CBloomFilter::IsWithinSizeConstraints() const
126 return vData.size() <= MAX_BLOOM_FILTER_SIZE && nHashFuncs <= MAX_HASH_FUNCS;
129 bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx)
132 // Match if the filter contains the hash of tx
133 // for finding tx when they appear in a block
138 const uint256& hash = tx.GetHash();
142 for (unsigned int i = 0; i < tx.vout.size(); i++)
144 const CTxOut& txout = tx.vout[i];
145 // Match if the filter contains any arbitrary script data element in any scriptPubKey in tx
146 // If this matches, also add the specific output that was matched.
147 // This means clients don't have to update the filter themselves when a new relevant tx
148 // is discovered in order to find spending transactions, which avoids round-tripping and race conditions.
149 CScript::const_iterator pc = txout.scriptPubKey.begin();
150 vector<unsigned char> data;
151 while (pc < txout.scriptPubKey.end())
154 if (!txout.scriptPubKey.GetOp(pc, opcode, data))
156 if (data.size() != 0 && contains(data))
159 if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
160 insert(COutPoint(hash, i));
161 else if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_P2PUBKEY_ONLY)
164 vector<vector<unsigned char> > vSolutions;
165 if (Solver(txout.scriptPubKey, type, vSolutions) &&
166 (type == TX_PUBKEY || type == TX_MULTISIG))
167 insert(COutPoint(hash, i));
177 BOOST_FOREACH(const CTxIn& txin, tx.vin)
179 // Match if the filter contains an outpoint tx spends
180 if (contains(txin.prevout))
183 // Match if the filter contains any arbitrary script data element in any scriptSig in tx
184 CScript::const_iterator pc = txin.scriptSig.begin();
185 vector<unsigned char> data;
186 while (pc < txin.scriptSig.end())
189 if (!txin.scriptSig.GetOp(pc, opcode, data))
191 if (data.size() != 0 && contains(data))
199 void CBloomFilter::UpdateEmptyFull()
203 for (unsigned int i = 0; i < vData.size(); i++)
205 full &= vData[i] == 0xff;
206 empty &= vData[i] == 0;
212 CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate, unsigned int nTweak) :
213 b1(nElements * 2, fpRate, nTweak), b2(nElements * 2, fpRate, nTweak)
215 // Implemented using two bloom filters of 2 * nElements each.
216 // We fill them up, and clear them, staggered, every nElements
217 // inserted, so at least one always contains the last nElements
219 nBloomSize = nElements * 2;
223 void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
225 if (nInsertions == 0) {
227 } else if (nInsertions == nBloomSize / 2) {
232 if (++nInsertions == nBloomSize) {
237 bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
239 if (nInsertions < nBloomSize / 2) {
240 return b2.contains(vKey);
242 return b1.contains(vKey);
245 void CRollingBloomFilter::clear()