2020-12-31 18:50:11 +01:00
|
|
|
// Copyright (c) 2018-2020 The Bitcoin Core developers
|
2018-08-26 16:57:01 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2021-08-12 09:04:28 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <sstream>
|
2019-12-06 21:47:55 +01:00
|
|
|
#include <set>
|
2021-08-12 09:04:28 +02:00
|
|
|
|
2018-08-26 16:57:01 +02:00
|
|
|
#include <blockfilter.h>
|
2021-06-26 12:37:06 +02:00
|
|
|
#include <crypto/siphash.h>
|
2018-08-26 16:57:01 +02:00
|
|
|
#include <hash.h>
|
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <script/script.h>
|
|
|
|
#include <streams.h>
|
2022-06-14 08:30:28 +02:00
|
|
|
#include <util/golombrice.h>
|
2018-08-26 16:57:01 +02:00
|
|
|
|
|
|
|
/// SerType used to serialize parameters in GCS filter encoding.
|
|
|
|
static constexpr int GCS_SER_TYPE = SER_NETWORK;
|
|
|
|
|
|
|
|
/// Protocol version used to serialize parameters in GCS filter encoding.
|
|
|
|
static constexpr int GCS_SER_VERSION = 0;
|
|
|
|
|
2021-08-12 09:04:28 +02:00
|
|
|
static const std::map<BlockFilterType, std::string> g_filter_types = {
|
|
|
|
{BlockFilterType::BASIC_FILTER, "basic"},
|
|
|
|
};
|
|
|
|
|
2018-08-26 16:57:01 +02:00
|
|
|
uint64_t GCSFilter::HashToRange(const Element& element) const
|
|
|
|
{
|
2021-08-12 09:14:02 +02:00
|
|
|
uint64_t hash = CSipHasher(m_params.m_siphash_k0, m_params.m_siphash_k1)
|
2018-08-26 16:57:01 +02:00
|
|
|
.Write(element.data(), element.size())
|
|
|
|
.Finalize();
|
2022-01-06 17:27:06 +01:00
|
|
|
return FastRange64(hash, m_F);
|
2018-08-26 16:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint64_t> GCSFilter::BuildHashedSet(const ElementSet& elements) const
|
|
|
|
{
|
|
|
|
std::vector<uint64_t> hashed_elements;
|
|
|
|
hashed_elements.reserve(elements.size());
|
|
|
|
for (const Element& element : elements) {
|
|
|
|
hashed_elements.push_back(HashToRange(element));
|
|
|
|
}
|
|
|
|
std::sort(hashed_elements.begin(), hashed_elements.end());
|
|
|
|
return hashed_elements;
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:14:02 +02:00
|
|
|
GCSFilter::GCSFilter(const Params& params)
|
|
|
|
: m_params(params), m_N(0), m_F(0), m_encoded{0}
|
2018-08-26 16:57:01 +02:00
|
|
|
{}
|
|
|
|
|
2024-10-13 18:16:07 +02:00
|
|
|
GCSFilter::GCSFilter(const Params& params, std::vector<unsigned char> encoded_filter, bool skip_decode_check)
|
2021-08-12 09:14:02 +02:00
|
|
|
: m_params(params), m_encoded(std::move(encoded_filter))
|
2018-08-26 16:57:01 +02:00
|
|
|
{
|
2021-12-01 20:40:25 +01:00
|
|
|
SpanReader stream{GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0};
|
2018-08-26 16:57:01 +02:00
|
|
|
|
|
|
|
uint64_t N = ReadCompactSize(stream);
|
|
|
|
m_N = static_cast<uint32_t>(N);
|
|
|
|
if (m_N != N) {
|
|
|
|
throw std::ios_base::failure("N must be <2^32");
|
|
|
|
}
|
2021-08-12 09:14:02 +02:00
|
|
|
m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
|
2018-08-26 16:57:01 +02:00
|
|
|
|
2024-10-13 18:16:07 +02:00
|
|
|
if (skip_decode_check) return;
|
|
|
|
|
2018-08-26 16:57:01 +02:00
|
|
|
// Verify that the encoded filter contains exactly N elements. If it has too much or too little
|
|
|
|
// data, a std::ios_base::failure exception will be raised.
|
2021-12-01 20:40:25 +01:00
|
|
|
BitStreamReader<SpanReader> bitreader{stream};
|
2018-08-26 16:57:01 +02:00
|
|
|
for (uint64_t i = 0; i < m_N; ++i) {
|
2021-08-12 09:14:02 +02:00
|
|
|
GolombRiceDecode(bitreader, m_params.m_P);
|
2018-08-26 16:57:01 +02:00
|
|
|
}
|
|
|
|
if (!stream.empty()) {
|
|
|
|
throw std::ios_base::failure("encoded_filter contains excess data");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:14:02 +02:00
|
|
|
GCSFilter::GCSFilter(const Params& params, const ElementSet& elements)
|
|
|
|
: m_params(params)
|
2018-08-26 16:57:01 +02:00
|
|
|
{
|
|
|
|
size_t N = elements.size();
|
|
|
|
m_N = static_cast<uint32_t>(N);
|
|
|
|
if (m_N != N) {
|
|
|
|
throw std::invalid_argument("N must be <2^32");
|
|
|
|
}
|
2021-08-12 09:14:02 +02:00
|
|
|
m_F = static_cast<uint64_t>(m_N) * static_cast<uint64_t>(m_params.m_M);
|
2018-08-26 16:57:01 +02:00
|
|
|
|
|
|
|
CVectorWriter stream(GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0);
|
|
|
|
|
|
|
|
WriteCompactSize(stream, m_N);
|
|
|
|
|
|
|
|
if (elements.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
BitStreamWriter<CVectorWriter> bitwriter(stream);
|
|
|
|
|
|
|
|
uint64_t last_value = 0;
|
|
|
|
for (uint64_t value : BuildHashedSet(elements)) {
|
|
|
|
uint64_t delta = value - last_value;
|
2021-08-12 09:14:02 +02:00
|
|
|
GolombRiceEncode(bitwriter, m_params.m_P, delta);
|
2018-08-26 16:57:01 +02:00
|
|
|
last_value = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bitwriter.Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCSFilter::MatchInternal(const uint64_t* element_hashes, size_t size) const
|
|
|
|
{
|
2021-12-01 20:40:25 +01:00
|
|
|
SpanReader stream{GCS_SER_TYPE, GCS_SER_VERSION, m_encoded, 0};
|
2018-08-26 16:57:01 +02:00
|
|
|
|
|
|
|
// Seek forward by size of N
|
|
|
|
uint64_t N = ReadCompactSize(stream);
|
|
|
|
assert(N == m_N);
|
|
|
|
|
2021-12-01 20:40:25 +01:00
|
|
|
BitStreamReader<SpanReader> bitreader{stream};
|
2018-08-26 16:57:01 +02:00
|
|
|
|
|
|
|
uint64_t value = 0;
|
|
|
|
size_t hashes_index = 0;
|
|
|
|
for (uint32_t i = 0; i < m_N; ++i) {
|
2021-08-12 09:14:02 +02:00
|
|
|
uint64_t delta = GolombRiceDecode(bitreader, m_params.m_P);
|
2018-08-26 16:57:01 +02:00
|
|
|
value += delta;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (hashes_index == size) {
|
|
|
|
return false;
|
|
|
|
} else if (element_hashes[hashes_index] == value) {
|
|
|
|
return true;
|
|
|
|
} else if (element_hashes[hashes_index] > value) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
hashes_index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCSFilter::Match(const Element& element) const
|
|
|
|
{
|
|
|
|
uint64_t query = HashToRange(element);
|
|
|
|
return MatchInternal(&query, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GCSFilter::MatchAny(const ElementSet& elements) const
|
|
|
|
{
|
|
|
|
const std::vector<uint64_t> queries = BuildHashedSet(elements);
|
|
|
|
return MatchInternal(queries.data(), queries.size());
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:04:28 +02:00
|
|
|
const std::string& BlockFilterTypeName(BlockFilterType filter_type)
|
|
|
|
{
|
|
|
|
static std::string unknown_retval = "";
|
|
|
|
auto it = g_filter_types.find(filter_type);
|
|
|
|
return it != g_filter_types.end() ? it->second : unknown_retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockFilterTypeByName(const std::string& name, BlockFilterType& filter_type) {
|
|
|
|
for (const auto& entry : g_filter_types) {
|
|
|
|
if (entry.second == name) {
|
|
|
|
filter_type = entry.first;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-12-06 21:47:55 +01:00
|
|
|
const std::set<BlockFilterType>& AllBlockFilterTypes()
|
2021-08-12 09:04:28 +02:00
|
|
|
{
|
2019-12-06 21:47:55 +01:00
|
|
|
static std::set<BlockFilterType> types;
|
2021-08-12 09:04:28 +02:00
|
|
|
|
|
|
|
static std::once_flag flag;
|
|
|
|
std::call_once(flag, []() {
|
|
|
|
for (auto entry : g_filter_types) {
|
2019-12-06 21:47:55 +01:00
|
|
|
types.insert(entry.first);
|
2021-08-12 09:04:28 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return types;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& ListBlockFilterTypes()
|
|
|
|
{
|
|
|
|
static std::string type_list;
|
|
|
|
|
|
|
|
static std::once_flag flag;
|
|
|
|
std::call_once(flag, []() {
|
|
|
|
std::stringstream ret;
|
|
|
|
bool first = true;
|
|
|
|
for (auto entry : g_filter_types) {
|
|
|
|
if (!first) ret << ", ";
|
|
|
|
ret << entry.second;
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
type_list = ret.str();
|
|
|
|
});
|
|
|
|
|
|
|
|
return type_list;
|
|
|
|
}
|
|
|
|
|
2018-08-26 16:57:01 +02:00
|
|
|
static GCSFilter::ElementSet BasicFilterElements(const CBlock& block,
|
|
|
|
const CBlockUndo& block_undo)
|
|
|
|
{
|
|
|
|
GCSFilter::ElementSet elements;
|
|
|
|
|
|
|
|
for (const CTransactionRef& tx : block.vtx) {
|
|
|
|
for (const CTxOut& txout : tx->vout) {
|
|
|
|
const CScript& script = txout.scriptPubKey;
|
2018-08-31 13:20:19 +02:00
|
|
|
if (script.empty() || script[0] == OP_RETURN) continue;
|
2018-08-26 16:57:01 +02:00
|
|
|
elements.emplace(script.begin(), script.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const CTxUndo& tx_undo : block_undo.vtxundo) {
|
|
|
|
for (const Coin& prevout : tx_undo.vprevout) {
|
|
|
|
const CScript& script = prevout.out.scriptPubKey;
|
2018-08-31 13:20:19 +02:00
|
|
|
if (script.empty()) continue;
|
2018-08-26 16:57:01 +02:00
|
|
|
elements.emplace(script.begin(), script.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return elements;
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:14:02 +02:00
|
|
|
BlockFilter::BlockFilter(BlockFilterType filter_type, const uint256& block_hash,
|
2024-10-13 18:16:07 +02:00
|
|
|
std::vector<unsigned char> filter, bool skip_decode_check)
|
2021-08-12 09:14:02 +02:00
|
|
|
: m_filter_type(filter_type), m_block_hash(block_hash)
|
|
|
|
{
|
|
|
|
GCSFilter::Params params;
|
|
|
|
if (!BuildParams(params)) {
|
|
|
|
throw std::invalid_argument("unknown filter_type");
|
|
|
|
}
|
2024-10-13 18:16:07 +02:00
|
|
|
m_filter = GCSFilter(params, std::move(filter), skip_decode_check);
|
2021-08-12 09:14:02 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 16:57:01 +02:00
|
|
|
BlockFilter::BlockFilter(BlockFilterType filter_type, const CBlock& block, const CBlockUndo& block_undo)
|
|
|
|
: m_filter_type(filter_type), m_block_hash(block.GetHash())
|
2021-08-12 09:14:02 +02:00
|
|
|
{
|
|
|
|
GCSFilter::Params params;
|
|
|
|
if (!BuildParams(params)) {
|
|
|
|
throw std::invalid_argument("unknown filter_type");
|
|
|
|
}
|
|
|
|
m_filter = GCSFilter(params, BasicFilterElements(block, block_undo));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlockFilter::BuildParams(GCSFilter::Params& params) const
|
2018-08-26 16:57:01 +02:00
|
|
|
{
|
|
|
|
switch (m_filter_type) {
|
2020-04-16 08:01:12 +02:00
|
|
|
case BlockFilterType::BASIC_FILTER:
|
2021-08-12 09:14:02 +02:00
|
|
|
params.m_siphash_k0 = m_block_hash.GetUint64(0);
|
|
|
|
params.m_siphash_k1 = m_block_hash.GetUint64(1);
|
|
|
|
params.m_P = BASIC_FILTER_P;
|
|
|
|
params.m_M = BASIC_FILTER_M;
|
|
|
|
return true;
|
2019-03-08 15:26:15 +01:00
|
|
|
case BlockFilterType::INVALID:
|
|
|
|
return false;
|
2018-08-26 16:57:01 +02:00
|
|
|
}
|
2021-08-12 09:14:02 +02:00
|
|
|
|
|
|
|
return false;
|
2018-08-26 16:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint256 BlockFilter::GetHash() const
|
|
|
|
{
|
2023-02-01 15:56:23 +01:00
|
|
|
return Hash(GetEncodedFilter());
|
2018-08-26 16:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint256 BlockFilter::ComputeHeader(const uint256& prev_header) const
|
|
|
|
{
|
2023-02-01 15:56:23 +01:00
|
|
|
return Hash(GetHash(), prev_header);
|
2018-08-26 16:57:01 +02:00
|
|
|
}
|