Skip to content

Commit

Permalink
Cleanup serialization/deserialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 6, 2023
1 parent 678230a commit 245cde7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 37 deletions.
4 changes: 1 addition & 3 deletions nano/core_test/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@ TEST (message, asc_pull_ack_serialization_blocks)
original.type = nano::asc_pull_type::blocks;

nano::asc_pull_ack::blocks_payload original_payload;
// Generate blocks
const int num_blocks = 128; // Maximum allowed
for (int n = 0; n < num_blocks; ++n)
for (int n = 0; n < nano::asc_pull_ack::blocks_payload::max_blocks; ++n)
{
original_payload.blocks.push_back (random_block ());
}
Expand Down
2 changes: 0 additions & 2 deletions nano/lib/numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ bool nano::public_key::decode_account (std::string const & source_a)
return error;
}

nano::uint256_union const nano::uint256_union::zero{ 0 };

nano::uint256_union::uint256_union (nano::uint256_t const & number_a)
{
bytes.fill (0);
Expand Down
2 changes: 0 additions & 2 deletions nano/lib/numbers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ class uint256_union
std::array<uint64_t, 4> qwords;
std::array<uint128_union, 2> owords;
};

static const uint256_union zero;
};
inline bool operator== (nano::uint256_union const & lhs, nano::uint256_union const & rhs)
{
Expand Down
49 changes: 25 additions & 24 deletions nano/node/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,7 @@ std::string nano::asc_pull_ack::to_string () const
void nano::asc_pull_ack::blocks_payload::serialize (nano::stream & stream) const
{
debug_assert (blocks.size () <= max_blocks);

for (auto & block : blocks)
{
debug_assert (block != nullptr);
Expand Down Expand Up @@ -1972,39 +1973,39 @@ void nano::asc_pull_ack::account_info_payload::deserialize (nano::stream & strea
* asc_pull_ack::frontiers_payload
*/

void nano::asc_pull_ack::frontiers_payload::serialize_frontier (nano::stream & stream, nano::asc_pull_ack::frontiers_payload::frontier const & frontier)
{
auto const & [account, hash] = frontier;
nano::write (stream, account);
nano::write (stream, hash);
}

nano::asc_pull_ack::frontiers_payload::frontier nano::asc_pull_ack::frontiers_payload::deserialize_frontier (nano::stream & stream)
{
nano::account account;
nano::block_hash hash;
nano::read (stream, account);
nano::read (stream, hash);
return { account, hash };
}

void nano::asc_pull_ack::frontiers_payload::serialize (nano::stream & stream) const
{
debug_assert (frontiers.size () <= max_frontiers);

for (auto const & [account, frontier] : frontiers)
for (auto const & frontier : frontiers)
{
nano::write (stream, account);
nano::write (stream, frontier);
serialize_frontier (stream, frontier);
}
nano::write (stream, nano::account::zero);
nano::write (stream, nano::block_hash::zero);
serialize_frontier (stream, { nano::account{ 0 }, nano::block_hash{ 0 } });
}

void nano::asc_pull_ack::frontiers_payload::deserialize (nano::stream & stream)
{
nano::account account;
nano::block_hash frontier;
while (true)
auto current = deserialize_frontier (stream);
while ((!current.first.is_zero () && !current.second.is_zero ()) && frontiers.size () < max_frontiers)
{
nano::read (stream, account);
nano::read (stream, frontier);
if (account.is_zero () || frontier.is_zero ())
{
break;
}
debug_assert (frontiers.size () < max_frontiers);
if (frontiers.size () < max_frontiers)
{
frontiers.emplace_back (account, frontier);
}
else
{
break;
}
frontiers.push_back (current);
current = deserialize_frontier (stream);
}
}
}
23 changes: 17 additions & 6 deletions nano/node/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ class asc_pull_req final : public message
void serialize (nano::stream &) const;
void deserialize (nano::stream &);

public: // Payload
nano::hash_or_account start{ 0 };
uint8_t count{ 0 };
hash_type start_type{ 0 };
Expand All @@ -459,6 +460,7 @@ class asc_pull_req final : public message
void serialize (nano::stream &) const;
void deserialize (nano::stream &);

public: // Payload
nano::hash_or_account target{ 0 };
hash_type target_type{ 0 };
};
Expand All @@ -468,6 +470,7 @@ class asc_pull_req final : public message
void serialize (nano::stream &) const;
void deserialize (nano::stream &);

public: // Payload
nano::account start{ 0 };
uint16_t count{ 0 };
};
Expand Down Expand Up @@ -520,20 +523,22 @@ class asc_pull_ack final : public message
public: // Payload definitions
struct blocks_payload
{
/* Header allows for 16 bit extensions; 65536 bytes / 500 bytes (block size with some future margin) ~ 131 */
constexpr static std::size_t max_blocks = 128;

void serialize (nano::stream &) const;
void deserialize (nano::stream &);

public: // Payload
std::vector<std::shared_ptr<nano::block>> blocks;

/* Header allows for 16 bit extensions; 65536 bytes / 500 bytes (block size with some future margin) ~ 131 */
constexpr static std::size_t max_blocks = 128;
};

struct account_info_payload
{
void serialize (nano::stream &) const;
void deserialize (nano::stream &);

public: // Payload
nano::account account{ 0 };
nano::block_hash account_open{ 0 };
nano::block_hash account_head{ 0 };
Expand All @@ -544,13 +549,19 @@ class asc_pull_ack final : public message

struct frontiers_payload
{
/* Header allows for 16 bit extensions; 65536 bytes / 64 bytes (account + frontier) ~ 1024, but we need some space for null frontier terminator*/
constexpr static std::size_t max_frontiers = 1000;

using frontier = std::pair<nano::account, nano::block_hash>;

void serialize (nano::stream &) const;
void deserialize (nano::stream &);

std::vector<std::pair<nano::account, nano::block_hash>> frontiers;
static void serialize_frontier (nano::stream &, frontier const &);
static frontier deserialize_frontier (nano::stream &);

/* Header allows for 16 bit extensions; 65536 bytes / 64 bytes (account + frontier) ~ 1024, but we need some space for null frontier terminator*/
constexpr static std::size_t max_frontiers = 1000;
public: // Payload
std::vector<frontier> frontiers;
};

public: // Payload
Expand Down

0 comments on commit 245cde7

Please sign in to comment.