2017-11-09 19:34:51 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (c) 2012-2015 The Bitcoin Core developers
|
2021-06-20 22:44:44 +02:00
|
|
|
* Copyright (c) 2020 Tom Zander <tom@flowee.org>
|
2017-11-09 19:34:51 +01:00
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
2012-08-13 05:26:27 +02:00
|
|
|
|
|
|
|
|
#include "bloom.h"
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2014-11-18 21:03:02 +00:00
|
|
|
#include "primitives/transaction.h"
|
2014-10-28 17:47:18 -04:00
|
|
|
#include "hash.h"
|
2019-03-11 14:47:12 +01:00
|
|
|
#include "primitives/script.h"
|
2015-07-20 04:43:34 +09:00
|
|
|
#include "random.h"
|
2019-03-11 14:47:12 +01:00
|
|
|
#include "streaming/streams.h"
|
2012-08-13 05:26:27 +02:00
|
|
|
|
2017-08-17 20:53:23 -06:00
|
|
|
#include <cmath>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2020-04-10 21:13:12 +02:00
|
|
|
#include <streaming/P2PBuilder.h>
|
|
|
|
|
|
2012-08-13 05:26:27 +02:00
|
|
|
#define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
|
|
|
|
|
#define LN2 0.6931471805599453094172321214581765680755001343602552
|
|
|
|
|
|
2013-01-10 20:23:28 -05:00
|
|
|
CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn, unsigned char nFlagsIn) :
|
2015-04-24 13:14:45 -04: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
|
|
|
|
|
*/
|
2021-05-23 17:49:57 +02:00
|
|
|
m_data(std::min((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
|
2015-04-24 13:14:45 -04:00
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2021-05-23 17:49:57 +02:00
|
|
|
m_isFull(false),
|
|
|
|
|
m_isEmpty(false),
|
|
|
|
|
m_numHashFuncs(std::min((unsigned int)(m_data.size() * 8 / nElements * LN2), MAX_HASH_FUNCS)),
|
|
|
|
|
m_tweak(nTweakIn),
|
|
|
|
|
m_flags(nFlagsIn)
|
2015-04-24 13:14:45 -04:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private constructor used by CRollingBloomFilter
|
|
|
|
|
CBloomFilter::CBloomFilter(unsigned int nElements, double nFPRate, unsigned int nTweakIn) :
|
2021-05-23 17:49:57 +02:00
|
|
|
m_data((unsigned int)(-1 / LN2SQUARED * nElements * log(nFPRate)) / 8),
|
|
|
|
|
m_isFull(false),
|
|
|
|
|
m_isEmpty(true),
|
|
|
|
|
m_numHashFuncs((unsigned int)(m_data.size() * 8 / nElements * LN2)),
|
|
|
|
|
m_tweak(nTweakIn),
|
|
|
|
|
m_flags(BLOOM_UPDATE_NONE)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
bool CBloomFilter::isEmpty() const
|
|
|
|
|
{
|
|
|
|
|
return m_isEmpty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t CBloomFilter::hash(uint32_t nHashNum, const std::vector<uint8_t> &vDataToHash) const
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
|
|
|
|
// 0xFBA4C795 chosen as it guarantees a reasonable bit difference between nHashNum values.
|
2021-05-23 17:49:57 +02:00
|
|
|
return MurmurHash3(nHashNum * 0xFBA4C795 + m_tweak, vDataToHash) % (m_data.size() * 8);
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
void CBloomFilter::insert(const std::vector<unsigned char> &vKey)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2021-05-23 17:49:57 +02:00
|
|
|
if (m_isFull)
|
2013-02-24 20:36:59 -05:00
|
|
|
return;
|
2021-05-23 17:49:57 +02:00
|
|
|
for (unsigned int i = 0; i < m_numHashFuncs; i++)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2020-04-10 12:21:49 +02:00
|
|
|
unsigned int nIndex = hash(i, vKey);
|
2012-08-13 05:26:27 +02:00
|
|
|
// Sets bit nIndex of vData
|
2021-05-23 17:49:57 +02:00
|
|
|
m_data[nIndex >> 3] |= (1 << (7 & nIndex));
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
2021-05-23 17:49:57 +02:00
|
|
|
m_isEmpty = false;
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
void CBloomFilter::insert(const COutPoint &outpoint)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
stream << outpoint;
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data(stream.begin(), stream.end());
|
2012-08-13 05:26:27 +02:00
|
|
|
insert(data);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
void CBloomFilter::insert(const uint256 &hash)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data(hash.begin(), hash.end());
|
2012-08-13 05:26:27 +02:00
|
|
|
insert(data);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 12:21:49 +02:00
|
|
|
void CBloomFilter::insert(const Streaming::ConstBuffer &buf)
|
|
|
|
|
{
|
|
|
|
|
std::vector<unsigned char> data(buf.begin(), buf.end());
|
|
|
|
|
insert(data);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
bool CBloomFilter::contains(const std::vector<unsigned char> &vKey) const
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2021-05-23 17:49:57 +02:00
|
|
|
if (m_isFull)
|
2013-02-24 20:36:59 -05:00
|
|
|
return true;
|
2021-05-23 17:49:57 +02:00
|
|
|
if (m_isEmpty)
|
2013-08-18 20:21:06 -07:00
|
|
|
return false;
|
2021-05-23 17:49:57 +02:00
|
|
|
for (unsigned int i = 0; i < m_numHashFuncs; i++)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2020-04-10 12:21:49 +02:00
|
|
|
unsigned int nIndex = hash(i, vKey);
|
2012-08-13 05:26:27 +02:00
|
|
|
// Checks bit nIndex of vData
|
2021-05-23 17:49:57 +02:00
|
|
|
if (!(m_data[nIndex >> 3] & (1 << (7 & nIndex))))
|
2012-08-13 05:26:27 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
bool CBloomFilter::contains(const COutPoint &outpoint) const
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
|
|
|
|
CDataStream stream(SER_NETWORK, PROTOCOL_VERSION);
|
|
|
|
|
stream << outpoint;
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data(stream.begin(), stream.end());
|
2012-08-13 05:26:27 +02:00
|
|
|
return contains(data);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
bool CBloomFilter::contains(const uint256 &hash) const
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data(hash.begin(), hash.end());
|
2012-08-13 05:26:27 +02:00
|
|
|
return contains(data);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-21 11:50:07 -07:00
|
|
|
void CBloomFilter::clear()
|
|
|
|
|
{
|
2021-05-23 17:49:57 +02:00
|
|
|
m_data.assign(m_data.size(), 0);
|
|
|
|
|
m_isFull = false;
|
|
|
|
|
m_isEmpty = true;
|
2014-07-21 11:50:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-07-20 04:43:34 +09:00
|
|
|
void CBloomFilter::reset(unsigned int nNewTweak)
|
|
|
|
|
{
|
|
|
|
|
clear();
|
2021-05-23 17:49:57 +02:00
|
|
|
m_tweak = nNewTweak;
|
2015-07-20 04:43:34 +09:00
|
|
|
}
|
|
|
|
|
|
2021-05-23 17:49:57 +02:00
|
|
|
bool CBloomFilter::isWithinSizeConstraints() const
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2021-05-23 17:49:57 +02:00
|
|
|
return m_data.size() <= MAX_BLOOM_FILTER_SIZE && m_numHashFuncs <= MAX_HASH_FUNCS;
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-17 09:33:08 +01:00
|
|
|
bool CBloomFilter::matchAndInsertOutputs(const CTransaction& tx)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2012-08-18 23:38:28 -04: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
|
2021-05-23 17:49:57 +02:00
|
|
|
if (m_isFull)
|
2013-08-18 20:21:06 -07:00
|
|
|
return true;
|
2021-05-23 17:49:57 +02:00
|
|
|
if (m_isEmpty)
|
2013-08-18 20:21:06 -07:00
|
|
|
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-18 23:38:28 -04:00
|
|
|
fFound = true;
|
2012-08-13 05:26:27 +02:00
|
|
|
|
2012-08-18 23:38:28 -04:00
|
|
|
for (unsigned int i = 0; i < tx.vout.size(); i++)
|
2012-08-13 05:26:27 +02:00
|
|
|
{
|
2012-08-18 23:38:28 -04: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-18 23:38:28 -04: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();
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data;
|
2012-08-13 05:26:27 +02:00
|
|
|
while (pc < txout.scriptPubKey.end())
|
|
|
|
|
{
|
|
|
|
|
opcodetype opcode;
|
|
|
|
|
if (!txout.scriptPubKey.GetOp(pc, opcode, data))
|
|
|
|
|
break;
|
|
|
|
|
if (data.size() != 0 && contains(data))
|
2012-08-18 23:38:28 -04:00
|
|
|
{
|
|
|
|
|
fFound = true;
|
2021-05-23 17:49:57 +02:00
|
|
|
if ((m_flags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_ALL)
|
2013-01-10 20:23:28 -05:00
|
|
|
insert(COutPoint(hash, i));
|
2021-05-23 17:49:57 +02:00
|
|
|
else if ((m_flags & BLOOM_UPDATE_MASK) == BLOOM_UPDATE_P2PUBKEY_ONLY)
|
2013-01-10 20:23:28 -05:00
|
|
|
{
|
2019-06-06 21:37:49 +02:00
|
|
|
Script::TxnOutType type;
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<std::vector<unsigned char> > vSolutions;
|
2019-06-06 21:37:49 +02:00
|
|
|
if (Script::solver(txout.scriptPubKey, type, vSolutions) &&
|
|
|
|
|
(type == Script::TX_PUBKEY || type == Script::TX_MULTISIG))
|
2013-01-10 20:23:28 -05:00
|
|
|
insert(COutPoint(hash, i));
|
|
|
|
|
}
|
2012-08-18 23:38:28 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2012-08-13 05:26:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 09:33:08 +01:00
|
|
|
return fFound;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CBloomFilter::matchInputs(const CTransaction &tx) {
|
2021-05-23 17:49:57 +02:00
|
|
|
if (m_isEmpty)
|
2020-12-17 09:33:08 +01:00
|
|
|
return false;
|
2012-08-18 23:38:28 -04:00
|
|
|
|
2018-12-30 15:33:11 +01:00
|
|
|
for (const CTxIn& txin : tx.vin) {
|
2012-08-13 05:26:27 +02:00
|
|
|
// 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();
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data;
|
2012-08-13 05:26:27 +02:00
|
|
|
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-18 20:21:06 -07:00
|
|
|
|
2020-04-10 12:21:49 +02:00
|
|
|
void CBloomFilter::updateEmptyFull()
|
2013-08-18 20:21:06 -07:00
|
|
|
{
|
|
|
|
|
bool full = true;
|
|
|
|
|
bool empty = true;
|
2021-05-23 17:49:57 +02:00
|
|
|
for (unsigned int i = 0; i < m_data.size(); i++)
|
2013-08-18 20:21:06 -07:00
|
|
|
{
|
2021-05-23 17:49:57 +02:00
|
|
|
full &= m_data[i] == 0xff;
|
|
|
|
|
empty &= m_data[i] == 0;
|
2013-08-18 20:21:06 -07:00
|
|
|
}
|
2021-05-23 17:49:57 +02:00
|
|
|
m_isFull = full;
|
|
|
|
|
m_isEmpty = empty;
|
2013-08-18 20:21:06 -07:00
|
|
|
}
|
2015-04-24 13:14:45 -04:00
|
|
|
|
2020-04-10 21:13:12 +02:00
|
|
|
void CBloomFilter::store(Streaming::P2PBuilder &builder) const
|
|
|
|
|
{
|
2021-05-23 17:49:57 +02:00
|
|
|
builder.writeByteArray(m_data, Streaming::WithLength);
|
|
|
|
|
builder.writeInt(m_numHashFuncs);
|
|
|
|
|
builder.writeInt(m_tweak);
|
|
|
|
|
builder.writeByte(m_flags);
|
2020-04-10 21:13:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ///////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
2015-07-27 18:58:00 +02:00
|
|
|
CRollingBloomFilter::CRollingBloomFilter(unsigned int nElements, double fpRate) :
|
|
|
|
|
b1(nElements * 2, fpRate, 0), b2(nElements * 2, fpRate, 0)
|
2015-04-24 13:14:45 -04:00
|
|
|
{
|
|
|
|
|
// Implemented using two bloom filters of 2 * nElements each.
|
|
|
|
|
// We fill them up, and clear them, staggered, every nElements
|
|
|
|
|
// inserted, so at least one always contains the last nElements
|
|
|
|
|
// inserted.
|
2015-07-27 18:58:00 +02:00
|
|
|
nInsertions = 0;
|
2015-04-24 13:14:45 -04:00
|
|
|
nBloomSize = nElements * 2;
|
2015-07-20 04:43:34 +09:00
|
|
|
|
2015-07-27 18:58:00 +02:00
|
|
|
reset();
|
2015-04-24 13:14:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CRollingBloomFilter::insert(const std::vector<unsigned char>& vKey)
|
|
|
|
|
{
|
|
|
|
|
if (nInsertions == 0) {
|
|
|
|
|
b1.clear();
|
|
|
|
|
} else if (nInsertions == nBloomSize / 2) {
|
|
|
|
|
b2.clear();
|
|
|
|
|
}
|
|
|
|
|
b1.insert(vKey);
|
|
|
|
|
b2.insert(vKey);
|
|
|
|
|
if (++nInsertions == nBloomSize) {
|
|
|
|
|
nInsertions = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 06:42:43 -04:00
|
|
|
void CRollingBloomFilter::insert(const uint256& hash)
|
|
|
|
|
{
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data(hash.begin(), hash.end());
|
2015-07-27 18:38:45 +02:00
|
|
|
insert(data);
|
2015-07-17 06:42:43 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-24 13:14:45 -04:00
|
|
|
bool CRollingBloomFilter::contains(const std::vector<unsigned char>& vKey) const
|
|
|
|
|
{
|
|
|
|
|
if (nInsertions < nBloomSize / 2) {
|
|
|
|
|
return b2.contains(vKey);
|
|
|
|
|
}
|
|
|
|
|
return b1.contains(vKey);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-17 06:42:43 -04:00
|
|
|
bool CRollingBloomFilter::contains(const uint256& hash) const
|
|
|
|
|
{
|
2017-06-18 18:15:06 -04:00
|
|
|
std::vector<unsigned char> data(hash.begin(), hash.end());
|
2015-07-27 18:38:45 +02:00
|
|
|
return contains(data);
|
2015-07-17 06:42:43 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-27 18:58:00 +02:00
|
|
|
void CRollingBloomFilter::reset()
|
2015-04-24 13:14:45 -04:00
|
|
|
{
|
2015-07-27 18:58:00 +02:00
|
|
|
unsigned int nNewTweak = GetRand(std::numeric_limits<unsigned int>::max());
|
2015-07-20 04:43:34 +09:00
|
|
|
b1.reset(nNewTweak);
|
|
|
|
|
b2.reset(nNewTweak);
|
2015-04-24 13:14:45 -04:00
|
|
|
nInsertions = 0;
|
|
|
|
|
}
|