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.
7 #include "core/transaction.h"
8 #include "script/script.h"
9 #include "script/standard.h"
15 #include <boost/foreach.hpp>
17 #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
18 #define LN2 0.6931471805599453094172321214581765680755001343602552
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
32 nHashFuncs(min((unsigned int)(vData.size() * 8 / nElements * LN2), MAX_HASH_FUNCS)),
38 inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const
40 // 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
41 return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
44 void CBloomFilter::insert(const vector<unsigned char>& vKey)
48 for (unsigned int i = 0; i < nHashFuncs; i++)
50 unsigned int nIndex = Hash(i, vKey);
51 // Sets bit nIndex of vData
52 vData[nIndex >> 3] |= (1 << (7 & nIndex));
57 void CBloomFilter::insert(const COutPoint& outpoint)
59 CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
61 vector<unsigned char> data(stream.begin(), stream.end());
65 void CBloomFilter::insert(const uint256& hash)
67 vector<unsigned char> data(hash.begin(), hash.end());
71 bool CBloomFilter::contains(const vector<unsigned char>& vKey) const
77 for (unsigned int i = 0; i < nHashFuncs; i++)
79 unsigned int nIndex = Hash(i, vKey);
80 // Checks bit nIndex of vData
81 if (!(vData[nIndex >> 3] & (1 << (7 & nIndex))))
87 bool CBloomFilter::contains(const COutPoint& outpoint) const
89 CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
91 vector<unsigned char> data(stream.begin(), stream.end());
92 return contains(data);
95 bool CBloomFilter::contains(const uint256& hash) const
97 vector<unsigned char> data(hash.begin(), hash.end());
98 return contains(data);
101 void CBloomFilter::clear()
103 vData.assign(vData.size(),0);
108 bool CBloomFilter::IsWithinSizeConstraints() const
110 return vData.size() <= MAX_BLOOM_FILTER_SIZE && nHashFuncs <= MAX_HASH_FUNCS;
113 bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx)
116 // Match if the filter contains the hash of tx
117 // for finding tx when they appear in a block
122 const uint256& hash = tx.GetHash();
126 for (unsigned int i = 0; i < tx.vout.size(); i++)
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())
138 if (!txout.scriptPubKey.GetOp(pc, opcode, data))
140 if (data.size() != 0 && contains(data))
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)
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));
161 BOOST_FOREACH(const CTxIn& txin, tx.vin)
163 // Match if the filter contains an outpoint tx spends
164 if (contains(txin.prevout))
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())
173 if (!txin.scriptSig.GetOp(pc, opcode, data))
175 if (data.size() != 0 && contains(data))
183 void CBloomFilter::UpdateEmptyFull()
187 for (unsigned int i = 0; i < vData.size(); i++)
189 full &= vData[i] == 0xff;
190 empty &= vData[i] == 0;