Skip to content

Commit

Permalink
Test telemetry data serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jul 10, 2024
1 parent 0ced680 commit f0f46a0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 28 deletions.
65 changes: 62 additions & 3 deletions nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ TEST (message, confirm_req_hash_batch_serialization_v2)
/**
* Test that a confirm_ack can encode an empty hash set
*/
TEST (confirm_ack, empty_vote_hashes)
TEST (message, confirm_ack_empty_vote_hashes)
{
nano::keypair key;
auto vote = std::make_shared<nano::vote> (key.pub, key.prv, 0, 0, std::vector<nano::block_hash>{} /* empty */);
Expand Down Expand Up @@ -781,7 +781,7 @@ TEST (message, node_id_handshake_response_v2_serialization)
ASSERT_TRUE (nano::at_end (stream));
}

TEST (handshake, signature)
TEST (message, handshake_signature)
{
nano::keypair node_id{};
nano::keypair node_id_2{};
Expand All @@ -801,7 +801,7 @@ TEST (handshake, signature)
ASSERT_FALSE (response.validate (cookie));
}

TEST (handshake, signature_v2)
TEST (message, handshake_signature_v2)
{
nano::keypair node_id{};
nano::keypair node_id_2{};
Expand Down Expand Up @@ -843,3 +843,62 @@ TEST (handshake, signature_v2)
ASSERT_FALSE (message.validate (cookie));
}
}

TEST (message, telemetry_data_serialization)
{
nano::telemetry_data original;
original.node_id = nano::account{ 11111 };
original.account_count = 22222;
original.block_count = 33333;
original.cemented_count = 44444;
original.bandwidth_cap = 55555;
original.account_count = 66666;
original.bandwidth_cap = 77777;
original.uptime = 88888;
original.peer_count = 99999;
original.protocol_version = 1;
original.genesis_block = nano::block_hash{ 22222 };
original.major_version = 4;
original.minor_version = 3;
original.patch_version = 2;
original.pre_release_version = 1;
original.maker = 5;
original.timestamp = std::chrono::system_clock::time_point{ 123456789s };
original.active_difficulty = 42;
original.database_backend = "Test";

// Serialize
std::vector<uint8_t> bytes;
{
nano::vectorstream stream{ bytes };
original.serialize (stream);
}
nano::bufferstream stream{ bytes.data (), bytes.size () };

nano::telemetry_data telemetry;
ASSERT_NO_THROW (telemetry.deserialize (stream, bytes.size ()));

// Compare
ASSERT_EQ (original.node_id, telemetry.node_id);
ASSERT_EQ (original.account_count, telemetry.account_count);
ASSERT_EQ (original.block_count, telemetry.block_count);
ASSERT_EQ (original.cemented_count, telemetry.cemented_count);
ASSERT_EQ (original.bandwidth_cap, telemetry.bandwidth_cap);
ASSERT_EQ (original.account_count, telemetry.account_count);
ASSERT_EQ (original.bandwidth_cap, telemetry.bandwidth_cap);
ASSERT_EQ (original.uptime, telemetry.uptime);
ASSERT_EQ (original.peer_count, telemetry.peer_count);
ASSERT_EQ (original.protocol_version, telemetry.protocol_version);
ASSERT_EQ (original.genesis_block, telemetry.genesis_block);
ASSERT_EQ (original.major_version, telemetry.major_version);
ASSERT_EQ (original.minor_version, telemetry.minor_version);
ASSERT_EQ (original.patch_version, telemetry.patch_version);
ASSERT_EQ (original.pre_release_version, telemetry.pre_release_version);
ASSERT_EQ (original.maker, telemetry.maker);
ASSERT_EQ (original.timestamp, telemetry.timestamp);
ASSERT_EQ (original.active_difficulty, telemetry.active_difficulty);
ASSERT_EQ (original.database_backend, telemetry.database_backend);
ASSERT_EQ (original, telemetry);

ASSERT_EQ (nano::telemetry_data::size, bytes.size ());
}
6 changes: 1 addition & 5 deletions nano/core_test/telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,6 @@ TEST (telemetry, consolidate_data)
ASSERT_TRUE (consolidated_telemetry_data1.protocol_version == 11 || consolidated_telemetry_data1.protocol_version == 12 || consolidated_telemetry_data1.protocol_version == 13);
ASSERT_EQ (consolidated_telemetry_data1.bandwidth_cap, 51);
ASSERT_EQ (consolidated_telemetry_data1.genesis_block, nano::block_hash (3));

// Test equality operator
ASSERT_FALSE (consolidated_telemetry_data == consolidated_telemetry_data1);
ASSERT_EQ (consolidated_telemetry_data, consolidated_telemetry_data);
}

TEST (telemetry, consolidate_data_remove_outliers)
Expand Down Expand Up @@ -433,7 +429,7 @@ TEST (telemetry, max_possible_size)
auto node_server = system.add_node (node_flags);

