Browse Source

Only use randomly created nonces in CRollingBloomFilter.

0.13
Pieter Wuille 9 years ago
parent
commit
d741371d7d
  1. 13
      src/bloom.cpp
  2. 8
      src/bloom.h
  3. 6
      src/test/bloom_tests.cpp

13
src/bloom.cpp

@ -216,16 +216,17 @@ void CBloomFilter::UpdateEmptyFull()
isEmpty = empty; isEmpty = empty;
} }
CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate, unsigned int nTweak) : CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate) :
b1(nElements * 2, fpRate, nTweak), b2(nElements * 2, fpRate, nTweak) b1(nElements * 2, fpRate, 0), b2(nElements * 2, fpRate, 0)
{ {
// Implemented using two bloom filters of 2 * nElements each. // Implemented using two bloom filters of 2 * nElements each.
// We fill them up, and clear them, staggered, every nElements // We fill them up, and clear them, staggered, every nElements
// inserted, so at least one always contains the last nElements // inserted, so at least one always contains the last nElements
// inserted. // inserted.
nInsertions = 0;
nBloomSize = nElements * 2; nBloomSize = nElements * 2;
reset(nTweak); reset();
} }
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey) void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
@ -262,11 +263,9 @@ bool CRollingBloomFilter::contains(const uint256& hash) const
return contains(data); return contains(data);
} }
void CRollingBloomFilter::reset(unsigned int nNewTweak) void CRollingBloomFilter::reset()
{ {
if (!nNewTweak) unsigned int nNewTweak = GetRand(std::numeric_limits<unsigned int>::max());
nNewTweak = GetRand(std::numeric_limits<unsigned int>::max());
b1.reset(nNewTweak); b1.reset(nNewTweak);
b2.reset(nNewTweak); b2.reset(nNewTweak);
nInsertions = 0; nInsertions = 0;

8
src/bloom.h

@ -116,15 +116,17 @@ public:
class CRollingBloomFilter class CRollingBloomFilter
{ {
public: public:
CRollingBloomFilter(unsigned int nElements, double nFPRate, // A random bloom filter calls GetRand() at creation time.
unsigned int nTweak = 0); // Don't create global CRollingBloomFilter objects, as they may be
// constructed before the randomizer is properly initialized.
CRollingBloomFilter(unsigned int nElements, double nFPRate);
void insert(const std::vector<unsigned char>& vKey); void insert(const std::vector<unsigned char>& vKey);
void insert(const uint256& hash); void insert(const uint256& hash);
bool contains(const std::vector<unsigned char>& vKey) const; bool contains(const std::vector<unsigned char>& vKey) const;
bool contains(const uint256& hash) const; bool contains(const uint256& hash) const;
void reset(unsigned int nNewTweak = 0); void reset();
private: private:
unsigned int nBloomSize; unsigned int nBloomSize;

6
src/test/bloom_tests.cpp

@ -469,7 +469,7 @@ static std::vector<unsigned char> RandomData()
BOOST_AUTO_TEST_CASE(rolling_bloom) BOOST_AUTO_TEST_CASE(rolling_bloom)
{ {
// last-100-entry, 1% false positive: // last-100-entry, 1% false positive:
CRollingBloomFilter rb1(100, 0.01, 1); CRollingBloomFilter rb1(100, 0.01);
// Overfill: // Overfill:
static const int DATASIZE=399; static const int DATASIZE=399;
@ -500,7 +500,7 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
BOOST_CHECK(nHits < 175); BOOST_CHECK(nHits < 175);
BOOST_CHECK(rb1.contains(data[DATASIZE-1])); BOOST_CHECK(rb1.contains(data[DATASIZE-1]));
rb1.reset(1); rb1.reset();
BOOST_CHECK(!rb1.contains(data[DATASIZE-1])); BOOST_CHECK(!rb1.contains(data[DATASIZE-1]));
// Now roll through data, make sure last 100 entries // Now roll through data, make sure last 100 entries
@ -527,7 +527,7 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
BOOST_CHECK(nHits < 100); BOOST_CHECK(nHits < 100);
// last-1000-entry, 0.01% false positive: // last-1000-entry, 0.01% false positive:
CRollingBloomFilter rb2(1000, 0.001, 1); CRollingBloomFilter rb2(1000, 0.001);
for (int i = 0; i < DATASIZE; i++) { for (int i = 0; i < DATASIZE; i++) {
rb2.insert(data[i]); rb2.insert(data[i]);
} }

Loading…
Cancel
Save