Changeset View
Changeset View
Standalone View
Standalone View
src/blockfilter.h
// Copyright (c) 2018 The Bitcoin Core developers | // Copyright (c) 2018 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_BLOCKFILTER_H | #ifndef BITCOIN_BLOCKFILTER_H | ||||
#define BITCOIN_BLOCKFILTER_H | #define BITCOIN_BLOCKFILTER_H | ||||
#include <primitives/block.h> | |||||
#include <serialize.h> | #include <serialize.h> | ||||
#include <uint256.h> | #include <uint256.h> | ||||
#include <undo.h> | |||||
#include <cstdint> | #include <cstdint> | ||||
#include <set> | #include <set> | ||||
#include <vector> | #include <vector> | ||||
/** | /** | ||||
* This implements a Golomb-coded set as defined in BIP 158. It is a | * This implements a Golomb-coded set as defined in BIP 158. It is a | ||||
* compact, probabilistic data structure for testing set membership. | * compact, probabilistic data structure for testing set membership. | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | public: | ||||
/** | /** | ||||
* Checks if any of the given elements may be in the set. False positives | * 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 | * are possible with probability 1/M per element checked. This is more | ||||
* efficient that checking Match on multiple elements separately. | * efficient that checking Match on multiple elements separately. | ||||
*/ | */ | ||||
bool MatchAny(const ElementSet &elements) const; | bool MatchAny(const ElementSet &elements) const; | ||||
}; | }; | ||||
constexpr uint8_t BASIC_FILTER_P = 19; | |||||
constexpr uint32_t BASIC_FILTER_M = 784931; | |||||
enum BlockFilterType : uint8_t { | |||||
BASIC = 0, | |||||
}; | |||||
/** | |||||
* Complete block filter struct as defined in BIP 157. Serialization matches | |||||
* payload of "cfilter" messages. | |||||
*/ | |||||
class BlockFilter { | |||||
private: | |||||
BlockFilterType m_filter_type; | |||||
uint256 m_block_hash; | |||||
GCSFilter m_filter; | |||||
public: | |||||
// Construct a new BlockFilter of the specified type from a block. | |||||
BlockFilter(BlockFilterType filter_type, const CBlock &block, | |||||
const CBlockUndo &block_undo); | |||||
BlockFilterType GetFilterType() const { return m_filter_type; } | |||||
const GCSFilter &GetFilter() const { return m_filter; } | |||||
const std::vector<uint8_t> &GetEncodedFilter() const { | |||||
return m_filter.GetEncoded(); | |||||
} | |||||
// Compute the filter hash. | |||||
uint256 GetHash() const; | |||||
// Compute the filter header given the previous one. | |||||
uint256 ComputeHeader(const uint256 &prev_header) const; | |||||
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<uint8_t> encoded_filter; | |||||
uint8_t filter_type; | |||||
s >> m_block_hash >> filter_type >> encoded_filter; | |||||
m_filter_type = static_cast<BlockFilterType>(filter_type); | |||||
switch (m_filter_type) { | |||||
case BlockFilterType::BASIC: | |||||
m_filter = GCSFilter(m_block_hash.GetUint64(0), | |||||
m_block_hash.GetUint64(1), BASIC_FILTER_P, | |||||
BASIC_FILTER_M, std::move(encoded_filter)); | |||||
break; | |||||
default: | |||||
throw std::ios_base::failure("unknown filter_type"); | |||||
} | |||||
} | |||||
}; | |||||
#endif // BITCOIN_BLOCKFILTER_H | #endif // BITCOIN_BLOCKFILTER_H |