Bitcoin ABC 0.30.5
P2P Digital Currency
bloom.h
Go to the documentation of this file.
1// Copyright (c) 2012-2016 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_COMMON_BLOOM_H
6#define BITCOIN_COMMON_BLOOM_H
7
8#include <serialize.h>
9
10#include <cstdint>
11#include <vector>
12
13class COutPoint;
14class CTransaction;
15
17static const uint32_t MAX_BLOOM_FILTER_SIZE = 36000; // bytes
18static const uint32_t MAX_HASH_FUNCS = 50;
19
27 // Only adds outpoints to the filter if the output is a
28 // pay-to-pubkey/pay-to-multisig script
31};
32
45private:
46 std::vector<uint8_t> vData;
47 uint32_t nHashFuncs;
48 uint32_t nTweak;
49 uint8_t nFlags;
50
51 uint32_t Hash(uint32_t nHashNum, Span<const uint8_t> vDataToHash) const;
52
53public:
65 CBloomFilter(const uint32_t nElements, const double nFPRate,
66 const uint32_t nTweak, uint8_t nFlagsIn);
68
70 READWRITE(obj.vData, obj.nHashFuncs, obj.nTweak, obj.nFlags);
71 }
72
73 void insert(Span<const uint8_t> vKey);
74 void insert(const COutPoint &outpoint);
75
76 bool contains(Span<const uint8_t> vKey) const;
77 bool contains(const COutPoint &outpoint) const;
78
82 bool IsWithinSizeConstraints() const;
83
87 bool MatchAndInsertOutputs(const CTransaction &tx);
88
91 bool MatchInputs(const CTransaction &tx);
92
97 return MatchAndInsertOutputs(tx) || MatchInputs(tx);
98 }
99};
100
116public:
117 CRollingBloomFilter(const uint32_t nElements, const double nFPRate);
118
119 void insert(Span<const uint8_t> vKey);
120 bool contains(Span<const uint8_t> vKey) const;
121
122 void reset();
123
124private:
128 std::vector<uint64_t> data;
129 uint32_t nTweak;
131};
132
133#endif // BITCOIN_COMMON_BLOOM_H
static const uint32_t MAX_BLOOM_FILTER_SIZE
20,000 items with fp rate < 0.1% or 10,000 items and <0.0001%
Definition: bloom.h:17
static const uint32_t MAX_HASH_FUNCS
Definition: bloom.h:18
bloomflags
First two bits of nFlags control how much IsRelevantAndUpdate actually updates.
Definition: bloom.h:24
@ BLOOM_UPDATE_NONE
Definition: bloom.h:25
@ BLOOM_UPDATE_P2PUBKEY_ONLY
Definition: bloom.h:29
@ BLOOM_UPDATE_ALL
Definition: bloom.h:26
@ BLOOM_UPDATE_MASK
Definition: bloom.h:30
BloomFilter is a probabilistic filter which SPV clients provide so that we can filter the transaction...
Definition: bloom.h:44
bool IsWithinSizeConstraints() const
True if the size is <= MAX_BLOOM_FILTER_SIZE and the number of hash functions is <= MAX_HASH_FUNCS (c...
Definition: bloom.cpp:93
bool contains(Span< const uint8_t > vKey) const
Definition: bloom.cpp:72
uint8_t nFlags
Definition: bloom.h:49
void insert(Span< const uint8_t > vKey)
Definition: bloom.cpp:53
uint32_t nHashFuncs
Definition: bloom.h:47
std::vector< uint8_t > vData
Definition: bloom.h:46
bool MatchInputs(const CTransaction &tx)
Scan inputs to see if the spent outpoints are a match, or the input scripts contain matching elements...
Definition: bloom.cpp:148
bool MatchAndInsertOutputs(const CTransaction &tx)
Scans output scripts for matches and adds those outpoints to the filter for spend detection.
Definition: bloom.cpp:98
CBloomFilter()
Definition: bloom.h:67
uint32_t Hash(uint32_t nHashNum, Span< const uint8_t > vDataToHash) const
Definition: bloom.cpp:45
uint32_t nTweak
Definition: bloom.h:48
bool IsRelevantAndUpdate(const CTransaction &tx)
Check if the transaction is relevant for any reason.
Definition: bloom.h:96
SERIALIZE_METHODS(CBloomFilter, obj)
Definition: bloom.h:69
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
RollingBloomFilter is a probabilistic "keep track of most recently inserted" set.
Definition: bloom.h:115
CRollingBloomFilter(const uint32_t nElements, const double nFPRate)
Definition: bloom.cpp:173
int nEntriesPerGeneration
Definition: bloom.h:125
int nEntriesThisGeneration
Definition: bloom.h:126
std::vector< uint64_t > data
Definition: bloom.h:128
uint32_t nTweak
Definition: bloom.h:129
void insert(Span< const uint8_t > vKey)
Definition: bloom.cpp:215
bool contains(Span< const uint8_t > vKey) const
Definition: bloom.cpp:249
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
#define READWRITE(...)
Definition: serialize.h:166