Skip to content

Commit

Permalink
Use enum as maker type
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Aug 6, 2024
1 parent 595fcad commit 009a4ba
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ TEST (message, telemetry_data_serialization)
original.minor_version = 3;
original.patch_version = 2;
original.pre_release_version = 1;
original.maker = 5;
original.maker = nano::telemetry_maker::nf_pruned_node;
original.timestamp = std::chrono::system_clock::time_point{ 123456789s };
original.active_difficulty = 42;
original.database_backend = nano::telemetry_backend::rocksdb;
Expand Down
5 changes: 2 additions & 3 deletions nano/core_test/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ TEST (telemetry, signatures)
data.minor_version = 1;
data.patch_version = 5;
data.pre_release_version = 2;
data.maker = 1;
data.maker = nano::telemetry_maker::nf_pruned_node;
data.timestamp = std::chrono::system_clock::time_point (100ms);
data.sign (node_id);
ASSERT_FALSE (data.validate_signature ());
auto signature = data.signature;
// Check that the signature is different if changing a piece of data
data.maker = 2;
data.maker = nano::telemetry_maker::nf_node;
data.sign (node_id);
ASSERT_NE (data.signature, signature);
}
Expand All @@ -39,7 +39,6 @@ TEST (telemetry, unknown_data)
data.minor_version = 1;
data.patch_version = 5;
data.pre_release_version = 2;
data.maker = 1;
data.timestamp = std::chrono::system_clock::time_point (100ms);
data.unknown_data.push_back (1);
data.sign (node_id);
Expand Down
18 changes: 12 additions & 6 deletions nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,10 @@ void nano::telemetry_data::deserialize (nano::stream & stream, uint16_t payload_
read (stream, minor_version);
read (stream, patch_version);
read (stream, pre_release_version);
read (stream, maker);

uint8_t maker_l;
read (stream, maker_l);
maker = static_cast<nano::telemetry_maker> (maker_l);

uint64_t timestamp_l;
read (stream, timestamp_l);
Expand Down Expand Up @@ -1146,7 +1149,7 @@ void nano::telemetry_data::serialize_without_signature (nano::stream & stream) c
write (stream, minor_version);
write (stream, patch_version);
write (stream, pre_release_version);
write (stream, maker);
write (stream, static_cast<std::underlying_type_t<nano::telemetry_maker>> (maker));
write (stream, boost::endian::native_to_big (std::chrono::duration_cast<std::chrono::milliseconds> (timestamp.time_since_epoch ()).count ()));
write (stream, boost::endian::native_to_big (active_difficulty));
write (stream, static_cast<std::underlying_type_t<nano::telemetry_backend>> (database_backend));
Expand Down Expand Up @@ -1177,7 +1180,7 @@ nano::error nano::telemetry_data::serialize_json (nano::jsonconfig & json, bool
json.put ("minor_version", minor_version);
json.put ("patch_version", patch_version);
json.put ("pre_release_version", pre_release_version);
json.put ("maker", maker); // TODO: This should be using a string representation
json.put ("maker", to_string (maker));
json.put ("timestamp", std::chrono::duration_cast<std::chrono::milliseconds> (timestamp.time_since_epoch ()).count ());
json.put ("active_difficulty", nano::to_string_hex (active_difficulty));
json.put ("database_backend", to_string (database_backend));
Expand Down Expand Up @@ -1236,12 +1239,15 @@ nano::error nano::telemetry_data::deserialize_json (nano::jsonconfig & json, boo
json.get_error ().set ("Could not deserialize genesis block");
}
}

json.get ("major_version", major_version);
json.get ("minor_version", minor_version);
json.get ("patch_version", patch_version);
json.get ("pre_release_version", pre_release_version);
json.get ("maker", maker); // TODO: This should be using a string representation

std::string maker_text;
json.get ("maker", maker_text);
maker = to_telemetry_maker (maker_text);

auto timestamp_l = json.get<uint64_t> ("timestamp");
timestamp = std::chrono::system_clock::time_point (std::chrono::milliseconds (timestamp_l));
Expand All @@ -1252,7 +1258,7 @@ nano::error nano::telemetry_data::deserialize_json (nano::jsonconfig & json, boo

std::string database_backend_text;
json.get ("database_backend", database_backend_text);
database_backend = nano::to_telemetry_backend (database_backend_text);
database_backend = to_telemetry_backend (database_backend_text);

json.get ("database_version_major", database_version_major);
json.get ("database_version_minor", database_version_minor);
Expand Down
5 changes: 4 additions & 1 deletion nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,15 @@ class frontier_req final : public message
void operator() (nano::object_stream &) const override;
};

// Indicates type of node
enum class telemetry_maker : uint8_t
{
// TODO: Default 0 value should be "unknown"
nf_node = 0,
nf_pruned_node = 1
};

// Indicates type of database backend
enum class telemetry_backend : uint8_t
{
unknown = 0,
Expand Down Expand Up @@ -341,7 +344,7 @@ class telemetry_data
uint8_t minor_version{ 0 };
uint8_t patch_version{ 0 };
uint8_t pre_release_version{ 0 };
uint8_t maker{ static_cast<std::underlying_type_t<telemetry_maker>> (telemetry_maker::nf_node) }; // Where this telemetry information originated
telemetry_maker maker{ telemetry_maker::nf_node };
std::chrono::system_clock::time_point timestamp;
uint64_t active_difficulty{ 0 };
telemetry_backend database_backend{ telemetry_backend::unknown };
Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1396,7 +1396,7 @@ nano::telemetry_data nano::node::local_telemetry () const
telemetry_data.minor_version = nano::get_minor_node_version ();
telemetry_data.patch_version = nano::get_patch_node_version ();
telemetry_data.pre_release_version = nano::get_pre_release_node_version ();
telemetry_data.maker = static_cast<std::underlying_type_t<telemetry_maker>> (ledger.pruning ? telemetry_maker::nf_pruned_node : telemetry_maker::nf_node);
telemetry_data.maker = ledger.pruning ? telemetry_maker::nf_pruned_node : telemetry_maker::nf_node;
telemetry_data.timestamp = std::chrono::system_clock::now ();
telemetry_data.active_difficulty = default_difficulty (nano::work_version::work_1);
telemetry_data.database_backend = backend_details.backend;
Expand Down
2 changes: 1 addition & 1 deletion nano/slow_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1632,7 +1632,7 @@ TEST (telemetry, many_nodes)
ASSERT_EQ (data.minor_version, nano::get_minor_node_version ());
ASSERT_EQ (data.patch_version, nano::get_patch_node_version ());
ASSERT_EQ (data.pre_release_version, nano::get_pre_release_node_version ());
ASSERT_EQ (data.maker, 0);
ASSERT_EQ (data.maker, nano::telemetry_maker::nf_node);
ASSERT_LT (data.uptime, 100);
ASSERT_EQ (data.genesis_block, nano::dev::genesis->hash ());
ASSERT_LE (data.timestamp, std::chrono::system_clock::now ());
Expand Down

0 comments on commit 009a4ba

Please sign in to comment.