|
| 1 | +/** |
| 2 | + * Copyright (c) 2011-2026 libbitcoin developers (see AUTHORS) |
| 3 | + * |
| 4 | + * This file is part of libbitcoin. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU Affero General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +#include "../../test.hpp" |
| 20 | +#include "electrum.hpp" |
| 21 | + |
| 22 | +BOOST_FIXTURE_TEST_SUITE(electrum_tests, electrum_setup_fixture) |
| 23 | + |
| 24 | +// blockchain.block.headers |
| 25 | + |
| 26 | +using namespace system; |
| 27 | +static const code not_found{ server::error::not_found }; |
| 28 | +static const code target_overflow{ server::error::target_overflow }; |
| 29 | +static const code invalid_argument{ server::error::invalid_argument }; |
| 30 | + |
| 31 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__genesis_count1_no_checkpoint__expected) |
| 32 | +{ |
| 33 | + BOOST_CHECK(handshake()); |
| 34 | + |
| 35 | + const auto response = get(R"({"id":60,"method":"blockchain.block.headers","params":[0,1]})" "\n"); |
| 36 | + const auto& result = response.at("result").as_object(); |
| 37 | + BOOST_CHECK_EQUAL(result.at("max").as_int64(), 5); |
| 38 | + BOOST_CHECK_EQUAL(result.at("count").as_int64(), 1); |
| 39 | + BOOST_CHECK(result.at("headers").is_array()); |
| 40 | + BOOST_CHECK_EQUAL(result.at("headers").as_array().size(), 1u); |
| 41 | + BOOST_CHECK_EQUAL(result.at("headers").as_array().at(0).as_string(), encode_base16(header0_data)); |
| 42 | +} |
| 43 | + |
| 44 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__block1to3_no_checkpoint__expected) |
| 45 | +{ |
| 46 | + BOOST_CHECK(handshake()); |
| 47 | + |
| 48 | + const auto response = get(R"({"id":61,"method":"blockchain.block.headers","params":[1,3]})" "\n"); |
| 49 | + const auto& result = response.at("result").as_object(); |
| 50 | + BOOST_CHECK_EQUAL(result.at("max").as_int64(), 5); |
| 51 | + BOOST_CHECK_EQUAL(result.at("count").as_int64(), 3); |
| 52 | + BOOST_CHECK(result.at("headers").is_array()); |
| 53 | + |
| 54 | + const auto& headers = result.at("headers").as_array(); |
| 55 | + BOOST_CHECK_EQUAL(headers.size(), 3u); |
| 56 | + BOOST_CHECK_EQUAL(headers.at(0).as_string(), encode_base16(header1_data)); |
| 57 | + BOOST_CHECK_EQUAL(headers.at(1).as_string(), encode_base16(header2_data)); |
| 58 | + BOOST_CHECK_EQUAL(headers.at(2).as_string(), encode_base16(header3_data)); |
| 59 | +} |
| 60 | + |
| 61 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__count_exceeds_max__capped) |
| 62 | +{ |
| 63 | + BOOST_CHECK(handshake()); |
| 64 | + |
| 65 | + const auto response = get(R"({"id":62,"method":"blockchain.block.headers","params":[0,10]})" "\n"); |
| 66 | + const auto& result = response.at("result").as_object(); |
| 67 | + BOOST_CHECK_EQUAL(result.at("max").as_int64(), 5); |
| 68 | + BOOST_CHECK_EQUAL(result.at("count").as_int64(), 5); |
| 69 | + BOOST_CHECK_EQUAL(result.at("headers").as_array().size(), 5u); |
| 70 | +} |
| 71 | + |
| 72 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__count_zero__empty_headers) |
| 73 | +{ |
| 74 | + BOOST_CHECK(handshake()); |
| 75 | + |
| 76 | + const auto response = get(R"({"id":63,"method":"blockchain.block.headers","params":[5,0]})" "\n"); |
| 77 | + const auto& result = response.at("result").as_object(); |
| 78 | + BOOST_CHECK_EQUAL(result.at("count").as_int64(), 0); |
| 79 | + BOOST_CHECK(result.at("headers").as_array().empty()); |
| 80 | +} |
| 81 | + |
| 82 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__proof_no_offset__expected) |
| 83 | +{ |
| 84 | + BOOST_CHECK(handshake()); |
| 85 | + |
| 86 | + const auto expected_root = encode_hash(merkle_root( |
| 87 | + { |
| 88 | + block0_hash, |
| 89 | + block1_hash, |
| 90 | + block2_hash, |
| 91 | + block3_hash, |
| 92 | + block4_hash, |
| 93 | + block5_hash, |
| 94 | + block6_hash, |
| 95 | + block7_hash, |
| 96 | + block8_hash |
| 97 | + })); |
| 98 | + |
| 99 | + const string_list expected_branch |
| 100 | + { |
| 101 | + encode_hash(block4_hash), |
| 102 | + encode_hash(root67), |
| 103 | + encode_hash(root03), |
| 104 | + encode_hash(root88) |
| 105 | + }; |
| 106 | + |
| 107 | + const auto response = get(R"({"id":64,"method":"blockchain.block.headers","params":[5,1,8]})" "\n"); |
| 108 | + const auto& result = response.at("result").as_object(); |
| 109 | + BOOST_CHECK_EQUAL(result.at("max").as_int64(), 5); |
| 110 | + BOOST_CHECK_EQUAL(result.at("count").as_int64(), 1); |
| 111 | + BOOST_CHECK_EQUAL(result.at("headers").as_array().size(), 1u); |
| 112 | + BOOST_CHECK_EQUAL(result.at("root").as_string(), expected_root); |
| 113 | + |
| 114 | + const auto& branch = result.at("branch").as_array(); |
| 115 | + BOOST_CHECK_EQUAL(branch.size(), expected_branch.size()); |
| 116 | + BOOST_CHECK_EQUAL(branch.at(0).as_string(), expected_branch[0]); |
| 117 | + BOOST_CHECK_EQUAL(branch.at(1).as_string(), expected_branch[1]); |
| 118 | + BOOST_CHECK_EQUAL(branch.at(2).as_string(), expected_branch[2]); |
| 119 | + BOOST_CHECK_EQUAL(branch.at(3).as_string(), expected_branch[3]); |
| 120 | +} |
| 121 | + |
| 122 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__proof_offset__expected) |
| 123 | +{ |
| 124 | + BOOST_CHECK(handshake()); |
| 125 | + |
| 126 | + const auto expected_root = encode_hash(merkle_root( |
| 127 | + { |
| 128 | + block0_hash, |
| 129 | + block1_hash, |
| 130 | + block2_hash, |
| 131 | + block3_hash, |
| 132 | + block4_hash, |
| 133 | + block5_hash, |
| 134 | + block6_hash, |
| 135 | + block7_hash, |
| 136 | + block8_hash |
| 137 | + })); |
| 138 | + |
| 139 | + const string_list expected_branch |
| 140 | + { |
| 141 | + encode_hash(block6_hash), |
| 142 | + encode_hash(root45), |
| 143 | + encode_hash(root03), |
| 144 | + encode_hash(root88) |
| 145 | + }; |
| 146 | + |
| 147 | + const auto response = get(R"({"id":64,"method":"blockchain.block.headers","params":[5,3,8]})" "\n"); |
| 148 | + const auto& result = response.at("result").as_object(); |
| 149 | + BOOST_CHECK_EQUAL(result.at("max").as_int64(), 5); |
| 150 | + BOOST_CHECK_EQUAL(result.at("count").as_int64(), 3); |
| 151 | + BOOST_CHECK_EQUAL(result.at("headers").as_array().size(), 3u); |
| 152 | + BOOST_CHECK_EQUAL(result.at("root").as_string(), expected_root); |
| 153 | + |
| 154 | + const auto& branch = result.at("branch").as_array(); |
| 155 | + BOOST_CHECK_EQUAL(branch.size(), expected_branch.size()); |
| 156 | + BOOST_CHECK_EQUAL(branch.at(0).as_string(), expected_branch[0]); |
| 157 | + BOOST_CHECK_EQUAL(branch.at(1).as_string(), expected_branch[1]); |
| 158 | + BOOST_CHECK_EQUAL(branch.at(2).as_string(), expected_branch[2]); |
| 159 | + BOOST_CHECK_EQUAL(branch.at(3).as_string(), expected_branch[3]); |
| 160 | +} |
| 161 | + |
| 162 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__start_above_top__not_found) |
| 163 | +{ |
| 164 | + BOOST_CHECK(handshake()); |
| 165 | + |
| 166 | + const auto response = get(R"({"id":65,"method":"blockchain.block.headers","params":[10,1]})" "\n"); |
| 167 | + BOOST_CHECK_EQUAL(response.at("error").as_object().at("code").as_int64(), not_found.value()); |
| 168 | +} |
| 169 | + |
| 170 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__target_exceeds_waypoint__target_overflow) |
| 171 | +{ |
| 172 | + BOOST_CHECK(handshake()); |
| 173 | + |
| 174 | + const auto response = get(R"({"id":66,"method":"blockchain.block.headers","params":[2,3,1]})" "\n"); |
| 175 | + BOOST_CHECK_EQUAL(response.at("error").as_object().at("code").as_int64(), target_overflow.value()); |
| 176 | +} |
| 177 | + |
| 178 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__waypoint_above_top__not_found) |
| 179 | +{ |
| 180 | + BOOST_CHECK(handshake()); |
| 181 | + |
| 182 | + const auto response = get(R"({"id":67,"method":"blockchain.block.headers","params":[0,1,10]})" "\n"); |
| 183 | + BOOST_CHECK_EQUAL(response.at("error").as_object().at("code").as_int64(), not_found.value()); |
| 184 | +} |
| 185 | + |
| 186 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__negative_start__invalid_argument) |
| 187 | +{ |
| 188 | + BOOST_CHECK(handshake()); |
| 189 | + |
| 190 | + const auto response = get(R"({"id":68,"method":"blockchain.block.headers","params":[-1,1]})" "\n"); |
| 191 | + BOOST_CHECK_EQUAL(response.at("error").as_object().at("code").as_int64(), invalid_argument.value()); |
| 192 | +} |
| 193 | + |
| 194 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__fractional_count__invalid_argument) |
| 195 | +{ |
| 196 | + BOOST_CHECK(handshake()); |
| 197 | + |
| 198 | + const auto response = get(R"({"id":69,"method":"blockchain.block.headers","params":[0,1.5]})" "\n"); |
| 199 | + BOOST_CHECK_EQUAL(response.at("error").as_object().at("code").as_int64(), invalid_argument.value()); |
| 200 | +} |
| 201 | + |
| 202 | +BOOST_AUTO_TEST_CASE(electrum__blockchain_block_headers__start_plus_count_huge__not_found) |
| 203 | +{ |
| 204 | + BOOST_CHECK(handshake()); |
| 205 | + |
| 206 | + // argument_overflow is not actually reachable via json due to its integer limits. |
| 207 | + const auto response = get(R"({"id":70,"method":"blockchain.block.headers","params":[9007199254740991,2]})" "\n"); |
| 208 | + BOOST_CHECK_EQUAL(response.at("error").as_object().at("code").as_int64(), not_found.value()); |
| 209 | +} |
| 210 | + |
| 211 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments