19 #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
20 #define LN2 0.6931471805599453094172321214581765680755001343602552
28 vData(
std::min((unsigned int)(-1 /
LN2SQUARED * nElements * log(nFPRate)), MAX_BLOOM_FILTER_SIZE * 8) / 8),
36 nHashFuncs(
std::min((unsigned int)(vData.size() * 8 / nElements *
LN2), MAX_HASH_FUNCS)),
42 inline unsigned int CBloomFilter::Hash(
unsigned int nHashNum,
const std::vector<unsigned char>& vDataToHash)
const
52 for (
unsigned int i = 0; i <
nHashFuncs; i++) {
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 for (
unsigned int i = 0; i <
nHashFuncs; i++) {
83 unsigned int nIndex =
Hash(i, vKey);
85 if (!(
vData[nIndex >> 3] & (1 << (7 & nIndex))))
95 std::vector<unsigned char> data(stream.
begin(), stream.
end());
101 std::vector<unsigned char> data(hash.
begin(), hash.
end());
120 return vData.size() <= MAX_BLOOM_FILTER_SIZE &&
nHashFuncs <= MAX_HASH_FUNCS;
128 for (
unsigned char b :
vData)
140 if(! (filter.
vData.size() == this->vData.size() &&
142 filter.
nTweak == this->nTweak)){
145 for (
unsigned int i = 0; i <
vData.size(); i++)
150 this->
vData[0] = 0xff;
168 for (
unsigned int i = 0; i < tx.
vout.size(); i++) {
175 std::vector<unsigned char> data;
182 if (data.size() != 0 &&
contains(data)) {
188 std::vector<std::vector<unsigned char> > vSolutions;
208 std::vector<unsigned char> data;
213 if (data.size() != 0 &&
contains(data)) {
226 for (
unsigned int i = 0; i <
vData.size(); i++) {
227 full &=
vData[i] == 0xff;
228 empty &=
vData[i] == 0;
236 double logFpRate = log(fpRate);
239 nHashFuncs = std::max(1, std::min((
int)round(logFpRate / log(0.5)), 50));
250 uint32_t nFilterBits = (uint32_t)ceil(-1.0 *
nHashFuncs * nMaxElements / log(1.0 - exp(logFpRate /
nHashFuncs)));
257 data.resize(((nFilterBits + 63) / 64) << 1);
262 static inline uint32_t RollingBloomHash(
unsigned int nHashNum, uint32_t nTweak,
const std::vector<unsigned char>& vDataToHash) {
263 return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
270 static inline uint32_t FastMod(uint32_t x,
size_t n) {
271 return ((uint64_t)x * (uint64_t)n) >> 32;
282 uint64_t nGenerationMask1 = -(uint64_t)(
nGeneration & 1);
283 uint64_t nGenerationMask2 = -(uint64_t)(
nGeneration >> 1);
285 for (uint32_t p = 0; p <
data.size(); p += 2) {
286 uint64_t p1 =
data[p], p2 =
data[p + 1];
287 uint64_t mask = (p1 ^ nGenerationMask1) | (p2 ^ nGenerationMask2);
289 data[p + 1] = p2 & mask;
295 uint32_t h = RollingBloomHash(n,
nTweak, vKey);
298 uint32_t pos = FastMod(h,
data.size());
300 data[pos & ~1] = (
data[pos & ~1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(
nGeneration & 1)) << bit;
301 data[pos | 1] = (
data[pos | 1] & ~(((uint64_t)1) << bit)) | ((uint64_t)(
nGeneration >> 1)) << bit;
307 std::vector<unsigned char>
data(hash.
begin(), hash.
end());
314 uint32_t h = RollingBloomHash(n,
nTweak, vKey);
316 uint32_t pos = FastMod(h,
data.size());
318 if (!(((
data[pos & ~1] |
data[pos | 1]) >> bit) & 1)) {
327 std::vector<unsigned char>
data(hash.
begin(), hash.
end());
336 for (std::vector<uint64_t>::iterator it =
data.begin(); it !=
data.end(); it++) {
@ BLOOM_UPDATE_P2PUBKEY_ONLY
const_iterator end() const
const_iterator begin() const
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
bool IsWithinSizeConstraints() const
True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS (c...
unsigned int Hash(unsigned int nHashNum, const std::vector< unsigned char > &vDataToHash) const
std::vector< unsigned char > vData
bool Merge(const CBloomFilter &filter)
Copies filter into this.
void reset(const unsigned int nNewTweak)
void insert(const std::vector< unsigned char > &vKey)
bool MatchesAll() const
Returns true if this filter will match anything.
bool IsRelevantAndUpdate(const CTransaction &tx)
Also adds any outputs which match the filter to the filter (to match their spending txes)
void UpdateEmptyFull()
Checks for empty and full filters to avoid wasting cpu.
bool contains(const std::vector< unsigned char > &vKey) const
An outpoint - a combination of a transaction hash and an index n into its vout.
void insert(const std::vector< unsigned char > &vKey)
bool contains(const std::vector< unsigned char > &vKey) const
CRollingBloomFilter(const unsigned int nElements, const double nFPRate)
int nEntriesPerGeneration
int nEntriesThisGeneration
std::vector< uint64_t > data
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
The basic transaction that is broadcasted on the network and contained in blocks.
const uint256 & GetHash() const
std::vector< CTxOut > vout
An input of a transaction.
An output of a transaction.
unsigned int MurmurHash3(unsigned int nHashSeed, const std::vector< unsigned char > &vDataToHash)
uint64_t GetRand(uint64_t nMax) noexcept
opcodetype
Script opcodes.
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.