30 .
Write(element.data(), element.size())
37 std::vector<uint64_t> hashed_elements;
38 hashed_elements.reserve(
elements.size());
42 std::sort(hashed_elements.begin(), hashed_elements.end());
43 return hashed_elements;
47 :
m_params(params), m_N(0), m_F(0), m_encoded{0} {}
50 :
m_params(params), m_encoded(
std::move(encoded_filter)) {
54 m_N =
static_cast<uint32_t
>(N);
56 throw std::ios_base::failure(
"N must be <2^32");
64 for (uint64_t i = 0; i <
m_N; ++i) {
67 if (!stream.empty()) {
68 throw std::ios_base::failure(
"encoded_filter contains excess data");
75 m_N =
static_cast<uint32_t
>(N);
77 throw std::invalid_argument(
"N must be <2^32");
91 uint64_t last_value = 0;
93 uint64_t delta = value - last_value;
112 size_t hashes_index = 0;
113 for (uint32_t i = 0; i <
m_N; ++i) {
118 if (hashes_index == size) {
120 }
else if (element_hashes[hashes_index] == value) {
122 }
else if (element_hashes[hashes_index] > value) {
144 static std::string unknown_retval =
"";
152 if (entry.second ==
name) {
153 filter_type = entry.first;
161 static std::set<BlockFilterType> types;
163 static std::once_flag flag;
164 std::call_once(flag, []() {
166 types.insert(entry.first);
174 static std::string type_list;
176 static std::once_flag flag;
177 std::call_once(flag, []() {
178 std::stringstream ret;
187 type_list = ret.str();
198 for (
const CTxOut &txout : tx->vout) {
200 if (script.empty() || script[0] ==
OP_RETURN) {
203 elements.emplace(script.begin(), script.end());
210 if (script.empty()) {
213 elements.emplace(script.begin(), script.end());
222 std::vector<uint8_t> filter)
223 : m_filter_type(filter_type), m_block_hash(block_hash) {
226 throw std::invalid_argument(
"unknown filter_type");
233 : m_filter_type(filter_type), m_block_hash(block.GetHash()) {
236 throw std::invalid_argument(
"unknown filter_type");
static const std::map< BlockFilterType, std::string > g_filter_types
static constexpr int GCS_SER_VERSION
Protocol version used to serialize parameters in GCS filter encoding.
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.
static constexpr int GCS_SER_TYPE
SerType used to serialize parameters in GCS filter encoding.
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
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
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
Minimal stream for overwriting and/or appending to an existing byte vector.
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.
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
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
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.