From 0418bfee307b3f1d7633d6ce3257022007e9b71b Mon Sep 17 00:00:00 2001 From: Some Random Crypto Guy Date: Thu, 27 Mar 2025 11:08:33 +0000 Subject: [PATCH] fixed asio-related tests --- tests/core_tests/transaction_tests.cpp | 8 +- tests/fuzz/levin.cpp | 10 +- tests/net_load_tests/srv.cpp | 2 +- tests/unit_tests/epee_boosted_tcp_server.cpp | 39 ++-- .../epee_levin_protocol_handler_async.cpp | 8 +- tests/unit_tests/levin.cpp | 202 +++++++++++++----- 6 files changed, 179 insertions(+), 90 deletions(-) diff --git a/tests/core_tests/transaction_tests.cpp b/tests/core_tests/transaction_tests.cpp index d853ec7..fba282d 100644 --- a/tests/core_tests/transaction_tests.cpp +++ b/tests/core_tests/transaction_tests.cpp @@ -52,9 +52,9 @@ bool test_genesis_tx() std::cout << "Miner account address:" << std::endl; std::cout << cryptonote::get_account_address_as_str((network_type)nettype, false, miner_acc1.get_keys().m_account_address); std::cout << std::endl << "Miner spend secret key:" << std::endl; - epee::to_hex::formatted(std::cout, epee::as_byte_span(miner_acc1.get_keys().m_spend_secret_key)); + epee::to_hex::formatted(std::cout, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_spend_secret_key)))); std::cout << std::endl << "Miner view secret key:" << std::endl; - epee::to_hex::formatted(std::cout, epee::as_byte_span(miner_acc1.get_keys().m_view_secret_key)); + epee::to_hex::formatted(std::cout, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_view_secret_key)))); std::cout << std::endl << std::endl; //Create file with miner keys information @@ -68,9 +68,9 @@ bool test_genesis_tx() miner_key_file << "Miner account address:" << std::endl; miner_key_file << cryptonote::get_account_address_as_str((network_type)nettype, false, miner_acc1.get_keys().m_account_address); miner_key_file << std::endl<< "Miner spend secret key:" << std::endl; - epee::to_hex::formatted(miner_key_file, epee::as_byte_span(miner_acc1.get_keys().m_spend_secret_key)); + epee::to_hex::formatted(miner_key_file, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_spend_secret_key)))); miner_key_file << std::endl << "Miner view secret key:" << std::endl; - epee::to_hex::formatted(miner_key_file, epee::as_byte_span(miner_acc1.get_keys().m_view_secret_key)); + epee::to_hex::formatted(miner_key_file, epee::as_byte_span(unwrap(unwrap(miner_acc1.get_keys().m_view_secret_key)))); miner_key_file << std::endl << std::endl; miner_key_file.close(); diff --git a/tests/fuzz/levin.cpp b/tests/fuzz/levin.cpp index d86f6b9..4c30980 100644 --- a/tests/fuzz/levin.cpp +++ b/tests/fuzz/levin.cpp @@ -138,7 +138,7 @@ namespace class test_connection : public epee::net_utils::i_service_endpoint { public: - test_connection(boost::asio::io_service& io_service, test_levin_protocol_handler_config& protocol_config) + test_connection(boost::asio::io_context& io_service, test_levin_protocol_handler_config& protocol_config) : m_io_service(io_service) , m_protocol_handler(this, protocol_config, m_context) , m_send_return(true) @@ -163,7 +163,7 @@ namespace virtual bool send_done() { return true; } virtual bool call_run_once_service_io() { return true; } virtual bool request_callback() { return true; } - virtual boost::asio::io_service& get_io_service() { return m_io_service; } + virtual boost::asio::io_context& get_io_context() { return m_io_service; } virtual bool add_ref() { return true; } virtual bool release() { return true; } @@ -180,7 +180,7 @@ namespace test_levin_protocol_handler m_protocol_handler; private: - boost::asio::io_service& m_io_service; + boost::asio::io_context& m_io_service; call_counter m_send_counter; boost::mutex m_mutex; @@ -224,7 +224,7 @@ namespace } protected: - boost::asio::io_service m_io_service; + boost::asio::io_context m_io_service; test_levin_protocol_handler_config m_handler_config; test_levin_commands_handler *m_pcommands_handler, &m_commands_handler; }; @@ -300,7 +300,7 @@ BEGIN_SIMPLE_FUZZER() fclose(f); #endif //std::unique_ptr conn = new test(); - boost::asio::io_service io_service; + boost::asio::io_context io_service; test_levin_protocol_handler_config m_handler_config; test_levin_commands_handler *m_pcommands_handler = new test_levin_commands_handler(); m_handler_config.set_handler(m_pcommands_handler, [](epee::levin::levin_commands_handler *handler) { delete handler; }); diff --git a/tests/net_load_tests/srv.cpp b/tests/net_load_tests/srv.cpp index 1fd2224..de06812 100644 --- a/tests/net_load_tests/srv.cpp +++ b/tests/net_load_tests/srv.cpp @@ -189,7 +189,7 @@ namespace if (0 < count) { // Perhaps not all connections were closed, try to close it after 7 seconds - boost::shared_ptr sh_deadline(new boost::asio::deadline_timer(m_tcp_server.get_io_service(), boost::posix_time::seconds(7))); + boost::shared_ptr sh_deadline(new boost::asio::deadline_timer(m_tcp_server.get_io_context(), boost::posix_time::seconds(7))); sh_deadline->async_wait([=](const boost::system::error_code& ec) { boost::shared_ptr t = sh_deadline; // Capture sh_deadline diff --git a/tests/unit_tests/epee_boosted_tcp_server.cpp b/tests/unit_tests/epee_boosted_tcp_server.cpp index c08a86a..c561741 100644 --- a/tests/unit_tests/epee_boosted_tcp_server.cpp +++ b/tests/unit_tests/epee_boosted_tcp_server.cpp @@ -28,6 +28,7 @@ // // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers +#include #include #include #include @@ -173,9 +174,9 @@ TEST(test_epee_connection, test_lifetime) using shared_states_t = std::vector; using tag_t = boost::uuids::uuid; using tags_t = std::vector; - using io_context_t = boost::asio::io_service; + using io_context_t = boost::asio::io_context; using endpoint_t = boost::asio::ip::tcp::endpoint; - using work_t = boost::asio::io_service::work; + using work_t = boost::asio::executor_work_guard; using work_ptr = std::shared_ptr; using workers_t = std::vector; using server_t = epee::net_utils::boosted_tcp_server; @@ -189,7 +190,7 @@ TEST(test_epee_connection, test_lifetime) using shared_conn_ptr = std::shared_ptr; io_context_t io_context; - work_ptr work(std::make_shared(io_context)); + work_ptr work(std::make_shared(io_context.get_executor())); workers_t workers; while (workers.size() < 4) { @@ -198,7 +199,7 @@ TEST(test_epee_connection, test_lifetime) }); } - endpoint_t endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 5262); + endpoint_t endpoint(boost::asio::ip::make_address("127.0.0.1"), 5262); server_t server(epee::net_utils::e_connection_type_P2P); server.init_server(endpoint.port(), endpoint.address().to_string(), @@ -211,7 +212,7 @@ TEST(test_epee_connection, test_lifetime) server.run_server(2, false); server.get_config_shared()->set_handler(new command_handler_t, &command_handler_t::destroy); - io_context.post([&io_context, &work, &endpoint, &server]{ + boost::asio::post(io_context, [&io_context, &work, &endpoint, &server]{ auto scope_exit_handler = epee::misc_utils::create_scope_leave_handler([&work]{ work.reset(); }); @@ -377,9 +378,7 @@ TEST(test_epee_connection, test_lifetime) connection_ptr conn(new connection_t(io_context, s, {}, {})); conn->socket().connect(endpoint); conn->start({}, {}); - io_context.post([conn]{ - conn->cancel(); - }); + boost::asio::post(io_context, [conn] { conn->cancel(); }); conn.reset(); s->del_out_connections(1); while (s->sock_count); @@ -449,9 +448,7 @@ TEST(test_epee_connection, test_lifetime) context_t context; conn->get_context(context); auto tag = context.m_connection_id; - io_context.post([conn]{ - conn->cancel(); - }); + boost::asio::post(io_context, [conn] { conn->cancel(); }); conn.reset(); s->close(tag); while (s->sock_count); @@ -494,7 +491,7 @@ TEST(test_epee_connection, ssl_shutdown) }; using handler_t = epee::levin::async_protocol_handler; - using io_context_t = boost::asio::io_service; + using io_context_t = boost::asio::io_context; using endpoint_t = boost::asio::ip::tcp::endpoint; using server_t = epee::net_utils::boosted_tcp_server; using socket_t = boost::asio::ip::tcp::socket; @@ -502,7 +499,7 @@ TEST(test_epee_connection, ssl_shutdown) using ssl_context_t = boost::asio::ssl::context; using ec_t = boost::system::error_code; - endpoint_t endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 5263); + endpoint_t endpoint(boost::asio::ip::make_address("127.0.0.1"), 5263); server_t server(epee::net_utils::e_connection_type_P2P); server.init_server(endpoint.port(), endpoint.address().to_string(), @@ -537,8 +534,8 @@ TEST(test_epee_connection, ssl_shutdown) TEST(test_epee_connection, ssl_handshake) { - using io_context_t = boost::asio::io_service; - using work_t = boost::asio::io_service::work; + using io_context_t = boost::asio::io_context; + using work_t = boost::asio::executor_work_guard; using work_ptr = std::shared_ptr; using workers_t = std::vector; using socket_t = boost::asio::ip::tcp::socket; @@ -546,7 +543,7 @@ TEST(test_epee_connection, ssl_handshake) using ssl_socket_ptr = std::unique_ptr; using ssl_options_t = epee::net_utils::ssl_options_t; io_context_t io_context; - work_ptr work(std::make_shared(io_context)); + work_ptr work(std::make_shared(io_context.get_executor())); workers_t workers; auto constexpr N = 2; while (workers.size() < N) { @@ -560,12 +557,14 @@ TEST(test_epee_connection, ssl_handshake) ssl_socket_ptr ssl_socket(new ssl_socket_t(io_context, ssl_context)); ssl_socket->next_layer().open(boost::asio::ip::tcp::v4()); for (size_t i = 0; i < N; ++i) { - io_context.post([]{ - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - }); + boost::asio::post( + io_context, + [] { std::this_thread::sleep_for(std::chrono::milliseconds(50)); } + ); } EXPECT_EQ( ssl_options.handshake( + io_context, *ssl_socket, ssl_socket_t::server, {}, @@ -670,7 +669,7 @@ TEST(boosted_tcp_server, strand_deadlock) using server_t = epee::net_utils::boosted_tcp_server; using endpoint_t = boost::asio::ip::tcp::endpoint; - endpoint_t endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 5262); + endpoint_t endpoint(boost::asio::ip::make_address("127.0.0.1"), 5262); server_t server(epee::net_utils::e_connection_type_P2P); server.init_server( endpoint.port(), diff --git a/tests/unit_tests/epee_levin_protocol_handler_async.cpp b/tests/unit_tests/epee_levin_protocol_handler_async.cpp index 38a1869..9dbb2c1 100644 --- a/tests/unit_tests/epee_levin_protocol_handler_async.cpp +++ b/tests/unit_tests/epee_levin_protocol_handler_async.cpp @@ -129,7 +129,7 @@ namespace class test_connection : public epee::net_utils::i_service_endpoint { public: - test_connection(boost::asio::io_service& io_service, test_levin_protocol_handler_config& protocol_config) + test_connection(boost::asio::io_context& io_service, test_levin_protocol_handler_config& protocol_config) : m_io_service(io_service) , m_protocol_handler(this, protocol_config, m_context) , m_send_return(true) @@ -155,7 +155,7 @@ namespace virtual bool send_done() { /*std::cout << "test_connection::send_done()" << std::endl; */return true; } virtual bool call_run_once_service_io() { std::cout << "test_connection::call_run_once_service_io()" << std::endl; return true; } virtual bool request_callback() { std::cout << "test_connection::request_callback()" << std::endl; return true; } - virtual boost::asio::io_service& get_io_service() { std::cout << "test_connection::get_io_service()" << std::endl; return m_io_service; } + virtual boost::asio::io_context& get_io_context() { std::cout << "test_connection::get_io_context()" << std::endl; return m_io_service; } virtual bool add_ref() { std::cout << "test_connection::add_ref()" << std::endl; return true; } virtual bool release() { std::cout << "test_connection::release()" << std::endl; return true; } @@ -171,7 +171,7 @@ namespace test_levin_protocol_handler m_protocol_handler; private: - boost::asio::io_service& m_io_service; + boost::asio::io_context& m_io_service; test_levin_connection_context m_context; unit_test::call_counter m_send_counter; @@ -216,7 +216,7 @@ namespace } protected: - boost::asio::io_service m_io_service; + boost::asio::io_context m_io_service; test_levin_protocol_handler_config m_handler_config; test_levin_commands_handler *m_pcommands_handler, &m_commands_handler; }; diff --git a/tests/unit_tests/levin.cpp b/tests/unit_tests/levin.cpp index 416c14b..25b6790 100644 --- a/tests/unit_tests/levin.cpp +++ b/tests/unit_tests/levin.cpp @@ -54,7 +54,7 @@ namespace { class test_endpoint final : public epee::net_utils::i_service_endpoint { - boost::asio::io_service& io_service_; + boost::asio::io_context& io_service_; std::size_t ref_count_; virtual bool do_send(epee::byte_slice message) override final @@ -83,7 +83,7 @@ namespace throw std::logic_error{"request_callback not implemented"}; } - virtual boost::asio::io_service& get_io_service() override final + virtual boost::asio::io_context& get_io_context() override final { return io_service_; } @@ -101,7 +101,7 @@ namespace } public: - test_endpoint(boost::asio::io_service& io_service) + test_endpoint(boost::asio::io_context& io_service) : epee::net_utils::i_service_endpoint(), io_service_(io_service), ref_count_(0), @@ -171,7 +171,7 @@ namespace epee::levin::async_protocol_handler handler_; public: - test_connection(boost::asio::io_service& io_service, cryptonote::levin::connections& connections, boost::uuids::random_generator& random_generator, const bool is_incoming) + test_connection(boost::asio::io_context& io_service, cryptonote::levin::connections& connections, boost::uuids::random_generator& random_generator, const bool is_incoming) : endpoint_(io_service), context_(), handler_(std::addressof(endpoint_), connections, context_) @@ -364,7 +364,7 @@ namespace } boost::uuids::random_generator random_generator_; - boost::asio::io_service io_service_; + boost::asio::io_context io_service_; test_receiver receiver_; std::deque contexts_; test_core_events events_; @@ -591,6 +591,7 @@ TEST_F(levin_notify, defaulted) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_FALSE(status.has_outgoing); } EXPECT_TRUE(notifier.send_txs({}, random_generator_(), cryptonote::relay_method::local)); @@ -611,6 +612,7 @@ TEST_F(levin_notify, fluff_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -624,7 +626,7 @@ TEST_F(levin_notify, fluff_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::fluff)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); ASSERT_LT(0u, io_service_.poll()); @@ -658,6 +660,7 @@ TEST_F(levin_notify, stem_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -677,7 +680,7 @@ TEST_F(levin_notify, stem_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); const bool is_stem = events_.has_stem_txes(); EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff)); @@ -731,6 +734,7 @@ TEST_F(levin_notify, stem_no_outs_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_FALSE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -747,7 +751,7 @@ TEST_F(levin_notify, stem_no_outs_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); if (events_.has_stem_txes()) @@ -788,6 +792,7 @@ TEST_F(levin_notify, local_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -815,7 +820,7 @@ TEST_F(levin_notify, local_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(their_txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); const bool is_stem = events_.has_stem_txes(); EXPECT_EQ(their_txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff)); @@ -855,7 +860,7 @@ TEST_F(levin_notify, local_without_padding) context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(my_txs, context->get_id(), cryptonote::relay_method::local)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_TRUE(events_.has_stem_txes()); EXPECT_EQ(my_txs, events_.take_relayed(cryptonote::relay_method::stem)); @@ -897,6 +902,7 @@ TEST_F(levin_notify, forward_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -916,7 +922,7 @@ TEST_F(levin_notify, forward_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::forward)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); const bool is_stem = events_.has_stem_txes(); EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff)); @@ -970,6 +976,7 @@ TEST_F(levin_notify, block_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -983,7 +990,7 @@ TEST_F(levin_notify, block_without_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::block)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1000,6 +1007,7 @@ TEST_F(levin_notify, none_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1013,7 +1021,7 @@ TEST_F(levin_notify, none_without_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::none)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1030,6 +1038,7 @@ TEST_F(levin_notify, fluff_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1043,7 +1052,7 @@ TEST_F(levin_notify, fluff_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::fluff)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); ASSERT_LT(0u, io_service_.poll()); @@ -1077,6 +1086,7 @@ TEST_F(levin_notify, stem_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1093,7 +1103,7 @@ TEST_F(levin_notify, stem_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); const bool is_stem = events_.has_stem_txes(); EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff)); @@ -1145,6 +1155,7 @@ TEST_F(levin_notify, stem_no_outs_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_FALSE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1161,7 +1172,7 @@ TEST_F(levin_notify, stem_no_outs_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); if (events_.has_stem_txes()) @@ -1202,6 +1213,7 @@ TEST_F(levin_notify, local_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1223,7 +1235,7 @@ TEST_F(levin_notify, local_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(their_txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); const bool is_stem = events_.has_stem_txes(); EXPECT_EQ(their_txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff)); @@ -1261,7 +1273,7 @@ TEST_F(levin_notify, local_with_padding) context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(my_txs, context->get_id(), cryptonote::relay_method::local)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_TRUE(events_.has_stem_txes()); EXPECT_EQ(my_txs, events_.take_relayed(cryptonote::relay_method::stem)); @@ -1303,6 +1315,7 @@ TEST_F(levin_notify, forward_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1319,7 +1332,7 @@ TEST_F(levin_notify, forward_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::forward)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); const bool is_stem = events_.has_stem_txes(); EXPECT_EQ(txs, events_.take_relayed(is_stem ? cryptonote::relay_method::stem : cryptonote::relay_method::fluff)); @@ -1371,6 +1384,7 @@ TEST_F(levin_notify, block_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1384,7 +1398,7 @@ TEST_F(levin_notify, block_with_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::block)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1401,6 +1415,7 @@ TEST_F(levin_notify, none_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1414,7 +1429,7 @@ TEST_F(levin_notify, none_with_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::none)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1431,6 +1446,7 @@ TEST_F(levin_notify, private_fluff_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1444,10 +1460,10 @@ TEST_F(levin_notify, private_fluff_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::fluff)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); @@ -1483,6 +1499,7 @@ TEST_F(levin_notify, private_stem_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1496,10 +1513,10 @@ TEST_F(levin_notify, private_stem_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::stem)); @@ -1535,6 +1552,7 @@ TEST_F(levin_notify, private_local_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1548,10 +1566,10 @@ TEST_F(levin_notify, private_local_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::local)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::local)); @@ -1587,6 +1605,7 @@ TEST_F(levin_notify, private_forward_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1600,10 +1619,10 @@ TEST_F(levin_notify, private_forward_without_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::forward)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::forward)); @@ -1639,6 +1658,7 @@ TEST_F(levin_notify, private_block_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1652,7 +1672,7 @@ TEST_F(levin_notify, private_block_without_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::block)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1670,6 +1690,7 @@ TEST_F(levin_notify, private_none_without_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1683,7 +1704,7 @@ TEST_F(levin_notify, private_none_without_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::none)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1700,6 +1721,7 @@ TEST_F(levin_notify, private_fluff_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1713,10 +1735,10 @@ TEST_F(levin_notify, private_fluff_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::fluff)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); @@ -1751,6 +1773,7 @@ TEST_F(levin_notify, private_stem_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1764,10 +1787,10 @@ TEST_F(levin_notify, private_stem_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::stem)); @@ -1802,6 +1825,7 @@ TEST_F(levin_notify, private_local_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1815,10 +1839,10 @@ TEST_F(levin_notify, private_local_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::local)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::local)); @@ -1853,6 +1877,7 @@ TEST_F(levin_notify, private_forward_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1866,10 +1891,10 @@ TEST_F(levin_notify, private_forward_with_padding) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::forward)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::forward)); @@ -1904,6 +1929,7 @@ TEST_F(levin_notify, private_block_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1917,7 +1943,7 @@ TEST_F(levin_notify, private_block_with_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::block)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1934,6 +1960,7 @@ TEST_F(levin_notify, private_none_with_padding) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1947,7 +1974,7 @@ TEST_F(levin_notify, private_none_with_padding) auto context = contexts_.begin(); EXPECT_FALSE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::none)); - io_service_.reset(); + io_service_.restart(); ASSERT_EQ(0u, io_service_.poll()); } } @@ -1966,6 +1993,7 @@ TEST_F(levin_notify, stem_mappings) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -1980,14 +2008,14 @@ TEST_F(levin_notify, stem_mappings) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); if (events_.has_stem_txes()) break; EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(0u, context->process_send_queue()); @@ -2004,7 +2032,7 @@ TEST_F(levin_notify, stem_mappings) } notifier.run_epoch(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); } EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::stem)); @@ -2042,7 +2070,7 @@ TEST_F(levin_notify, stem_mappings) auto& incoming = contexts_[i % contexts_.size()]; EXPECT_TRUE(notifier.send_txs(txs, incoming.get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::stem)); @@ -2090,6 +2118,7 @@ TEST_F(levin_notify, fluff_multiple) const auto status = notifier.get_status(); EXPECT_FALSE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.new_out_connection(); io_service_.poll(); @@ -2104,7 +2133,7 @@ TEST_F(levin_notify, fluff_multiple) auto context = contexts_.begin(); EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); if (!events_.has_stem_txes()) break; @@ -2135,12 +2164,12 @@ TEST_F(levin_notify, fluff_multiple) } notifier.run_epoch(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); } EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); { auto context = contexts_.begin(); @@ -2163,10 +2192,10 @@ TEST_F(levin_notify, fluff_multiple) auto& incoming = contexts_[i % contexts_.size()]; EXPECT_TRUE(notifier.send_txs(txs, incoming.get_id(), cryptonote::relay_method::stem)); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); notifier.run_fluff(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); @@ -2190,6 +2219,63 @@ TEST_F(levin_notify, fluff_multiple) } } +TEST_F(levin_notify, fluff_with_duplicate) +{ + std::shared_ptr notifier_ptr = make_notifier(0, true, false); + auto ¬ifier = *notifier_ptr; + + for (unsigned count = 0; count < 10; ++count) + add_connection(count % 2 == 0); + + { + const auto status = notifier.get_status(); + EXPECT_FALSE(status.has_noise); + EXPECT_FALSE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); + } + notifier.new_out_connection(); + io_service_.poll(); + + std::vector txs(9); + txs[0].resize(100, 'e'); + txs[1].resize(100, 'e'); + txs[2].resize(100, 'e'); + txs[3].resize(100, 'e'); + txs[4].resize(200, 'f'); + txs[5].resize(200, 'f'); + txs[6].resize(200, 'f'); + txs[7].resize(200, 'f'); + txs[8].resize(200, 'f'); + + ASSERT_EQ(10u, contexts_.size()); + { + auto context = contexts_.begin(); + EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), cryptonote::relay_method::fluff)); + + io_service_.restart(); + ASSERT_LT(0u, io_service_.poll()); + notifier.run_fluff(); + ASSERT_LT(0u, io_service_.poll()); + + EXPECT_EQ(0u, context->process_send_queue()); + for (++context; context != contexts_.end(); ++context) + EXPECT_EQ(1u, context->process_send_queue()); + + EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); + std::sort(txs.begin(), txs.end()); + ASSERT_EQ(9u, receiver_.notified_size()); + for (unsigned count = 0; count < 9; ++count) + { + auto notification = receiver_.get_notification().second; + EXPECT_NE(txs, notification.txs); + EXPECT_EQ(notification.txs.size(), 2); + EXPECT_TRUE(notification._.empty()); + EXPECT_TRUE(notification.dandelionpp_fluff); + } + } + +} + TEST_F(levin_notify, noise) { for (unsigned count = 0; count < 10; ++count) @@ -2206,16 +2292,18 @@ TEST_F(levin_notify, noise) const auto status = notifier.get_status(); EXPECT_TRUE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_FALSE(status.has_outgoing); } ASSERT_LT(0u, io_service_.poll()); { const auto status = notifier.get_status(); EXPECT_TRUE(status.has_noise); EXPECT_TRUE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.run_stems(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); { std::size_t sent = 0; @@ -2228,7 +2316,7 @@ TEST_F(levin_notify, noise) EXPECT_TRUE(notifier.send_txs(txs, incoming_id, cryptonote::relay_method::local)); notifier.run_stems(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::local)); @@ -2250,7 +2338,7 @@ TEST_F(levin_notify, noise) txs[0].resize(3000, 'r'); EXPECT_TRUE(notifier.send_txs(txs, incoming_id, cryptonote::relay_method::fluff)); notifier.run_stems(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); EXPECT_EQ(txs, events_.take_relayed(cryptonote::relay_method::fluff)); @@ -2264,7 +2352,7 @@ TEST_F(levin_notify, noise) } notifier.run_stems(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); { std::size_t sent = 0; @@ -2298,16 +2386,18 @@ TEST_F(levin_notify, noise_stem) const auto status = notifier.get_status(); EXPECT_TRUE(status.has_noise); EXPECT_FALSE(status.connections_filled); + EXPECT_FALSE(status.has_outgoing); } ASSERT_LT(0u, io_service_.poll()); { const auto status = notifier.get_status(); EXPECT_TRUE(status.has_noise); EXPECT_TRUE(status.connections_filled); + EXPECT_TRUE(status.has_outgoing); } notifier.run_stems(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); { std::size_t sent = 0; @@ -2320,7 +2410,7 @@ TEST_F(levin_notify, noise_stem) EXPECT_TRUE(notifier.send_txs(txs, incoming_id, cryptonote::relay_method::stem)); notifier.run_stems(); - io_service_.reset(); + io_service_.restart(); ASSERT_LT(0u, io_service_.poll()); // downgraded to local when being notified