31 std::vector<uint64_t> hashed_elements;
32 hashed_elements.reserve(
elements.size());
36 std::sort(hashed_elements.begin(), hashed_elements.end());
37 return hashed_elements;
41 :
m_params(params), m_N(0), m_F(0), m_encoded{0} {}
44 :
m_params(params), m_encoded(
std::move(encoded_filter)) {
48 m_N =
static_cast<uint32_t
>(N);
50 throw std::ios_base::failure(
"N must be <2^32");
58 for (uint64_t i = 0; i <
m_N; ++i) {
61 if (!stream.empty()) {
62 throw std::ios_base::failure(
"encoded_filter contains excess data");
69 m_N =
static_cast<uint32_t
>(N);
71 throw std::invalid_argument(
"N must be <2^32");
85 uint64_t last_value = 0;
87 uint64_t delta = value - last_value;
106 size_t hashes_index = 0;
107 for (uint32_t i = 0; i <
m_N; ++i) {
112 if (hashes_index == size) {
114 }
else if (element_hashes[hashes_index] == value) {
116 }
else if (element_hashes[hashes_index] > value) {
138 static std::string unknown_retval =
"";
146 if (entry.second ==
name) {
147 filter_type = entry.first;
155 static std::set<BlockFilterType> types;
157 static std::once_flag flag;
158 std::call_once(flag, []() {
160 types.insert(entry.first);
168 static std::string type_list;
170 static std::once_flag flag;
171 std::call_once(flag, []() {
172 std::stringstream ret;
181 type_list = ret.str();
192 for (
const CTxOut &txout : tx->vout) {
194 if (script.empty() || script[0] ==
OP_RETURN) {
197 elements.emplace(script.begin(), script.end());
204 if (script.empty()) {
207 elements.emplace(script.begin(), script.end());
216 std::vector<uint8_t> filter)
217 : m_filter_type(filter_type), m_block_hash(block_hash) {
220 throw std::invalid_argument(
"unknown filter_type");
227 : m_filter_type(filter_type), m_block_hash(block.GetHash()) {
230 throw std::invalid_argument(
"unknown filter_type");
static const std::map< BlockFilterType, std::string > g_filter_types
static GCSFilter::ElementSet BasicFilterElements(const CBlock &block, const CBlockUndo &block_undo)
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
const std::set< BlockFilterType > & AllBlockFilterTypes()
Get a list of known filter types.
const std::string & ListBlockFilterTypes()
Get a comma-separated list of known filter type names.
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
constexpr uint8_t BASIC_FILTER_P
constexpr uint32_t BASIC_FILTER_M
bool BuildParams(GCSFilter::Params ¶ms) const
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
BlockFilterType m_filter_type
uint256 GetHash() const
Compute the filter hash.
const std::vector< uint8_t > & GetEncodedFilter() const
std::vector< CTransactionRef > vtx
Undo information for a CBlock.
std::vector< CTxUndo > vtxundo
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
CHash256 & Write(Span< const uint8_t > input)
void Finalize(Span< uint8_t > output)
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data.
An output of a transaction.
Restore the UTXO in a Coin at a given COutPoint.
std::vector< Coin > vprevout
This implements a Golomb-coded set as defined in BIP 158.
uint64_t m_F
Range of element hashes, F = N * M.
bool MatchInternal(const uint64_t *sorted_element_hashes, size_t size) const
Helper method used to implement Match and MatchAny.
std::unordered_set< Element, ByteVectorHash > ElementSet
uint64_t HashToRange(const Element &element) const
Hash a data element to an integer in the range [0, N * M).
std::vector< uint8_t > Element
uint32_t m_N
Number of elements in the filter.
bool Match(const Element &element) const
Checks if the element may be in the set.
GCSFilter(const Params ¶ms=Params())
Constructs an empty filter.
bool MatchAny(const ElementSet &elements) const
Checks if any of the given elements may be in the set.
std::vector< uint64_t > BuildHashedSet(const ElementSet &elements) const
std::vector< uint8_t > m_encoded
Minimal stream for reading from an existing byte array by Span.
Minimal stream for overwriting and/or appending to an existing byte vector.
uint64_t GetUint64(int pos) const
static uint64_t FastRange64(uint64_t x, uint64_t n)
Fast range reduction with 64-bit input and 64-bit range.
static uint64_t GolombRiceDecode(BitStreamReader< IStream > &bitreader, uint8_t P)
static void GolombRiceEncode(BitStreamWriter< OStream > &bitwriter, uint8_t P, uint64_t x)
static unsigned char elements[DATACOUNT][DATALEN]
Implement std::hash so RCUPtr can be used as a key for maps or sets.
const CChainParams & m_params
std::shared_ptr< const CTransaction > CTransactionRef
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
A BlockHash is a unqiue identifier for a block.
uint32_t m_M
Inverse false positive rate.
uint8_t m_P
Golomb-Rice coding parameter.