Skip to content

Commit

Permalink
Filter and disconnect from peers with mismatched genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 31, 2023
1 parent 5b6b54f commit 8a24a49
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 1 deletion.
23 changes: 23 additions & 0 deletions nano/core_test/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,27 @@ TEST (telemetry, ongoing_broadcasts)

ASSERT_TIMELY (5s, node1.stats.count (nano::stat::type::telemetry, nano::stat::detail::process) >= 3);
ASSERT_TIMELY (5s, node2.stats.count (nano::stat::type::telemetry, nano::stat::detail::process) >= 3)
}

TEST (telemetry, mismatched_genesis)
{
// Only second node will broadcast telemetry
nano::test::system system;
nano::node_flags node_flags;
node_flags.disable_ongoing_telemetry_requests = true;
node_flags.disable_providing_telemetry_metrics = true;
auto & node1 = *system.add_node (node_flags);

// Set up a node with different genesis
nano::network_params network_params{ nano::networks::nano_dev_network };
network_params.ledger.genesis = network_params.ledger.nano_live_genesis;
nano::node_config node_config{ network_params };
node_flags.disable_providing_telemetry_metrics = false;
auto & node2 = *system.add_node (node_config, node_flags);

ASSERT_TIMELY (5s, node1.stats.count (nano::stat::type::telemetry, nano::stat::detail::genesis_mismatch) > 0);
ASSERT_ALWAYS (1s, node1.stats.count (nano::stat::type::telemetry, nano::stat::detail::process) == 0)

// Ensure node with different genesis gets disconnected
ASSERT_TIMELY (5s, !node1.network.find_node_id (node2.get_node_id ()));
}
1 change: 1 addition & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ enum class detail : uint8_t
invalid_signature,
different_genesis_hash,
node_id_mismatch,
genesis_mismatch,
request_within_protection_cache_zone,
no_response_received,
unsolicited_telemetry_ack,
Expand Down
17 changes: 17 additions & 0 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

#include <boost/format.hpp>

/*
* network
*/

nano::network::network (nano::node & node_a, uint16_t port_a) :
id (nano::network_constants::active_network),
syn_cookies (node_a.network_params.network.max_peers_per_ip),
Expand Down Expand Up @@ -824,6 +828,19 @@ void nano::network::erase (nano::transport::channel const & channel_a)
}
}

void nano::network::exclude (std::shared_ptr<nano::transport::channel> const & channel)
{
// Add to peer exclusion list
excluded_peers.add (channel->get_tcp_endpoint ());

// Disconnect
erase (*channel);
}

/*
* message_buffer_manager
*/

nano::message_buffer_manager::message_buffer_manager (nano::stats & stats_a, std::size_t size, std::size_t count) :
stats (stats_a),
free (count),
Expand Down
3 changes: 3 additions & 0 deletions nano/node/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ class network final
float size_sqrt () const;
bool empty () const;
void erase (nano::transport::channel const &);
/** Disconnects and adds peer to exclusion list */
void exclude (std::shared_ptr<nano::transport::channel> const & channel);

static std::string to_string (nano::networks);

private:
Expand Down
4 changes: 3 additions & 1 deletion nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ class node_config
public:
node_config (nano::network_params & network_params = nano::dev::network_params);
node_config (const std::optional<uint16_t> &, nano::logging const &, nano::network_params & network_params = nano::dev::network_params);

nano::error serialize_toml (nano::tomlconfig &) const;
nano::error deserialize_toml (nano::tomlconfig &);

bool upgrade_json (unsigned, nano::jsonconfig &);
nano::account random_representative () const;
nano::network_params & network_params;
nano::network_params network_params;
std::optional<uint16_t> peering_port{};
nano::logging logging;
std::vector<std::pair<std::string, uint16_t>> work_peers;
Expand Down
8 changes: 8 additions & 0 deletions nano/node/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ bool nano::telemetry::verify (const nano::telemetry_ack & telemetry, const std::
return false;
}

if (telemetry.data.genesis_block != network_params.ledger.genesis->hash ())
{
network.exclude (channel);

stats.inc (nano::stat::type::telemetry, nano::stat::detail::genesis_mismatch);
return false;
}

return true; // Telemetry is OK
}

Expand Down

0 comments on commit 8a24a49

Please sign in to comment.