diff options
author | Peter Todd <pete@petertodd.org> | 2015-07-20 04:43:34 +0900 |
---|---|---|
committer | Peter Todd <pete@petertodd.org> | 2015-10-02 12:17:44 +0200 |
commit | 83671efe996f6ea695d051594acc7f5fb4c500fc (patch) | |
tree | 1834c3a7b79369af23a72367d7737b2199ba0449 /src/bloom.h | |
parent | 25cf1220e64a418420ae030091882337046fb3cf (diff) |
Make CRollingBloomFilter set nTweak for you
While CBloomFilter is usually used with an explicitly set nTweak,
CRollingBloomFilter is only used internally. Requiring every caller to
set nTweak is error-prone and redundant; better to have the class handle
that for you with a high-quality randomness source.
Additionally when clearing the filter it makes sense to change nTweak as
well to recover from a bad setting, e.g. due to insufficient randomness
at initialization, so the clear() method is replaced by a reset() method
that sets a new, random, nTweak value.
(cherry picked from commit d2d7ee0e863b286e1c9f9c54659d494fb0a7712d)
Diffstat (limited to 'src/bloom.h')
-rw-r--r-- | src/bloom.h | 12 |
1 files changed, 9 insertions, 3 deletions
diff --git a/src/bloom.h b/src/bloom.h index 0daa3728ed..12bf6d99a8 100644 --- a/src/bloom.h +++ b/src/bloom.h @@ -89,6 +89,7 @@ public: bool contains(const uint256& hash) const; void clear(); + void reset(unsigned int nNewTweak); //! True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS //! (catch a filter which was just deserialized which was too big) @@ -103,7 +104,11 @@ public: /** * RollingBloomFilter is a probabilistic "keep track of most recently inserted" set. - * Construct it with the number of items to keep track of, and a false-positive rate. + * Construct it with the number of items to keep track of, and a false-positive + * rate. Unlike CBloomFilter, by default nTweak is set to a cryptographically + * secure random value for you. Similarly rather than clear() the method + * reset() is provided, which also changes nTweak to decrease the impact of + * false-positives. * * contains(item) will always return true if item was one of the last N things * insert()'ed ... but may also return true for items that were not inserted. @@ -111,14 +116,15 @@ public: class CRollingBloomFilter { public: - CRollingBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak); + CRollingBloomFilter(unsigned int nElements, double nFPRate, + unsigned int nTweak = 0); void insert(const std::vector<unsigned char>& vKey); void insert(const uint256& hash); bool contains(const std::vector<unsigned char>& vKey) const; bool contains(const uint256& hash) const; - void clear(); + void reset(unsigned int nNewTweak = 0); private: unsigned int nBloomSize; |