18 #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455 19 #define LN2 0.6931471805599453094172321214581765680755001343602552 27 vData(
std::min((unsigned int)(-1 /
LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
35 nHashFuncs(
std::min((unsigned int)(vData.size() * 8 / nElements *
LN2), MAX_HASH_FUNCS)),
41 inline unsigned int CBloomFilter::Hash(
unsigned int nHashNum,
const std::vector<unsigned char>& vDataToHash)
const 53 unsigned int nIndex =
Hash(i, vKey);
55 vData[nIndex >> 3] |= (1 << (7 & nIndex));
64 std::vector<unsigned char> data(stream.
begin(), stream.
end());
70 std::vector<unsigned char> data(hash.
begin(), hash.
end());
82 unsigned int nIndex =
Hash(i, vKey);
84 if (!(
vData[nIndex >> 3] & (1 << (7 & nIndex))))
94 std::vector<unsigned char> data(stream.
begin(), stream.
end());
100 std::vector<unsigned char> data(hash.
begin(), hash.
end());
119 return vData.size() <= MAX_BLOOM_FILTER_SIZE &&
nHashFuncs <= MAX_HASH_FUNCS;
135 for (
unsigned int i = 0; i < tx.
vout.size(); i++)
143 std::vector<unsigned char> data;
149 if (data.size() != 0 &&
contains(data))
156 std::vector<std::vector<unsigned char> > vSolutions;
178 std::vector<unsigned char> data;
184 if (data.size() != 0 &&
contains(data))
196 for (
unsigned int i = 0; i <
vData.size(); i++)
198 full &=
vData[i] == 0xff;
199 empty &=
vData[i] == 0;
207 double logFpRate = log(fpRate);
210 nHashFuncs = std::max(1, std::min((
int)round(logFpRate / log(0.5)), 50));
221 uint32_t nFilterBits = (uint32_t)ceil(-1.0 *
nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate /
nHashFuncs)));
228 data.resize(((nFilterBits + 63) / 64) << 1);
233 static inline uint32_t RollingBloomHash(
unsigned int nHashNum, uint32_t nTweak,
const std::vector<unsigned char>& vDataToHash) {
234 return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
241 static inline uint32_t FastMod(uint32_t x,
size_t n) {
242 return ((uint64_t)x * (uint64_t)n) >> 32;
253 uint64_t nGenerationMask1 = 0 - (uint64_t)(
nGeneration & 1);
254 uint64_t nGenerationMask2 = 0 - (uint64_t)(
nGeneration >> 1);
256 for (uint32_t p = 0; p <
data.size(); p += 2) {
257 uint64_t p1 =
data[p], p2 =
data[p + 1];
258 uint64_t mask = (p1 ^ nGenerationMask1) | (p2 ^ nGenerationMask2);
260 data[p + 1] = p2 & mask;
266 uint32_t h = RollingBloomHash(n,
nTweak, vKey);
269 uint32_t pos = FastMod(h,
data.size());
271 data[pos & ~1] = (
data[pos & ~1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(
nGeneration & 1)) << bit;
272 data[pos | 1] = (
data[pos | 1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(
nGeneration >> 1)) << bit;
278 std::vector<unsigned char> vData(hash.
begin(), hash.
end());
285 uint32_t h = RollingBloomHash(n,
nTweak, vKey);
287 uint32_t pos = FastMod(h,
data.size());
289 if (!(((
data[pos & ~1] |
data[pos | 1]) >> bit) & 1)) {
298 std::vector<unsigned char> vData(hash.
begin(), hash.
end());
307 for (std::vector<uint64_t>::iterator it =
data.begin(); it !=
data.end(); it++) {
CRollingBloomFilter(const unsigned int nElements, const double nFPRate)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
void insert(const std::vector< unsigned char > &vKey)
bool IsRelevantAndUpdate(const CTransaction &tx)
Also adds any outputs which match the filter to the filter (to match their spending txes) ...
std::vector< unsigned char > vData
txnouttype Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
unsigned int Hash(unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
void reset(const unsigned int nNewTweak)
Double ended buffer combining vector and stream-like interfaces.
void insert(const std::vector< unsigned char > &vKey)
const std::vector< CTxIn > vin
bool contains(const std::vector< unsigned char > &vKey) const
opcodetype
Script opcodes.
An input of a transaction.
const uint256 & GetHash() const
const std::vector< CTxOut > vout
An output of a transaction.
An outpoint - a combination of a transaction hash and an index n into its vout.
std::vector< uint64_t > data
int nEntriesThisGeneration
const_iterator end() const
const_iterator begin() const
void UpdateEmptyFull()
Checks for empty and full filters to avoid wasting cpu.
The basic transaction that is broadcasted on the network and contained in blocks. ...
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
bool IsWithinSizeConstraints() const
True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS (c...
uint64_t GetRand(uint64_t nMax)
bool contains(const std::vector< unsigned char > &vKey) const
int nEntriesPerGeneration