You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bloom.cpp 5.9KB

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