Skip to content

Commit d741371

Browse files
committed
Only use randomly created nonces in CRollingBloomFilter.
1 parent d2d7ee0 commit d741371

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

src/bloom.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,17 @@ void CBloomFilter::UpdateEmptyFull()
216216
isEmpty = empty;
217217
}
218218

219-
CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate, unsigned int nTweak) :
220-
b1(nElements * 2, fpRate, nTweak), b2(nElements * 2, fpRate, nTweak)
219+
CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate) :
220+
b1(nElements * 2, fpRate, 0), b2(nElements * 2, fpRate, 0)
221221
{
222222
// Implemented using two bloom filters of 2 * nElements each.
223223
// We fill them up, and clear them, staggered, every nElements
224224
// inserted, so at least one always contains the last nElements
225225
// inserted.
226+
nInsertions = 0;
226227
nBloomSize = nElements * 2;
227228

228-
reset(nTweak);
229+
reset();
229230
}
230231

231232
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
@@ -262,11 +263,9 @@ bool CRollingBloomFilter::contains(const uint256& hash) const
262263
return contains(data);
263264
}
264265

265-
void CRollingBloomFilter::reset(unsigned int nNewTweak)
266+
void CRollingBloomFilter::reset()
266267
{
267-
if (!nNewTweak)
268-
nNewTweak = GetRand(std::numeric_limits<unsigned int>::max());
269-
268+
unsigned int nNewTweak = GetRand(std::numeric_limits<unsigned int>::max());
270269
b1.reset(nNewTweak);
271270
b2.reset(nNewTweak);
272271
nInsertions = 0;

src/bloom.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,17 @@ class CBloomFilter
116116
class CRollingBloomFilter
117117
{
118118
public:
119-
CRollingBloomFilter(unsigned int nElements, double nFPRate,
120-
unsigned int nTweak = 0);
119+
// A random bloom filter calls GetRand() at creation time.
120+
// Don't create global CRollingBloomFilter objects, as they may be
121+
// constructed before the randomizer is properly initialized.
122+
CRollingBloomFilter(unsigned int nElements, double nFPRate);
121123

122124
void insert(const std::vector<unsigned char>& vKey);
123125
void insert(const uint256& hash);
124126
bool contains(const std::vector<unsigned char>& vKey) const;
125127
bool contains(const uint256& hash) const;
126128

127-
void reset(unsigned int nNewTweak = 0);
129+
void reset();
128130

129131
private:
130132
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)