Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert static network id code to instance code #3368

Merged
merged 6 commits into from
Jul 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ TEST (message, publish_serialization)
{
nano::network_params params;
nano::publish publish (std::make_shared<nano::send_block> (0, 1, 2, nano::keypair ().prv, 4, 5));
publish.header.network = nano::networks::nano_dev_network;
ASSERT_EQ (nano::block_type::send, publish.header.block_type ());
std::vector<uint8_t> bytes;
{
Expand Down
14 changes: 14 additions & 0 deletions nano/core_test/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1256,3 +1256,17 @@ TEST (network, loopback_channel)
++node1.network.port;
ASSERT_NE (channel1.get_endpoint (), node1.network.endpoint ());
}

// Ensure the network filters messages with the incorrect magic number
TEST (network, filter)
{
nano::system system{ 1 };
auto & node1 = *system.nodes[0];
nano::keepalive keepalive;
keepalive.header.network = nano::networks::nano_dev_network;
node1.network.inbound (keepalive, std::make_shared<nano::transport::channel_loopback> (node1));
ASSERT_EQ (0, node1.stats.count (nano::stat::type::message, nano::stat::detail::invalid_network));
keepalive.header.network = nano::networks::invalid;
node1.network.inbound (keepalive, std::make_shared<nano::transport::channel_loopback> (node1));
ASSERT_EQ (1, node1.stats.count (nano::stat::type::message, nano::stat::detail::invalid_network));
}
2 changes: 1 addition & 1 deletion nano/lib/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ std::array<uint8_t, 2> test_magic_number ()

void force_nano_dev_network ()
{
nano::network_constants::set_active_network (nano::nano_networks::nano_dev_network);
nano::network_constants::set_active_network (nano::networks::nano_dev_network);
}

bool running_within_valgrind ()
Expand Down
38 changes: 19 additions & 19 deletions nano/lib/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,18 @@ std::array<uint8_t, 2> test_magic_number ();

/**
* Network variants with different genesis blocks and network parameters
* @warning Enum values are used in integral comparisons; do not change.
*/
enum class nano_networks
enum class networks : uint16_t
{
invalid = 0x0,
// Low work parameters, publicly known genesis key, dev IP ports
nano_dev_network = 0,
nano_dev_network = 0x5241, // 'R', 'A'
// Normal work parameters, secret beta genesis key, beta IP ports
nano_beta_network = 1,
nano_beta_network = 0x5242, // 'R', 'B'
// Normal work parameters, secret live key, live IP ports
nano_live_network = 2,
nano_live_network = 0x5243, // 'R', 'C'
// Normal work parameters, secret test genesis key, test IP ports
nano_test_network = 3,
nano_test_network = 0x5258, // 'R', 'X'
};

struct work_thresholds
Expand Down Expand Up @@ -108,7 +108,7 @@ class network_constants
{
}

network_constants (nano_networks network_a) :
network_constants (nano::networks network_a) :
current_network (network_a),
publish_thresholds (is_live_network () ? publish_full : is_beta_network () ? publish_beta : is_test_network () ? publish_test : publish_dev)
{
Expand All @@ -132,7 +132,7 @@ class network_constants
static const char * active_network_err_msg;

/** The network this param object represents. This may differ from the global active network; this is needed for certain --debug... commands */
nano_networks current_network{ nano::network_constants::active_network };
nano::networks current_network{ nano::network_constants::active_network };
nano::work_thresholds publish_thresholds;

unsigned principal_weight_factor;
Expand All @@ -143,7 +143,7 @@ class network_constants
unsigned request_interval_ms;

/** Returns the network this object contains values for */
nano_networks network () const
nano::networks network () const
{
return current_network;
}
Expand All @@ -153,7 +153,7 @@ class network_constants
* If not called, the compile-time option will be used.
* @param network_a The new active network
*/
static void set_active_network (nano_networks network_a)
static void set_active_network (nano::networks network_a)
{
active_network = network_a;
}
Expand All @@ -168,19 +168,19 @@ class network_constants
auto error{ false };
if (network_a == "live")
{
active_network = nano::nano_networks::nano_live_network;
active_network = nano::networks::nano_live_network;
}
else if (network_a == "beta")
{
active_network = nano::nano_networks::nano_beta_network;
active_network = nano::networks::nano_beta_network;
}
else if (network_a == "dev")
{
active_network = nano::nano_networks::nano_dev_network;
active_network = nano::networks::nano_dev_network;
}
else if (network_a == "test")
{
active_network = nano::nano_networks::nano_test_network;
active_network = nano::networks::nano_test_network;
}
else
{
Expand All @@ -196,23 +196,23 @@ class network_constants

bool is_live_network () const
{
return current_network == nano_networks::nano_live_network;
return current_network == nano::networks::nano_live_network;
}
bool is_beta_network () const
{
return current_network == nano_networks::nano_beta_network;
return current_network == nano::networks::nano_beta_network;
}
bool is_dev_network () const
{
return current_network == nano_networks::nano_dev_network;
return current_network == nano::networks::nano_dev_network;
}
bool is_test_network () const
{
return current_network == nano_networks::nano_test_network;
return current_network == nano::networks::nano_test_network;
}

/** Initial value is ACTIVE_NETWORK compile flag, but can be overridden by a CLI flag */
static nano::nano_networks active_network;
static nano::networks active_network;
};

std::string get_config_path (boost::filesystem::path const & data_path);
Expand Down
3 changes: 3 additions & 0 deletions nano/lib/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,9 @@ std::string nano::stat::detail_to_string (uint32_t key)
case nano::stat::detail::generator_spacing:
res = "generator_spacing";
break;
case nano::stat::detail::invalid_network:
res = "invalid_network";
break;
}
return res;
}
Expand Down
1 change: 1 addition & 0 deletions nano/lib/stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ class stat final
insufficient_work,
http_callback,
unreachable_host,
invalid_network,

