forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockfilter_tests.cpp
194 lines (154 loc) · 7.35 KB
/
blockfilter_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2018-2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/data/blockfilters.json.h>
#include <test/util/setup_common.h>
#include <blockfilter.h>
#include <core_io.h>
#include <serialize.h>
#include <streams.h>
#include <univalue.h>
#include <util/strencodings.h>
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(blockfilter_tests)
BOOST_AUTO_TEST_CASE(gcsfilter_test)
{
GCSFilter::ElementSet included_elements, excluded_elements;
for (int i = 0; i < 100; ++i) {
GCSFilter::Element element1(32);
element1[0] = i;
included_elements.insert(std::move(element1));
GCSFilter::Element element2(32);
element2[1] = i;
excluded_elements.insert(std::move(element2));
}
GCSFilter filter({0, 0, 10, 1 << 10}, included_elements);
for (const auto& element : included_elements) {
BOOST_CHECK(filter.Match(element));
auto insertion = excluded_elements.insert(element);
BOOST_CHECK(filter.MatchAny(excluded_elements));
excluded_elements.erase(insertion.first);
}
}
BOOST_AUTO_TEST_CASE(gcsfilter_default_constructor)
{
GCSFilter filter;
BOOST_CHECK_EQUAL(filter.GetN(), 0U);
BOOST_CHECK_EQUAL(filter.GetEncoded().size(), 1U);
const GCSFilter::Params& params = filter.GetParams();
BOOST_CHECK_EQUAL(params.m_siphash_k0, 0U);
BOOST_CHECK_EQUAL(params.m_siphash_k1, 0U);
BOOST_CHECK_EQUAL(params.m_P, 0);
BOOST_CHECK_EQUAL(params.m_M, 1U);
}
BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
{
CScript included_scripts[5], excluded_scripts[4];
// First two are outputs on a single transaction.
included_scripts[0] << std::vector<unsigned char>(0, 65) << OP_CHECKSIG;
included_scripts[1] << OP_DUP << OP_HASH160 << std::vector<unsigned char>(1, 20) << OP_EQUALVERIFY << OP_CHECKSIG;
// Third is an output on in a second transaction.
included_scripts[2] << OP_1 << std::vector<unsigned char>(2, 33) << OP_1 << OP_CHECKMULTISIG;
// Last two are spent by a single transaction.
included_scripts[3] << OP_0 << std::vector<unsigned char>(3, 32);
included_scripts[4] << OP_4 << OP_ADD << OP_8 << OP_EQUAL;
// OP_RETURN output is an output on the second transaction.
excluded_scripts[0] << OP_RETURN << std::vector<unsigned char>(4, 40);
// This script is not related to the block at all.
excluded_scripts[1] << std::vector<unsigned char>(5, 33) << OP_CHECKSIG;
// OP_RETURN is non-standard since it's not followed by a data push, but is still excluded from
// filter.
excluded_scripts[2] << OP_RETURN << OP_4 << OP_ADD << OP_8 << OP_EQUAL;
CMutableTransaction tx_1;
tx_1.vout.emplace_back(100, included_scripts[0]);
tx_1.vout.emplace_back(200, included_scripts[1]);
tx_1.vout.emplace_back(0, excluded_scripts[0]);
CMutableTransaction tx_2;
tx_2.vout.emplace_back(300, included_scripts[2]);
tx_2.vout.emplace_back(0, excluded_scripts[2]);
tx_2.vout.emplace_back(400, excluded_scripts[3]); // Script is empty
CBlock block;
block.vtx.push_back(MakeTransactionRef(tx_1));
block.vtx.push_back(MakeTransactionRef(tx_2));
CBlockUndo block_undo;
block_undo.vtxundo.emplace_back();
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(500, included_scripts[3]), 1000, true);
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(600, included_scripts[4]), 10000, false);
block_undo.vtxundo.back().vprevout.emplace_back(CTxOut(700, excluded_scripts[3]), 100000, false);
BlockFilter block_filter(BlockFilterType::BASIC, block, block_undo);
const GCSFilter& filter = block_filter.GetFilter();
for (const CScript& script : included_scripts) {
BOOST_CHECK(filter.Match(GCSFilter::Element(script.begin(), script.end())));
}
for (const CScript& script : excluded_scripts) {
BOOST_CHECK(!filter.Match(GCSFilter::Element(script.begin(), script.end())));
}
// Test serialization/unserialization.
BlockFilter block_filter2;
DataStream stream{};
stream << block_filter;
stream >> block_filter2;
BOOST_CHECK_EQUAL(block_filter.GetFilterType(), block_filter2.GetFilterType());
BOOST_CHECK_EQUAL(block_filter.GetBlockHash(), block_filter2.GetBlockHash());
BOOST_CHECK(block_filter.GetEncodedFilter() == block_filter2.GetEncodedFilter());
BlockFilter default_ctor_block_filter_1;
BlockFilter default_ctor_block_filter_2;
BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetFilterType(), default_ctor_block_filter_2.GetFilterType());
BOOST_CHECK_EQUAL(default_ctor_block_filter_1.GetBlockHash(), default_ctor_block_filter_2.GetBlockHash());
BOOST_CHECK(default_ctor_block_filter_1.GetEncodedFilter() == default_ctor_block_filter_2.GetEncodedFilter());
}
BOOST_AUTO_TEST_CASE(blockfilters_json_test)
{
UniValue json;
std::string json_data(json_tests::blockfilters,
json_tests::blockfilters + sizeof(json_tests::blockfilters));
if (!json.read(json_data) || !json.isArray()) {
BOOST_ERROR("Parse error.");
return;
}
const UniValue& tests = json.get_array();
for (unsigned int i = 0; i < tests.size(); i++) {
const UniValue& test = tests[i];
std::string strTest = test.write();
if (test.size() == 1) {
continue;
} else if (test.size() < 7) {
BOOST_ERROR("Bad test: " << strTest);
continue;
}
unsigned int pos = 0;
/*int block_height =*/ test[pos++].getInt<int>();
uint256 block_hash;
BOOST_CHECK(ParseHashStr(test[pos++].get_str(), block_hash));
CBlock block;
BOOST_REQUIRE(DecodeHexBlk(block, test[pos++].get_str()));
CBlockUndo block_undo;
block_undo.vtxundo.emplace_back();
CTxUndo& tx_undo = block_undo.vtxundo.back();
const UniValue& prev_scripts = test[pos++].get_array();
for (unsigned int ii = 0; ii < prev_scripts.size(); ii++) {
std::vector<unsigned char> raw_script = ParseHex(prev_scripts[ii].get_str());
CTxOut txout(0, CScript(raw_script.begin(), raw_script.end()));
tx_undo.vprevout.emplace_back(txout, 0, false);
}
uint256 prev_filter_header_basic;
BOOST_CHECK(ParseHashStr(test[pos++].get_str(), prev_filter_header_basic));
std::vector<unsigned char> filter_basic = ParseHex(test[pos++].get_str());
uint256 filter_header_basic;
BOOST_CHECK(ParseHashStr(test[pos++].get_str(), filter_header_basic));
BlockFilter computed_filter_basic(BlockFilterType::BASIC, block, block_undo);
BOOST_CHECK(computed_filter_basic.GetFilter().GetEncoded() == filter_basic);
uint256 computed_header_basic = computed_filter_basic.ComputeHeader(prev_filter_header_basic);
BOOST_CHECK(computed_header_basic == filter_header_basic);
}
}
BOOST_AUTO_TEST_CASE(blockfilter_type_names)
{
BOOST_CHECK_EQUAL(BlockFilterTypeName(BlockFilterType::BASIC), "basic");
BOOST_CHECK_EQUAL(BlockFilterTypeName(static_cast<BlockFilterType>(255)), "");
BlockFilterType filter_type;
BOOST_CHECK(BlockFilterTypeByName("basic", filter_type));
BOOST_CHECK_EQUAL(filter_type, BlockFilterType::BASIC);
BOOST_CHECK(!BlockFilterTypeByName("unknown", filter_type));
}
BOOST_AUTO_TEST_SUITE_END()