mirror of
https://github.com/dashpay/dash.git
synced 2024-12-25 20:12:57 +01:00
merge bitcoin#23115: use Span instead of std::vector for insert and contains
This commit is contained in:
parent
5d7367e366
commit
8c2d480f1a
@ -4,23 +4,27 @@
|
|||||||
|
|
||||||
#include <bloom.h>
|
#include <bloom.h>
|
||||||
|
|
||||||
#include <primitives/transaction.h>
|
|
||||||
#include <evo/assetlocktx.h>
|
#include <evo/assetlocktx.h>
|
||||||
#include <evo/specialtx.h>
|
|
||||||
#include <evo/providertx.h>
|
#include <evo/providertx.h>
|
||||||
#include <logging.h>
|
#include <evo/specialtx.h>
|
||||||
#include <hash.h>
|
#include <hash.h>
|
||||||
|
#include <logging.h>
|
||||||
|
#include <primitives/transaction.h>
|
||||||
|
#include <random.h>
|
||||||
#include <script/script.h>
|
#include <script/script.h>
|
||||||
#include <script/standard.h>
|
#include <script/standard.h>
|
||||||
#include <random.h>
|
#include <span.h>
|
||||||
#include <streams.h>
|
#include <streams.h>
|
||||||
#include <util/fastrange.h>
|
#include <util/fastrange.h>
|
||||||
|
|
||||||
#include <math.h>
|
#include <algorithm>
|
||||||
#include <stdlib.h>
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <limits>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
|
static constexpr double LN2SQUARED = 0.4804530139182014246671025263266649717305529515945455;
|
||||||
#define LN2 0.6931471805599453094172321214581765680755001343602552
|
static constexpr double LN2 = 0.6931471805599453094172321214581765680755001343602552;
|
||||||
|
|
||||||
CBloomFilter::CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweakIn, unsigned char nFlagsIn) :
|
CBloomFilter::CBloomFilter(const unsigned int nElements, const double nFPRate, const unsigned int nTweakIn, unsigned char nFlagsIn) :
|
||||||
/**
|
/**
|
||||||
@ -40,13 +44,13 @@ CBloomFilter::CBloomFilter(const unsigned int nElements, const double nFPRate, c
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const
|
inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, Span<const unsigned char> vDataToHash) const
|
||||||
{
|
{
|
||||||
// 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
|
// 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
|
||||||
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
|
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CBloomFilter::insert(const std::vector<unsigned char>& vKey)
|
void CBloomFilter::insert(Span<const unsigned char> vKey)
|
||||||
{
|
{
|
||||||
if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700)
|
if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700)
|
||||||
return;
|
return;
|
||||||
@ -62,17 +66,10 @@ void CBloomFilter::insert(const COutPoint& outpoint)
|
|||||||
{
|
{
|
||||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
stream << outpoint;
|
stream << outpoint;
|
||||||
std::vector<unsigned char> data(stream.begin(), stream.end());
|
insert(stream);
|
||||||
insert(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CBloomFilter::insert(const uint256& hash)
|
bool CBloomFilter::contains(Span<const unsigned char> vKey) const
|
||||||
{
|
|
||||||
std::vector<unsigned char> data(hash.begin(), hash.end());
|
|
||||||
insert(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CBloomFilter::contains(const std::vector<unsigned char>& vKey) const
|
|
||||||
{
|
{
|
||||||
if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700)
|
if (vData.empty()) // Avoid divide-by-zero (CVE-2013-5700)
|
||||||
return true;
|
return true;
|
||||||
@ -90,20 +87,7 @@ bool CBloomFilter::contains(const COutPoint& outpoint) const
|
|||||||
{
|
{
|
||||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
stream << outpoint;
|
stream << outpoint;
|
||||||
std::vector<unsigned char> data(stream.begin(), stream.end());
|
return contains(MakeUCharSpan(stream));
|
||||||
return contains(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CBloomFilter::contains(const uint256& hash) const
|
|
||||||
{
|
|
||||||
std::vector<unsigned char> data(hash.begin(), hash.end());
|
|
||||||
return contains(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CBloomFilter::contains(const uint160& hash) const
|
|
||||||
{
|
|
||||||
std::vector<unsigned char> data(hash.begin(), hash.end());
|
|
||||||
return contains(data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CBloomFilter::IsWithinSizeConstraints() const
|
bool CBloomFilter::IsWithinSizeConstraints() const
|
||||||
@ -304,11 +288,12 @@ CRollingBloomFilter::CRollingBloomFilter(const unsigned int nElements, const dou
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Similar to CBloomFilter::Hash */
|
/* Similar to CBloomFilter::Hash */
|
||||||
static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, const std::vector<unsigned char>& vDataToHash) {
|
static inline uint32_t RollingBloomHash(unsigned int nHashNum, uint32_t nTweak, Span<const unsigned char> vDataToHash)
|
||||||
|
{
|
||||||
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
|
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
|
void CRollingBloomFilter::insert(Span<const unsigned char> vKey)
|
||||||
{
|
{
|
||||||
if (nEntriesThisGeneration == nEntriesPerGeneration) {
|
if (nEntriesThisGeneration == nEntriesPerGeneration) {
|
||||||
nEntriesThisGeneration = 0;
|
nEntriesThisGeneration = 0;
|
||||||
@ -339,13 +324,7 @@ void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CRollingBloomFilter::insert(const uint256& hash)
|
bool CRollingBloomFilter::contains(Span<const unsigned char> vKey) const
|
||||||
{
|
|
||||||
std::vector<unsigned char> vData(hash.begin(), hash.end());
|
|
||||||
insert(vData);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
|
|
||||||
{
|
{
|
||||||
for (int n = 0; n < nHashFuncs; n++) {
|
for (int n = 0; n < nHashFuncs; n++) {
|
||||||
uint32_t h = RollingBloomHash(n, nTweak, vKey);
|
uint32_t h = RollingBloomHash(n, nTweak, vKey);
|
||||||
@ -359,12 +338,6 @@ bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CRollingBloomFilter::contains(const uint256& hash) const
|
|
||||||
{
|
|
||||||
std::vector<unsigned char> vData(hash.begin(), hash.end());
|
|
||||||
return contains(vData);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CRollingBloomFilter::reset()
|
void CRollingBloomFilter::reset()
|
||||||
{
|
{
|
||||||
nTweak = GetRand(std::numeric_limits<unsigned int>::max());
|
nTweak = GetRand(std::numeric_limits<unsigned int>::max());
|
||||||
|
21
src/bloom.h
21
src/bloom.h
@ -6,6 +6,7 @@
|
|||||||
#define BITCOIN_BLOOM_H
|
#define BITCOIN_BLOOM_H
|
||||||
|
|
||||||
#include <serialize.h>
|
#include <serialize.h>
|
||||||
|
#include <span.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -14,11 +15,10 @@ class CScript;
|
|||||||
class CTransaction;
|
class CTransaction;
|
||||||
class CTxOut;
|
class CTxOut;
|
||||||
class uint256;
|
class uint256;
|
||||||
class uint160;
|
|
||||||
|
|
||||||
//! 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
|
//! 20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
|
||||||
static const unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes
|
static constexpr unsigned int MAX_BLOOM_FILTER_SIZE = 36000; // bytes
|
||||||
static const unsigned int MAX_HASH_FUNCS = 50;
|
static constexpr unsigned int MAX_HASH_FUNCS = 50;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* First two bits of nFlags control how much IsRelevantAndUpdate actually updates
|
* First two bits of nFlags control how much IsRelevantAndUpdate actually updates
|
||||||
@ -52,7 +52,7 @@ private:
|
|||||||
unsigned int nTweak;
|
unsigned int nTweak;
|
||||||
unsigned char nFlags;
|
unsigned char nFlags;
|
||||||
|
|
||||||
unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
|
unsigned int Hash(unsigned int nHashNum, Span<const unsigned char> vDataToHash) const;
|
||||||
|
|
||||||
// Check matches for arbitrary script data elements
|
// Check matches for arbitrary script data elements
|
||||||
bool CheckScript(const CScript& script) const;
|
bool CheckScript(const CScript& script) const;
|
||||||
@ -75,14 +75,11 @@ public:
|
|||||||
|
|
||||||
SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); }
|
SERIALIZE_METHODS(CBloomFilter, obj) { READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags); }
|
||||||
|
|
||||||
void insert(const std::vector<unsigned char>& vKey);
|
void insert(Span<const unsigned char> vKey);
|
||||||
void insert(const COutPoint& outpoint);
|
void insert(const COutPoint& outpoint);
|
||||||
void insert(const uint256& hash);
|
|
||||||
|
|
||||||
bool contains(const std::vector<unsigned char>& vKey) const;
|
bool contains(Span<const unsigned char> vKey) const;
|
||||||
bool contains(const COutPoint& outpoint) const;
|
bool contains(const COutPoint& outpoint) const;
|
||||||
bool contains(const uint256& hash) const;
|
|
||||||
bool contains(const uint160& hash) const;
|
|
||||||
|
|
||||||
//! True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS
|
//! 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)
|
//! (catch a filter which was just deserialized which was too big)
|
||||||
@ -122,10 +119,8 @@ class CRollingBloomFilter
|
|||||||
public:
|
public:
|
||||||
CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
|
CRollingBloomFilter(const unsigned int nElements, const double nFPRate);
|
||||||
|
|
||||||
void insert(const std::vector<unsigned char>& vKey);
|
void insert(Span<const unsigned char> vKey);
|
||||||
void insert(const uint256& hash);
|
bool contains(Span<const unsigned char> vKey) const;
|
||||||
bool contains(const std::vector<unsigned char>& vKey) const;
|
|
||||||
bool contains(const uint256& hash) const;
|
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#include <hash.h>
|
#include <hash.h>
|
||||||
|
#include <span.h>
|
||||||
#include <crypto/common.h>
|
#include <crypto/common.h>
|
||||||
#include <crypto/hmac_sha512.h>
|
#include <crypto/hmac_sha512.h>
|
||||||
|
|
||||||
|
@ -81,7 +81,7 @@ BOOST_AUTO_TEST_CASE(bloom_create_insert_key)
|
|||||||
CBloomFilter filter(2, 0.001, 0, BLOOM_UPDATE_ALL);
|
CBloomFilter filter(2, 0.001, 0, BLOOM_UPDATE_ALL);
|
||||||
filter.insert(vchPubKey);
|
filter.insert(vchPubKey);
|
||||||
uint160 hash = pubkey.GetID();
|
uint160 hash = pubkey.GetID();
|
||||||
filter.insert(std::vector<unsigned char>(hash.begin(), hash.end()));
|
filter.insert(hash);
|
||||||
|
|
||||||
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
||||||
stream << filter;
|
stream << filter;
|
||||||
|
Loading…
Reference in New Issue
Block a user