diff --git a/CMakeLists.txt b/CMakeLists.txt index aa7dc3bb..0c893171 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -442,6 +442,7 @@ if (NANO_TEST OR RAIBLOCKS_TEST) set_target_properties(gtest PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/gtest/googletest/include") + add_subdirectory(nano/test_common) add_subdirectory(nano/core_test) add_subdirectory(nano/rpc_test) add_subdirectory(nano/slow_test) @@ -526,7 +527,7 @@ if (NANO_GUI OR RAIBLOCKS_GUI) nano/qt_test/entry.cpp nano/qt_test/qt.cpp) - target_link_libraries(qt_test gtest gtest_main qt Qt5::Test) + target_link_libraries(qt_test test_common gtest gtest_main qt Qt5::Test) set_target_properties (qt_test PROPERTIES COMPILE_FLAGS "-DQT_NO_KEYWORDS -DBOOST_ASIO_HAS_STD_ARRAY=1") endif () diff --git a/nano/core_test/CMakeLists.txt b/nano/core_test/CMakeLists.txt index 2e22f329..2535bd9f 100644 --- a/nano/core_test/CMakeLists.txt +++ b/nano/core_test/CMakeLists.txt @@ -1,6 +1,5 @@ add_executable (core_test core_test_main.cc - testutil.hpp fakes/websocket_client.hpp fakes/work_peer.hpp active_transactions.cpp @@ -8,14 +7,12 @@ add_executable (core_test block_store.cpp bootstrap.cpp cli.cpp - common.hpp confirmation_height.cpp confirmation_solicitor.cpp conflicts.cpp difficulty.cpp distributed_work.cpp election.cpp - entry.cpp epochs.cpp gap_cache.cpp ipc.cpp @@ -51,4 +48,4 @@ target_compile_definitions(core_test -DBOOST_PROCESS_SUPPORTED=${BOOST_PROCESS_SUPPORTED} -DCI=${CI_TEST}) -target_link_libraries (core_test node secure gtest libminiupnpc-static Boost::log_setup Boost::log Boost::boost) +target_link_libraries (core_test node secure test_common gtest libminiupnpc-static Boost::log_setup Boost::log Boost::boost) diff --git a/nano/core_test/active_transactions.cpp b/nano/core_test/active_transactions.cpp index 0b381bd6..33a4a2a4 100644 --- a/nano/core_test/active_transactions.cpp +++ b/nano/core_test/active_transactions.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include diff --git a/nano/core_test/block.cpp b/nano/core_test/block.cpp index e6360be0..4795a79f 100644 --- a/nano/core_test/block.cpp +++ b/nano/core_test/block.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include diff --git a/nano/core_test/block_store.cpp b/nano/core_test/block_store.cpp index b5eedb86..4fcffd04 100644 --- a/nano/core_test/block_store.cpp +++ b/nano/core_test/block_store.cpp @@ -1,4 +1,3 @@ -#include #include #include #include @@ -8,6 +7,7 @@ #include #include #include +#include #include diff --git a/nano/core_test/bootstrap.cpp b/nano/core_test/bootstrap.cpp index e32b6498..36ab5744 100644 --- a/nano/core_test/bootstrap.cpp +++ b/nano/core_test/bootstrap.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include diff --git a/nano/core_test/cli.cpp b/nano/core_test/cli.cpp index eda13af6..e5388fc3 100644 --- a/nano/core_test/cli.cpp +++ b/nano/core_test/cli.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include diff --git a/nano/core_test/common.hpp b/nano/core_test/common.hpp deleted file mode 100644 index a4d33282..00000000 --- a/nano/core_test/common.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include -#include - -#include - -#include - -using namespace std::chrono_literals; - -namespace nano -{ -inline void wait_peer_connections (nano::system & system_a) -{ - auto wait_peer_count = [&system_a](bool in_memory) { - auto num_nodes = system_a.nodes.size (); - system_a.deadline_set (20s); - size_t peer_count = 0; - while (peer_count != num_nodes * (num_nodes - 1)) - { - ASSERT_NO_ERROR (system_a.poll ()); - peer_count = std::accumulate (system_a.nodes.cbegin (), system_a.nodes.cend (), std::size_t{ 0 }, [in_memory](auto total, auto const & node) { - if (in_memory) - { - return total += node->network.size (); - } - else - { - auto transaction = node->store.tx_begin_read (); - return total += node->store.peer_count (transaction); - } - }); - } - }; - - // Do a pre-pass with in-memory containers to reduce IO if still in the process of connecting to peers - wait_peer_count (true); - wait_peer_count (false); -} -} diff --git a/nano/core_test/confirmation_height.cpp b/nano/core_test/confirmation_height.cpp index 67757730..9bae54bd 100644 --- a/nano/core_test/confirmation_height.cpp +++ b/nano/core_test/confirmation_height.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include diff --git a/nano/core_test/confirmation_solicitor.cpp b/nano/core_test/confirmation_solicitor.cpp index 1d108712..36b6b38c 100644 --- a/nano/core_test/confirmation_solicitor.cpp +++ b/nano/core_test/confirmation_solicitor.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include diff --git a/nano/core_test/conflicts.cpp b/nano/core_test/conflicts.cpp index 8c84bad5..94245e33 100644 --- a/nano/core_test/conflicts.cpp +++ b/nano/core_test/conflicts.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include diff --git a/nano/core_test/difficulty.cpp b/nano/core_test/difficulty.cpp index 1a987ec3..61affc0b 100644 --- a/nano/core_test/difficulty.cpp +++ b/nano/core_test/difficulty.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include diff --git a/nano/core_test/distributed_work.cpp b/nano/core_test/distributed_work.cpp index c5ecfb87..f38dbdf0 100644 --- a/nano/core_test/distributed_work.cpp +++ b/nano/core_test/distributed_work.cpp @@ -1,6 +1,6 @@ #include -#include #include +#include #include diff --git a/nano/core_test/election.cpp b/nano/core_test/election.cpp index b112aab5..e47e7946 100644 --- a/nano/core_test/election.cpp +++ b/nano/core_test/election.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include diff --git a/nano/core_test/gap_cache.cpp b/nano/core_test/gap_cache.cpp index 2e5cf321..bd135408 100644 --- a/nano/core_test/gap_cache.cpp +++ b/nano/core_test/gap_cache.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include diff --git a/nano/core_test/ipc.cpp b/nano/core_test/ipc.cpp index ce0487de..5092588d 100644 --- a/nano/core_test/ipc.cpp +++ b/nano/core_test/ipc.cpp @@ -1,10 +1,10 @@ -#include #include #include #include #include #include #include +#include #include diff --git a/nano/core_test/ledger.cpp b/nano/core_test/ledger.cpp index b99f1995..afd3eff1 100644 --- a/nano/core_test/ledger.cpp +++ b/nano/core_test/ledger.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include diff --git a/nano/core_test/locks.cpp b/nano/core_test/locks.cpp index bebb2dc2..cfcd51f1 100644 --- a/nano/core_test/locks.cpp +++ b/nano/core_test/locks.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include diff --git a/nano/core_test/logger.cpp b/nano/core_test/logger.cpp index 01d33736..8a769e04 100644 --- a/nano/core_test/logger.cpp +++ b/nano/core_test/logger.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include diff --git a/nano/core_test/message_parser.cpp b/nano/core_test/message_parser.cpp index a0649af8..b1e36436 100644 --- a/nano/core_test/message_parser.cpp +++ b/nano/core_test/message_parser.cpp @@ -1,4 +1,5 @@ #include +#include #include diff --git a/nano/core_test/network.cpp b/nano/core_test/network.cpp index 5e4ce894..4c34f693 100644 --- a/nano/core_test/network.cpp +++ b/nano/core_test/network.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include diff --git a/nano/core_test/network_filter.cpp b/nano/core_test/network_filter.cpp index ad54457d..861b1fc5 100644 --- a/nano/core_test/network_filter.cpp +++ b/nano/core_test/network_filter.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include diff --git a/nano/core_test/node.cpp b/nano/core_test/node.cpp index 8d3774dd..87598932 100644 --- a/nano/core_test/node.cpp +++ b/nano/core_test/node.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include @@ -3681,7 +3681,7 @@ TEST (node, dont_write_lock_node) write_lock_held_promise.get_future ().wait (); // Check inactive node can finish executing while a write lock is open - nano::inactive_node node (path); + nano::inactive_node node (path, nano::inactive_node_flag_defaults ()); finished_promise.set_value (); } diff --git a/nano/core_test/peer_container.cpp b/nano/core_test/peer_container.cpp index 738e2617..b60d2542 100644 --- a/nano/core_test/peer_container.cpp +++ b/nano/core_test/peer_container.cpp @@ -1,6 +1,5 @@ -#include -#include #include +#include #include diff --git a/nano/core_test/processor_service.cpp b/nano/core_test/processor_service.cpp index 85f5a584..17b9c468 100644 --- a/nano/core_test/processor_service.cpp +++ b/nano/core_test/processor_service.cpp @@ -1,11 +1,11 @@ #include -#include #include #include #include #include #include #include +#include #include diff --git a/nano/core_test/request_aggregator.cpp b/nano/core_test/request_aggregator.cpp index fa9a4df4..ee1599d7 100644 --- a/nano/core_test/request_aggregator.cpp +++ b/nano/core_test/request_aggregator.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include diff --git a/nano/core_test/socket.cpp b/nano/core_test/socket.cpp index b16552ee..24709ae2 100644 --- a/nano/core_test/socket.cpp +++ b/nano/core_test/socket.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include diff --git a/nano/core_test/telemetry.cpp b/nano/core_test/telemetry.cpp index 043d2f8d..0ebc5e3e 100644 --- a/nano/core_test/telemetry.cpp +++ b/nano/core_test/telemetry.cpp @@ -1,7 +1,7 @@ -#include -#include #include #include +#include +#include #include diff --git a/nano/core_test/timer.cpp b/nano/core_test/timer.cpp index 1aa29739..4cadb836 100644 --- a/nano/core_test/timer.cpp +++ b/nano/core_test/timer.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include diff --git a/nano/core_test/toml.cpp b/nano/core_test/toml.cpp index df5b2ca3..59017599 100644 --- a/nano/core_test/toml.cpp +++ b/nano/core_test/toml.cpp @@ -1,9 +1,9 @@ -#include #include #include #include #include -#include +#include +#include #include diff --git a/nano/core_test/uint256_union.cpp b/nano/core_test/uint256_union.cpp index eb8304d2..0e3996fc 100644 --- a/nano/core_test/uint256_union.cpp +++ b/nano/core_test/uint256_union.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index 1ffc13d2..856e8e9c 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index 344636d1..f57c2e50 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -1,8 +1,8 @@ -#include #include #include #include #include +#include #include diff --git a/nano/core_test/wallets.cpp b/nano/core_test/wallets.cpp index 7cb963a4..cdb857de 100644 --- a/nano/core_test/wallets.cpp +++ b/nano/core_test/wallets.cpp @@ -1,7 +1,6 @@ -#include -#include #include #include +#include #include @@ -108,7 +107,7 @@ TEST (wallets, reload) ASSERT_EQ (1, node1.wallets.items.size ()); { nano::lock_guard lock_wallet (node1.wallets.mutex); - nano::inactive_node node (node1.application_path); + nano::inactive_node node (node1.application_path, nano::inactive_node_flag_defaults ()); auto wallet (node.node->wallets.create (one)); ASSERT_NE (wallet, nullptr); } diff --git a/nano/core_test/websocket.cpp b/nano/core_test/websocket.cpp index fe5d4a0c..84db0e89 100644 --- a/nano/core_test/websocket.cpp +++ b/nano/core_test/websocket.cpp @@ -1,8 +1,8 @@ -#include #include -#include #include #include +#include +#include #include diff --git a/nano/lib/config.hpp b/nano/lib/config.hpp index 43c25973..21e2c186 100644 --- a/nano/lib/config.hpp +++ b/nano/lib/config.hpp @@ -209,4 +209,7 @@ void force_nano_test_network (); /** Checks if we are running inside a valgrind instance */ bool running_within_valgrind (); + +/** Set the active network to the test network */ +void force_nano_test_network (); } \ No newline at end of file diff --git a/nano/load_test/CMakeLists.txt b/nano/load_test/CMakeLists.txt index 90d54d7a..08f49e68 100644 --- a/nano/load_test/CMakeLists.txt +++ b/nano/load_test/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable (load_test entry.cpp) -target_link_libraries (load_test node secure Boost::boost) +target_link_libraries (load_test node secure test_common gtest Boost::boost) target_compile_definitions(load_test PRIVATE diff --git a/nano/load_test/entry.cpp b/nano/load_test/entry.cpp index 8299d7bb..f2a03d67 100644 --- a/nano/load_test/entry.cpp +++ b/nano/load_test/entry.cpp @@ -1,15 +1,15 @@ #include #include +#include +#include #include #include #include -#define IGNORE_GTEST_INCL -#include #include #include #include -#include #include +#include #include #include diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 0b2f8649..8eca92a1 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -6,7 +6,6 @@ #include #include #include -#include #include #include @@ -78,7 +77,6 @@ int main (int argc, char * const * argv) ("debug_dump_online_weight", "Dump online_weights table") ("debug_dump_representatives", "List representatives and weights") ("debug_account_count", "Display the number of accounts") - ("debug_mass_activity", "(Deprecated) Generates fake debug activity. Can use slow_test's generate_mass_activity test for the same behavior.") ("debug_profile_generate", "Profile work generation") ("debug_profile_validate", "Profile work validation") ("debug_opencl", "OpenCL work generation") @@ -439,12 +437,6 @@ int main (int argc, char * const * argv) nano::inactive_node inactive_node (data_path, node_flags); std::cout << boost::str (boost::format ("Frontier count: %1%\n") % inactive_node.node->ledger.cache.account_count); } - else if (vm.count ("debug_mass_activity")) - { - nano::system system (1); - uint32_t count (1000000); - system.generate_mass_activity (count, *system.nodes[0]); - } else if (vm.count ("debug_profile_kdf")) { nano::network_params network_params; @@ -910,14 +902,15 @@ int main (int argc, char * const * argv) size_t num_iterations (5); // 100,000 * 5 * 2 = 1,000,000 blocks size_t max_blocks (2 * num_accounts * num_iterations + num_accounts * 2); // 1,000,000 + 2 * 100,000 = 1,200,000 blocks std::cout << boost::str (boost::format ("Starting pregenerating %1% blocks\n") % max_blocks); - nano::system system; + boost::asio::io_context io_ctx; + nano::alarm alarm (io_ctx); nano::work_pool work (std::numeric_limits::max ()); nano::logging logging; auto path (nano::unique_path ()); logging.init (path); nano::node_flags node_flags; nano::update_flags (node_flags, vm); - auto node (std::make_shared (system.io_ctx, 24001, path, system.alarm, logging, work, node_flags)); + auto node (std::make_shared (io_ctx, 24001, path, alarm, logging, work, node_flags)); nano::block_hash genesis_latest (node->latest (test_params.ledger.test_genesis_key.pub)); nano::uint128_t genesis_balance (std::numeric_limits::max ()); // Generating keys @@ -1035,12 +1028,13 @@ int main (int argc, char * const * argv) size_t num_representatives (25); size_t max_votes (num_elections * num_representatives); // 40,000 * 25 = 1,000,000 votes std::cerr << boost::str (boost::format ("Starting pregenerating %1% votes\n") % max_votes); - nano::system system (1); + boost::asio::io_context io_ctx; + nano::alarm alarm (io_ctx); nano::work_pool work (std::numeric_limits::max ()); nano::logging logging; auto path (nano::unique_path ()); logging.init (path); - auto node (std::make_shared (system.io_ctx, 24001, path, system.alarm, logging, work)); + auto node (std::make_shared (io_ctx, 24001, path, alarm, logging, work)); nano::block_hash genesis_latest (node->latest (test_params.ledger.test_genesis_key.pub)); nano::uint128_t genesis_balance (std::numeric_limits::max ()); // Generating keys @@ -1741,7 +1735,7 @@ int main (int argc, char * const * argv) auto node_flags = nano::inactive_node_flag_defaults (); node_flags.read_only = false; nano::update_flags (node_flags, vm); - nano::inactive_node node2 (nano::unique_path (), node_flags); + nano::inactive_node node (nano::unique_path (), node_flags); nano::genesis genesis; auto begin (std::chrono::high_resolution_clock::now ()); uint64_t block_count (0); @@ -1769,7 +1763,7 @@ int main (int argc, char * const * argv) std::cout << boost::str (boost::format ("%1% blocks retrieved") % count) << std::endl; } nano::unchecked_info unchecked_info (block, account, 0, nano::signature_verification::unknown); - node2.node->block_processor.add (unchecked_info); + node->block_processor.add (unchecked_info); // Retrieving previous block hash hash = block->previous (); } @@ -1777,14 +1771,14 @@ int main (int argc, char * const * argv) } } nano::timer timer_l (nano::timer_state::started); - while (node2.node->ledger.cache.block_count != block_count) + while (node.node->ledger.cache.block_count != block_count) { std::this_thread::sleep_for (std::chrono::milliseconds (50)); // Message each 60 seconds if (timer_l.after_deadline (std::chrono::seconds (60))) { timer_l.restart (); - std::cout << boost::str (boost::format ("%1% (%2%) blocks processed (unchecked)") % node2.node->ledger.cache.block_count % node2.node->ledger.cache.unchecked_count) << std::endl; + std::cout << boost::str (boost::format ("%1% (%2%) blocks processed (unchecked)") % node.node->ledger.cache.block_count % node.node->ledger.cache.unchecked_count) << std::endl; } } // Waiting for final transaction commit @@ -1792,8 +1786,8 @@ int main (int argc, char * const * argv) while (block_count_2 != block_count) { std::this_thread::sleep_for (std::chrono::milliseconds (50)); - auto transaction_2 (node2.node->store.tx_begin_read ()); - block_count_2 = node2.node->store.block_count (transaction_2).sum (); + auto transaction_2 (node.node->store.tx_begin_read ()); + block_count_2 = node.node->store.block_count (transaction_2).sum (); } auto end (std::chrono::high_resolution_clock::now ()); auto time (std::chrono::duration_cast (end - begin).count ()); diff --git a/nano/node/node.cpp b/nano/node/node.cpp index 89e65c12..80b16e3e 100644 --- a/nano/node/node.cpp +++ b/nano/node/node.cpp @@ -1,5 +1,3 @@ -#define IGNORE_GTEST_INCL -#include #include #include #include @@ -7,6 +5,7 @@ #include #include #include +#include #include #include #include diff --git a/nano/node/node.hpp b/nano/node/node.hpp index f016eb9b..b89c8173 100644 --- a/nano/node/node.hpp +++ b/nano/node/node.hpp @@ -219,7 +219,7 @@ nano::node_flags const & inactive_node_flag_defaults (); class inactive_node final { public: - inactive_node (boost::filesystem::path const & path_a, nano::node_flags const & node_flags_a = nano::inactive_node_flag_defaults ()); + inactive_node (boost::filesystem::path const & path_a, nano::node_flags const & node_flags_a); ~inactive_node (); std::shared_ptr io_context; nano::alarm alarm; diff --git a/nano/node/testing.cpp b/nano/node/testing.cpp index 461abec1..d185779f 100644 --- a/nano/node/testing.cpp +++ b/nano/node/testing.cpp @@ -1,5 +1,3 @@ -#define IGNORE_GTEST_INCL -#include #include #include #include @@ -9,6 +7,8 @@ #include +using namespace std::chrono_literals; + std::string nano::error_system_messages::message (int ev) const { switch (static_cast (ev)) @@ -176,19 +176,20 @@ uint64_t nano::system::work_generate_limited (nano::block_hash const & root_a, u std::unique_ptr nano::upgrade_epoch (nano::work_pool & pool_a, nano::ledger & ledger_a, nano::epoch epoch_a) { auto transaction (ledger_a.store.tx_begin_write ()); - auto account = nano::test_genesis_key.pub; + auto test_genesis_key = nano::ledger_constants (nano::nano_networks::nano_test_network).test_genesis_key; + auto account = test_genesis_key.pub; auto latest = ledger_a.latest (transaction, account); auto balance = ledger_a.account_balance (transaction, account); nano::state_block_builder builder; std::error_code ec; auto epoch = builder - .account (nano::test_genesis_key.pub) + .account (test_genesis_key.pub) .previous (latest) .balance (balance) .link (ledger_a.epoch_link (epoch_a)) - .representative (nano::test_genesis_key.pub) - .sign (nano::test_genesis_key.prv, nano::test_genesis_key.pub) + .representative (test_genesis_key.pub) + .sign (test_genesis_key.prv, test_genesis_key.pub) .work (*pool_a.generate (latest, nano::work_threshold (nano::work_version::work_1, nano::block_details (epoch_a, false, false, true)))) .build (ec); @@ -339,7 +340,7 @@ void nano::system::generate_receive (nano::node & node_a) } if (send_block != nullptr) { - auto receive_error (wallet (0)->receive_sync (send_block, nano::genesis_account, std::numeric_limits::max ())); + auto receive_error (wallet (0)->receive_sync (send_block, nano::ledger_constants (nano::nano_networks::nano_test_network).genesis_account, std::numeric_limits::max ())); (void)receive_error; } } @@ -464,8 +465,9 @@ void nano::system::generate_send_new (nano::node & node_a, std::vector accounts; - wallet (0)->insert_adhoc (nano::test_genesis_key.prv); - accounts.push_back (nano::test_genesis_key.pub); + auto test_genesis_key = nano::ledger_constants (nano::nano_networks::nano_test_network).test_genesis_key; + wallet (0)->insert_adhoc (test_genesis_key.prv); + accounts.push_back (test_genesis_key.pub); auto previous (std::chrono::steady_clock::now ()); for (uint32_t i (0); i < count_a; ++i) { @@ -497,9 +499,31 @@ void nano::system::stop () work.stop (); } -namespace nano +uint16_t nano::get_available_port () { -void cleanup_test_directories_on_exit () + // Maximum possible sockets which may feasibly be used in 1 test + constexpr auto max = 200; + static uint16_t current = 0; + // Read the TEST_BASE_PORT environment and override the default base port if it exists + auto base_str = std::getenv ("TEST_BASE_PORT"); + uint16_t base_port = 24000; + if (base_str) + { + base_port = boost::lexical_cast (base_str); + } + + uint16_t const available_port = base_port + current; + ++current; + // Reset port number once we have reached the maximum + if (current == max) + { + current = 0; + } + + return available_port; +} + +void nano::cleanup_test_directories_on_exit () { // Makes sure everything is cleaned up nano::logging::release_file_sink (); @@ -511,4 +535,3 @@ void cleanup_test_directories_on_exit () nano::remove_temporary_directories (); } } -} diff --git a/nano/node/testing.hpp b/nano/node/testing.hpp index b981f033..4a607b9d 100644 --- a/nano/node/testing.hpp +++ b/nano/node/testing.hpp @@ -57,5 +57,7 @@ public: }; std::unique_ptr upgrade_epoch (nano::work_pool &, nano::ledger &, nano::epoch); void blocks_confirm (nano::node &, std::vector> const &); +uint16_t get_available_port (); +void cleanup_test_directories_on_exit (); } REGISTER_ERROR_CODES (nano, error_system); diff --git a/nano/node/vote_processor.hpp b/nano/node/vote_processor.hpp index c23eaf1f..a0d0577c 100644 --- a/nano/node/vote_processor.hpp +++ b/nano/node/vote_processor.hpp @@ -22,6 +22,7 @@ class logger_mt; class online_reps; class ledger; class network_params; +class node_flags; class transaction; namespace transport diff --git a/nano/qt_system/entry.cpp b/nano/qt_system/entry.cpp index 59b0c74e..28a7232f 100644 --- a/nano/qt_system/entry.cpp +++ b/nano/qt_system/entry.cpp @@ -8,10 +8,9 @@ #include #include - int main (int argc, char ** argv) { - nano::network_constants::set_active_network (nano::nano_networks::nano_test_network); + nano::force_nano_test_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; QApplication application (argc, argv); QCoreApplication::setOrganizationName ("Nano"); diff --git a/nano/qt_test/qt.cpp b/nano/qt_test/qt.cpp index 4fb19704..7d005d37 100644 --- a/nano/qt_test/qt.cpp +++ b/nano/qt_test/qt.cpp @@ -1,6 +1,6 @@ -#include #include #include +#include #include diff --git a/nano/rpc_test/CMakeLists.txt b/nano/rpc_test/CMakeLists.txt index 7a76a1b0..abbb1c28 100644 --- a/nano/rpc_test/CMakeLists.txt +++ b/nano/rpc_test/CMakeLists.txt @@ -2,7 +2,7 @@ add_executable (rpc_test entry.cpp rpc.cpp) -target_link_libraries(rpc_test gtest rpc node) +target_link_libraries(rpc_test node rpc test_common gtest) target_compile_definitions(rpc_test PUBLIC diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index cc58e0ad..083f0560 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -9,6 +8,8 @@ #include #include #include +#include +#include #include diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 0cea331e..a2a11e16 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -1,5 +1,3 @@ -#define IGNORE_GTEST_INCL -#include #include #include #include @@ -165,21 +163,6 @@ nano::bootstrap_constants::bootstrap_constants (nano::network_constants & networ gap_cache_bootstrap_start_interval = network_constants.is_test_network () ? std::chrono::milliseconds (5) : std::chrono::milliseconds (30 * 1000); } -/* Convenience constants for core_test which is always on the test network */ -namespace -{ -nano::ledger_constants test_constants (nano::nano_networks::nano_test_network); -} - -nano::keypair const & nano::zero_key (test_constants.zero_key); -nano::keypair const & nano::test_genesis_key (test_constants.test_genesis_key); -nano::account const & nano::nano_test_account (test_constants.nano_test_account); -std::string const & nano::nano_test_genesis (test_constants.nano_test_genesis); -nano::account const & nano::genesis_account (test_constants.genesis_account); -nano::block_hash const & nano::genesis_hash (test_constants.genesis_hash); -nano::uint128_t const & nano::genesis_amount (test_constants.genesis_amount); -nano::account const & nano::burn_account (test_constants.burn_account); - // Create a new random keypair nano::keypair::keypair () { diff --git a/nano/slow_test/CMakeLists.txt b/nano/slow_test/CMakeLists.txt index 0606eb1a..d1e19ec1 100644 --- a/nano/slow_test/CMakeLists.txt +++ b/nano/slow_test/CMakeLists.txt @@ -2,4 +2,4 @@ add_executable (slow_test entry.cpp node.cpp) -target_link_libraries (slow_test node secure gtest libminiupnpc-static Boost::boost) +target_link_libraries (slow_test secure node test_common gtest libminiupnpc-static Boost::boost) diff --git a/nano/slow_test/node.cpp b/nano/slow_test/node.cpp index 6f12d13c..a11604c9 100644 --- a/nano/slow_test/node.cpp +++ b/nano/slow_test/node.cpp @@ -1,10 +1,9 @@ -#include -#include #include #include #include #include #include +#include #include diff --git a/nano/test_common/CMakeLists.txt b/nano/test_common/CMakeLists.txt new file mode 100644 index 00000000..afd7596c --- /dev/null +++ b/nano/test_common/CMakeLists.txt @@ -0,0 +1,7 @@ +add_library (test_common + telemetry.hpp + telemetry.cpp + testutil.hpp + testutil.cpp) + +target_link_libraries (test_common node gtest Boost::log_setup Boost::log Boost::boost) diff --git a/nano/test_common/telemetry.cpp b/nano/test_common/telemetry.cpp new file mode 100644 index 00000000..ad09e77d --- /dev/null +++ b/nano/test_common/telemetry.cpp @@ -0,0 +1,36 @@ +#include +#include + +#include + +void nano::compare_default_telemetry_response_data_excluding_signature (nano::telemetry_data const & telemetry_data_a, nano::network_params const & network_params_a, uint64_t bandwidth_limit_a, uint64_t active_difficulty_a) +{ + ASSERT_EQ (telemetry_data_a.block_count, 1); + ASSERT_EQ (telemetry_data_a.cemented_count, 1); + ASSERT_EQ (telemetry_data_a.bandwidth_cap, bandwidth_limit_a); + ASSERT_EQ (telemetry_data_a.peer_count, 1); + ASSERT_EQ (telemetry_data_a.protocol_version, network_params_a.protocol.telemetry_protocol_version_min); + ASSERT_EQ (telemetry_data_a.unchecked_count, 0); + ASSERT_EQ (telemetry_data_a.account_count, 1); + ASSERT_LT (telemetry_data_a.uptime, 100); + ASSERT_EQ (telemetry_data_a.genesis_block, network_params_a.ledger.genesis_hash); + ASSERT_EQ (telemetry_data_a.major_version, nano::get_major_node_version ()); + ASSERT_EQ (telemetry_data_a.minor_version, nano::get_minor_node_version ()); + ASSERT_EQ (telemetry_data_a.patch_version, nano::get_patch_node_version ()); + ASSERT_EQ (telemetry_data_a.pre_release_version, nano::get_pre_release_node_version ()); + ASSERT_EQ (telemetry_data_a.maker, 0); + ASSERT_GT (telemetry_data_a.timestamp, std::chrono::system_clock::now () - std::chrono::seconds (100)); + ASSERT_EQ (telemetry_data_a.active_difficulty, active_difficulty_a); +} + +void nano::compare_default_telemetry_response_data (nano::telemetry_data const & telemetry_data_a, nano::network_params const & network_params_a, uint64_t bandwidth_limit_a, uint64_t active_difficulty_a, nano::keypair const & node_id_a) +{ + ASSERT_FALSE (telemetry_data_a.validate_signature (nano::telemetry_data::size)); + nano::telemetry_data telemetry_data_l = telemetry_data_a; + telemetry_data_l.signature.clear (); + telemetry_data_l.sign (node_id_a); + // Signature should be different because uptime/timestamp will have changed. + ASSERT_NE (telemetry_data_a.signature, telemetry_data_l.signature); + compare_default_telemetry_response_data_excluding_signature (telemetry_data_a, network_params_a, bandwidth_limit_a, active_difficulty_a); + ASSERT_EQ (telemetry_data_a.node_id, node_id_a.pub); +} diff --git a/nano/test_common/telemetry.hpp b/nano/test_common/telemetry.hpp new file mode 100644 index 00000000..16c0b080 --- /dev/null +++ b/nano/test_common/telemetry.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include + +namespace nano +{ +class keypair; +class network_params; +class telemetry_data; + +void compare_default_telemetry_response_data_excluding_signature (nano::telemetry_data const & telemetry_data_a, nano::network_params const & network_params_a, uint64_t bandwidth_limit_a, uint64_t active_difficulty_a); +void compare_default_telemetry_response_data (nano::telemetry_data const & telemetry_data_a, nano::network_params const & network_params_a, uint64_t bandwidth_limit_a, uint64_t active_difficulty_a, nano::keypair const & node_id_a); +} \ No newline at end of file diff --git a/nano/test_common/testutil.cpp b/nano/test_common/testutil.cpp new file mode 100644 index 00000000..b9029067 --- /dev/null +++ b/nano/test_common/testutil.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +#include + +#include +#include + +using namespace std::chrono_literals; + +/* Convenience constants for tests which are always on the test network */ +namespace +{ +nano::ledger_constants test_constants (nano::nano_networks::nano_test_network); +} + +nano::keypair const & nano::zero_key (test_constants.zero_key); +nano::keypair const & nano::test_genesis_key (test_constants.test_genesis_key); +nano::account const & nano::nano_test_account (test_constants.nano_test_account); +std::string const & nano::nano_test_genesis (test_constants.nano_test_genesis); +nano::account const & nano::genesis_account (test_constants.genesis_account); +nano::block_hash const & nano::genesis_hash (test_constants.genesis_hash); +nano::uint128_t const & nano::genesis_amount (test_constants.genesis_amount); +nano::account const & nano::burn_account (test_constants.burn_account); + +void nano::wait_peer_connections (nano::system & system_a) +{ + auto wait_peer_count = [&system_a](bool in_memory) { + auto num_nodes = system_a.nodes.size (); + system_a.deadline_set (20s); + size_t peer_count = 0; + while (peer_count != num_nodes * (num_nodes - 1)) + { + ASSERT_NO_ERROR (system_a.poll ()); + peer_count = std::accumulate (system_a.nodes.cbegin (), system_a.nodes.cend (), std::size_t{ 0 }, [in_memory](auto total, auto const & node) { + if (in_memory) + { + return total += node->network.size (); + } + else + { + auto transaction = node->store.tx_begin_read (); + return total += node->store.peer_count (transaction); + } + }); + } + }; + + // Do a pre-pass with in-memory containers to reduce IO if still in the process of connecting to peers + wait_peer_count (true); + wait_peer_count (false); +} diff --git a/nano/core_test/testutil.hpp b/nano/test_common/testutil.hpp similarity index 65% rename from nano/core_test/testutil.hpp rename to nano/test_common/testutil.hpp index 469edd40..d10771be 100644 --- a/nano/core_test/testutil.hpp +++ b/nano/test_common/testutil.hpp @@ -1,22 +1,16 @@ #pragma once +#include #include #include -// This test header is unfortunately included in many different files. -// Requires guarding in some cases. -#ifndef IGNORE_GTEST_INCL -#include - -#include -#endif - #include #include #include #include #include +#include #include #include #include @@ -48,13 +42,17 @@ ASSERT_NO_ERROR (system.poll ()); \ } -/* Convenience globals for core_test */ +/* Convenience globals for gtest projects */ namespace nano { using uint128_t = boost::multiprecision::uint128_t; class keypair; class public_key; class block_hash; +class telemetry_data; +class network_params; +class system; + extern nano::keypair const & zero_key; extern nano::keypair const & test_genesis_key; extern std::string const & nano_test_genesis; @@ -214,61 +212,5 @@ namespace util }; } -inline uint16_t get_available_port () -{ - // Maximum possible sockets which may feasibly be used in 1 test - constexpr auto max = 200; - static uint16_t current = 0; - // Read the TEST_BASE_PORT environment and override the default base port if it exists - auto base_str = std::getenv ("TEST_BASE_PORT"); - uint16_t base_port = 24000; - if (base_str) - { - base_port = boost::lexical_cast (base_str); - } - - uint16_t const available_port = base_port + current; - ++current; - // Reset port number once we have reached the maximum - if (current == max) - { - current = 0; - } - - return available_port; -} - -#ifndef IGNORE_GTEST_INCL -inline void compare_default_telemetry_response_data_excluding_signature (nano::telemetry_data const & telemetry_data_a, nano::network_params const & network_params_a, uint64_t bandwidth_limit_a, uint64_t active_difficulty_a) -{ - ASSERT_EQ (telemetry_data_a.block_count, 1); - ASSERT_EQ (telemetry_data_a.cemented_count, 1); - ASSERT_EQ (telemetry_data_a.bandwidth_cap, bandwidth_limit_a); - ASSERT_EQ (telemetry_data_a.peer_count, 1); - ASSERT_EQ (telemetry_data_a.protocol_version, network_params_a.protocol.telemetry_protocol_version_min); - ASSERT_EQ (telemetry_data_a.unchecked_count, 0); - ASSERT_EQ (telemetry_data_a.account_count, 1); - ASSERT_LT (telemetry_data_a.uptime, 100); - ASSERT_EQ (telemetry_data_a.genesis_block, network_params_a.ledger.genesis_hash); - ASSERT_EQ (telemetry_data_a.major_version, nano::get_major_node_version ()); - ASSERT_EQ (telemetry_data_a.minor_version, nano::get_minor_node_version ()); - ASSERT_EQ (telemetry_data_a.patch_version, nano::get_patch_node_version ()); - ASSERT_EQ (telemetry_data_a.pre_release_version, nano::get_pre_release_node_version ()); - ASSERT_EQ (telemetry_data_a.maker, 0); - ASSERT_GT (telemetry_data_a.timestamp, std::chrono::system_clock::now () - std::chrono::seconds (100)); - ASSERT_EQ (telemetry_data_a.active_difficulty, active_difficulty_a); -} - -inline void compare_default_telemetry_response_data (nano::telemetry_data const & telemetry_data_a, nano::network_params const & network_params_a, uint64_t bandwidth_limit_a, uint64_t active_difficulty_a, nano::keypair const & node_id_a) -{ - ASSERT_FALSE (telemetry_data_a.validate_signature (nano::telemetry_data::size)); - nano::telemetry_data telemetry_data_l = telemetry_data_a; - telemetry_data_l.signature.clear (); - telemetry_data_l.sign (node_id_a); - // Signature should be different because uptime/timestamp will have changed. - ASSERT_NE (telemetry_data_a.signature, telemetry_data_l.signature); - compare_default_telemetry_response_data_excluding_signature (telemetry_data_a, network_params_a, bandwidth_limit_a, active_difficulty_a); - ASSERT_EQ (telemetry_data_a.node_id, node_id_a.pub); -} -#endif +void wait_peer_connections (nano::system &); }