2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2012-2015 The Bitcoin Core developers
|
2014-10-31 01:43:19 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-08-13 05:26:27 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "bloom.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2014-11-18 22:03:02 +01:00
|
|
|
#include "primitives/transaction.h"
|
2014-10-28 22:47:18 +01:00
|
|
|
#include "hash.h"
|
2014-08-23 03:35:51 +02:00
|
|
|
#include "script/script.h"
|
|
|
|
#include "script/standard.h"
|
2015-07-19 21:43:34 +02:00
|
|
|
#include "random.h"
|
2014-10-22 21:08:30 +02:00
|
|
|
#include "streams.h"
|
2012-08-13 05:26:27 +02:00
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-08-23 05:09:47 +02:00
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
|
2012-08-13 05:26:27 +02:00
|
|
|
#define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
|
|
|
|
#define LN2 0.6931471805599453094172321214581765680755001343602552
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2013-01-11 02:23:28 +01:00
|
|
|
CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn, unsigned char nFlagsIn) :
|
2015-04-24 19:14:45 +02:00
|
|
|
/**
|
|
|
|
* The ideal size for a bloom filter with a given number of elements and false positive rate is:
|
|
|
|
* - nElements * log(fp rate) / ln(2)^2
|
|
|
|
* We ignore filter parameters which will create a bloom filter larger than the protocol limits
|
|
|
|
*/
|
|
|
|
vData(min((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
isFull(false),
|
|
|
|
isEmpty(false),
|
|
|
|
nHashFuncs(min((unsigned int)(vData.size() * 8 / nElements * LN2), MAX_HASH_FUNCS)),
|
|
|
|
nTweak(nTweakIn),
|
|
|
|
nFlags(nFlagsIn)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private constructor used by CRollingBloomFilter
|
|
|
|
CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn) :
|
|
|
|
vData((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)) / 8),
|
|
|
|
isFull(false),
|
|
|
|
isEmpty(true),
|
|
|
|
nHashFuncs((unsigned int)(vData.size() * 8 / nElements * LN2)),
|
|
|
|
nTweak(nTweakIn),
|
|
|
|
nFlags(BLOOM_UPDATE_NONE)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
inline unsigned int CBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const
|
|
|
|
{
|
|
|
|
// 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
|
2012-11-02 23:33:50 +01:00
|
|
|
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (vData.size() * 8);
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBloomFilter::insert(const vector<unsigned char>& vKey)
|
|
|
|
{
|
2013-08-19 05:21:06 +02:00
|
|
|
if (isFull)
|
2013-02-25 02:36:59 +01:00
|
|
|
return;
|
2012-08-13 05:26:27 +02:00
|
|
|
for (unsigned int i = 0; i < nHashFuncs; i++)
|
|
|
|
{
|
|
|
|
unsigned int nIndex = Hash(i, vKey);
|
|
|
|
// Sets bit nIndex of vData
|
2014-03-20 05:21:23 +01:00
|
|
|
vData[nIndex >> 3] |= (1 << (7 & nIndex));
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
2013-08-19 05:21:06 +02:00
|
|
|
isEmpty = false;
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CBloomFilter::insert(const COutPoint& outpoint)
|
|
|
|
{
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
stream << outpoint;
|
|
|
|
vector<unsigned char> data(stream.begin(), stream.end());
|
|
|
|
insert(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CBloomFilter::insert(const uint256& hash)
|
|
|
|
{
|
|
|
|
vector<unsigned char> data(hash.begin(), hash.end());
|
|
|
|
insert(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBloomFilter::contains(const vector<unsigned char>& vKey) const
|
|
|
|
{
|
2013-08-19 05:21:06 +02:00
|
|
|
if (isFull)
|
2013-02-25 02:36:59 +01:00
|
|
|
return true;
|
2013-08-19 05:21:06 +02:00
|
|
|
if (isEmpty)
|
|
|
|
return false;
|
2012-08-13 05:26:27 +02:00
|
|
|
for (unsigned int i = 0; i < nHashFuncs; i++)
|
|
|
|
{
|
|
|
|
unsigned int nIndex = Hash(i, vKey);
|
|
|
|
// Checks bit nIndex of vData
|
2014-03-20 05:21:23 +01:00
|
|
|
if (!(vData[nIndex >> 3] & (1 << (7 & nIndex))))
|
2012-08-13 05:26:27 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBloomFilter::contains(const COutPoint& outpoint) const
|
|
|
|
{
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
stream << outpoint;
|
|
|
|
vector<unsigned char> data(stream.begin(), stream.end());
|
|
|
|
return contains(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBloomFilter::contains(const uint256& hash) const
|
|
|
|
{
|
|
|
|
vector<unsigned char> data(hash.begin(), hash.end());
|
|
|
|
return contains(data);
|
|
|
|
}
|
|
|
|
|
2014-07-21 20:50:07 +02:00
|
|
|
void CBloomFilter::clear()
|
|
|
|
{
|
|
|
|
vData.assign(vData.size(),0);
|
|
|
|
isFull = false;
|
|
|
|
isEmpty = true;
|
|
|
|
}
|
|
|
|
|
2015-07-19 21:43:34 +02:00
|
|
|
void CBloomFilter::reset(unsigned int nNewTweak)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
nTweak = nNewTweak;
|
|
|
|
}
|
|
|
|
|
2012-08-13 05:26:27 +02:00
|
|
|
bool CBloomFilter::IsWithinSizeConstraints() const
|
|
|
|
{
|
|
|
|
return vData.size() <= MAX_BLOOM_FILTER_SIZE && nHashFuncs <= MAX_HASH_FUNCS;
|
|
|
|
}
|
|
|
|
|
2014-06-09 10:02:00 +02:00
|
|
|
bool CBloomFilter::IsRelevantAndUpdate(const CTransaction& tx)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2012-08-19 05:38:28 +02:00
|
|
|
bool fFound = false;
|
2012-08-13 05:26:27 +02:00
|
|
|
// Match if the filter contains the hash of tx
|
|
|
|
// for finding tx when they appear in a block
|
2013-08-19 05:21:06 +02:00
|
|
|
if (isFull)
|
|
|
|
return true;
|
|
|
|
if (isEmpty)
|
|
|
|
return false;
|
2014-06-09 10:02:00 +02:00
|
|
|
const uint256& hash = tx.GetHash();
|
2012-08-13 05:26:30 +02:00
|
|
|
if (contains(hash))
|
2012-08-19 05:38:28 +02:00
|
|
|
fFound = true;
|
2012-08-13 05:26:27 +02:00
|
|
|
|
2012-08-19 05:38:28 +02:00
|
|
|
for (unsigned int i = 0; i < tx.vout.size(); i++)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2012-08-19 05:38:28 +02:00
|
|
|
const CTxOut& txout = tx.vout[i];
|
2012-08-13 05:26:27 +02:00
|
|
|
// Match if the filter contains any arbitrary script data element in any scriptPubKey in tx
|
2012-08-19 05:38:28 +02:00
|
|
|
// If this matches, also add the specific output that was matched.
|
|
|
|
// This means clients don't have to update the filter themselves when a new relevant tx
|
|
|
|
// is discovered in order to find spending transactions, which avoids round-tripping and race conditions.
|
2012-08-13 05:26:27 +02:00
|
|
|
CScript::const_iterator pc = txout.scriptPubKey.begin();
|
|
|
|
vector<unsigned char> data;
|
|
|
|
while (pc < txout.scriptPubKey.end())
|
|
|
|
{
|
|
|
|
opcodetype opcode;
|
|
|
|
if (!txout.scriptPubKey.GetOp(pc, opcode, data))
|
|
|
|
break;
|
|
|
|
if (data.size() != 0 && contains(data))
|
2012-08-19 05:38:28 +02:00
|
|
|
{
|
|
|
|
fFound = true;
|
2013-01-11 02:23:28 +01:00
|
|
|
if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
|
|
|
|
insert(COutPoint(hash, i));
|
|
|
|
else if ((nFlags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_P2PUBKEY_ONLY)
|
|
|
|
{
|
|
|
|
txnouttype type;
|
|
|
|
vector<vector<unsigned char> > vSolutions;
|
|
|
|
if (Solver(txout.scriptPubKey, type, vSolutions) &&
|
|
|
|
(type == TX_PUBKEY || type == TX_MULTISIG))
|
|
|
|
insert(COutPoint(hash, i));
|
|
|
|
}
|
2012-08-19 05:38:28 +02:00
|
|
|
break;
|
|
|
|
}
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-19 05:38:28 +02:00
|
|
|
if (fFound)
|
|
|
|
return true;
|
|
|
|
|
2012-08-13 05:26:27 +02:00
|
|
|
BOOST_FOREACH(const CTxIn& txin, tx.vin)
|
|
|
|
{
|
|
|
|
// Match if the filter contains an outpoint tx spends
|
|
|
|
if (contains(txin.prevout))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Match if the filter contains any arbitrary script data element in any scriptSig in tx
|
|
|
|
CScript::const_iterator pc = txin.scriptSig.begin();
|
|
|
|
vector<unsigned char> data;
|
|
|
|
while (pc < txin.scriptSig.end())
|
|
|
|
{
|
|
|
|
opcodetype opcode;
|
|
|
|
if (!txin.scriptSig.GetOp(pc, opcode, data))
|
|
|
|
break;
|
|
|
|
if (data.size() != 0 && contains(data))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-19 05:21:06 +02:00
|
|
|
|
|
|
|
void CBloomFilter::UpdateEmptyFull()
|
|
|
|
{
|
|
|
|
bool full = true;
|
|
|
|
bool empty = true;
|
|
|
|
for (unsigned int i = 0; i < vData.size(); i++)
|
|
|
|
{
|
|
|
|
full &= vData[i] == 0xff;
|
|
|
|
empty &= vData[i] == 0;
|
|
|
|
}
|
|
|
|
isFull = full;
|
|
|
|
isEmpty = empty;
|
|
|
|
}
|
2015-04-24 19:14:45 +02:00
|
|
|
|
2015-11-27 13:20:29 +01:00
|
|
|
CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate)
|
2015-04-24 19:14:45 +02:00
|
|
|
{
|
2015-11-27 13:20:29 +01:00
|
|
|
double logFpRate = log(fpRate);
|
|
|
|
/* The optimal number of hash functions is log(fpRate) / log(0.5), but
|
|
|
|
* restrict it to the range 1-50. */
|
|
|
|
nHashFuncs = std::max(1, std::min((int)round(logFpRate / log(0.5)), 50));
|
|
|
|
/* In this rolling bloom filter, we'll store between 2 and 3 generations of nElements / 2 entries. */
|
|
|
|
nEntriesPerGeneration = (nElements + 1) / 2;
|
|
|
|
uint32_t nMaxElements = nEntriesPerGeneration * 3;
|
|
|
|
/* The maximum fpRate = pow(1.0 - exp(-nHashFuncs * nMaxElements / nFilterBits), nHashFuncs)
|
|
|
|
* => pow(fpRate, 1.0 / nHashFuncs) = 1.0 - exp(-nHashFuncs * nMaxElements / nFilterBits)
|
|
|
|
* => 1.0 - pow(fpRate, 1.0 / nHashFuncs) = exp(-nHashFuncs * nMaxElements / nFilterBits)
|
|
|
|
* => log(1.0 - pow(fpRate, 1.0 / nHashFuncs)) = -nHashFuncs * nMaxElements / nFilterBits
|
|
|
|
* => nFilterBits = -nHashFuncs * nMaxElements / log(1.0 - pow(fpRate, 1.0 / nHashFuncs))
|
|
|
|
* => nFilterBits = -nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate / nHashFuncs))
|
|
|
|
*/
|
|
|
|
uint32_t nFilterBits = (uint32_t)ceil(-1.0 * nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate / nHashFuncs)));
|
|
|
|
data.clear();
|
|
|
|
/* We store up to 16 'bits' per data element. */
|
|
|
|
data.resize((nFilterBits + 15) / 16);
|
2015-07-27 18:58:00 +02:00
|
|
|
reset();
|
2015-04-24 19:14:45 +02:00
|
|
|
}
|
|
|
|
|
2015-11-27 13:20:29 +01:00
|
|
|
/* Similar to CBloomFilter::Hash */
|
|
|
|
inline unsigned int CRollingBloomFilter::Hash(unsigned int nHashNum, const std::vector<unsigned char>& vDataToHash) const {
|
|
|
|
return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash) % (data.size() * 16);
|
|
|
|
}
|
|
|
|
|
2015-04-24 19:14:45 +02:00
|
|
|
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
|
|
|
|
{
|
2015-11-27 13:20:29 +01:00
|
|
|
if (nEntriesThisGeneration == nEntriesPerGeneration) {
|
|
|
|
nEntriesThisGeneration = 0;
|
|
|
|
nGeneration++;
|
|
|
|
if (nGeneration == 4) {
|
|
|
|
nGeneration = 1;
|
|
|
|
}
|
|
|
|
/* Wipe old entries that used this generation number. */
|
|
|
|
for (uint32_t p = 0; p < data.size() * 16; p++) {
|
|
|
|
if (get(p) == nGeneration) {
|
|
|
|
put(p, 0);
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 19:14:45 +02:00
|
|
|
}
|
2015-11-27 13:20:29 +01:00
|
|
|
nEntriesThisGeneration++;
|
|
|
|
|
|
|
|
for (int n = 0; n < nHashFuncs; n++) {
|
|
|
|
uint32_t h = Hash(n, vKey);
|
|
|
|
put(h, nGeneration);
|
2015-04-24 19:14:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-17 12:42:43 +02:00
|
|
|
void CRollingBloomFilter::insert(const uint256& hash)
|
|
|
|
{
|
2015-07-27 18:38:45 +02:00
|
|
|
vector<unsigned char> data(hash.begin(), hash.end());
|
|
|
|
insert(data);
|
2015-07-17 12:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 19:14:45 +02:00
|
|
|
bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
|
|
|
|
{
|
2015-11-27 13:20:29 +01:00
|
|
|
for (int n = 0; n < nHashFuncs; n++) {
|
|
|
|
uint32_t h = Hash(n, vKey);
|
|
|
|
if (get(h) == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-24 19:14:45 +02:00
|
|
|
}
|
2015-11-27 13:20:29 +01:00
|
|
|
return true;
|
2015-04-24 19:14:45 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 12:42:43 +02:00
|
|
|
bool CRollingBloomFilter::contains(const uint256& hash) const
|
|
|
|
{
|
2015-07-27 18:38:45 +02:00
|
|
|
vector<unsigned char> data(hash.begin(), hash.end());
|
|
|
|
return contains(data);
|
2015-07-17 12:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-07-27 18:58:00 +02:00
|
|
|
void CRollingBloomFilter::reset()
|
2015-04-24 19:14:45 +02:00
|
|
|
{
|
2015-11-27 13:20:29 +01:00
|
|
|
nTweak = GetRand(std::numeric_limits<unsigned int>::max());
|
|
|
|
nEntriesThisGeneration = 0;
|
|
|
|
nGeneration = 1;
|
|
|
|
for (std::vector<uint32_t>::iterator it = data.begin(); it != data.end(); it++) {
|
|
|
|
*it = 0;
|
|
|
|
}
|
2015-04-24 19:14:45 +02:00
|
|
|
}
|