PIVX Core  5.6.99
P2P Digital Currency
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CBloomFilter Class Reference

BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transactions we sends them. More...

#include <bloom.h>

Public Member Functions

 CBloomFilter (const unsigned int nElements, const double nFPRate, const unsigned int nTweak, unsigned char nFlagsIn)
 Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements Note that if the given parameters will result in a filter outside the bounds of the protocol limits, the filter created will be as close to the given parameters as possible within the protocol limits. More...
 
 CBloomFilter ()
 
 SERIALIZE_METHODS (CBloomFilter, obj)
 
void insert (const std::vector< unsigned char > &vKey)
 
void insert (const COutPoint &outpoint)
 
void insert (const uint256 &hash)
 
bool contains (const std::vector< unsigned char > &vKey) const
 
bool contains (const COutPoint &outpoint) const
 
bool contains (const uint256 &hash) const
 
void clear ()
 
void reset (const unsigned int nNewTweak)
 
bool IsWithinSizeConstraints () const
 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) More...
 
bool Merge (const CBloomFilter &filter)
 Copies filter into this. More...
 
bool MatchesAll () const
 Returns true if this filter will match anything. More...
 
bool IsRelevantAndUpdate (const CTransaction &tx)
 Also adds any outputs which match the filter to the filter (to match their spending txes) More...
 
void UpdateEmptyFull ()
 Checks for empty and full filters to avoid wasting cpu. More...
 

Private Member Functions

unsigned int Hash (unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
 

Private Attributes

std::vector< unsigned char > vData
 
bool isFull
 
bool isEmpty
 
unsigned int nHashFuncs
 
unsigned int nTweak
 
unsigned char nFlags
 

Detailed Description

BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transactions we sends them.

This allows for significantly more efficient transaction and block downloads.

Because bloom filters are probabilistic, an SPV node can increase the false- positive rate, making us send them transactions which aren't actually theirs, allowing clients to trade more bandwidth for more privacy by obfuscating which keys are owned by them.

Definition at line 44 of file bloom.h.

Constructor & Destructor Documentation

◆ CBloomFilter() [1/2]

CBloomFilter::CBloomFilter ( const unsigned int  nElements,
const double  nFPRate,
const unsigned int  nTweakIn,
unsigned char  nFlagsIn 
)

Creates a new bloom filter which will provide the given fp rate when filled with the given number of elements Note that if the given parameters will result in a filter outside the bounds of the protocol limits, the filter created will be as close to the given parameters as possible within the protocol limits.

The ideal size for a bloom filter with a given number of elements and false positive rate is:

This will apply if nFPRate is very low or nElements is unreasonably high. nTweak is a constant which is added to the seed value passed to the hash function It should generally always be a random value (and is largely only exposed for unit testing) nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK)

  • nElements * log(fp rate) / ln(2)^2 We ignore filter parameters which will create a bloom filter larger than the protocol limits The ideal number of hash functions is filter size * ln(2) / number of elements Again, we ignore filter parameters which will create a bloom filter with more hash functions than the protocol limits See https://en.wikipedia.org/wiki/Bloom_filter for an explanation of these formulas

Definition at line 22 of file bloom.cpp.

◆ CBloomFilter() [2/2]

CBloomFilter::CBloomFilter ( )
inline

Definition at line 67 of file bloom.h.

Member Function Documentation

◆ clear()

void CBloomFilter::clear ( )

Definition at line 105 of file bloom.cpp.

Here is the caller graph for this function:

◆ contains() [1/3]

bool CBloomFilter::contains ( const COutPoint outpoint) const

Definition at line 91 of file bloom.cpp.

◆ contains() [2/3]

bool CBloomFilter::contains ( const std::vector< unsigned char > &  vKey) const

Definition at line 74 of file bloom.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ contains() [3/3]

bool CBloomFilter::contains ( const uint256 hash) const

Definition at line 99 of file bloom.cpp.

◆ Hash()

unsigned int CBloomFilter::Hash ( unsigned int  nHashNum,
const std::vector< unsigned char > &  vDataToHash 
) const
inlineprivate

Definition at line 42 of file bloom.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ insert() [1/3]

void CBloomFilter::insert ( const COutPoint outpoint)

Definition at line 60 of file bloom.cpp.

◆ insert() [2/3]

void CBloomFilter::insert ( const std::vector< unsigned char > &  vKey)

Definition at line 48 of file bloom.cpp.

Here is the call graph for this function:

◆ insert() [3/3]

void CBloomFilter::insert ( const uint256 hash)

Definition at line 68 of file bloom.cpp.

◆ IsRelevantAndUpdate()

bool CBloomFilter::IsRelevantAndUpdate ( const CTransaction tx)

Also adds any outputs which match the filter to the filter (to match their spending txes)

Definition at line 155 of file bloom.cpp.

Here is the call graph for this function:

◆ IsWithinSizeConstraints()

bool CBloomFilter::IsWithinSizeConstraints ( ) const

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)

Definition at line 118 of file bloom.cpp.

◆ MatchesAll()

bool CBloomFilter::MatchesAll ( ) const

Returns true if this filter will match anything.

See org.pivxj.core.BloomFilter#setMatchAll() for when this can be a useful thing to do.

Definition at line 127 of file bloom.cpp.

◆ Merge()

bool CBloomFilter::Merge ( const CBloomFilter filter)

Copies filter into this.

Filter must have the same size, hash function count and nTweak or an IllegalArgumentException will be thrown.

Todo:
: Check this.

Definition at line 138 of file bloom.cpp.

◆ reset()

void CBloomFilter::reset ( const unsigned int  nNewTweak)

Definition at line 112 of file bloom.cpp.

Here is the call graph for this function:

◆ SERIALIZE_METHODS()

CBloomFilter::SERIALIZE_METHODS ( CBloomFilter  ,
obj   
)
inline

Definition at line 69 of file bloom.h.

◆ UpdateEmptyFull()

void CBloomFilter::UpdateEmptyFull ( )

Checks for empty and full filters to avoid wasting cpu.

Definition at line 222 of file bloom.cpp.

Member Data Documentation

◆ isEmpty

bool CBloomFilter::isEmpty
private

Definition at line 49 of file bloom.h.

◆ isFull

bool CBloomFilter::isFull
private

Definition at line 48 of file bloom.h.

◆ nFlags

unsigned char CBloomFilter::nFlags
private

Definition at line 52 of file bloom.h.

◆ nHashFuncs

unsigned int CBloomFilter::nHashFuncs
private

Definition at line 50 of file bloom.h.

◆ nTweak

unsigned int CBloomFilter::nTweak
private

Definition at line 51 of file bloom.h.

◆ vData

std::vector<unsigned char> CBloomFilter::vData
private

Definition at line 47 of file bloom.h.


The documentation for this class was generated from the following files: