Skip to content

Commit

Permalink
Add to_string functions to the classes asc_pull_req and asc_pull_ack (#…
Browse files Browse the repository at this point in the history
…4255)

Adding ::to_string methods to asc_pull_req and asc_pull_ack messages.
  • Loading branch information
brandon-bb committed Aug 30, 2023
1 parent da9cf6a commit b7dc1da
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
70 changes: 70 additions & 0 deletions nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <variant>
#include <vector>

/*
Expand Down Expand Up @@ -1656,6 +1658,35 @@ bool nano::asc_pull_req::verify_consistency () const
return true; // Just for convenience of calling from asserts
}

std::string nano::asc_pull_req::to_string () const
{
std::string s = header.to_string () + "\n";

std::visit ([&s] (auto && arg) {
using T = std::decay_t<decltype (arg)>;

if constexpr (std::is_same_v<T, nano::empty_payload>)
{
s += "missing payload";
}

else if constexpr (std::is_same_v<T, nano::asc_pull_req::blocks_payload>)
{
s += "acc:" + arg.start.to_string ();
s += " max block count:" + to_string_hex (static_cast<uint16_t> (arg.count));
s += " hash type:" + to_string_hex (static_cast<uint16_t> (arg.start_type));
}

else if constexpr (std::is_same_v<T, nano::asc_pull_req::account_info_payload>)
{
s += "target:" + arg.target.to_string ();
s += " hash type:" + to_string_hex (static_cast<uint16_t> (arg.target_type));
}
},
payload);

return s;
}
/*
* asc_pull_req::blocks_payload
*/
Expand Down Expand Up @@ -1809,6 +1840,45 @@ bool nano::asc_pull_ack::verify_consistency () const
return true; // Just for convenience of calling from asserts
}

std::string nano::asc_pull_ack::to_string () const
{
std::string s = header.to_string () + "\n";

std::visit ([&s] (auto && arg) {
using T = std::decay_t<decltype (arg)>;

if constexpr (std::is_same_v<T, nano::empty_payload>)
{
s += "missing payload";
}

else if constexpr (std::is_same_v<T, nano::asc_pull_ack::blocks_payload>)
{
auto block = std::begin (arg.blocks);
auto end_block = std::end (arg.blocks);

while (block != end_block)
{
s += (*block)->to_json ();
++block;
}
}

else if constexpr (std::is_same_v<T, nano::asc_pull_ack::account_info_payload>)
{
s += "account public key:" + arg.account.to_account ();
s += " account open:" + arg.account_open.to_string ();
s += " account head:" + arg.account_head.to_string ();
s += " block count:" + to_string_hex (arg.account_block_count);
s += " confirmation frontier:" + arg.account_conf_frontier.to_string ();
s += " confirmation height:" + to_string_hex (arg.account_conf_height);
}
},
payload);

return s;
}

/*
* asc_pull_ack::blocks_payload
*/
Expand Down
4 changes: 3 additions & 1 deletion nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ class asc_pull_req final : public message

void serialize_payload (nano::stream &) const;
void deserialize_payload (nano::stream &);
std::string to_string () const;

private: // Debug
/**
Expand Down Expand Up @@ -504,6 +505,7 @@ class asc_pull_ack final : public message

void serialize_payload (nano::stream &) const;
void deserialize_payload (nano::stream &);
std::string to_string () const;

private: // Debug
/**
Expand Down Expand Up @@ -613,4 +615,4 @@ class message_visitor
}
virtual void default_handler (nano::message const &){};
};
}
}

0 comments on commit b7dc1da

Please sign in to comment.