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.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright (c) 2012-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #ifndef STARWELS_BLOOM_H
  5. #define STARWELS_BLOOM_H
  6. #include "serialize.h"
  7. #include <vector>
  8. class COutPoint;
  9. class CTransaction;
  10. class uint256;
  11. //! 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
  12. static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes
  13. static const unsigned int MAX_HASH_FUNCS = 50;
  14. /**
  15. * First two bits of nFlags control how much IsRelevantAndUpdate actually updates
  16. * The remaining bits are reserved
  17. */
  18. enum bloomflags
  19. {
  20. BLOOM_UPDATE_NONE = 0,
  21. BLOOM_UPDATE_ALL = 1,
  22. // Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script
  23. BLOOM_UPDATE_P2PUBKEY_ONLY = 2,
  24. BLOOM_UPDATE_MASK = 3,
  25. };
  26. /**
  27. * BloomFilter is a probabilistic filter which SPV clients provide
  28. * so that we can filter the transactions we send them.
  29. *
  30. * This allows for significantly more efficient transaction and block downloads.
  31. *
  32. * Because bloom filters are probabilistic, a SPV node can increase the false-
  33. * positive rate, making us send it transactions which aren't actually its,
  34. * allowing clients to trade more bandwidth for more privacy by obfuscating which
  35. * keys are controlled by them.
  36. */
  37. class CBloomFilter
  38. {
  39. private:
  40. std::vector<unsigned char> vData;
  41. bool isFull;
  42. bool isEmpty;
  43. unsigned int nHashFuncs;
  44. unsigned int nTweak;
  45. unsigned char nFlags;
  46. unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
  47. // Private constructor for CRollingBloomFilter, no restrictions on size
  48. CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak);
  49. friend class CRollingBloomFilter;
  50. public:
  51. /**
  52. * Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements
  53. * Note that if the given parameters will result in a filter outside the bounds of the protocol limits,
  54. * the filter created will be as close to the given parameters as possible within the protocol limits.
  55. * This will apply if nFPRate is very low or nElements is unreasonably high.
  56. * nTweak is a constant which is added to the seed value passed to the hash function
  57. * It should generally always be a random value (and is largely only exposed for unit testing)
  58. * nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK)
  59. */
  60. CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweak, unsigned char nFlagsIn);
  61. CBloomFilter() : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {}
  62. ADD_SERIALIZE_METHODS;
  63. template <typename Stream, typename Operation>
  64. inline void SerializationOp(Stream& s, Operation ser_action) {
  65. READWRITE(vData);
  66. READWRITE(nHashFuncs);
  67. READWRITE(nTweak);
  68. READWRITE(nFlags);
  69. }
  70. void insert(const std::vector<unsigned char>& vKey);
  71. void insert(const COutPoint& outpoint);
  72. void insert(const uint256& hash);
  73. bool contains(const std::vector<unsigned char>& vKey) const;
  74. bool contains(const COutPoint& outpoint) const;
  75. bool contains(const uint256& hash) const;
  76. void clear();
  77. void reset(const unsigned int nNewTweak);
  78. //! True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS
  79. //! (catch a filter which was just deserialized which was too big)
  80. bool IsWithinSizeConstraints() const;
  81. //! Also adds any outputs which match the filter to the filter (to match their spending txes)
  82. bool IsRelevantAndUpdate(const CTransaction& tx);
  83. //! Checks for empty and full filters to avoid wasting cpu
  84. void UpdateEmptyFull();
  85. };
  86. /**
  87. * RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
  88. * Construct it with the number of items to keep track of, and a false-positive
  89. * rate. Unlike CBloomFilter, by default nTweak is set to a cryptographically
  90. * secure random value for you. Similarly rather than clear() the method
  91. * reset() is provided, which also changes nTweak to decrease the impact of
  92. * false-positives.
  93. *
  94. * contains(item) will always return true if item was one of the last N to 1.5*N
  95. * insert()'ed ... but may also return true for items that were not inserted.
  96. *
  97. * It needs around 1.8 bytes per element per factor 0.1 of false positive rate.
  98. * (More accurately: 3/(log(256)*log(2)) * log(1/fpRate) * nElements bytes)
  99. */
  100. class CRollingBloomFilter
  101. {
  102. public:
  103. // A random bloom filter calls GetRand() at creation time.
  104. // Don't create global CRollingBloomFilter objects, as they may be
  105. // constructed before the randomizer is properly initialized.
  106. CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
  107. void insert(const std::vector<unsigned char>& vKey);
  108. void insert(const uint256& hash);
  109. bool contains(const std::vector<unsigned char>& vKey) const;
  110. bool contains(const uint256& hash) const;
  111. void reset();
  112. private:
  113. int nEntriesPerGeneration;
  114. int nEntriesThisGeneration;
  115. int nGeneration;
  116. std::vector<uint64_t> data;
  117. unsigned int nTweak;
  118. int nHashFuncs;
  119. };
  120. #endif // STARWELS_BLOOM_H