Skip to content

Commit

Permalink
Lookup channels by node id instead of endpoints in test code (#3897)
Browse files Browse the repository at this point in the history
* Add `get_node_id` helper function

* Lookup channels by node id instead of endpoints in test code

Co-authored-by: Piotr Wójcik <[email protected]>
  • Loading branch information
pwojcikdev and pwojcikdev authored Oct 12, 2022
1 parent de2da60 commit 92b9943
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 36 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ TEST (election, quorum_minimum_update_weight_before_quorum_checks)
auto const vote1 = std::make_shared<nano::vote> (nano::dev::genesis_key.pub, nano::dev::genesis_key.prv, nano::vote::timestamp_max, nano::vote::duration_max, std::vector<nano::block_hash>{ send1->hash () });
ASSERT_EQ (nano::vote_code::vote, node1.active.vote (vote1));

auto channel = node1.network.find_channel (node2.network.endpoint ());
auto channel = node1.network.find_node_id (node2.get_node_id ());
ASSERT_NE (channel, nullptr);

auto const vote2 = std::make_shared<nano::vote> (key1.pub, key1.prv, nano::vote::timestamp_max, nano::vote::duration_max, std::vector<nano::block_hash>{ send1->hash () });
Expand Down
21 changes: 11 additions & 10 deletions nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ TEST (network, send_node_id_handshake_tcp)
ASSERT_EQ (1, node1->network.size ());
auto list1 (node0->network.list (1));
ASSERT_EQ (nano::transport::transport_type::tcp, list1[0]->get_type ());
ASSERT_EQ (node1->network.endpoint (), list1[0]->get_endpoint ());
ASSERT_EQ (node1->get_node_id (), list1[0]->get_node_id ());
auto list2 (node1->network.list (1));
ASSERT_EQ (nano::transport::transport_type::tcp, list2[0]->get_type ());
ASSERT_EQ (node0->network.endpoint (), list2[0]->get_endpoint ());
ASSERT_EQ (node0->get_node_id (), list2[0]->get_node_id ());
node1->stop ();
}

Expand Down Expand Up @@ -384,7 +384,8 @@ TEST (network, send_insufficient_work)
.work (0)
.build_shared ();
nano::publish publish1{ nano::dev::network_params.network, block1 };
auto tcp_channel (node1.network.tcp_channels.find_channel (nano::transport::map_endpoint_to_tcp (node2.network.endpoint ())));
auto tcp_channel (node1.network.tcp_channels.find_node_id (node2.get_node_id ()));
ASSERT_NE (nullptr, tcp_channel);
tcp_channel->send (publish1, [] (boost::system::error_code const & ec, size_t size) {});
ASSERT_EQ (0, node1.stats.count (nano::stat::type::error, nano::stat::detail::insufficient_work));
ASSERT_TIMELY (10s, node2.stats.count (nano::stat::type::error, nano::stat::detail::insufficient_work) != 0);
Expand Down Expand Up @@ -1073,7 +1074,8 @@ TEST (network, duplicate_detection)
ASSERT_TIMELY (2s, node1.stats.count (nano::stat::type::filter, nano::stat::detail::duplicate_publish) == 1);

// Publish duplicate detection through TCP
auto tcp_channel (node0.network.tcp_channels.find_channel (nano::transport::map_endpoint_to_tcp (node1.network.endpoint ())));
auto tcp_channel (node0.network.tcp_channels.find_node_id (node1.get_node_id ()));
ASSERT_NE (nullptr, tcp_channel);
ASSERT_EQ (1, node1.stats.count (nano::stat::type::filter, nano::stat::detail::duplicate_publish));
tcp_channel->send (publish);
ASSERT_TIMELY (2s, node1.stats.count (nano::stat::type::filter, nano::stat::detail::duplicate_publish) == 2);
Expand Down Expand Up @@ -1221,19 +1223,18 @@ TEST (network, tcp_no_connect_excluded_peers)
auto node1 (std::make_shared<nano::node> (system.io_ctx, nano::test::get_available_port (), nano::unique_path (), system.logging, system.work));
node1->start ();
system.nodes.push_back (node1);
auto endpoint1 (node1->network.endpoint ());
auto endpoint1_tcp (nano::transport::map_endpoint_to_tcp (endpoint1));
auto endpoint1_tcp (nano::transport::map_endpoint_to_tcp (node1->network.endpoint ()));
while (!node0->network.excluded_peers.check (endpoint1_tcp))
{
node0->network.excluded_peers.add (endpoint1_tcp, 1);
}
ASSERT_EQ (0, node0->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_excluded));
node1->network.merge_peer (node0->network.endpoint ());
ASSERT_TIMELY (5s, node0->stats.count (nano::stat::type::tcp, nano::stat::detail::tcp_excluded) >= 1);
ASSERT_EQ (nullptr, node0->network.find_channel (endpoint1));
ASSERT_EQ (nullptr, node0->network.find_node_id (node1->get_node_id ()));

// Should not actively reachout to excluded peers
ASSERT_TRUE (node0->network.reachout (endpoint1, true));
ASSERT_TRUE (node0->network.reachout (node1->network.endpoint (), true));

// Erasing from excluded peers should allow a connection
node0->network.excluded_peers.remove (endpoint1_tcp);
Expand Down Expand Up @@ -1374,7 +1375,7 @@ TEST (network, filter_invalid_network_bytes)
auto & node2 = *system.nodes[1];

// find the comms channel that goes from node2 to node1
auto channel = node2.network.find_channel (node1.network.endpoint ());
auto channel = node2.network.find_node_id (node1.get_node_id ());
ASSERT_NE (nullptr, channel);

// send a keepalive, from node2 to node1, with the wrong network bytes
Expand All @@ -1393,7 +1394,7 @@ TEST (network, filter_invalid_version_using)
auto & node2 = *system.nodes[1];

// find the comms channel that goes from node2 to node1
auto channel = node2.network.find_channel (node1.network.endpoint ());
auto channel = node2.network.find_node_id (node1.get_node_id ());
ASSERT_NE (nullptr, channel);

// send a keepalive, from node2 to node1, with the wrong version_using
Expand Down
10 changes: 5 additions & 5 deletions nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@ TEST (node, rep_remove)
// Add working node for genesis representative
auto node_genesis_rep = system.add_node (nano::node_config (nano::test::get_available_port (), system.logging));
system.wallet (1)->insert_adhoc (nano::dev::genesis_key.prv);
auto channel_genesis_rep (searching_node.network.find_channel (node_genesis_rep->network.endpoint ()));
auto channel_genesis_rep (searching_node.network.find_node_id (node_genesis_rep->get_node_id ()));
ASSERT_NE (nullptr, channel_genesis_rep);

// genesis_rep should be found as principal representative after receiving a vote from it
Expand All @@ -1870,7 +1870,7 @@ TEST (node, rep_remove)
node_rep2->start ();
searching_node.network.tcp_channels.start_tcp (node_rep2->network.endpoint ());
std::shared_ptr<nano::transport::channel> channel_rep2;
ASSERT_TIMELY (10s, (channel_rep2 = searching_node.network.tcp_channels.find_channel (nano::transport::map_endpoint_to_tcp (node_rep2->network.endpoint ()))) != nullptr);
ASSERT_TIMELY (10s, (channel_rep2 = searching_node.network.tcp_channels.find_node_id (node_rep2->get_node_id ())) != nullptr);

