19 #define LN2SQUARED 0.4804530139182014246671025263266649717305529515945455
20 #define LN2 0.6931471805599453094172321214581765680755001343602552
36 const uint32_t nTweakIn, uint8_t nFlagsIn)
37 : vData(std::min<uint32_t>(-1 /
LN2SQUARED * nElements * log(nFPRate),
40 nHashFuncs(std::min<uint32_t>(vData.size() * 8 / nElements *
LN2,
42 nTweak(nTweakIn), nFlags(nFlagsIn) {}
46 const std::vector<uint8_t> &vDataToHash)
const {
60 uint32_t nIndex =
Hash(i, vKey);
62 vData[nIndex >> 3] |= (1 << (7 & nIndex));
69 std::vector<uint8_t> data(stream.
begin(), stream.
end());
74 std::vector<uint8_t> data(hash.
begin(), hash.
end());
84 uint32_t nIndex =
Hash(i, vKey);
86 if (!(
vData[nIndex >> 3] & (1 << (7 & nIndex)))) {
96 std::vector<uint8_t> data(stream.
begin(), stream.
end());
101 std::vector<uint8_t> data(hash.
begin(), hash.
end());
124 for (
size_t i = 0; i < tx.
vout.size(); i++) {
133 std::vector<uint8_t> data;
139 if (data.size() != 0 &&
contains(data)) {
145 std::vector<std::vector<uint8_t>> vSolutions;
170 std::vector<uint8_t> data;
176 if (data.size() != 0 &&
contains(data)) {
186 const double fpRate) {
187 double logFpRate = log(fpRate);
190 nHashFuncs = std::max(1, std::min<int>(round(logFpRate / log(0.5)), 50));
208 uint32_t nFilterBits =
209 uint32_t(ceil(-1.0 *
nHashFuncs * nMaxElements /
217 data.resize(((nFilterBits + 63) / 64) << 1);
222 static inline uint32_t
224 const std::vector<uint8_t> &vDataToHash) {
225 return MurmurHash3(nHashNum * 0xFBA4C795 + nTweak, vDataToHash);
232 static inline uint32_t
FastMod(uint32_t x,
size_t n) {
233 return (uint64_t(x) * uint64_t(n)) >> 32;
243 uint64_t nGenerationMask1 = 0 - uint64_t(
nGeneration & 1);
244 uint64_t nGenerationMask2 = 0 - uint64_t(
nGeneration >> 1);
246 for (uint32_t p = 0; p <
data.size(); p += 2) {
247 uint64_t p1 =
data[p], p2 =
data[p + 1];
248 uint64_t mask = (p1 ^ nGenerationMask1) | (p2 ^ nGenerationMask2);
250 data[p + 1] = p2 & mask;
263 data[pos & ~1U] = (
data[pos & ~1U] & ~(uint64_t(1) << bit)) |
265 data[pos | 1U] = (
data[pos | 1] & ~(uint64_t(1) << bit)) |
271 std::vector<uint8_t> vData(hash.
begin(), hash.
end());
282 if (!(((
data[pos & ~1] |
data[pos | 1]) >> bit) & 1)) {
290 std::vector<uint8_t> vData(hash.
begin(), hash.
end());
298 std::fill(
data.begin(),
data.end(), 0);
static uint32_t FastMod(uint32_t x, size_t n)
static uint32_t RollingBloomHash(uint32_t nHashNum, uint32_t nTweak, const std::vector< uint8_t > &vDataToHash)
static const uint32_t MAX_BLOOM_FILTER_SIZE
20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
static const uint32_t MAX_HASH_FUNCS
@ BLOOM_UPDATE_P2PUBKEY_ONLY
bool IsWithinSizeConstraints() const
True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS (c...
uint32_t Hash(uint32_t nHashNum, const std::vector< uint8_t > &vDataToHash) const
std::vector< uint8_t > vData
bool MatchInputs(const CTransaction &tx)
Scan inputs to see if the spent outpoints are a match, or the input scripts contain matching elements...
bool MatchAndInsertOutputs(const CTransaction &tx)
Scans output scripts for matches and adds those outpoints to the filter for spend detection.
void insert(const std::vector< uint8_t > &vKey)
bool contains(const std::vector< uint8_t > &vKey) const
Double ended buffer combining vector and stream-like interfaces.
const_iterator begin() const
const_iterator end() const
An outpoint - a combination of a transaction hash and an index n into its vout.
bool contains(const std::vector< uint8_t > &vKey) const
CRollingBloomFilter(const uint32_t nElements, const double nFPRate)
int nEntriesPerGeneration
int nEntriesThisGeneration
std::vector< uint64_t > data
void insert(const std::vector< uint8_t > &vKey)
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
const std::vector< CTxIn > vin
An input of a transaction.
An output of a transaction.
uint32_t MurmurHash3(uint32_t nHashSeed, Span< const uint8_t > vDataToHash)
uint64_t GetRand(uint64_t nMax) noexcept
Generate a uniform random integer in the range [0..range).
opcodetype
Script opcodes.
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< uint8_t >> &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
A TxId is the identifier of a transaction.
static const int PROTOCOL_VERSION
network protocol versioning