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

Add to_string functions to the classes asc_pull_req and asc_pull_ack #4255

Merged
merged 16 commits into from
Aug 30, 2023
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
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 &){};
};
}
}
Loading