2012-08-13 05:26:27 +02:00
|
|
|
// Copyright (c) 2012 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2012-08-13 05:26:27 +02:00
|
|
|
#ifndef BITCOIN_BLOOM_H
|
|
|
|
#define BITCOIN_BLOOM_H
|
|
|
|
|
|
|
|
#include "serialize.h"
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2012-08-13 05:26:27 +02:00
|
|
|
class COutPoint;
|
|
|
|
class CTransaction;
|
2013-04-13 07:13:08 +02:00
|
|
|
class uint256;
|
2012-08-13 05:26:27 +02:00
|
|
|
|
|
|
|
// 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 const unsigned int MAX_HASH_FUNCS = 50;
|
|
|
|
|
2013-01-11 02:23:28 +01:00
|
|
|
// First two bits of nFlags control how much IsRelevantAndUpdate actually updates
|
|
|
|
// The remaining bits are reserved
|
|
|
|
enum bloomflags
|
|
|
|
{
|
|
|
|
BLOOM_UPDATE_NONE = 0,
|
|
|
|
BLOOM_UPDATE_ALL = 1,
|
|
|
|
// Only adds outpoints to the filter if the output is a pay-to-pubkey/pay-to-multisig script
|
|
|
|
BLOOM_UPDATE_P2PUBKEY_ONLY = 2,
|
|
|
|
BLOOM_UPDATE_MASK = 3,
|
|
|
|
};
|
2012-08-13 05:26:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
class CBloomFilter
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector<unsigned char> vData;
|
2013-08-19 05:21:06 +02:00
|
|
|
bool isFull;
|
|
|
|
bool isEmpty;
|
2012-08-13 05:26:27 +02:00
|
|
|
unsigned int nHashFuncs;
|
2012-11-02 23:33:50 +01:00
|
|
|
unsigned int nTweak;
|
2013-01-11 02:23:28 +01:00
|
|
|
unsigned char nFlags;
|
2012-08-13 05:26:27 +02:00
|
|
|
|
|
|
|
unsigned int Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// 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.
|
|
|
|
// This will apply if nFPRate is very low or nElements is unreasonably high.
|
2012-11-02 23:33:50 +01:00
|
|
|
// 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)
|
2013-01-11 02:23:28 +01:00
|
|
|
// nFlags should be one of the BLOOM_UPDATE_* enums (not _MASK)
|
|
|
|
CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweak, unsigned char nFlagsIn);
|
2014-08-28 15:28:57 +02:00
|
|
|
CBloomFilter() : isFull(true), isEmpty(false), nHashFuncs(0), nTweak(0), nFlags(0) {}
|
2012-08-13 05:26:27 +02:00
|
|
|
|
2014-09-02 09:58:09 +02:00
|
|
|
ADD_SERIALIZE_METHODS;
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 08:42:31 +02:00
|
|
|
|
2014-08-20 22:44:38 +02:00
|
|
|
template <typename Stream, typename Operation>
|
2014-08-21 00:49:32 +02:00
|
|
|
inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
|
2012-08-13 05:26:27 +02:00
|
|
|
READWRITE(vData);
|
|
|
|
READWRITE(nHashFuncs);
|
2012-11-02 23:33:50 +01:00
|
|
|
READWRITE(nTweak);
|
2013-01-11 02:23:28 +01:00
|
|
|
READWRITE(nFlags);
|
overhaul serialization code
The implementation of each class' serialization/deserialization is no longer
passed within a macro. The implementation now lies within a template of form:
template <typename T, typename Stream, typename Operation>
inline static size_t SerializationOp(T thisPtr, Stream& s, Operation ser_action, int nType, int nVersion) {
size_t nSerSize = 0;
/* CODE */
return nSerSize;
}
In cases when codepath should depend on whether or not we are just deserializing
(old fGetSize, fWrite, fRead flags) an additional clause can be used:
bool fRead = boost::is_same<Operation, CSerActionUnserialize>();
The IMPLEMENT_SERIALIZE macro will now be a freestanding clause added within
class' body (similiar to Qt's Q_OBJECT) to implement GetSerializeSize,
Serialize and Unserialize. These are now wrappers around
the "SerializationOp" template.
2014-08-20 08:42:31 +02:00
|
|
|
}
|
2012-08-13 05:26:27 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-07-21 20:50:07 +02:00
|
|
|
void clear();
|
|
|
|
|
2012-08-13 05:26:27 +02:00
|
|
|
// 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)
|
|
|
|
bool IsWithinSizeConstraints() const;
|
|
|
|
|
2012-08-19 05:38:28 +02:00
|
|
|
// Also adds any outputs which match the filter to the filter (to match their spending txes)
|
2014-06-09 10:02:00 +02:00
|
|
|
bool IsRelevantAndUpdate(const CTransaction& tx);
|
2013-08-19 05:21:06 +02:00
|
|
|
|
|
|
|
// Checks for empty and full filters to avoid wasting cpu
|
|
|
|
void UpdateEmptyFull();
|
2012-08-13 05:26:27 +02:00
|
|
|
};
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_BLOOM_H
|