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

Remove vote-by-block support from vote class #3813

Merged
merged 5 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
95 changes: 11 additions & 84 deletions nano/secure/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,47 +557,9 @@ nano::vote::vote (nano::vote const & other_a) :
{
}

nano::vote::vote (bool & error_a, nano::stream & stream_a, nano::block_uniquer * uniquer_a)
{
error_a = deserialize (stream_a, uniquer_a);
}

nano::vote::vote (bool & error_a, nano::stream & stream_a, nano::block_type type_a, nano::block_uniquer * uniquer_a)
{
try
{
nano::read (stream_a, account.bytes);
nano::read (stream_a, signature.bytes);
nano::read (stream_a, timestamp_m);

while (stream_a.in_avail () > 0)
{
if (type_a == nano::block_type::not_a_block)
{
nano::block_hash block_hash;
nano::read (stream_a, block_hash);
blocks.push_back (block_hash);
}
else
{
auto block (nano::deserialize_block (stream_a, type_a, uniquer_a));
if (block == nullptr)
{
throw std::runtime_error ("Block is null");
}
blocks.push_back (block);
}
}
}
catch (std::runtime_error const &)
{
error_a = true;
}

if (blocks.empty ())
{
error_a = true;
}
error_a = deserialize (stream_a, type_a, uniquer_a);
}

nano::vote::vote (nano::account const & account_a, nano::raw_key const & prv_a, uint64_t timestamp_a, uint8_t duration, std::shared_ptr<nano::block> const & block_a) :
Expand Down Expand Up @@ -694,74 +656,39 @@ void nano::vote::serialize (nano::stream & stream_a, nano::block_type type) cons
}
}

void nano::vote::serialize (nano::stream & stream_a) const
bool nano::vote::deserialize (nano::stream & stream_a, nano::block_type type_a, nano::block_uniquer * uniquer_a)
{
write (stream_a, account);
write (stream_a, signature);
write (stream_a, boost::endian::native_to_little (timestamp_m));
for (auto const & block : blocks)
{
if (block.which ())
{
write (stream_a, nano::block_type::not_a_block);
write (stream_a, boost::get<nano::block_hash> (block));
}
else
{
nano::serialize_block (stream_a, *boost::get<std::shared_ptr<nano::block>> (block));
}
}
}

bool nano::vote::deserialize (nano::stream & stream_a, nano::block_uniquer * uniquer_a)
{
auto error (false);
auto result = false;
try
{
nano::read (stream_a, account);
nano::read (stream_a, signature);
nano::read (stream_a, account.bytes);
nano::read (stream_a, signature.bytes);
nano::read (stream_a, timestamp_m);
boost::endian::little_to_native_inplace (timestamp_m);

nano::block_type type;

while (true)
while (stream_a.in_avail () > 0)
{
if (nano::try_read (stream_a, type))
{
// Reached the end of the stream
break;
}

if (type == nano::block_type::not_a_block)
if (type_a == nano::block_type::not_a_block)
{
nano::block_hash block_hash;
nano::read (stream_a, block_hash);
blocks.push_back (block_hash);
}
else
{
auto block (nano::deserialize_block (stream_a, type, uniquer_a));
auto block (nano::deserialize_block (stream_a, type_a, uniquer_a));
if (block == nullptr)
{
throw std::runtime_error ("Block is empty");
throw std::runtime_error ("Block is null");
}

blocks.push_back (block);
}
}
}
catch (std::runtime_error const &)
{
error = true;
}

if (blocks.empty ())
{
error = true;
result = true;
clemahieu marked this conversation as resolved.
Show resolved Hide resolved
}

return error;
return result;
}

bool nano::vote::validate () const
Expand Down
2 changes: 1 addition & 1 deletion nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class vote final
void serialize (nano::stream &, nano::block_type) const;
void serialize (nano::stream &) const;
void serialize_json (boost::property_tree::ptree & tree) const;
bool deserialize (nano::stream &, nano::block_uniquer * = nullptr);
bool deserialize (nano::stream &, nano::block_type, nano::block_uniquer * = nullptr);
bool validate () const;
boost::transform_iterator<nano::iterate_vote_blocks_as_hash, nano::vote_blocks_vec_iter> begin () const;
boost::transform_iterator<nano::iterate_vote_blocks_as_hash, nano::vote_blocks_vec_iter> end () const;
Expand Down