Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

bloom.h 3.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #ifndef BITCOIN_BLOOM_H
  5. #define BITCOIN_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. // First two bits of nFlags control how much IsRelevantAndUpdate actually updates
  15. // The remaining bits are reserved
  16. enum bloomflags
  17. {
  18. BLOOM_UPDATE_NONE = 0,
  19. BLOOM_UPDATE_ALL = 1,
  20. // Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script
  21. BLOOM_UPDATE_P2PUBKEY_ONLY = 2,
  22. BLOOM_UPDATE_MASK = 3,
  23. };
  24. /**
  25. * BloomFilter is a probabilistic filter which SPV clients provide
  26. * so that we can filter the transactions we sends them.
  27. *
  28. * This allows for significantly more efficient transaction and block downloads.
  29. *
  30. * Because bloom filters are probabilistic, an SPV node can increase the false-
  31. * positive rate, making us send them transactions which aren't actually theirs,
  32. * allowing clients to trade more bandwidth for more privacy by obfuscating which
  33. * keys are owned by them.
  34. */
  35. class CBloomFilter
  36. {
  37. private:
  38. std::vector<unsigned char> vData;
  39. bool isFull;
  40. bool isEmpty;
  41. unsigned int nHashFuncs;
  42. unsigned int nTweak;
  43. unsigned char nFlags;
  44. unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
  45. public:
  46. // Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements
  47. // Note that if the given parameters will result in a filter outside the bounds of the protocol limits,
  48. // the filter created will be as close to the given parameters as possible within the protocol limits.
  49. // This will apply if nFPRate is very low or nElements is unreasonably high.
  50. // nTweak is a constant which is added to the seed value passed to the hash function
  51. // It should generally always be a random value (and is largely only exposed for unit testing)
  52. // nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK)
  53. CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn);
  54. CBloomFilter() : isFull(true) {}
  55. IMPLEMENT_SERIALIZE
  56. (
  57. READWRITE(vData);
  58. READWRITE(nHashFuncs);
  59. READWRITE(nTweak);
  60. READWRITE(nFlags);
  61. )
  62. void insert(const std::vector<unsigned char>& vKey);
  63. void insert(const COutPoint& outpoint);
  64. void insert(const uint256& hash);
  65. bool contains(const std::vector<unsigned char>& vKey) const;
  66. bool contains(const COutPoint& outpoint) const;
  67. bool contains(const uint256& hash) const;
  68. // True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS
  69. // (catch a filter which was just deserialized which was too big)
  70. bool IsWithinSizeConstraints() const;
  71. // Also adds any outputs which match the filter to the filter (to match their spending txes)
  72. bool IsRelevantAndUpdate(const CTransaction& tx, const uint256& hash);
  73. // Checks for empty and full filters to avoid wasting cpu
  74. void UpdateEmptyFull();
  75. };
  76. #endif /* BITCOIN_BLOOM_H */