diff --git a/src/bloom.h b/src/bloom.h --- a/src/bloom.h +++ b/src/bloom.h @@ -45,8 +45,6 @@ class CBloomFilter { private: std::vector vData; - bool isFull; - bool isEmpty; uint32_t nHashFuncs; uint32_t nTweak; uint8_t nFlags; @@ -68,8 +66,7 @@ */ CBloomFilter(const uint32_t nElements, const double nFPRate, const uint32_t nTweak, uint8_t nFlagsIn); - CBloomFilter() - : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {} + CBloomFilter() : nHashFuncs(0), nTweak(0), nFlags(0) {} ADD_SERIALIZE_METHODS; @@ -109,9 +106,6 @@ bool IsRelevantAndUpdate(const CTransaction &tx) { return MatchAndInsertOutputs(tx) || MatchInputs(tx); } - - //! Checks for empty and full filters to avoid wasting cpu - void UpdateEmptyFull(); }; /** diff --git a/src/bloom.cpp b/src/bloom.cpp --- a/src/bloom.cpp +++ b/src/bloom.cpp @@ -37,7 +37,6 @@ : vData(std::min(-1 / LN2SQUARED * nElements * log(nFPRate), MAX_BLOOM_FILTER_SIZE * 8) / 8), - isFull(false), isEmpty(true), nHashFuncs(std::min(vData.size() * 8 / nElements * LN2, MAX_HASH_FUNCS)), nTweak(nTweakIn), nFlags(nFlagsIn) {} @@ -52,7 +51,8 @@ } void CBloomFilter::insert(const std::vector &vKey) { - if (isFull) { + if (vData.empty()) { + // Avoid divide-by-zero (CVE-2013-5700) return; } @@ -61,7 +61,6 @@ // Sets bit nIndex of vData vData[nIndex >> 3] |= (1 << (7 & nIndex)); } - isEmpty = false; } void CBloomFilter::insert(const COutPoint &outpoint) { @@ -77,12 +76,10 @@ } bool CBloomFilter::contains(const std::vector &vKey) const { - if (isFull) { + if (vData.empty()) { + // Avoid divide-by-zero (CVE-2013-5700) return true; } - if (isEmpty) { - return false; - } for (uint32_t i = 0; i < nHashFuncs; i++) { uint32_t nIndex = Hash(i, vKey); // Checks bit nIndex of vData @@ -114,12 +111,10 @@ bool fFound = false; // Match if the filter contains the hash of tx for finding tx when they // appear in a block - if (isFull) { + if (vData.empty()) { + // zero-size = "match-all" filter return true; } - if (isEmpty) { - return false; - } const TxId &txid = tx.GetId(); if (contains(txid)) { @@ -162,10 +157,6 @@ } bool CBloomFilter::MatchInputs(const CTransaction &tx) { - if (isEmpty) { - return false; - } - for (const CTxIn &txin : tx.vin) { // Match if the filter contains an outpoint tx spends if (contains(txin.prevout)) { @@ -190,17 +181,6 @@ return false; } -void CBloomFilter::UpdateEmptyFull() { - bool full = true; - bool empty = true; - for (const auto d : vData) { - full &= (d == 0xff); - empty &= (d == 0); - } - isFull = full; - isEmpty = empty; -} - CRollingBloomFilter::CRollingBloomFilter(const uint32_t nElements, const double fpRate) { double logFpRate = log(fpRate); diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4268,7 +4268,6 @@ } else if (pfrom.m_tx_relay != nullptr) { LOCK(pfrom.m_tx_relay->cs_filter); pfrom.m_tx_relay->pfilter.reset(new CBloomFilter(filter)); - pfrom.m_tx_relay->pfilter->UpdateEmptyFull(); pfrom.m_tx_relay->fRelayTxes = true; } return; diff --git a/src/test/fuzz/bloom_filter.cpp b/src/test/fuzz/bloom_filter.cpp --- a/src/test/fuzz/bloom_filter.cpp +++ b/src/test/fuzz/bloom_filter.cpp @@ -26,7 +26,7 @@ {BLOOM_UPDATE_NONE, BLOOM_UPDATE_ALL, BLOOM_UPDATE_P2PUBKEY_ONLY, BLOOM_UPDATE_MASK}))}; while (fuzzed_data_provider.remaining_bytes() > 0) { - switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 4)) { + switch (fuzzed_data_provider.ConsumeIntegralInRange(0, 3)) { case 0: { const std::vector b = ConsumeRandomLengthByteVector(fuzzed_data_provider); @@ -71,9 +71,6 @@ (void)bloom_filter.IsRelevantAndUpdate(tx); break; } - case 4: - bloom_filter.UpdateEmptyFull(); - break; } (void)bloom_filter.IsWithinSizeConstraints(); }