// confirmation_observer specific
active_quorum,
Expand Down
4 changes: 2 additions & 2 deletions nano/nano_node/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ int main (int argc, char * const * argv)
}
else if (vm.count ("debug_profile_process"))
{
nano::network_constants::set_active_network (nano::nano_networks::nano_dev_network);
nano::network_constants::set_active_network (nano::networks::nano_dev_network);
nano::network_params dev_params;
nano::block_builder builder;
size_t num_accounts (100000);
Expand Down Expand Up @@ -1004,7 +1004,7 @@ int main (int argc, char * const * argv)
}
else if (vm.count ("debug_profile_votes"))
{
nano::network_constants::set_active_network (nano::nano_networks::nano_dev_network);
nano::network_constants::set_active_network (nano::networks::nano_dev_network);
nano::network_params dev_params;
nano::block_builder builder;
size_t num_elections (40000);
Expand Down
15 changes: 6 additions & 9 deletions nano/node/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ uint64_t nano::ip_address_hash_raw (boost::asio::ip::address const & ip_a, uint1
}

nano::message_header::message_header (nano::message_type type_a) :
network (nano::network_constants::active_network),
version_max (get_protocol_constants ().protocol_version),
version_using (get_protocol_constants ().protocol_version),
type (type_a)
Expand All @@ -67,7 +68,7 @@ nano::message_header::message_header (bool & error_a, nano::stream & stream_a)
void nano::message_header::serialize (nano::stream & stream_a) const
{
static nano::network_params network_params;
nano::write (stream_a, network_params.header_magic_number);
nano::write (stream_a, boost::endian::native_to_big (static_cast<uint16_t> (network)));
nano::write (stream_a, version_max);
nano::write (stream_a, version_using);
nano::write (stream_a, get_protocol_constants ().protocol_version_min ());
Expand All @@ -81,18 +82,14 @@ bool nano::message_header::deserialize (nano::stream & stream_a)
try
{
static nano::network_params network_params;
uint16_t extensions_l;
std::array<uint8_t, 2> magic_number_l;
read (stream_a, magic_number_l);
if (magic_number_l != network_params.header_magic_number)
{
throw std::runtime_error ("Magic numbers do not match");
}

uint16_t network_bytes;
nano::read (stream_a, network_bytes);
network = static_cast<nano::networks> (boost::endian::big_to_native (network_bytes));
nano::read (stream_a, version_max);
nano::read (stream_a, version_using);
nano::read (stream_a, version_min_m);
nano::read (stream_a, type);
uint16_t extensions_l;
nano::read (stream_a, extensions_l);
extensions = extensions_l;
}
Expand Down
3 changes: 2 additions & 1 deletion nano/node/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class message_header final
void block_type_set (nano::block_type);
uint8_t count_get () const;
void count_set (uint8_t);
nano::networks network;
uint8_t version_max;
uint8_t version_using;

Expand All @@ -204,7 +205,7 @@ class message_header final
public:
nano::message_type type;
std::bitset<16> extensions;
static size_t constexpr size = sizeof (network_params::header_magic_number) + sizeof (version_max) + sizeof (version_using) + sizeof (version_min_m) + sizeof (type) + sizeof (/* extensions */ uint16_t);
static size_t constexpr size = sizeof (nano::networks) + sizeof (version_max) + sizeof (version_using) + sizeof (version_min_m) + sizeof (type) + sizeof (/* extensions */ uint16_t);

void flag_set (uint8_t);
static uint8_t constexpr bulk_pull_count_present_flag = 0;
Expand Down
12 changes: 11 additions & 1 deletion nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,18 @@
#include <numeric>

nano::network::network (nano::node & node_a, uint16_t port_a) :
id (nano::network_constants::active_network),
syn_cookies (node_a.network_params.node.max_peers_per_ip),
inbound{ [this] (nano::message const & message, std::shared_ptr<nano::transport::channel> const & channel) { process_message (message, channel); } },
inbound{ [this] (nano::message const & message, std::shared_ptr<nano::transport::channel> const & channel) {
if (message.header.network == id)
{
process_message (message, channel);
}
else
{
this->node.stats.inc (nano::stat::type::message, nano::stat::detail::invalid_network);
}
} },
buffer_container (node_a.stats, nano::network::buffer_size, 4096), // 2Mb receive buffer
resolver (node_a.io_ctx),
limiter (node_a.config.bandwidth_limit_burst_ratio, node_a.config.bandwidth_limit),
Expand Down
1 change: 1 addition & 0 deletions nano/node/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class network final
public:
network (nano::node &, uint16_t);
~network ();
nano::networks id;
void start ();
void stop ();
void flood_message (nano::message const &, nano::buffer_drop_policy const = nano::buffer_drop_policy::limiter, float const = 1.0f);
Expand Down
8 changes: 4 additions & 4 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ nano::node_config::node_config (uint16_t peering_port_a, nano::logging const & l
}
switch (network_params.network.network ())
{
case nano::nano_networks::nano_dev_network:
case nano::networks::nano_dev_network:
enable_voting = true;
preconfigured_representatives.push_back (network_params.ledger.genesis_account);
break;
case nano::nano_networks::nano_beta_network:
case nano::networks::nano_beta_network:
{
preconfigured_peers.push_back (default_beta_peer_network);
nano::account offline_representative;
release_assert (!offline_representative.decode_account ("nano_1defau1t9off1ine9rep99999999999999999999999999999999wgmuzxxy"));
preconfigured_representatives.emplace_back (offline_representative);
break;
}
case nano::nano_networks::nano_live_network:
case nano::networks::nano_live_network:
preconfigured_peers.push_back (default_live_peer_network);
preconfigured_representatives.emplace_back ("A30E0A32ED41C8607AA9212843392E853FCBCB4E7CB194E35C94F07F91DE59EF");
preconfigured_representatives.emplace_back ("67556D31DDFC2A440BF6147501449B4CB9572278D034EE686A6BEE29851681DF");
Expand All @@ -61,7 +61,7 @@ nano::node_config::node_config (uint16_t peering_port_a, nano::logging const & l
preconfigured_representatives.emplace_back ("2298FAB7C61058E77EA554CB93EDEEDA0692CBFCC540AB213B2836B29029E23A");
preconfigured_representatives.emplace_back ("3FE80B4BC842E82C1C18ABFEEC47EA989E63953BC82AC411F304D13833D52A56");
break;
case nano::nano_networks::nano_test_network:
case nano::networks::nano_test_network:
preconfigured_peers.push_back (default_test_peer_network);
preconfigured_representatives.push_back (network_params.ledger.genesis_account);
break;
Expand Down
17 changes: 8 additions & 9 deletions nano/secure/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ size_t constexpr nano::open_block::size;
size_t constexpr nano::change_block::size;
size_t constexpr nano::state_block::size;

nano::nano_networks nano::network_constants::active_network = nano::nano_networks::ACTIVE_NETWORK;
nano::networks nano::network_constants::active_network = nano::networks::ACTIVE_NETWORK;

namespace
{
Expand Down Expand Up @@ -84,13 +84,12 @@ nano::network_params::network_params () :
{
}

nano::network_params::network_params (nano::nano_networks network_a) :
nano::network_params::network_params (nano::networks network_a) :
network (network_a), ledger (network), voting (network), node (network), portmapping (network), bootstrap (network)
{
unsigned constexpr kdf_full_work = 64 * 1024;
unsigned constexpr kdf_dev_work = 8;
kdf_work = network.is_dev_network () ? kdf_dev_work : kdf_full_work;
header_magic_number = network.is_dev_network () ? std::array<uint8_t, 2>{ { 'R', 'A' } } : network.is_beta_network () ? std::array<uint8_t, 2>{ { 'R', 'B' } } : network.is_live_network () ? std::array<uint8_t, 2>{ { 'R', 'C' } } : nano::test_magic_number ();
}

uint8_t nano::protocol_constants::protocol_version_min () const
Expand All @@ -103,7 +102,7 @@ nano::ledger_constants::ledger_constants (nano::network_constants & network_cons
{
}

nano::ledger_constants::ledger_constants (nano::nano_networks network_a) :
nano::ledger_constants::ledger_constants (nano::networks network_a) :
zero_key ("0"),
dev_genesis_key (dev_private_key_data),
nano_dev_account (dev_public_key_data),
Expand All @@ -114,21 +113,21 @@ nano::ledger_constants::ledger_constants (nano::nano_networks network_a) :
nano_beta_genesis (beta_genesis_data),
nano_live_genesis (live_genesis_data),
nano_test_genesis (test_genesis_data),
genesis_account (network_a == nano::nano_networks::nano_dev_network ? nano_dev_account : network_a == nano::nano_networks::nano_beta_network ? nano_beta_account : network_a == nano::nano_networks::nano_test_network ? nano_test_account : nano_live_account),
genesis_block (network_a == nano::nano_networks::nano_dev_network ? nano_dev_genesis : network_a == nano::nano_networks::nano_beta_network ? nano_beta_genesis : network_a == nano::nano_networks::nano_test_network ? nano_test_genesis : nano_live_genesis),
genesis_account (network_a == nano::networks::nano_dev_network ? nano_dev_account : network_a == nano::networks::nano_beta_network ? nano_beta_account : network_a == nano::networks::nano_test_network ? nano_test_account : nano_live_account),
genesis_block (network_a == nano::networks::nano_dev_network ? nano_dev_genesis : network_a == nano::networks::nano_beta_network ? nano_beta_genesis : network_a == nano::networks::nano_test_network ? nano_test_genesis : nano_live_genesis),
genesis_hash (parse_block_from_genesis_data (genesis_block)->hash ()),
genesis_amount (std::numeric_limits<nano::uint128_t>::max ()),
burn_account (0),
nano_dev_final_votes_canary_account (dev_public_key_data),
nano_beta_final_votes_canary_account (beta_canary_public_key_data),
nano_live_final_votes_canary_account (live_canary_public_key_data),
nano_test_final_votes_canary_account (test_canary_public_key_data),
final_votes_canary_account (network_a == nano::nano_networks::nano_dev_network ? nano_dev_final_votes_canary_account : network_a == nano::nano_networks::nano_beta_network ? nano_beta_final_votes_canary_account : network_a == nano::nano_networks::nano_test_network ? nano_test_final_votes_canary_account : nano_live_final_votes_canary_account),
final_votes_canary_account (network_a == nano::networks::nano_dev_network ? nano_dev_final_votes_canary_account : network_a == nano::networks::nano_beta_network ? nano_beta_final_votes_canary_account : network_a == nano::networks::nano_test_network ? nano_test_final_votes_canary_account : nano_live_final_votes_canary_account),
nano_dev_final_votes_canary_height (1),
nano_beta_final_votes_canary_height (1),
nano_live_final_votes_canary_height (1),
nano_test_final_votes_canary_height (1),
final_votes_canary_height (network_a == nano::nano_networks::nano_dev_network ? nano_dev_final_votes_canary_height : network_a == nano::nano_networks::nano_beta_network ? nano_beta_final_votes_canary_height : network_a == nano::nano_networks::nano_test_network ? nano_test_final_votes_canary_height : nano_live_final_votes_canary_height)
final_votes_canary_height (network_a == nano::networks::nano_dev_network ? nano_dev_final_votes_canary_height : network_a == nano::networks::nano_beta_network ? nano_beta_final_votes_canary_height : network_a == nano::networks::nano_test_network ? nano_test_final_votes_canary_height : nano_live_final_votes_canary_height)
{
nano::link epoch_link_v1;
const char * epoch_message_v1 ("epoch v1 block");
Expand All @@ -139,7 +138,7 @@ nano::ledger_constants::ledger_constants (nano::nano_networks network_a) :
nano::account nano_live_epoch_v2_signer;
auto error (nano_live_epoch_v2_signer.decode_account ("nano_3qb6o6i1tkzr6jwr5s7eehfxwg9x6eemitdinbpi7u8bjjwsgqfj4wzser3x"));
debug_assert (!error);
auto epoch_v2_signer (network_a == nano::nano_networks::nano_dev_network ? nano_dev_account : network_a == nano::nano_networks::nano_beta_network ? nano_beta_account : network_a == nano::nano_networks::nano_test_network ? nano_test_account : nano_live_epoch_v2_signer);
auto epoch_v2_signer (network_a == nano::networks::nano_dev_network ? nano_dev_account : network_a == nano::networks::nano_beta_network ? nano_beta_account : network_a == nano::networks::nano_test_network ? nano_test_account : nano_live_epoch_v2_signer);
const char * epoch_message_v2 ("epoch v2 block");
strncpy ((char *)epoch_link_v2.bytes.data (), epoch_message_v2, epoch_link_v2.bytes.size ());
epochs.add (nano::epoch::epoch_2, epoch_v2_signer, epoch_link_v2);
Expand Down
5 changes: 2 additions & 3 deletions nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class ledger_constants
{
public:
ledger_constants (nano::network_constants & network_constants);
ledger_constants (nano::nano_networks network_a);
ledger_constants (nano::networks network_a);
nano::keypair zero_key;
nano::keypair dev_genesis_key;
nano::account nano_dev_account;
Expand Down Expand Up @@ -473,9 +473,8 @@ class network_params
network_params ();

/** Populate values based on \p network_a */
network_params (nano::nano_networks network_a);
network_params (nano::networks network_a);

std::array<uint8_t, 2> header_magic_number;
unsigned kdf_work;
network_constants network;
protocol_constants protocol;
Expand Down
Loading