Skip to content

Commit 34ee777

Browse files
sipaFuzzbawls
authored andcommitted
Only use randomly created nonces in CRollingBloomFilter.
1 parent 338d346 commit 34ee777

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

src/bloom.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,17 @@ void CBloomFilter::UpdateEmptyFull()
262262
isEmpty = empty;
263263
}
264264

265-
CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate, unsigned int nTweak) :
266-
b1(nElements * 2, fpRate, nTweak), b2(nElements * 2, fpRate, nTweak)
265+
CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate) :
266+
b1(nElements * 2, fpRate, 0), b2(nElements * 2, fpRate, 0)
267267
{
268268
// Implemented using two bloom filters of 2 * nElements each.
269269
// We fill them up, and clear them, staggered, every nElements
270270
// inserted, so at least one always contains the last nElements
271271
// inserted.
272+
nInsertions = 0;
272273
nBloomSize = nElements * 2;
273274

274-
reset(nTweak);
275+
reset();
275276
}
276277

277278
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
@@ -308,11 +309,9 @@ bool CRollingBloomFilter::contains(const uint256& hash) const
308309
return contains(data);
309310
}
310311

311-
void CRollingBloomFilter::reset(unsigned int nNewTweak)
312+
void CRollingBloomFilter::reset()
312313
{
313-
if (!nNewTweak)
314-
nNewTweak = GetRand(std::numeric_limits<unsigned int>::max());
315-
314+
unsigned int nNewTweak = GetRand(std::numeric_limits<unsigned int>::max());
316315
b1.reset(nNewTweak);
317316
b2.reset(nNewTweak);
318317
nInsertions = 0;

src/bloom.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,17 @@ class CBloomFilter
123123
class CRollingBloomFilter
124124
{
125125
public:
126-
CRollingBloomFilter(unsigned int nElements, double nFPRate,
127-
unsigned int nTweak = 0);
126+
// A random bloom filter calls GetRand() at creation time.
127+
// Don't create global CRollingBloomFilter objects, as they may be
128+
// constructed before the randomizer is properly initialized.
129+
CRollingBloomFilter(unsigned int nElements, double nFPRate);
128130

129131
void insert(const std::vector<unsigned char>& vKey);
130132
void insert(const uint256& hash);
131133
bool contains(const std::vector<unsigned char>& vKey) const;
132134
bool contains(const uint256& hash) const;
133135

134-
void reset(unsigned int nNewTweak = 0);
136+
void reset();
135137

136138
private:
137139
unsigned int nBloomSize;

src/test/bloom_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ static std::vector<unsigned char> RandomData()
469469
BOOST_AUTO_TEST_CASE(rolling_bloom)
470470
{
471471
// last-100-entry, 1% false positive:
472-
CRollingBloomFilter rb1(100, 0.01, 1);
472+
CRollingBloomFilter rb1(100, 0.01);
473473

474474
// Overfill:
475475
static const int DATASIZE = 399;
@@ -500,7 +500,7 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
500500
BOOST_CHECK(nHits < 175);
501501

502502
BOOST_CHECK(rb1.contains(data[DATASIZE - 1]));
503-
rb1.reset(1);
503+
rb1.reset();
504504
BOOST_CHECK(!rb1.contains(data[DATASIZE - 1]));
505505

506506
// Now roll through data, make sure last 100 entries
@@ -527,7 +527,7 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
527527
BOOST_CHECK(nHits < 100);
528528

529529
// last-1000-entry, 0.01% false positive:
530-
CRollingBloomFilter rb2(1000, 0.001, 1);
530+
CRollingBloomFilter rb2(1000, 0.001);
531531
for (int i = 0; i < DATASIZE; i++) {
532532
rb2.insert(data[i]);
533533
}

0 commit comments

Comments
 (0)