diff options
author | Jim Posen <jimpo@coinbase.com> | 2018-01-23 17:38:23 -0800 |
---|---|---|
committer | Jim Posen <jim.posen@gmail.com> | 2018-08-25 10:02:37 -0700 |
commit | 97b64d67daf0336dfb64b132f3e4d6a4c1967da4 (patch) | |
tree | 56ceac58100d97c9cf70f5c0ec7fd0d83e5bee46 /src/test/blockfilter_tests.cpp | |
parent | a4afb9cadbaecb0676e6475ab8d32a52faecb47a (diff) | |
download | bitcoin-97b64d67daf0336dfb64b132f3e4d6a4c1967da4.tar.xz |
blockfilter: Unit test against BIP 158 test vectors.
Full test of block filter and header construction.
Diffstat (limited to 'src/test/blockfilter_tests.cpp')
-rw-r--r-- | src/test/blockfilter_tests.cpp | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/test/blockfilter_tests.cpp b/src/test/blockfilter_tests.cpp index 4fe34dedcb..e3cb05f09e 100644 --- a/src/test/blockfilter_tests.cpp +++ b/src/test/blockfilter_tests.cpp @@ -2,11 +2,15 @@ // 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/test_bitcoin.h> #include <blockfilter.h> +#include <core_io.h> #include <serialize.h> #include <streams.h> +#include <univalue.h> +#include <utilstrencodings.h> #include <boost/test/unit_test.hpp> @@ -84,4 +88,55 @@ BOOST_AUTO_TEST_CASE(blockfilter_basic_test) } } +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++) { + 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++].get_int(); + /*uint256 block_hash =*/ 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 = ParseHashStr(test[pos++].get_str(), "prev_filter_header_basic"); + std::vector<unsigned char> filter_basic = ParseHex(test[pos++].get_str()); + uint256 filter_header_basic = 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_SUITE_END() |