// Rep2 should be found as a principal representative after receiving a vote from it
auto vote_rep2 = std::make_shared<nano::vote> (keys_rep2.pub, keys_rep2.prv, 0, 0, std::vector<nano::block_hash>{ nano::dev::genesis->hash () });
Expand Down Expand Up @@ -3196,11 +3196,11 @@ TEST (node, peers)
// Confirm that the peers match with the endpoints we are expecting
ASSERT_EQ (1, node1->network.size ());
auto list1 (node1->network.list (2));
ASSERT_EQ (node2->network.endpoint (), list1[0]->get_endpoint ());
ASSERT_EQ (node2->get_node_id (), list1[0]->get_node_id ());
ASSERT_EQ (nano::transport::transport_type::tcp, list1[0]->get_type ());
ASSERT_EQ (1, node2->network.size ());
auto list2 (node2->network.list (2));
ASSERT_EQ (node1->network.endpoint (), list2[0]->get_endpoint ());
ASSERT_EQ (node1->get_node_id (), list2[0]->get_node_id ());
ASSERT_EQ (nano::transport::transport_type::tcp, list2[0]->get_type ());
// Stop the peer node and check that it is removed from the store
node1->stop ();
Expand Down Expand Up @@ -4244,7 +4244,7 @@ TEST (rep_crawler, recently_confirmed)
node1.active.recently_confirmed.put (block->qualified_root (), block->hash ());
auto & node2 (*system.add_node ());
system.wallet (1)->insert_adhoc (nano::dev::genesis_key.prv);
auto channel = node1.network.find_channel (node2.network.endpoint ());
auto channel = node1.network.find_node_id (node2.get_node_id ());
ASSERT_NE (nullptr, channel);
node1.rep_crawler.query (channel);
ASSERT_TIMELY (3s, node1.rep_crawler.representative_count () == 1);
Expand Down
33 changes: 21 additions & 12 deletions nano/core_test/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,18 @@ TEST (telemetry, basic)
// Request telemetry metrics
nano::telemetry_data telemetry_data;
auto server_endpoint = node_server->network.endpoint ();
auto channel = node_client->network.find_channel (node_server->network.endpoint ());
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);
{
std::atomic<bool> done{ false };
node_client->telemetry->get_metrics_single_peer_async (channel, [&done, &server_endpoint, &telemetry_data] (nano::telemetry_data_response const & response_a) {
ASSERT_FALSE (response_a.error);
ASSERT_EQ (server_endpoint, response_a.endpoint);
telemetry_data = response_a.telemetry_data;
done = true;
});

ASSERT_TIMELY (10s, done);
ASSERT_EQ (node_server->get_node_id (), telemetry_data.node_id);
}

// Check the metrics are correct
Expand Down Expand Up @@ -357,7 +358,8 @@ TEST (telemetry, over_tcp)
nano::test::wait_peer_connections (system);

std::atomic<bool> done{ false };
auto channel = node_client->network.find_channel (node_server->network.endpoint ());
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);
node_client->telemetry->get_metrics_single_peer_async (channel, [&done, &node_server] (nano::telemetry_data_response const & response_a) {
ASSERT_FALSE (response_a.error);
nano::test::compare_default_telemetry_response_data (response_a.telemetry_data, node_server->network_params, node_server->config.bandwidth_limit, node_server->default_difficulty (nano::work_version::work_1), node_server->node_id);
Expand Down Expand Up @@ -423,7 +425,9 @@ TEST (telemetry, blocking_request)
node_client->workers.push_task (call_system_poll);

// Now try single request metric
auto telemetry_data_response = node_client->telemetry->get_metrics_single_peer (node_client->network.find_channel (node_server->network.endpoint ()));
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);
auto telemetry_data_response = node_client->telemetry->get_metrics_single_peer (channel);
ASSERT_FALSE (telemetry_data_response.error);
nano::test::compare_default_telemetry_response_data (telemetry_data_response.telemetry_data, node_server->network_params, node_server->config.bandwidth_limit, node_server->default_difficulty (nano::work_version::work_1), node_server->node_id);

Expand All @@ -442,7 +446,8 @@ TEST (telemetry, disconnects)
nano::test::wait_peer_connections (system);

// Try and request metrics from a node which is turned off but a channel is not closed yet
auto channel = node_client->network.find_channel (node_server->network.endpoint ());
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);
node_server->stop ();
ASSERT_TRUE (channel);

Expand All @@ -468,7 +473,8 @@ TEST (telemetry, dos_tcp)
nano::test::wait_peer_connections (system);

