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.
13 #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
14 #define LN2 0.6931471805599453094172321214581765680755001343602552
18 static const unsigned char bit_mask[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};
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
30 nHashFuncs(min((unsigned int)(vData.size() * 8 / nElements * LN2), MAX_HASH_FUNCS)),
36 inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const
38 // 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
39 return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
42 void CBloomFilter::insert(const vector<unsigned char>& vKey)
46 for (unsigned int i = 0; i < nHashFuncs; i++)
48 unsigned int nIndex = Hash(i, vKey);
49 // Sets bit nIndex of vData
50 vData[nIndex >> 3] |= bit_mask[7 & nIndex];
55 void CBloomFilter::insert(const COutPoint& outpoint)
57 CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
59 vector<unsigned char> data(stream.begin(), stream.end());
63 void CBloomFilter::insert(const uint256& hash)
65 vector<unsigned char> data(hash.begin(), hash.end());
69 bool CBloomFilter::contains(const vector<unsigned char>& vKey) const
75 for (unsigned int i = 0; i < nHashFuncs; i++)
77 unsigned int nIndex = Hash(i, vKey);
78 // Checks bit nIndex of vData
79 if (!(vData[nIndex >> 3] & bit_mask[7 & nIndex]))
85 bool CBloomFilter::contains(const COutPoint& outpoint) const
87 CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
89 vector<unsigned char> data(stream.begin(), stream.end());
90 return contains(data);
93 bool CBloomFilter::contains(const uint256& hash) const
95 vector<unsigned char> data(hash.begin(), hash.end());
96 return contains(data);
99 bool CBloomFilter::IsWithinSizeConstraints() const
101 return vData.size() <= MAX_BLOOM_FILTER_SIZE && nHashFuncs <= MAX_HASH_FUNCS;
104 bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx, const uint256& hash)
107 // Match if the filter contains the hash of tx
108 // for finding tx when they appear in a block
116 for (unsigned int i = 0; i < tx.vout.size(); i++)
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())
128 if (!txout.scriptPubKey.GetOp(pc, opcode, data))
130 if (data.size() != 0 && contains(data))
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)
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));
151 BOOST_FOREACH(const CTxIn& txin, tx.vin)
153 // Match if the filter contains an outpoint tx spends
154 if (contains(txin.prevout))
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())
163 if (!txin.scriptSig.GetOp(pc, opcode, data))
165 if (data.size() != 0 && contains(data))
173 void CBloomFilter::UpdateEmptyFull()
177 for (unsigned int i = 0; i < vData.size(); i++)
179 full &= vData[i] == 0xff;
180 empty &= vData[i] == 0;