Bitcoin Core  22.99.0
P2P Digital Currency
blockfilter_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 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 
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  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++].get_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 
ParseHashStr
bool ParseHashStr(const std::string &strHex, uint256 &result)
Parse a hex string into 256 bits.
Definition: core_read.cpp:239
BlockFilterTypeName
const std::string & BlockFilterTypeName(BlockFilterType filter_type)
Get the human-readable name for a filter type.
Definition: blockfilter.cpp:178
ParseHex
std::vector< unsigned char > ParseHex(const char *psz)
Definition: strencodings.cpp:84
BlockFilter::GetFilterType
BlockFilterType GetFilterType() const
Definition: blockfilter.h:130
BOOST_AUTO_TEST_SUITE
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
BlockFilter::GetBlockHash
const uint256 & GetBlockHash() const
Definition: blockfilter.h:131
GCSFilter::Params
Definition: blockfilter.h:30
GCSFilter::Match
bool Match(const Element &element) const
Checks if the element may be in the set.
Definition: blockfilter.cpp:166
CTxUndo
Undo information for a CTransaction.
Definition: undo.h:53
streams.h
setup_common.h
BlockFilter::GetEncodedFilter
const std::vector< unsigned char > & GetEncodedFilter() const
Definition: blockfilter.h:134
OP_ADD
@ OP_ADD
Definition: script.h:154
GCSFilter::Params::m_siphash_k0
uint64_t m_siphash_k0
Definition: blockfilter.h:32
BlockFilterTypeByName
bool BlockFilterTypeByName(const std::string &name, BlockFilterType &filter_type)
Find a filter type by its human-readable name.
Definition: blockfilter.cpp:185
OP_CHECKMULTISIG
@ OP_CHECKMULTISIG
Definition: script.h:185
BlockFilter::ComputeHeader
uint256 ComputeHeader(const uint256 &prev_header) const
Compute the filter header given the previous one.
Definition: blockfilter.cpp:298
GCSFilter::GetParams
const Params & GetParams() const
Definition: blockfilter.h:68
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
OP_8
@ OP_8
Definition: script.h:84
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(gcsfilter_test)
Definition: blockfilter_tests.cpp:19
BlockFilter::GetFilter
const GCSFilter & GetFilter() const
Definition: blockfilter.h:132
UniValue::write
std::string write(unsigned int prettyIndent=0, unsigned int indentLevel=0) const
Definition: univalue_write.cpp:29
CBlockUndo::vtxundo
std::vector< CTxUndo > vtxundo
Definition: undo.h:66
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
core_io.h
UniValue
Definition: univalue.h:19
GCSFilter::Params::m_M
uint32_t m_M
Inverse false positive rate.
Definition: blockfilter.h:35
GCSFilter
This implements a Golomb-coded set as defined in BIP 158.
Definition: blockfilter.h:24
OP_1
@ OP_1
Definition: script.h:76
strencodings.h
GCSFilter::Params::m_P
uint8_t m_P
Golomb-Rice coding parameter.
Definition: blockfilter.h:34
OP_RETURN
@ OP_RETURN
Definition: script.h:104
CTxOut
An output of a transaction.
Definition: transaction.h:128
OP_EQUAL
@ OP_EQUAL
Definition: script.h:139
univalue.h
OP_4
@ OP_4
Definition: script.h:80
uint256
256-bit opaque blob.
Definition: uint256.h:124
GCSFilter::Params::m_siphash_k1
uint64_t m_siphash_k1
Definition: blockfilter.h:33
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
GCSFilter::GetN
uint32_t GetN() const
Definition: blockfilter.h:67
BlockFilterType::BASIC
@ BASIC
CBlock
Definition: block.h:62
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
CTxUndo::vprevout
std::vector< Coin > vprevout
Definition: undo.h:57
CBlock::vtx
std::vector< CTransactionRef > vtx
Definition: block.h:66
ankerl::nanobench::templates::json
char const * json() noexcept
Template to generate JSON data.
UniValue::get_int
int get_int() const
Definition: univalue_get.cpp:104
GCSFilter::ElementSet
std::unordered_set< Element, ByteVectorHash > ElementSet
Definition: blockfilter.h:28
OP_EQUALVERIFY
@ OP_EQUALVERIFY
Definition: script.h:140
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
BlockFilterType
BlockFilterType
Definition: blockfilter.h:88
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
UniValue::size
size_t size() const
Definition: univalue.h:68
DecodeHexBlk
bool DecodeHexBlk(CBlock &, const std::string &strHexBlk)
Definition: core_read.cpp:222
GCSFilter::GetEncoded
const std::vector< unsigned char > & GetEncoded() const
Definition: blockfilter.h:69
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
UniValue::get_array
const UniValue & get_array() const
Definition: univalue_get.cpp:141
GCSFilter::Element
std::vector< unsigned char > Element
Definition: blockfilter.h:27
OP_CHECKSIG
@ OP_CHECKSIG
Definition: script.h:183
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
CBlockUndo
Undo information for a CBlock.
Definition: undo.h:63
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
BlockFilter
Complete block filter struct as defined in BIP 157.
Definition: blockfilter.h:110