nano::telemetry_req message{ nano::dev::network_params.network };
auto channel = node_client->network.tcp_channels.find_channel (nano::transport::map_endpoint_to_tcp (node_server->network.endpoint ()));
auto channel = node_client->network.tcp_channels.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);
channel->send (message, [] (boost::system::error_code const & ec, size_t size_a) {
ASSERT_FALSE (ec);
});
Expand Down Expand Up @@ -508,8 +514,8 @@ TEST (telemetry, disable_metrics)
nano::test::wait_peer_connections (system);

// Try and request metrics from a node which is turned off but a channel is not closed yet
auto channel = node_client->network.find_channel (node_server->network.endpoint ());
ASSERT_TRUE (channel);
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);

std::atomic<bool> done{ false };
node_client->telemetry->get_metrics_single_peer_async (channel, [&done] (nano::telemetry_data_response const & response_a) {
Expand All @@ -521,7 +527,8 @@ TEST (telemetry, disable_metrics)

// It should still be able to receive metrics though
done = false;
auto channel1 = node_server->network.find_channel (node_client->network.endpoint ());
auto channel1 = node_server->network.find_node_id (node_client->get_node_id ());
ASSERT_NE (nullptr, channel1);
node_server->telemetry->get_metrics_single_peer_async (channel1, [&done, node_client] (nano::telemetry_data_response const & response_a) {
ASSERT_FALSE (response_a.error);
nano::test::compare_default_telemetry_response_data (response_a.telemetry_data, node_client->network_params, node_client->config.bandwidth_limit, node_client->default_difficulty (nano::work_version::work_1), node_client->node_id);
Expand All @@ -546,7 +553,8 @@ TEST (telemetry, max_possible_size)
nano::telemetry_ack message{ nano::dev::network_params.network, data };
nano::test::wait_peer_connections (system);

auto channel = node_client->network.tcp_channels.find_channel (nano::transport::map_endpoint_to_tcp (node_server->network.endpoint ()));
auto channel = node_client->network.tcp_channels.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);
channel->send (message, [] (boost::system::error_code const & ec, size_t size_a) {
ASSERT_FALSE (ec);
});
Expand Down Expand Up @@ -629,17 +637,18 @@ TEST (telemetry, maker_pruning)
// Request telemetry metrics
nano::telemetry_data telemetry_data;
auto server_endpoint = node_server->network.endpoint ();
auto channel = node_client->network.find_channel (node_server->network.endpoint ());
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
ASSERT_NE (nullptr, channel);
{
std::atomic<bool> done{ false };
node_client->telemetry->get_metrics_single_peer_async (channel, [&done, &server_endpoint, &telemetry_data] (nano::telemetry_data_response const & response_a) {
ASSERT_FALSE (response_a.error);
ASSERT_EQ (server_endpoint, response_a.endpoint);
telemetry_data = response_a.telemetry_data;
done = true;
});

ASSERT_TIMELY (10s, done);
ASSERT_EQ (node_server->get_node_id (), telemetry_data.node_id);
}

ASSERT_EQ (nano::telemetry_maker::nf_pruned_node, static_cast<nano::telemetry_maker> (telemetry_data.maker));
Expand Down
2 changes: 1 addition & 1 deletion nano/core_test/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,7 @@ TEST (websocket, telemetry)

ASSERT_TIMELY (10s, done);

node1->telemetry->get_metrics_single_peer_async (node1->network.find_channel (node2->network.endpoint ()), [] (auto const & response_a) {
node1->telemetry->get_metrics_single_peer_async (node1->network.find_node_id (node2->get_node_id ()), [] (auto const & response_a) {
ASSERT_FALSE (response_a.error);
});

Expand Down
5 changes: 5 additions & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1807,6 +1807,11 @@ uint64_t nano::node::get_confirmation_height (nano::transaction const & transact
nano::confirmation_height_info info;
store.confirmation_height.get (transaction_a, account_a, info);
return info.height;
}

nano::account nano::node::get_node_id () const
{
return node_id.pub;
};

nano::node_wrapper::node_wrapper (boost::filesystem::path const & path_a, boost::filesystem::path const & config_path_a, nano::node_flags const & node_flags_a) :
Expand Down
1 change: 1 addition & 0 deletions nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class node final : public std::enable_shared_from_this<nano::node>
* Attempts to bootstrap block. This is the best effort, there is no guarantee that the block will be bootstrapped.
*/
void bootstrap_block (nano::block_hash const &);
nano::account get_node_id () const;
nano::write_database_queue write_database_queue;
boost::asio::io_context & io_ctx;
boost::latch node_initialized_latch;
Expand Down
6 changes: 3 additions & 3 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1900,11 +1900,11 @@ TEST (rpc, keepalive)
auto port (boost::str (boost::format ("%1%") % node1->network.endpoint ().port ()));
request.put ("address", address);
request.put ("port", port);
ASSERT_EQ (nullptr, node0->network.tcp_channels.find_channel (nano::transport::map_endpoint_to_tcp (node1->network.endpoint ())));
ASSERT_EQ (nullptr, node0->network.tcp_channels.find_node_id (node1->get_node_id ()));
ASSERT_EQ (0, node0->network.size ());
auto response (wait_response (system, rpc_ctx, request));
system.deadline_set (10s);
while (node0->network.find_channel (node1->network.endpoint ()) == nullptr)
while (node0->network.find_node_id (node1->get_node_id ()) == nullptr)
{
ASSERT_EQ (0, node0->network.size ());
ASSERT_NO_ERROR (system.poll ());
Expand Down Expand Up @@ -7374,7 +7374,7 @@ TEST (rpc, telemetry_all)
// First need to set up the cached data
std::atomic<bool> done{ false };
auto node = system.nodes.front ();
node1->telemetry->get_metrics_single_peer_async (node1->network.find_channel (node->network.endpoint ()), [&done] (nano::telemetry_data_response const & telemetry_data_response_a) {
node1->telemetry->get_metrics_single_peer_async (node1->network.find_node_id (node->get_node_id ()), [&done] (nano::telemetry_data_response const & telemetry_data_response_a) {
ASSERT_FALSE (telemetry_data_response_a.error);
done = true;
});
Expand Down
8 changes: 4 additions & 4 deletions nano/slow_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ TEST (confirmation_height, dynamic_algorithm)
* of blocks uncemented is > unbounded_cutoff so that it hits the bounded processor), the main `run` loop on the conf height processor is iterated.
*
* This cause unbounded pending entries not to be written, and then the bounded processor would write them, causing some inconsistencies.
*/
*/
TEST (confirmation_height, dynamic_algorithm_no_transition_while_pending)
{
// Repeat in case of intermittent issues not replicating the issue talked about above.
Expand Down Expand Up @@ -1595,7 +1595,7 @@ TEST (telemetry, cache_read_and_timeout)
nano::telemetry_data telemetry_data;
{
std::atomic<bool> done{ false };
auto channel = node_client->network.find_channel (node_server->network.endpoint ());
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
node_client->telemetry->get_metrics_single_peer_async (channel, [&done, &telemetry_data] (nano::telemetry_data_response const & response_a) {
telemetry_data = response_a.telemetry_data;
done = true;
Expand Down Expand Up @@ -1625,7 +1625,7 @@ TEST (telemetry, cache_read_and_timeout)
// Request telemetry metrics again
{
std::atomic<bool> done{ false };
auto channel = node_client->network.find_channel (node_server->network.endpoint ());
auto channel = node_client->network.find_node_id (node_server->get_node_id ());
node_client->telemetry->get_metrics_single_peer_async (channel, [&done, &telemetry_data] (nano::telemetry_data_response const & response_a) {
telemetry_data = response_a.telemetry_data;
done = true;
Expand Down Expand Up @@ -1845,7 +1845,7 @@ TEST (node, mass_epoch_upgrader)
nano::test::system system;
nano::node_config node_config (nano::test::get_available_port (), system.logging);
node_config.work_threads = 4;
//node_config.work_peers = { { "192.168.1.101", 7000 } };
// node_config.work_peers = { { "192.168.1.101", 7000 } };
auto & node = *system.add_node (node_config);

auto balance = node.balance (nano::dev::genesis_key.pub);
Expand Down

0 comments on commit 92b9943

Please sign in to comment.