Skip to content

Commit

Permalink
Use enum as backend type
Browse files Browse the repository at this point in the history
# Conflicts:
#	nano/core_test/message.cpp
#	nano/core_test/telemetry.cpp
#	nano/node/telemetry.cpp
  • Loading branch information
pwojcikdev committed Aug 6, 2024
1 parent 07185c0 commit 595fcad
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 9 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ TEST (message, telemetry_data_serialization)
original.maker = 5;
original.timestamp = std::chrono::system_clock::time_point{ 123456789s };
original.active_difficulty = 42;
original.database_backend = 2;
original.database_backend = nano::telemetry_backend::rocksdb;
original.database_version_major = 3;
original.database_version_minor = 2;
original.database_version_patch = 1;
Expand Down
79 changes: 75 additions & 4 deletions nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,9 +1111,14 @@ void nano::telemetry_data::deserialize (nano::stream & stream, uint16_t payload_
read (stream, timestamp_l);
boost::endian::big_to_native_inplace (timestamp_l);
timestamp = std::chrono::system_clock::time_point (std::chrono::milliseconds (timestamp_l));

read (stream, active_difficulty);
boost::endian::big_to_native_inplace (active_difficulty);
read (stream, database_backend);

uint8_t database_backend_l;
read (stream, database_backend_l);
database_backend = static_cast<nano::telemetry_backend> (database_backend_l);

read (stream, database_version_major);
read (stream, database_version_minor);
read (stream, database_version_patch);
Expand Down Expand Up @@ -1144,7 +1149,7 @@ void nano::telemetry_data::serialize_without_signature (nano::stream & stream) c
write (stream, 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, database_backend);
write (stream, static_cast<std::underlying_type_t<nano::telemetry_backend>> (database_backend));
write (stream, database_version_major);
write (stream, database_version_minor);
write (stream, database_version_patch);
Expand Down Expand Up @@ -1175,7 +1180,7 @@ nano::error nano::telemetry_data::serialize_json (nano::jsonconfig & json, bool
json.put ("maker", maker); // TODO: This should be using a string representation
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", database_backend); // TODO: This should be using a string representation
json.put ("database_backend", to_string (database_backend));
json.put ("database_version_major", database_version_major);
json.put ("database_version_minor", database_version_minor);
json.put ("database_version_patch", database_version_patch);
Expand Down Expand Up @@ -1221,6 +1226,7 @@ nano::error nano::telemetry_data::deserialize_json (nano::jsonconfig & json, boo
json.get ("peer_count", peer_count);
json.get ("protocol_version", protocol_version);
json.get ("uptime", uptime);

std::string genesis_block_l;
json.get ("genesis_block", genesis_block_l);
if (!json.get_error ())
Expand All @@ -1230,17 +1236,24 @@ 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

auto timestamp_l = json.get<uint64_t> ("timestamp");
timestamp = std::chrono::system_clock::time_point (std::chrono::milliseconds (timestamp_l));

auto current_active_difficulty_text = json.get<std::string> ("active_difficulty");
auto ec = nano::from_string_hex (current_active_difficulty_text, active_difficulty);
debug_assert (!ec);
json.get ("database_backend", database_backend); // TODO: This should be using a string representation

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

json.get ("database_version_major", database_version_major);
json.get ("database_version_minor", database_version_minor);
json.get ("database_version_patch", database_version_patch);
Expand Down Expand Up @@ -1273,6 +1286,64 @@ void nano::telemetry_data::operator() (nano::object_stream & obs) const
// TODO: Telemetry data
}

std::string nano::to_string (nano::telemetry_maker maker)
{
switch (maker)
{
case nano::telemetry_maker::nf_node:
return "nf";
case nano::telemetry_maker::nf_pruned_node:
return "nf-pruned";
}
return "unknown (" + std::to_string (static_cast<std::underlying_type_t<nano::telemetry_maker>> (maker)) + ")";
}

nano::telemetry_maker nano::to_telemetry_maker (std::string str)
{
// Convert to lowercase
std::transform (str.begin (), str.end (), str.begin (), ::tolower);

if (str == "nf")
{
return nano::telemetry_maker::nf_node;
}
if (str == "nf-pruned")
{
return nano::telemetry_maker::nf_pruned_node;
}
return {}; // TODO: Enumeration should have a default "unknown" value, at this point it's probably too late to add it
}

std::string nano::to_string (nano::telemetry_backend backend)
{
switch (backend)
{
case nano::telemetry_backend::unknown:
return "Unknown";
case nano::telemetry_backend::lmdb:
return "LMDB";
case nano::telemetry_backend::rocksdb:
return "RocksDB";
}
return "Unknown (" + std::to_string (static_cast<std::underlying_type_t<nano::telemetry_backend>> (backend)) + ")";
}

nano::telemetry_backend nano::to_telemetry_backend (std::string str)
{
// Convert to lowercase
std::transform (str.begin (), str.end (), str.begin (), ::tolower);

if (str == "lmdb")
{
return nano::telemetry_backend::lmdb;
}
if (str == "rocksdb")
{
return nano::telemetry_backend::rocksdb;
}
return nano::telemetry_backend::unknown;
}

/*
* node_id_handshake
*/
Expand Down
8 changes: 7 additions & 1 deletion nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ enum class telemetry_backend : uint8_t
rocksdb = 2,
};

std::string to_string (telemetry_maker);
telemetry_maker to_telemetry_maker (std::string);

std::string to_string (telemetry_backend);
telemetry_backend to_telemetry_backend (std::string);

class telemetry_data
{
public: // Payload
Expand All @@ -338,7 +344,7 @@ class telemetry_data
uint8_t maker{ static_cast<std::underlying_type_t<telemetry_maker>> (telemetry_maker::nf_node) }; // Where this telemetry information originated
std::chrono::system_clock::time_point timestamp;
uint64_t active_difficulty{ 0 };
uint8_t database_backend{ 0 };
telemetry_backend database_backend{ telemetry_backend::unknown };
uint8_t database_version_major{ 0 };
uint8_t database_version_minor{ 0 };
uint8_t database_version_patch{ 0 };
Expand Down
4 changes: 2 additions & 2 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ nano::telemetry_data nano::node::local_telemetry () const
};

auto parse_backend_type = [] (std::string backend) -> telemetry_backend {
// Compare with lowercase
// Convert to lowercase
std::transform (backend.begin (), backend.end (), backend.begin (), ::tolower);

if (backend == "lmdb")
Expand Down Expand Up @@ -1399,7 +1399,7 @@ nano::telemetry_data nano::node::local_telemetry () const
telemetry_data.maker = static_cast<std::underlying_type_t<telemetry_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 = static_cast<std::underlying_type_t<telemetry_backend>> (backend_details.backend);
telemetry_data.database_backend = backend_details.backend;
telemetry_data.database_version_major = backend_details.version_major;
telemetry_data.database_version_minor = backend_details.version_minor;
telemetry_data.database_version_patch = backend_details.version_patch;
Expand Down
4 changes: 3 additions & 1 deletion nano/node/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include <numeric>
#include <set>

#include <fmt/core.h>

using namespace std::chrono_literals;

nano::telemetry::telemetry (const config & config_a, nano::node & node_a, nano::network & network_a, nano::node_observers & observers_a, nano::network_params & network_params_a, nano::stats & stats_a) :
Expand Down Expand Up @@ -296,4 +298,4 @@ std::unique_ptr<nano::container_info_component> nano::telemetry::collect_contain
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "telemetries", telemetries.size (), sizeof (decltype (telemetries)::value_type) }));
return composite;
}
}

0 comments on commit 595fcad

Please sign in to comment.