2018-01-24 01:10:08 +01:00
|
|
|
// Copyright (c) 2018 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_BLOCKFILTER_H
|
|
|
|
#define BITCOIN_BLOCKFILTER_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2018-08-21 08:35:29 +02:00
|
|
|
#include <unordered_set>
|
2018-01-24 01:10:08 +01:00
|
|
|
#include <vector>
|
|
|
|
|
2018-01-24 02:25:30 +01:00
|
|
|
#include <primitives/block.h>
|
2018-01-24 01:10:08 +01:00
|
|
|
#include <serialize.h>
|
|
|
|
#include <uint256.h>
|
2018-01-24 02:25:30 +01:00
|
|
|
#include <undo.h>
|
2018-08-21 08:35:29 +02:00
|
|
|
#include <util/bytevectorhash.h>
|
2018-01-24 01:10:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This implements a Golomb-coded set as defined in BIP 158. It is a
|
|
|
|
* compact, probabilistic data structure for testing set membership.
|
|
|
|
*/
|
|
|
|
class GCSFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef std::vector<unsigned char> Element;
|
2018-08-21 08:35:29 +02:00
|
|
|
typedef std::unordered_set<Element, ByteVectorHash> ElementSet;
|
2018-01-24 01:10:08 +01:00
|
|
|
|
2018-08-28 00:04:43 +02:00
|
|
|
struct Params
|
|
|
|
{
|
|
|
|
uint64_t m_siphash_k0;
|
|
|
|
uint64_t m_siphash_k1;
|
|
|
|
uint8_t m_P; //!< Golomb-Rice coding parameter
|
|
|
|
uint32_t m_M; //!< Inverse false positive rate
|
|
|
|
|
|
|
|
Params(uint64_t siphash_k0 = 0, uint64_t siphash_k1 = 0, uint8_t P = 0, uint32_t M = 1)
|
|
|
|
: m_siphash_k0(siphash_k0), m_siphash_k1(siphash_k1), m_P(P), m_M(M)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2018-01-24 01:10:08 +01:00
|
|
|
private:
|
2018-08-28 00:04:43 +02:00
|
|
|
Params m_params;
|
2018-01-24 01:10:08 +01:00
|
|
|
uint32_t m_N; //!< Number of elements in the filter
|
|
|
|
uint64_t m_F; //!< Range of element hashes, F = N * M
|
|
|
|
std::vector<unsigned char> m_encoded;
|
|
|
|
|
2018-01-24 01:25:21 +01:00
|
|
|
/** Hash a data element to an integer in the range [0, N * M). */
|
|
|
|
uint64_t HashToRange(const Element& element) const;
|
|
|
|
|
|
|
|
std::vector<uint64_t> BuildHashedSet(const ElementSet& elements) const;
|
|
|
|
|
2018-01-24 01:33:26 +01:00
|
|
|
/** Helper method used to implement Match and MatchAny */
|
|
|
|
bool MatchInternal(const uint64_t* sorted_element_hashes, size_t size) const;
|
|
|
|
|
2018-01-24 01:10:08 +01:00
|
|
|
public:
|
|
|
|
|
|
|
|
/** Constructs an empty filter. */
|
2018-08-28 00:04:43 +02:00
|
|
|
explicit GCSFilter(const Params& params = Params());
|
2018-01-24 01:10:08 +01:00
|
|
|
|
|
|
|
/** Reconstructs an already-created filter from an encoding. */
|
2018-08-28 00:04:43 +02:00
|
|
|
GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter);
|
2018-01-24 01:10:08 +01:00
|
|
|
|
|
|
|
/** Builds a new filter from the params and set of elements. */
|
2018-08-28 00:04:43 +02:00
|
|
|
GCSFilter(const Params& params, const ElementSet& elements);
|
2018-01-24 01:10:08 +01:00
|
|
|
|
|
|
|
uint32_t GetN() const { return m_N; }
|
2018-08-28 00:04:43 +02:00
|
|
|
const Params& GetParams() const { return m_params; }
|
2018-01-24 01:10:08 +01:00
|
|
|
const std::vector<unsigned char>& GetEncoded() const { return m_encoded; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the element may be in the set. False positives are possible
|
|
|
|
* with probability 1/M.
|
|
|
|
*/
|
|
|
|
bool Match(const Element& element) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if any of the given elements may be in the set. False positives
|
|
|
|
* are possible with probability 1/M per element checked. This is more
|
|
|
|
* efficient that checking Match on multiple elements separately.
|
|
|
|
*/
|
|
|
|
bool MatchAny(const ElementSet& elements) const;
|
|
|
|
};
|
|
|
|
|
2018-01-24 02:25:30 +01:00
|
|
|
constexpr uint8_t BASIC_FILTER_P = 19;
|
|
|
|
constexpr uint32_t BASIC_FILTER_M = 784931;
|
|
|
|
|
2019-03-04 19:52:50 +01:00
|
|
|
enum class BlockFilterType : uint8_t
|
2018-01-24 02:25:30 +01:00
|
|
|
{
|
|
|
|
BASIC = 0,
|
2019-03-04 19:52:50 +01:00
|
|
|
INVALID = 255,
|
2018-01-24 02:25:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-01-24 02:27:06 +01:00
|
|
|
* Complete block filter struct as defined in BIP 157. Serialization matches
|
|
|
|
* payload of "cfilter" messages.
|
2018-01-24 02:25:30 +01:00
|
|
|
*/
|
|
|
|
class BlockFilter
|
|
|
|
{
|
|
|
|
private:
|
2019-03-04 19:52:50 +01:00
|
|
|
BlockFilterType m_filter_type = BlockFilterType::INVALID;
|
2018-01-24 02:25:30 +01:00
|
|
|
uint256 m_block_hash;
|
|
|
|
GCSFilter m_filter;
|
|
|
|
|
2018-08-28 00:04:43 +02:00
|
|
|
bool BuildParams(GCSFilter::Params& params) const;
|
|
|
|
|
2018-01-24 02:25:30 +01:00
|
|
|
public:
|
|
|
|
|
2018-08-28 00:08:31 +02:00
|
|
|
BlockFilter() = default;
|
|
|
|
|
|
|
|
//! Reconstruct a BlockFilter from parts.
|
|
|
|
BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
|
|
|
|
std::vector<unsigned char> filter);
|
|
|
|
|
|
|
|
//! Construct a new BlockFilter of the specified type from a block.
|
2018-01-24 02:25:30 +01:00
|
|
|
BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo);
|
|
|
|
|
|
|
|
BlockFilterType GetFilterType() const { return m_filter_type; }
|
2018-08-28 00:08:31 +02:00
|
|
|
const uint256& GetBlockHash() const { return m_block_hash; }
|
2018-01-24 02:25:30 +01:00
|
|
|
const GCSFilter& GetFilter() const { return m_filter; }
|
|
|
|
|
|
|
|
const std::vector<unsigned char>& GetEncodedFilter() const
|
|
|
|
{
|
|
|
|
return m_filter.GetEncoded();
|
|
|
|
}
|
2018-01-24 02:27:06 +01:00
|
|
|
|
2018-08-28 00:08:31 +02:00
|
|
|
//! Compute the filter hash.
|
2018-01-24 02:32:46 +01:00
|
|
|
uint256 GetHash() const;
|
|
|
|
|
2018-08-28 00:08:31 +02:00
|
|
|
//! Compute the filter header given the previous one.
|
2018-01-24 02:32:46 +01:00
|
|
|
uint256 ComputeHeader(const uint256& prev_header) const;
|
|
|
|
|
2018-01-24 02:27:06 +01:00
|
|
|
template <typename Stream>
|
|
|
|
void Serialize(Stream& s) const {
|
|
|
|
s << m_block_hash
|
|
|
|
<< static_cast<uint8_t>(m_filter_type)
|
|
|
|
<< m_filter.GetEncoded();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Stream>
|
|
|
|
void Unserialize(Stream& s) {
|
|
|
|
std::vector<unsigned char> encoded_filter;
|
|
|
|
uint8_t filter_type;
|
|
|
|
|
|
|
|
s >> m_block_hash
|
|
|
|
>> filter_type
|
|
|
|
>> encoded_filter;
|
|
|
|
|
|
|
|
m_filter_type = static_cast<BlockFilterType>(filter_type);
|
|
|
|
|
2018-08-28 00:04:43 +02:00
|
|
|
GCSFilter::Params params;
|
|
|
|
if (!BuildParams(params)) {
|
2018-01-24 02:27:06 +01:00
|
|
|
throw std::ios_base::failure("unknown filter_type");
|
|
|
|
}
|
2018-08-28 00:04:43 +02:00
|
|
|
m_filter = GCSFilter(params, std::move(encoded_filter));
|
2018-01-24 02:27:06 +01:00
|
|
|
}
|
2018-01-24 02:25:30 +01:00
|
|
|
};
|
|
|
|
|
2018-01-24 01:10:08 +01:00
|
|
|
#endif // BITCOIN_BLOCKFILTER_H
|