Bitcoin Core  24.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 <serialize.h>
11 #include <streams.h>
12 #include <univalue.h>
13 #include <util/strencodings.h>
14 
15 #include <boost/test/unit_test.hpp>
16 
17 BOOST_AUTO_TEST_SUITE(blockfilter_tests)
18 
19 BOOST_AUTO_TEST_CASE(gcsfilter_test)
20 {
21  GCSFilter::ElementSet included_elements, excluded_elements;
22  for (int i = 0; i < 100; ++i) {
23  GCSFilter::Element element1(32);
24  element1[0] = i;
25  included_elements.insert(std::move(element1));
26 
27  GCSFilter::Element element2(32);
28  element2[1] = i;
29  excluded_elements.insert(std::move(element2));
30  }
31 
32  GCSFilter filter({0, 0, 10, 1 << 10}, included_elements);
33  for (const auto& element : included_elements) {
34  BOOST_CHECK(filter.Match(element));
35 
36  auto insertion = excluded_elements.insert(element);
37  BOOST_CHECK(filter.MatchAny(excluded_elements));
38  excluded_elements.erase(insertion.first);
39  }
40 }
41 
42 BOOST_AUTO_TEST_CASE(gcsfilter_default_constructor)
43 {
44  GCSFilter filter;
45  BOOST_CHECK_EQUAL(filter.GetN(), 0U);
46  BOOST_CHECK_EQUAL(filter.GetEncoded().size(), 1U);
47 
48  const GCSFilter::Params& params = filter.GetParams();
49  BOOST_CHECK_EQUAL(params.m_siphash_k0, 0U);
50  BOOST_CHECK_EQUAL(params.m_siphash_k1, 0U);
51  BOOST_CHECK_EQUAL(params.m_P, 0);
52  BOOST_CHECK_EQUAL(params.m_M, 1U);
53 }
54 
55 BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
56 {
57  CScript included_scripts[5], excluded_scripts[4];
58 
59  // First two are outputs on a single transaction.
60  included_scripts[0] << std::vector<unsigned char>(0, 65) << OP_CHECKSIG;
61  included_scripts[1] << OP_DUP << OP_HASH160 << std::vector<unsigned char>(1, 20) << OP_EQUALVERIFY << OP_CHECKSIG;
62 
63  // Third is an output on in a second transaction.
64  included_scripts[2] << OP_1 << std::vector<unsigned char>(2, 33) << OP_1 << OP_CHECKMULTISIG;
65 
66  // Last two are spent by a single transaction.
67  included_scripts[3] << OP_0 << std::vector<unsigned char>(3, 32);
68  included_scripts[4] << OP_4 << OP_ADD << OP_8 << OP_EQUAL;
69 
70  // OP_RETURN output is an output on the second transaction.
71  excluded_scripts[0] << OP_RETURN << std::vector<unsigned char>(4, 40);
72 
73  // This script is not related to the block at all.
74  excluded_scripts[1] << std::vector<unsigned char>(5, 33) << OP_CHECKSIG;
75 
76  // OP_RETURN is non-standard since it's not followed by a data push, but is still excluded from
77  // filter.
78  excluded_scripts[2] << OP_RETURN << OP_4 << OP_ADD << OP_8 << OP_EQUAL;
79 
81  tx_1.vout.emplace_back(100, included_scripts[0]);
82  tx_1.vout.emplace_back(200, included_scripts[1]);
83  tx_1.vout.emplace_back(0, excluded_scripts[0]);
84 
86  tx_2.vout.emplace_back(300, included_scripts[2]);
87  tx_2.vout.emplace_back(0, excluded_scripts[2]);
88  tx_2.vout.emplace_back(400, excluded_scripts[3]); // Script is empty
89 
90  CBlock block;
91  block.vtx.push_back(MakeTransactionRef(tx_1));
92  block.vtx.push_back(MakeTransactionRef(tx_2));
93 
94  CBlockUndo block_undo;
95  block_undo.vtxundo.emplace_back();
96  block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(500, included_scripts[3]), 1000, true);
97  block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(600, included_scripts[4]), 10000, false);
98  block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(700, excluded_scripts[3]), 100000, false);
99 
100  BlockFilter block_filter(BlockFilterType::BASIC, block, block_undo);
101  const GCSFilter& filter = block_filter.GetFilter();
102 
103  for (const CScript& script : included_scripts) {
104  BOOST_CHECK(filter.Match(GCSFilter::Element(script.begin(), script.end())));
105  }
106  for (const CScript& script : excluded_scripts) {
107  BOOST_CHECK(!filter.Match(GCSFilter::Element(script.begin(), script.end())));
108  }
109 
110  // Test serialization/unserialization.
111  BlockFilter block_filter2;
112 
113  DataStream stream{};
114  stream << block_filter;
115  stream >> block_filter2;
116 
117  BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType());
118  BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash());
119  BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter());
120 
121  BlockFilter default_ctor_block_filter_1;
122  BlockFilter default_ctor_block_filter_2;
123  BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetFilterType(), default_ctor_block_filter_2.GetFilterType());
124  BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetBlockHash(), default_ctor_block_filter_2.GetBlockHash());
125  BOOST_CHECK(default_ctor_block_filter_1.GetEncodedFilter() == default_ctor_block_filter_2.GetEncodedFilter());
126 }
127 
128 BOOST_AUTO_TEST_CASE(blockfilters_json_test)
129 {
130  UniValue json;
131  std::string json_data(json_tests::blockfilters,
132  json_tests::blockfilters + sizeof(json_tests::blockfilters));
133  if (!json.read(json_data) || !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  uint256 block_hash;
153  BOOST_CHECK(ParseHashStr(test[pos++].get_str(), block_hash));
154 
155  CBlock block;
156  BOOST_REQUIRE(DecodeHexBlk(block, test[pos++].get_str()));
157 
158  CBlockUndo block_undo;
159  block_undo.vtxundo.emplace_back();
160  CTxUndo& tx_undo = block_undo.vtxundo.back();
161  const UniValue& prev_scripts = test[pos++].get_array();
162  for (unsigned int ii = 0; ii < prev_scripts.size(); ii++) {
163  std::vector<unsigned char> raw_script = ParseHex(prev_scripts[ii].get_str());
164  CTxOut txout(0, CScript(raw_script.begin(), raw_script.end()));
165  tx_undo.vprevout.emplace_back(txout, 0, false);
166  }
167 
168  uint256 prev_filter_header_basic;
169  BOOST_CHECK(ParseHashStr(test[pos++].get_str(), prev_filter_header_basic));
170  std::vector<unsigned char> filter_basic = ParseHex(test[pos++].get_str());
171  uint256 filter_header_basic;
172  BOOST_CHECK(ParseHashStr(test[pos++].get_str(), filter_header_basic));
173 
174  BlockFilter computed_filter_basic(BlockFilterType::BASIC, block, block_undo);
175  BOOST_CHECK(computed_filter_basic.GetFilter().GetEncoded() == filter_basic);
176 
177  uint256 computed_header_basic = computed_filter_basic.ComputeHeader(prev_filter_header_basic);
178  BOOST_CHECK(computed_header_basic == filter_header_basic);
179  }
180 }
181 
182 BOOST_AUTO_TEST_CASE(blockfilter_type_names)
183 {
185  BOOST_CHECK_EQUAL(BlockFilterTypeName(static_cast<BlockFilterType>(255)), "");
186 
187  BlockFilterType filter_type;
188  BOOST_CHECK(BlockFilterTypeByName("basic", filter_type));
190 
191  BOOST_CHECK(!BlockFilterTypeByName("unknown", filter_type));
192 }
193 
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:90
BOOST_AUTO_TEST_CASE(gcsfilter_test)
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:112
const GCSFilter & GetFilter() const LIFETIMEBOUND
Definition: blockfilter.h:133
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
BlockFilterType GetFilterType() const
Definition: blockfilter.h:131
const uint256 & GetBlockHash() const LIFETIMEBOUND
Definition: blockfilter.h:132
const std::vector< unsigned char > & GetEncodedFilter() const LIFETIMEBOUND
Definition: blockfilter.h:135
Definition: block.h:69
std::vector< CTransactionRef > vtx
Definition: block.h:72
Undo information for a CBlock.
Definition: undo.h:64
std::vector< CTxUndo > vtxundo
Definition: undo.h:66
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:411
An output of a transaction.
Definition: transaction.h:158
Undo information for a CTransaction.
Definition: undo.h:54
std::vector< Coin > vprevout
Definition: undo.h:57
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:186
This implements a Golomb-coded set as defined in BIP 158.
Definition: blockfilter.h:26
std::vector< unsigned char > Element
Definition: blockfilter.h:28
std::unordered_set< Element, ByteVectorHash > ElementSet
Definition: blockfilter.h:29
const std::vector< unsigned char > & GetEncoded() const LIFETIMEBOUND
Definition: blockfilter.h:70
bool Match(const Element &element) const
Checks if the element may be in the set.
uint32_t GetN() const
Definition: blockfilter.h:68
const Params & GetParams() const LIFETIMEBOUND
Definition: blockfilter.h:69
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
size_t size() const
Definition: univalue.h:67
Int getInt() const
Definition: univalue.h:138
const UniValue & get_array() const
256-bit opaque blob.
Definition: uint256.h:105
bool DecodeHexBlk(CBlock &, const std::string &strHexBlk)
Definition: core_read.cpp:219
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
Definition: core_read.cpp:236
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
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:422
@ OP_CHECKMULTISIG
Definition: script.h:188
@ OP_CHECKSIG
Definition: script.h:186
@ OP_EQUAL
Definition: script.h:142
@ OP_4
Definition: script.h:83
@ OP_1
Definition: script.h:79
@ OP_ADD
Definition: script.h:157
@ OP_8
Definition: script.h:87
@ OP_RETURN
Definition: script.h:107
@ OP_EQUALVERIFY
Definition: script.h:143
A mutable version of CTransaction.
Definition: transaction.h:380
std::vector< CTxOut > vout
Definition: transaction.h:382
uint32_t m_M
Inverse false positive rate.
Definition: blockfilter.h:36
uint64_t m_siphash_k1
Definition: blockfilter.h:34
uint8_t m_P
Golomb-Rice coding parameter.
Definition: blockfilter.h:35
uint64_t m_siphash_k0
Definition: blockfilter.h:33
std::vector< Byte > ParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).