| 1 | // Copyright (c) 2020-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #include <boost/preprocessor/stringize.hpp> |
| 31 | #include <gtest/gtest.h> |
| 32 | #include <rapidjson/document.h> |
| 33 | |
| 34 | #include "cryptonote_basic/account.h" |
| 35 | #include "cryptonote_basic/cryptonote_basic.h" |
| 36 | #include "cryptonote_basic/events.h" |
| 37 | #include "cryptonote_basic/cryptonote_format_utils.h" |
| 38 | #include "json_serialization.h" |
| 39 | #include "net/zmq.h" |
| 40 | #include "rpc/message.h" |
| 41 | #include "rpc/zmq_pub.h" |
| 42 | #include "rpc/zmq_restricted_methods.h" |
| 43 | #include "rpc/zmq_server.h" |
| 44 | #include "serialization/json_object.h" |
| 45 | |
| 46 | #define MASSERT(...) \ |
| 47 | if (!(__VA_ARGS__)) \ |
| 48 | return testing::AssertionFailure() << BOOST_PP_STRINGIZE(__VA_ARGS__) |
| 49 | |
| 50 | TEST(ZmqFullMessage, InvalidRequest) |
| 51 | { |
| 52 | EXPECT_THROW( |
| 53 | (cryptonote::rpc::FullMessage{"{\"jsonrpc\":\"2.0\",\"id\":0,\"params\":[]}", true}), |
| 54 | cryptonote::json::MISSING_KEY |
| 55 | ); |
| 56 | EXPECT_THROW( |
| 57 | (cryptonote::rpc::FullMessage{"{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":3,\"params\":[]}", true}), |
| 58 | cryptonote::json::WRONG_TYPE |
| 59 | ); |
| 60 | } |
| 61 | |
| 62 | TEST(ZmqFullMessage, Request) |
| 63 | { |
| 64 | static constexpr const char request[] = "{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"foo\",\"params\":[]}"; |
| 65 | EXPECT_NO_THROW( |
| 66 | (cryptonote::rpc::FullMessage{request, true}) |
| 67 | ); |
| 68 | |
| 69 | cryptonote::rpc::FullMessage parsed{request, true}; |
| 70 | EXPECT_STREQ("foo", parsed.getRequestType().c_str()); |
| 71 | } |
| 72 | |
| 73 | TEST(ZmqRestrictedMethods, BasicCoverage) |
| 74 | { |
| 75 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("flush_txpool")); |
| 76 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("get_peer_list")); |
| 77 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("mining_status")); |
| 78 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("relay_tx")); |
| 79 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("save_bc")); |
| 80 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("set_log_categories")); |
| 81 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("set_log_level")); |
| 82 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("start_mining")); |
| 83 | EXPECT_TRUE(cryptonote::rpc::is_blocked_in_restricted_mode("stop_mining")); |
| 84 | |
| 85 | EXPECT_FALSE(cryptonote::rpc::is_blocked_in_restricted_mode("get_height")); |
| 86 | EXPECT_FALSE(cryptonote::rpc::is_blocked_in_restricted_mode("get_info")); |
| 87 | EXPECT_FALSE(cryptonote::rpc::is_blocked_in_restricted_mode("send_raw_tx")); |
| 88 | } |
| 89 | |
| 90 | namespace |
| 91 | { |
| 92 | using published_json = std::pair<std::string, rapidjson::Document>; |
| 93 | |
| 94 | constexpr const char inproc_pub[] = "inproc://dummy_pub"; |
| 95 | |
| 96 | net::zmq::socket create_socket(void* ctx, const char* address) |
| 97 | { |
| 98 | net::zmq::socket sock{zmq_socket(ctx, ZMQ_PAIR)}; |
| 99 | if (!sock) |
| 100 | MONERO_ZMQ_THROW("failed to create socket"); |
| 101 | if (zmq_bind(sock.get(), address) != 0) |
| 102 | MONERO_ZMQ_THROW("socket bind failure"); |
| 103 | return sock; |
| 104 | } |
| 105 | |
| 106 | std::vector<std::string> get_messages(void* socket, int count = -1) |
| 107 | { |
| 108 | std::vector<std::string> out; |
| 109 | for ( ; count || count < 0; --count) |
| 110 | { |
| 111 | expect<std::string> next = net::zmq::receive(socket, (count < 0 ? ZMQ_DONTWAIT : 0)); |
| 112 | if (next == net::zmq::make_error_code(EAGAIN)) |
| 113 | return out; |
| 114 | out.push_back(std::move(*next)); |
| 115 | } |
| 116 | return out; |
| 117 | } |
| 118 | |
| 119 | std::vector<published_json> get_published(void* socket, int count = -1) |
| 120 | { |
| 121 | std::vector<published_json> out; |
| 122 | |
| 123 | const auto messages = get_messages(socket, count); |
| 124 | out.reserve(messages.size()); |
| 125 | |
| 126 | for (const std::string& message : messages) |
| 127 | { |
| 128 | const char* split = std::strchr(message.c_str(), ':'); |
| 129 | if (!split) |
| 130 | throw std::runtime_error{"Invalid ZMQ/Pub message"}; |
| 131 | |
| 132 | out.emplace_back(); |
| 133 | out.back().first = {message.c_str(), split}; |
| 134 | if (out.back().second.Parse(split + 1).HasParseError()) |
| 135 | throw std::runtime_error{"Failed to parse ZMQ/Pub message"}; |
| 136 | } |
| 137 | |
| 138 | return out; |
| 139 | } |
| 140 | |
| 141 | testing::AssertionResult compare_full_txpool(epee::span<const cryptonote::txpool_event> events, const published_json& pub) |
| 142 | { |
| 143 | MASSERT(pub.first == "json-full-txpool_add"); |
| 144 | MASSERT(pub.second.IsArray()); |
| 145 | MASSERT(pub.second.Size() <= events.size()); |
| 146 | |
| 147 | std::size_t i = 0; |
| 148 | for (const cryptonote::txpool_event& event : events) |
| 149 | { |
| 150 | MASSERT(i <= pub.second.Size()); |
| 151 | if (!event.res) |
| 152 | continue; |
| 153 | |
| 154 | cryptonote::transaction tx{}; |
| 155 | cryptonote::json::fromJsonValue(pub.second[i], tx); |
| 156 | |
| 157 | crypto::hash id{}; |
| 158 | MASSERT(cryptonote::get_transaction_hash(event.tx, id)); |
| 159 | MASSERT(cryptonote::get_transaction_hash(tx, id)); |
| 160 | MASSERT(event.tx.hash == tx.hash); |
| 161 | ++i; |
| 162 | } |
| 163 | return testing::AssertionSuccess(); |
| 164 | } |
| 165 | |
| 166 | testing::AssertionResult compare_minimal_txpool(epee::span<const cryptonote::txpool_event> events, const published_json& pub) |
| 167 | { |
| 168 | MASSERT(pub.first == "json-minimal-txpool_add"); |
| 169 | MASSERT(pub.second.IsArray()); |
| 170 | MASSERT(pub.second.Size() <= events.size()); |
| 171 | |
| 172 | std::size_t i = 0; |
| 173 | for (const cryptonote::txpool_event& event : events) |
| 174 | { |
| 175 | MASSERT(i <= pub.second.Size()); |
| 176 | if (!event.res) |
| 177 | continue; |
| 178 | |
| 179 | std::size_t actual_size = 0; |
| 180 | crypto::hash actual_id{}; |
| 181 | |
| 182 | MASSERT(pub.second[i].IsObject()); |
| 183 | GET_FROM_JSON_OBJECT(pub.second[i], actual_id, id); |
| 184 | GET_FROM_JSON_OBJECT(pub.second[i], actual_size, blob_size); |
| 185 | |
| 186 | std::size_t expected_size = 0; |
| 187 | crypto::hash expected_id{}; |
| 188 | MASSERT(cryptonote::get_transaction_hash(event.tx, expected_id, expected_size)); |
| 189 | MASSERT(expected_size == actual_size); |
| 190 | MASSERT(expected_id == actual_id); |
| 191 | ++i; |
| 192 | } |
| 193 | return testing::AssertionSuccess(); |
| 194 | } |
| 195 | |
| 196 | testing::AssertionResult compare_full_block(const epee::span<const cryptonote::block> expected, const published_json& pub) |
| 197 | { |
| 198 | MASSERT(pub.first == "json-full-chain_main"); |
| 199 | MASSERT(pub.second.IsArray()); |
| 200 | |
| 201 | std::vector<cryptonote::block> actual; |
| 202 | cryptonote::json::fromJsonValue(pub.second, actual); |
| 203 | |
| 204 | MASSERT(expected.size() == actual.size()); |
| 205 | |
| 206 | for (std::size_t i = 0; i < expected.size(); ++i) |
| 207 | { |
| 208 | crypto::hash id; |
| 209 | MASSERT(cryptonote::get_block_hash(expected[i], id)); |
| 210 | MASSERT(cryptonote::get_block_hash(actual[i], id)); |
| 211 | MASSERT(expected[i].hash == actual[i].hash); |
| 212 | } |
| 213 | |
| 214 | return testing::AssertionSuccess(); |
| 215 | } |
| 216 | |
| 217 | testing::AssertionResult compare_minimal_block(std::size_t height, const epee::span<const cryptonote::block> expected, const published_json& pub) |
| 218 | { |
| 219 | MASSERT(pub.first == "json-minimal-chain_main"); |
| 220 | MASSERT(pub.second.IsObject()); |
| 221 | MASSERT(!expected.empty()); |
| 222 | |
| 223 | std::size_t actual_height = 0; |
| 224 | crypto::hash actual_prev_id{}; |
| 225 | std::vector<crypto::hash> actual_ids{}; |
| 226 | GET_FROM_JSON_OBJECT(pub.second, actual_height, first_height); |
| 227 | GET_FROM_JSON_OBJECT(pub.second, actual_prev_id, first_prev_id); |
| 228 | GET_FROM_JSON_OBJECT(pub.second, actual_ids, ids); |
| 229 | |
| 230 | MASSERT(height == actual_height); |
| 231 | MASSERT(expected[0].prev_id == actual_prev_id); |
| 232 | MASSERT(expected.size() == actual_ids.size()); |
| 233 | |
| 234 | for (std::size_t i = 0; i < expected.size(); ++i) |
| 235 | { |
| 236 | crypto::hash id; |
| 237 | MASSERT(cryptonote::get_block_hash(expected[i], id)); |
| 238 | MASSERT(id == actual_ids[i]); |
| 239 | } |
| 240 | |
| 241 | return testing::AssertionSuccess(); |
| 242 | } |
| 243 | |
| 244 | struct zmq_base : public testing::Test |
| 245 | { |
| 246 | cryptonote::account_base acct; |
| 247 | |
| 248 | zmq_base() |
| 249 | : testing::Test(), acct() |
| 250 | { |
| 251 | acct.generate(); |
| 252 | } |
| 253 | |
| 254 | cryptonote::transaction make_miner_transaction() |
| 255 | { |
| 256 | return test::make_miner_transaction(acct.get_keys().m_account_address); |
| 257 | } |
| 258 | |
| 259 | cryptonote::transaction make_transaction(const std::vector<cryptonote::account_public_address>& destinations) |
| 260 | { |
| 261 | return test::make_transaction(acct.get_keys(), {make_miner_transaction()}, destinations, true, true); |
| 262 | } |
| 263 | |
| 264 | cryptonote::transaction make_transaction() |
| 265 | { |
| 266 | cryptonote::account_base temp_account; |
| 267 | temp_account.generate(); |
| 268 | return make_transaction({temp_account.get_keys().m_account_address}); |
| 269 | } |
| 270 | |
| 271 | cryptonote::block make_block() |
| 272 | { |
| 273 | cryptonote::block block{}; |
| 274 | block.major_version = 1; |
| 275 | block.minor_version = 3; |
| 276 | block.timestamp = 100; |
| 277 | block.prev_id = crypto::rand<crypto::hash>(); |
| 278 | block.nonce = 100; |
| 279 | block.miner_tx = make_miner_transaction(); |
| 280 | return block; |
| 281 | } |
| 282 | }; |
| 283 | |
| 284 | struct zmq_pub : public zmq_base |
| 285 | { |
| 286 | net::zmq::context ctx; |
| 287 | net::zmq::socket relay; |
| 288 | net::zmq::socket dummy_pub; |
| 289 | net::zmq::socket dummy_client; |
| 290 | std::shared_ptr<cryptonote::listener::zmq_pub> pub; |
| 291 | |
| 292 | zmq_pub() |
| 293 | : zmq_base(), |
| 294 | ctx(zmq_init(1)), |
| 295 | relay(create_socket(ctx.get(), cryptonote::listener::zmq_pub::relay_endpoint())), |
| 296 | dummy_pub(create_socket(ctx.get(), inproc_pub)), |
| 297 | dummy_client(zmq_socket(ctx.get(), ZMQ_PAIR)), |
| 298 | pub(std::make_shared<cryptonote::listener::zmq_pub>(ctx.get())) |
| 299 | { |
| 300 | if (!dummy_client) |
| 301 | MONERO_ZMQ_THROW("failed to create socket"); |
| 302 | if (zmq_connect(dummy_client.get(), inproc_pub) != 0) |
| 303 | MONERO_ZMQ_THROW("failed to connect to dummy pub"); |
| 304 | } |
| 305 | |
| 306 | virtual void TearDown() override final |
| 307 | { |
| 308 | EXPECT_EQ(0u, get_messages(relay.get()).size()); |
| 309 | EXPECT_EQ(0u, get_messages(dummy_client.get()).size()); |
| 310 | } |
| 311 | |
| 312 | template<std::size_t N> |
| 313 | bool sub_request(const char (&topic)[N]) |
| 314 | { |
| 315 | return pub->sub_request({topic, N - 1}); |
| 316 | } |
| 317 | }; |
| 318 | |
| 319 | struct dummy_handler final : cryptonote::rpc::RpcHandler |
| 320 | { |
| 321 | dummy_handler() |
| 322 | : cryptonote::rpc::RpcHandler() |
| 323 | {} |
| 324 | |
| 325 | virtual epee::byte_slice handle(std::string&& request) override final |
| 326 | { |
| 327 | throw std::logic_error{"not implemented"}; |
| 328 | } |
| 329 | }; |
| 330 | |
| 331 | struct zmq_server : public zmq_base |
| 332 | { |
| 333 | dummy_handler handler; |
| 334 | cryptonote::rpc::ZmqServer server; |
| 335 | std::shared_ptr<cryptonote::listener::zmq_pub> pub; |
| 336 | net::zmq::socket sub; |
| 337 | |
| 338 | zmq_server() |
| 339 | : zmq_base(), |
| 340 | handler(), |
| 341 | server(handler), |
| 342 | pub(), |
| 343 | sub() |
| 344 | { |
| 345 | void* ctx = server.init_rpc({}, {}); |
| 346 | if (!ctx) |
| 347 | throw std::runtime_error{"init_rpc failure"}; |
| 348 | |
| 349 | const std::string endpoint = inproc_pub; |
| 350 | pub = server.init_pub({std::addressof(endpoint), 1}); |
| 351 | if (!pub) |
| 352 | throw std::runtime_error{"failed to initiaze zmq/pub"}; |
| 353 | |
| 354 | sub.reset(zmq_socket(ctx, ZMQ_SUB)); |
| 355 | if (!sub) |
| 356 | MONERO_ZMQ_THROW("failed to create socket"); |
| 357 | if (zmq_connect(sub.get(), inproc_pub) != 0) |
| 358 | MONERO_ZMQ_THROW("failed to connect to dummy pub"); |
| 359 | |
| 360 | server.run(); |
| 361 | } |
| 362 | |
| 363 | virtual void TearDown() override final |
| 364 | { |
| 365 | EXPECT_EQ(0u, get_messages(sub.get()).size()); |
| 366 | sub.reset(); |
| 367 | pub.reset(); |
| 368 | server.stop(); |
| 369 | } |
| 370 | |
| 371 | template<std::size_t N> |
| 372 | void subscribe(const char (&topic)[N]) |
| 373 | { |
| 374 | if (zmq_setsockopt(sub.get(), ZMQ_SUBSCRIBE, topic, N - 1) != 0) |
| 375 | MONERO_ZMQ_THROW("failed to subscribe"); |
| 376 | } |
| 377 | }; |
| 378 | } |
| 379 | |
| 380 | TEST_F(zmq_pub, InvalidContext) |
| 381 | { |
| 382 | EXPECT_THROW(cryptonote::listener::zmq_pub{nullptr}, std::logic_error); |
| 383 | } |
| 384 | |
| 385 | TEST_F(zmq_pub, NoBlocking) |
| 386 | { |
| 387 | EXPECT_FALSE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 388 | } |
| 389 | |
| 390 | TEST_F(zmq_pub, DefaultDrop) |
| 391 | { |
| 392 | EXPECT_EQ(0u, pub->send_txpool_add({{make_transaction(), {}, true}})); |
| 393 | |
| 394 | const cryptonote::block bl = make_block(); |
| 395 | EXPECT_EQ(0u,pub->send_chain_main(5, {std::addressof(bl), 1})); |
| 396 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::chain_main{pub}(5, {std::addressof(bl), 1})); |
| 397 | } |
| 398 | |
| 399 | TEST_F(zmq_pub, JsonFullTxpool) |
| 400 | { |
| 401 | static constexpr const char topic[] = "\1json-full-txpool_add"; |
| 402 | |
| 403 | ASSERT_TRUE(sub_request(topic)); |
| 404 | |
| 405 | std::vector<cryptonote::txpool_event> events |
| 406 | { |
| 407 | {make_transaction(), {}, true}, {make_transaction(), {}, true} |
| 408 | }; |
| 409 | |
| 410 | EXPECT_NO_THROW(pub->send_txpool_add(events)); |
| 411 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 412 | |
| 413 | auto pubs = get_published(dummy_client.get()); |
| 414 | EXPECT_EQ(1u, pubs.size()); |
| 415 | ASSERT_LE(1u, pubs.size()); |
| 416 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 417 | |
| 418 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 419 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 420 | |
| 421 | pubs = get_published(dummy_client.get()); |
| 422 | EXPECT_EQ(1u, pubs.size()); |
| 423 | ASSERT_LE(1u, pubs.size()); |
| 424 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 425 | |
| 426 | events.at(0).res = false; |
| 427 | EXPECT_EQ(1u, pub->send_txpool_add(events)); |
| 428 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 429 | |
| 430 | pubs = get_published(dummy_client.get()); |
| 431 | EXPECT_EQ(1u, pubs.size()); |
| 432 | ASSERT_LE(1u, pubs.size()); |
| 433 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 434 | |
| 435 | events.at(0).res = false; |
| 436 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 437 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 438 | |
| 439 | pubs = get_published(dummy_client.get()); |
| 440 | EXPECT_EQ(1u, pubs.size()); |
| 441 | ASSERT_LE(1u, pubs.size()); |
| 442 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 443 | } |
| 444 | |
| 445 | TEST_F(zmq_pub, JsonMinimalTxpool) |
| 446 | { |
| 447 | static constexpr const char topic[] = "\1json-minimal-txpool_add"; |
| 448 | |
| 449 | ASSERT_TRUE(sub_request(topic)); |
| 450 | |
| 451 | std::vector<cryptonote::txpool_event> events |
| 452 | { |
| 453 | {make_transaction(), {}, true}, {make_transaction(), {}, true} |
| 454 | }; |
| 455 | |
| 456 | EXPECT_NO_THROW(pub->send_txpool_add(events)); |
| 457 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 458 | |
| 459 | auto pubs = get_published(dummy_client.get()); |
| 460 | EXPECT_EQ(1u, pubs.size()); |
| 461 | ASSERT_LE(1u, pubs.size()); |
| 462 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 463 | |
| 464 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 465 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 466 | |
| 467 | pubs = get_published(dummy_client.get()); |
| 468 | EXPECT_EQ(1u, pubs.size()); |
| 469 | ASSERT_LE(1u, pubs.size()); |
| 470 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 471 | |
| 472 | events.at(0).res = false; |
| 473 | EXPECT_EQ(1u, pub->send_txpool_add(events)); |
| 474 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 475 | |
| 476 | pubs = get_published(dummy_client.get()); |
| 477 | EXPECT_EQ(1u, pubs.size()); |
| 478 | ASSERT_LE(1u, pubs.size()); |
| 479 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 480 | |
| 481 | events.at(0).res = false; |
| 482 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 483 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 484 | |
| 485 | pubs = get_published(dummy_client.get()); |
| 486 | EXPECT_EQ(1u, pubs.size()); |
| 487 | ASSERT_LE(1u, pubs.size()); |
| 488 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 489 | } |
| 490 | |
| 491 | TEST_F(zmq_pub, JsonFullChain) |
| 492 | { |
| 493 | static constexpr const char topic[] = "\1json-full-chain_main"; |
| 494 | |
| 495 | ASSERT_TRUE(sub_request(topic)); |
| 496 | |
| 497 | const std::array<cryptonote::block, 2> blocks{{make_block(), make_block()}}; |
| 498 | |
| 499 | EXPECT_EQ(1u, pub->send_chain_main(100, epee::to_span(blocks))); |
| 500 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 501 | |
| 502 | auto pubs = get_published(dummy_client.get()); |
| 503 | EXPECT_EQ(1u, pubs.size()); |
| 504 | ASSERT_LE(1u, pubs.size()); |
| 505 | EXPECT_TRUE(compare_full_block(epee::to_span(blocks), pubs.front())); |
| 506 | |
| 507 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::chain_main{pub}(533, epee::to_span(blocks))); |
| 508 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 509 | |
| 510 | pubs = get_published(dummy_client.get()); |
| 511 | EXPECT_EQ(1u, pubs.size()); |
| 512 | ASSERT_LE(1u, pubs.size()); |
| 513 | EXPECT_TRUE(compare_full_block(epee::to_span(blocks), pubs.front())); |
| 514 | } |
| 515 | |
| 516 | TEST_F(zmq_pub, JsonMinimalChain) |
| 517 | { |
| 518 | static constexpr const char topic[] = "\1json-minimal-chain_main"; |
| 519 | |
| 520 | ASSERT_TRUE(sub_request(topic)); |
| 521 | |
| 522 | const std::array<cryptonote::block, 2> blocks{{make_block(), make_block()}}; |
| 523 | |
| 524 | EXPECT_EQ(1u, pub->send_chain_main(100, epee::to_span(blocks))); |
| 525 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 526 | |
| 527 | auto pubs = get_published(dummy_client.get()); |
| 528 | EXPECT_EQ(1u, pubs.size()); |
| 529 | ASSERT_LE(1u, pubs.size()); |
| 530 | EXPECT_TRUE(compare_minimal_block(100, epee::to_span(blocks), pubs.front())); |
| 531 | |
| 532 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::chain_main{pub}(533, epee::to_span(blocks))); |
| 533 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 534 | |
| 535 | pubs = get_published(dummy_client.get()); |
| 536 | EXPECT_EQ(1u, pubs.size()); |
| 537 | ASSERT_LE(1u, pubs.size()); |
| 538 | EXPECT_TRUE(compare_minimal_block(533, epee::to_span(blocks), pubs.front())); |
| 539 | } |
| 540 | |
| 541 | TEST_F(zmq_pub, JsonFullAll) |
| 542 | { |
| 543 | static constexpr const char topic[] = "\1json-full"; |
| 544 | |
| 545 | ASSERT_TRUE(sub_request(topic)); |
| 546 | { |
| 547 | std::vector<cryptonote::txpool_event> events |
| 548 | { |
| 549 | {make_transaction(), {}, true}, {make_transaction(), {}, true} |
| 550 | }; |
| 551 | |
| 552 | EXPECT_EQ(1u, pub->send_txpool_add(events)); |
| 553 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 554 | |
| 555 | auto pubs = get_published(dummy_client.get()); |
| 556 | EXPECT_EQ(1u, pubs.size()); |
| 557 | ASSERT_LE(1u, pubs.size()); |
| 558 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 559 | |
| 560 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 561 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 562 | |
| 563 | pubs = get_published(dummy_client.get()); |
| 564 | EXPECT_EQ(1u, pubs.size()); |
| 565 | ASSERT_LE(1u, pubs.size()); |
| 566 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 567 | |
| 568 | events.at(0).res = false; |
| 569 | EXPECT_NO_THROW(pub->send_txpool_add(events)); |
| 570 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 571 | |
| 572 | pubs = get_published(dummy_client.get()); |
| 573 | EXPECT_EQ(1u, pubs.size()); |
| 574 | ASSERT_LE(1u, pubs.size()); |
| 575 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 576 | |
| 577 | events.at(0).res = false; |
| 578 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 579 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 580 | |
| 581 | pubs = get_published(dummy_client.get()); |
| 582 | EXPECT_EQ(1u, pubs.size()); |
| 583 | ASSERT_LE(1u, pubs.size()); |
| 584 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 585 | } |
| 586 | { |
| 587 | const std::array<cryptonote::block, 2> blocks{{make_block(), make_block()}}; |
| 588 | |
| 589 | EXPECT_EQ(1u, pub->send_chain_main(100, epee::to_span(blocks))); |
| 590 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 591 | |
| 592 | auto pubs = get_published(dummy_client.get()); |
| 593 | EXPECT_EQ(1u, pubs.size()); |
| 594 | ASSERT_LE(1u, pubs.size()); |
| 595 | EXPECT_TRUE(compare_full_block(epee::to_span(blocks), pubs.front())); |
| 596 | |
| 597 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::chain_main{pub}(533, epee::to_span(blocks))); |
| 598 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 599 | |
| 600 | pubs = get_published(dummy_client.get()); |
| 601 | EXPECT_EQ(1u, pubs.size()); |
| 602 | ASSERT_LE(1u, pubs.size()); |
| 603 | EXPECT_TRUE(compare_full_block(epee::to_span(blocks), pubs.front())); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | TEST_F(zmq_pub, JsonMinimalAll) |
| 608 | { |
| 609 | static constexpr const char topic[] = "\1json-minimal"; |
| 610 | |
| 611 | ASSERT_TRUE(sub_request(topic)); |
| 612 | |
| 613 | { |
| 614 | std::vector<cryptonote::txpool_event> events |
| 615 | { |
| 616 | {make_transaction(), {}, true}, {make_transaction(), {}, true} |
| 617 | }; |
| 618 | |
| 619 | EXPECT_EQ(1u, pub->send_txpool_add(events)); |
| 620 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 621 | |
| 622 | auto pubs = get_published(dummy_client.get()); |
| 623 | EXPECT_EQ(1u, pubs.size()); |
| 624 | ASSERT_LE(1u, pubs.size()); |
| 625 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 626 | |
| 627 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 628 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 629 | |
| 630 | pubs = get_published(dummy_client.get()); |
| 631 | EXPECT_EQ(1u, pubs.size()); |
| 632 | ASSERT_LE(1u, pubs.size()); |
| 633 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 634 | |
| 635 | events.at(0).res = false; |
| 636 | EXPECT_NO_THROW(pub->send_txpool_add(events)); |
| 637 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 638 | |
| 639 | pubs = get_published(dummy_client.get()); |
| 640 | EXPECT_EQ(1u, pubs.size()); |
| 641 | ASSERT_LE(1u, pubs.size()); |
| 642 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 643 | |
| 644 | events.at(0).res = false; |
| 645 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 646 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 647 | |
| 648 | pubs = get_published(dummy_client.get()); |
| 649 | EXPECT_EQ(1u, pubs.size()); |
| 650 | ASSERT_LE(1u, pubs.size()); |
| 651 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 652 | } |
| 653 | { |
| 654 | const std::array<cryptonote::block, 2> blocks{{make_block(), make_block()}}; |
| 655 | |
| 656 | EXPECT_EQ(1u, pub->send_chain_main(100, epee::to_span(blocks))); |
| 657 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 658 | |
| 659 | auto pubs = get_published(dummy_client.get()); |
| 660 | EXPECT_EQ(1u, pubs.size()); |
| 661 | ASSERT_LE(1u, pubs.size()); |
| 662 | EXPECT_TRUE(compare_minimal_block(100, epee::to_span(blocks), pubs.front())); |
| 663 | |
| 664 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::chain_main{pub}(533, epee::to_span(blocks))); |
| 665 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 666 | |
| 667 | pubs = get_published(dummy_client.get()); |
| 668 | EXPECT_EQ(1u, pubs.size()); |
| 669 | ASSERT_LE(1u, pubs.size()); |
| 670 | EXPECT_TRUE(compare_minimal_block(533, epee::to_span(blocks), pubs.front())); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | TEST_F(zmq_pub, JsonAll) |
| 675 | { |
| 676 | static constexpr const char topic[] = "\1json"; |
| 677 | |
| 678 | ASSERT_TRUE(sub_request(topic)); |
| 679 | |
| 680 | { |
| 681 | std::vector<cryptonote::txpool_event> events |
| 682 | { |
| 683 | {make_transaction(), {}, true}, {make_transaction(), {}, true} |
| 684 | }; |
| 685 | |
| 686 | EXPECT_EQ(1u, pub->send_txpool_add(events)); |
| 687 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 688 | |
| 689 | auto pubs = get_published(dummy_client.get()); |
| 690 | EXPECT_EQ(2u, pubs.size()); |
| 691 | ASSERT_LE(2u, pubs.size()); |
| 692 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 693 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.back())); |
| 694 | |
| 695 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 696 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 697 | |
| 698 | pubs = get_published(dummy_client.get()); |
| 699 | EXPECT_EQ(2u, pubs.size()); |
| 700 | ASSERT_LE(2u, pubs.size()); |
| 701 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 702 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.back())); |
| 703 | |
| 704 | events.at(0).res = false; |
| 705 | EXPECT_EQ(1u, pub->send_txpool_add(events)); |
| 706 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 707 | |
| 708 | pubs = get_published(dummy_client.get()); |
| 709 | EXPECT_EQ(2u, pubs.size()); |
| 710 | ASSERT_LE(2u, pubs.size()); |
| 711 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 712 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.back())); |
| 713 | |
| 714 | events.at(0).res = false; |
| 715 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(events)); |
| 716 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 717 | |
| 718 | pubs = get_published(dummy_client.get()); |
| 719 | EXPECT_EQ(2u, pubs.size()); |
| 720 | ASSERT_LE(2u, pubs.size()); |
| 721 | EXPECT_TRUE(compare_full_txpool(epee::to_span(events), pubs.front())); |
| 722 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.back())); |
| 723 | } |
| 724 | { |
| 725 | const std::array<cryptonote::block, 1> blocks{{make_block()}}; |
| 726 | |
| 727 | EXPECT_EQ(2u, pub->send_chain_main(100, epee::to_span(blocks))); |
| 728 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 729 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 730 | |
| 731 | auto pubs = get_published(dummy_client.get()); |
| 732 | EXPECT_EQ(2u, pubs.size()); |
| 733 | ASSERT_LE(2u, pubs.size()); |
| 734 | EXPECT_TRUE(compare_full_block(epee::to_span(blocks), pubs.front())); |
| 735 | EXPECT_TRUE(compare_minimal_block(100, epee::to_span(blocks), pubs.back())); |
| 736 | |
| 737 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::chain_main{pub}(533, epee::to_span(blocks))); |
| 738 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 739 | EXPECT_TRUE(pub->relay_to_pub(relay.get(), dummy_pub.get())); |
| 740 | |
| 741 | pubs = get_published(dummy_client.get()); |
| 742 | EXPECT_EQ(2u, pubs.size()); |
| 743 | ASSERT_LE(2u, pubs.size()); |
| 744 | EXPECT_TRUE(compare_full_block(epee::to_span(blocks), pubs.front())); |
| 745 | EXPECT_TRUE(compare_minimal_block(533, epee::to_span(blocks), pubs.back())); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | TEST_F(zmq_pub, JsonChainWeakPtrSkip) |
| 750 | { |
| 751 | static constexpr const char topic[] = "\1json"; |
| 752 | |
| 753 | ASSERT_TRUE(sub_request(topic)); |
| 754 | |
| 755 | const std::array<cryptonote::block, 1> blocks{{make_block()}}; |
| 756 | |
| 757 | pub.reset(); |
| 758 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::chain_main{pub}(533, epee::to_span(blocks))); |
| 759 | } |
| 760 | |
| 761 | TEST_F(zmq_pub, JsonTxpoolWeakPtrSkip) |
| 762 | { |
| 763 | static constexpr const char topic[] = "\1json"; |
| 764 | |
| 765 | ASSERT_TRUE(sub_request(topic)); |
| 766 | |
| 767 | std::vector<cryptonote::txpool_event> events |
| 768 | { |
| 769 | {make_transaction(), {}, true}, {make_transaction(), {}, true} |
| 770 | }; |
| 771 | |
| 772 | pub.reset(); |
| 773 | EXPECT_NO_THROW(cryptonote::listener::zmq_pub::txpool_add{pub}(std::move(events))); |
| 774 | } |
| 775 | |
| 776 | TEST_F(zmq_server, pub) |
| 777 | { |
| 778 | subscribe("json-minimal"); |
| 779 | |
| 780 | std::vector<cryptonote::txpool_event> events |
| 781 | { |
| 782 | {make_transaction(), {}, true}, {make_transaction(), {}, true} |
| 783 | }; |
| 784 | |
| 785 | const std::array<cryptonote::block, 1> blocks{{make_block()}}; |
| 786 | |
| 787 | ASSERT_EQ(1u, pub->send_txpool_add(events)); |
| 788 | ASSERT_EQ(1u, pub->send_chain_main(200, epee::to_span(blocks))); |
| 789 | |
| 790 | auto pubs = get_published(sub.get(), 2); |
| 791 | EXPECT_EQ(2u, pubs.size()); |
| 792 | ASSERT_LE(2u, pubs.size()); |
| 793 | EXPECT_TRUE(compare_minimal_txpool(epee::to_span(events), pubs.front())); |
| 794 | EXPECT_TRUE(compare_minimal_block(200, epee::to_span(blocks), pubs.back())); |
| 795 | } |