Bitcoin Core 28.99.0
P2P Digital Currency
blockfilter_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <test/data/blockfilters.json.h>
7
8#include <blockfilter.h>
9#include <core_io.h>
10#include <primitives/block.h>
11#include <serialize.h>
12#include <streams.h>
13#include <undo.h>
14#include <univalue.h>
15#include <util/strencodings.h>
16
17#include <boost/test/unit_test.hpp>
18
19BOOST_AUTO_TEST_SUITE(blockfilter_tests)
20
21BOOST_AUTO_TEST_CASE(gcsfilter_test)
22{
23 GCSFilter::ElementSet included_elements, excluded_elements;
24 for (int i = 0; i < 100; ++i) {
25 GCSFilter::Element element1(32);
26 element1[0] = i;
27 included_elements.insert(std::move(element1));
28
29 GCSFilter::Element element2(32);
30 element2[1] = i;
31 excluded_elements.insert(std::move(element2));
32 }
33
34 GCSFilter filter({0, 0, 10, 1 << 10}, included_elements);
35 for (const auto& element : included_elements) {
36 BOOST_CHECK(filter.Match(element));
37
38 auto insertion = excluded_elements.insert(element);
39 BOOST_CHECK(filter.MatchAny(excluded_elements));
40 excluded_elements.erase(insertion.first);
41 }
42}
43
44BOOST_AUTO_TEST_CASE(gcsfilter_default_constructor)
45{
46 GCSFilter filter;
47 BOOST_CHECK_EQUAL(filter.GetN(), 0U);
48 BOOST_CHECK_EQUAL(filter.GetEncoded().size(), 1U);
49
50 const GCSFilter::Params& params = filter.GetParams();
53 BOOST_CHECK_EQUAL(params.m_P, 0);
54 BOOST_CHECK_EQUAL(params.m_M, 1U);
55}
56
57BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
58{
59 CScript included_scripts[5], excluded_scripts[4];
60
61 // First two are outputs on a single transaction.
62 included_scripts[0] << std::vector<unsigned char>(0, 65) << OP_CHECKSIG;
63 included_scripts[1] << OP_DUP << OP_HASH160 << std::vector<unsigned char>(1, 20) << OP_EQUALVERIFY << OP_CHECKSIG;
64
65 // Third is an output on in a second transaction.
66 included_scripts[2] << OP_1 << std::vector<unsigned char>(2, 33) << OP_1 << OP_CHECKMULTISIG;
67
68 // Last two are spent by a single transaction.
69 included_scripts[3] << OP_0 << std::vector<unsigned char>(3, 32);
70 included_scripts[4] << OP_4 << OP_ADD << OP_8 << OP_EQUAL;
71
72 // OP_RETURN output is an output on the second transaction.
73 excluded_scripts[0] << OP_RETURN << std::vector<unsigned char>(4, 40);
74
75 // This script is not related to the block at all.
76 excluded_scripts[1] << std::vector<unsigned char>(5, 33) << OP_CHECKSIG;
77
78 // OP_RETURN is non-standard since it's not followed by a data push, but is still excluded from
79 // filter.
80 excluded_scripts[2] << OP_RETURN << OP_4 << OP_ADD << OP_8 << OP_EQUAL;
81
83 tx_1.vout.emplace_back(100, included_scripts[0]);
84 tx_1.vout.emplace_back(200, included_scripts[1]);
85 tx_1.vout.emplace_back(0, excluded_scripts[0]);
86
88 tx_2.vout.emplace_back(300, included_scripts[2]);
89 tx_2.vout.emplace_back(0, excluded_scripts[2]);
90 tx_2.vout.emplace_back(400, excluded_scripts[3]); // Script is empty
91
92 CBlock block;
93 block.vtx.push_back(MakeTransactionRef(tx_1));
94 block.vtx.push_back(MakeTransactionRef(tx_2));
95
96 CBlockUndo block_undo;
97 block_undo.vtxundo.emplace_back();
98 block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(500, included_scripts[3]), 1000, true);
99 block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(600, included_scripts[4]), 10000, false);
100 block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(700, excluded_scripts[3]), 100000, false);
101
102 BlockFilter block_filter(BlockFilterType::BASIC, block, block_undo);
103 const GCSFilter& filter = block_filter.GetFilter();
104
105 for (const CScript& script : included_scripts) {
106 BOOST_CHECK(filter.Match(GCSFilter::Element(script.begin(), script.end())));
107 }
108 for (const CScript& script : excluded_scripts) {
109 BOOST_CHECK(!filter.Match(GCSFilter::Element(script.begin(), script.end())));
110 }
111
112 // Test serialization/unserialization.
113 BlockFilter block_filter2;
114
115 DataStream stream{};
116 stream << block_filter;
117 stream >> block_filter2;
118
119 BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType());
120 BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash());
121 BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter());
122
123 BlockFilter default_ctor_block_filter_1;
124 BlockFilter default_ctor_block_filter_2;
125 BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetFilterType(), default_ctor_block_filter_2.GetFilterType());
126 BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetBlockHash(), default_ctor_block_filter_2.GetBlockHash());
127 BOOST_CHECK(default_ctor_block_filter_1.GetEncodedFilter() == default_ctor_block_filter_2.GetEncodedFilter());
128}
129
130BOOST_AUTO_TEST_CASE(blockfilters_json_test)
131{
133 if (!json.read(json_tests::blockfilters) || !json.isArray()) {
134 BOOST_ERROR("Parse error.");
135 return;
136 }
137
138 const UniValue& tests = json.get_array();
139 for (unsigned int i = 0; i < tests.size(); i++) {
140 const UniValue& test = tests[i];
141 std::string strTest = test.write();
142
143 if (test.size() == 1) {
144 continue;
145 } else if (test.size() < 7) {
146 BOOST_ERROR("Bad test: " << strTest);
147 continue;
148 }
149
150 unsigned int pos = 0;
151 /*int block_height =*/ test[pos++].getInt<int>();
152 BOOST_CHECK(uint256::FromHex(test[pos++].get_str()));
153
154 CBlock block;
155 BOOST_REQUIRE(DecodeHexBlk(block, test[pos++].get_str()));
156
157 CBlockUndo block_undo;
158 block_undo.vtxundo.emplace_back();
159 CTxUndo& tx_undo = block_undo.vtxundo.back();
160 const UniValue& prev_scripts = test[pos++].get_array();
161 for (unsigned int ii = 0; ii < prev_scripts.size(); ii++) {
162 std::vector<unsigned char> raw_script = ParseHex(prev_scripts[ii].get_str());
163 CTxOut txout(0, CScript(raw_script.begin(), raw_script.end()));
164 tx_undo.vprevout.emplace_back(txout, 0, false);
165 }
166
167 uint256 prev_filter_header_basic{*Assert(uint256::FromHex(test[pos++].get_str()))};
168 std::vector<unsigned char> filter_basic = ParseHex(test[pos++].get_str());
169 uint256 filter_header_basic{*Assert(uint256::FromHex(test[pos++].get_str()))};
170
171 BlockFilter computed_filter_basic(BlockFilterType::BASIC, block, block_undo);
172 BOOST_CHECK(computed_filter_basic.GetFilter().GetEncoded() == filter_basic);
173
174 uint256 computed_header_basic = computed_filter_basic.ComputeHeader(prev_filter_header_basic);
175 BOOST_CHECK(computed_header_basic == filter_header_basic);
176 }
177}
178
179BOOST_AUTO_TEST_CASE(blockfilter_type_names)
180{
183
184 BlockFilterType filter_type;
185 BOOST_CHECK(BlockFilterTypeByName("basic", filter_type));
187
188 BOOST_CHECK(!BlockFilterTypeByName("unknown", filter_type));
189}
190
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
BlockFilterType
Definition: blockfilter.h:93
BOOST_AUTO_TEST_CASE(gcsfilter_test)
#define Assert(val)
Identity function.
Definition: check.h:85
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:115
const uint256 & GetBlockHash() const LIFETIMEBOUND
Definition: blockfilter.h:135
const std::vector< unsigned char > & GetEncodedFilter() const LIFETIMEBOUND
Definition: blockfilter.h:138
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
const GCSFilter & GetFilter() const LIFETIMEBOUND
Definition: blockfilter.h:136
BlockFilterType GetFilterType() const
Definition: blockfilter.h:134
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
Undo information for a CBlock.
Definition: undo.h:63
std::vector< CTxUndo > vtxundo
Definition: undo.h:65
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
An output of a transaction.
Definition: transaction.h:150
Undo information for a CTransaction.
Definition: undo.h:53
std::vector< Coin > vprevout
Definition: undo.h:56
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
This implements a Golomb-coded set as defined in BIP 158.
Definition: blockfilter.h:29
std::vector< unsigned char > Element
Definition: blockfilter.h:31
std::unordered_set< Element, ByteVectorHash > ElementSet
Definition: blockfilter.h:32
const Params & GetParams() const LIFETIMEBOUND
Definition: blockfilter.h:72
bool Match(const Element &element) const
Checks if the element may be in the set.
uint32_t GetN() const
Definition: blockfilter.h:71
const std::vector< unsigned char > & GetEncoded() const LIFETIMEBOUND
Definition: blockfilter.h:73
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
size_t size() const
Definition: univalue.h:71
Int getInt() const
Definition: univalue.h:138
const UniValue & get_array() const
256-bit opaque blob.
Definition: uint256.h:190
static std::optional< uint256 > FromHex(std::string_view str)
Definition: uint256.h:192
bool DecodeHexBlk(CBlock &, const std::string &strHexBlk)
Definition: core_read.cpp:220
BOOST_AUTO_TEST_SUITE_END()
char const * json() noexcept
Template to generate JSON data.
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
@ OP_CHECKMULTISIG
Definition: script.h:192
@ OP_CHECKSIG
Definition: script.h:190
@ OP_EQUAL
Definition: script.h:146
@ OP_4
Definition: script.h:87
@ OP_1
Definition: script.h:83
@ OP_ADD
Definition: script.h:161
@ OP_8
Definition: script.h:91
@ OP_RETURN
Definition: script.h:111
@ OP_EQUALVERIFY
Definition: script.h:147
std::vector< Byte > ParseHex(std::string_view hex_str)
Like TryParseHex, but returns an empty vector on invalid input.
Definition: strencodings.h:68
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
uint32_t m_M
Inverse false positive rate.
Definition: blockfilter.h:39
uint64_t m_siphash_k1
Definition: blockfilter.h:37
uint8_t m_P
Golomb-Rice coding parameter.
Definition: blockfilter.h:38
uint64_t m_siphash_k0
Definition: blockfilter.h:36