nano::telemetry_data data;
data.unknown_data.resize (nano::message_header::telemetry_size_mask.to_ulong () - nano::telemetry_data::latest_size);
data.unknown_data.resize (nano::message_header::telemetry_size_mask.to_ulong () - nano::telemetry_data::size);

nano::telemetry_ack message{ nano::dev::network_params.network, data };
nano::test::wait_peer_connections (system);
Expand Down
14 changes: 2 additions & 12 deletions nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,9 +1105,9 @@ void nano::telemetry_data::deserialize (nano::stream & stream_a, uint16_t payloa
timestamp = std::chrono::system_clock::time_point (std::chrono::milliseconds (timestamp_l));
read (stream_a, active_difficulty);
boost::endian::big_to_native_inplace (active_difficulty);
if (payload_length_a > latest_size)
if (payload_length_a > size)
{
read (stream_a, unknown_data, payload_length_a - latest_size);
read (stream_a, unknown_data, payload_length_a - size);
}
}

Expand Down Expand Up @@ -1223,16 +1223,6 @@ nano::error nano::telemetry_data::deserialize_json (nano::jsonconfig & json, boo
return json.get_error ();
}

bool nano::telemetry_data::operator== (nano::telemetry_data const & data_a) const
{
return (signature == data_a.signature && node_id == data_a.node_id && block_count == data_a.block_count && cemented_count == data_a.cemented_count && unchecked_count == data_a.unchecked_count && account_count == data_a.account_count && bandwidth_cap == data_a.bandwidth_cap && uptime == data_a.uptime && peer_count == data_a.peer_count && protocol_version == data_a.protocol_version && genesis_block == data_a.genesis_block && major_version == data_a.major_version && minor_version == data_a.minor_version && patch_version == data_a.patch_version && pre_release_version == data_a.pre_release_version && maker == data_a.maker && timestamp == data_a.timestamp && active_difficulty == data_a.active_difficulty && unknown_data == data_a.unknown_data);
}

bool nano::telemetry_data::operator!= (nano::telemetry_data const & data_a) const
{
return !(*this == data_a);
}

void nano::telemetry_data::sign (nano::keypair const & node_id_a)
{
debug_assert (node_id == node_id_a.pub);
Expand Down
22 changes: 14 additions & 8 deletions nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ enum class telemetry_maker : uint8_t

class telemetry_data
{
public:
public: // Payload
nano::signature signature{ 0 };
nano::account node_id{};
uint64_t block_count{ 0 };
Expand All @@ -327,18 +327,22 @@ class telemetry_data
std::string database_backend{ "Unknown" };
std::vector<uint8_t> unknown_data;

public:
void serialize (nano::stream &) const;
void deserialize (nano::stream &, uint16_t);
nano::error serialize_json (nano::jsonconfig &, bool) const;
nano::error deserialize_json (nano::jsonconfig &, bool);
void deserialize (nano::stream &, uint16_t payload_length);

nano::error serialize_json (nano::jsonconfig &, bool ignore_identification_metrics) const;
nano::error deserialize_json (nano::jsonconfig &, bool ignore_identification_metrics);

void sign (nano::keypair const &);
bool validate_signature () const;
bool operator== (nano::telemetry_data const &) const;
bool operator!= (nano::telemetry_data const &) const;

bool operator== (nano::telemetry_data const &) const = default;
bool operator!= (nano::telemetry_data const &) const = default;

// Size does not include unknown_data
static auto constexpr size = sizeof (signature) + sizeof (node_id) + sizeof (block_count) + sizeof (cemented_count) + sizeof (unchecked_count) + sizeof (account_count) + sizeof (bandwidth_cap) + sizeof (peer_count) + sizeof (protocol_version) + sizeof (uptime) + sizeof (genesis_block) + sizeof (major_version) + sizeof (minor_version) + sizeof (patch_version) + sizeof (pre_release_version) + sizeof (maker) + sizeof (uint64_t) + sizeof (active_difficulty);
static auto constexpr latest_size = size; // This needs to be updated for each new telemetry version
// This needs to be updated for each new telemetry version
static size_t constexpr size = sizeof (signature) + sizeof (node_id) + sizeof (block_count) + sizeof (cemented_count) + sizeof (unchecked_count) + sizeof (account_count) + sizeof (bandwidth_cap) + sizeof (peer_count) + sizeof (protocol_version) + sizeof (uptime) + sizeof (genesis_block) + sizeof (major_version) + sizeof (minor_version) + sizeof (patch_version) + sizeof (pre_release_version) + sizeof (maker) + sizeof (uint64_t) + sizeof (active_difficulty);

private:
void serialize_without_signature (nano::stream &) const;
Expand Down Expand Up @@ -372,6 +376,8 @@ class telemetry_ack final : public message
uint16_t size () const;
bool is_empty_payload () const;
static uint16_t size (nano::message_header const &);

public: // Payload
nano::telemetry_data data;

public: // Logging
Expand Down

0 comments on commit f0f46a0

Please sign in to comment.