|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
| 5 | +#include <test/data/blockfilters.json.h> |
5 | 6 | #include <test/test_bitcoin.h>
|
6 | 7 |
|
7 | 8 | #include <blockfilter.h>
|
| 9 | +#include <core_io.h> |
8 | 10 | #include <serialize.h>
|
9 | 11 | #include <streams.h>
|
| 12 | +#include <univalue.h> |
| 13 | +#include <utilstrencodings.h> |
10 | 14 |
|
11 | 15 | #include <boost/test/unit_test.hpp>
|
12 | 16 |
|
@@ -84,4 +88,55 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test)
|
84 | 88 | }
|
85 | 89 | }
|
86 | 90 |
|
| 91 | +BOOST_AUTO_TEST_CASE(blockfilters_json_test) |
| 92 | +{ |
| 93 | + UniValue json; |
| 94 | + std::string json_data(json_tests::blockfilters, |
| 95 | + json_tests::blockfilters + sizeof(json_tests::blockfilters)); |
| 96 | + if (!json.read(json_data) || !json.isArray()) { |
| 97 | + BOOST_ERROR("Parse error."); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const UniValue& tests = json.get_array(); |
| 102 | + for (unsigned int i = 0; i < tests.size(); i++) { |
| 103 | + UniValue test = tests[i]; |
| 104 | + std::string strTest = test.write(); |
| 105 | + |
| 106 | + if (test.size() == 1) { |
| 107 | + continue; |
| 108 | + } else if (test.size() < 7) { |
| 109 | + BOOST_ERROR("Bad test: " << strTest); |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + unsigned int pos = 0; |
| 114 | + /*int block_height =*/ test[pos++].get_int(); |
| 115 | + /*uint256 block_hash =*/ ParseHashStr(test[pos++].get_str(), "block_hash"); |
| 116 | + |
| 117 | + CBlock block; |
| 118 | + BOOST_REQUIRE(DecodeHexBlk(block, test[pos++].get_str())); |
| 119 | + |
| 120 | + CBlockUndo block_undo; |
| 121 | + block_undo.vtxundo.emplace_back(); |
| 122 | + CTxUndo& tx_undo = block_undo.vtxundo.back(); |
| 123 | + const UniValue& prev_scripts = test[pos++].get_array(); |
| 124 | + for (unsigned int ii = 0; ii < prev_scripts.size(); ii++) { |
| 125 | + std::vector<unsigned char> raw_script = ParseHex(prev_scripts[ii].get_str()); |
| 126 | + CTxOut txout(0, CScript(raw_script.begin(), raw_script.end())); |
| 127 | + tx_undo.vprevout.emplace_back(txout, 0, false); |
| 128 | + } |
| 129 | + |
| 130 | + uint256 prev_filter_header_basic = ParseHashStr(test[pos++].get_str(), "prev_filter_header_basic"); |
| 131 | + std::vector<unsigned char> filter_basic = ParseHex(test[pos++].get_str()); |
| 132 | + uint256 filter_header_basic = ParseHashStr(test[pos++].get_str(), "filter_header_basic"); |
| 133 | + |
| 134 | + BlockFilter computed_filter_basic(BlockFilterType::BASIC, block, block_undo); |
| 135 | + BOOST_CHECK(computed_filter_basic.GetFilter().GetEncoded() == filter_basic); |
| 136 | + |
| 137 | + uint256 computed_header_basic = computed_filter_basic.ComputeHeader(prev_filter_header_basic); |
| 138 | + BOOST_CHECK(computed_header_basic == filter_header_basic); |
| 139 | + } |
| 140 | +} |
| 141 | + |
87 | 142 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments