197 lines
5.5 KiB
C++
197 lines
5.5 KiB
C++
#include <nano/lib/stream.hpp>
|
|
#include <nano/lib/utility.hpp>
|
|
#include <nano/secure/common.hpp>
|
|
#include <nano/secure/vote.hpp>
|
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
|
|
nano::vote::vote (bool & error_a, nano::stream & stream_a)
|
|
{
|
|
error_a = deserialize (stream_a);
|
|
}
|
|
|
|
nano::vote::vote (nano::account const & account_a, nano::raw_key const & prv_a, uint64_t timestamp_a, uint8_t duration, std::vector<nano::block_hash> const & hashes) :
|
|
hashes{ hashes },
|
|
timestamp_m{ packed_timestamp (timestamp_a, duration) },
|
|
account{ account_a }
|
|
{
|
|
debug_assert (hashes.size () <= max_hashes);
|
|
|
|
signature = nano::sign_message (prv_a, account_a, hash ());
|
|
}
|
|
|
|
void nano::vote::serialize (nano::stream & stream_a) const
|
|
{
|
|
debug_assert (hashes.size () <= max_hashes);
|
|
|
|
write (stream_a, account);
|
|
write (stream_a, signature);
|
|
write (stream_a, boost::endian::native_to_little (timestamp_m));
|
|
for (auto const & hash : hashes)
|
|
{
|
|
write (stream_a, hash);
|
|
}
|
|
}
|
|
|
|
bool nano::vote::deserialize (nano::stream & stream_a)
|
|
{
|
|
auto error = false;
|
|
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 && hashes.size () < max_hashes)
|
|
{
|
|
nano::block_hash block_hash;
|
|
nano::read (stream_a, block_hash);
|
|
hashes.push_back (block_hash);
|
|
}
|
|
}
|
|
catch (std::runtime_error const &)
|
|
{
|
|
error = true;
|
|
}
|
|
return error;
|
|
}
|
|
|
|
std::size_t nano::vote::size (uint8_t count)
|
|
{
|
|
debug_assert (count <= max_hashes);
|
|
return partial_size + count * sizeof (nano::block_hash);
|
|
}
|
|
|
|
std::string const nano::vote::hash_prefix = "vote ";
|
|
|
|
nano::block_hash nano::vote::hash () const
|
|
{
|
|
nano::block_hash result;
|
|
blake2b_state hash;
|
|
blake2b_init (&hash, sizeof (result.bytes));
|
|
blake2b_update (&hash, hash_prefix.data (), hash_prefix.size ());
|
|
for (auto const & block_hash : hashes)
|
|
{
|
|
blake2b_update (&hash, block_hash.bytes.data (), sizeof (block_hash.bytes));
|
|
}
|
|
union
|
|
{
|
|
uint64_t qword;
|
|
std::array<uint8_t, 8> bytes;
|
|
};
|
|
qword = timestamp_m;
|
|
blake2b_update (&hash, bytes.data (), sizeof (bytes));
|
|
blake2b_final (&hash, result.bytes.data (), sizeof (result.bytes));
|
|
return result;
|
|
}
|
|
|
|
nano::block_hash nano::vote::full_hash () const
|
|
{
|
|
nano::block_hash result;
|
|
blake2b_state state;
|
|
blake2b_init (&state, sizeof (result.bytes));
|
|
blake2b_update (&state, hash ().bytes.data (), sizeof (hash ().bytes));
|
|
blake2b_update (&state, account.bytes.data (), sizeof (account.bytes.data ()));
|
|
blake2b_update (&state, signature.bytes.data (), sizeof (signature.bytes.data ()));
|
|
blake2b_final (&state, result.bytes.data (), sizeof (result.bytes));
|
|
return result;
|
|
}
|
|
|
|
bool nano::vote::validate () const
|
|
{
|
|
return nano::validate_message (account, hash (), signature);
|
|
}
|
|
|
|
bool nano::vote::operator== (nano::vote const & other_a) const
|
|
{
|
|
return timestamp_m == other_a.timestamp_m && hashes == other_a.hashes && account == other_a.account && signature == other_a.signature;
|
|
}
|
|
|
|
bool nano::vote::operator!= (nano::vote const & other_a) const
|
|
{
|
|
return !(*this == other_a);
|
|
}
|
|
|
|
/**
|
|
* Returns the timestamp of the vote (with the duration bits masked, set to zero)
|
|
* If it is a final vote, all the bits including duration bits are returned as they are, all FF
|
|
*/
|
|
uint64_t nano::vote::timestamp () const
|
|
{
|
|
return (timestamp_m == std::numeric_limits<uint64_t>::max ())
|
|
? timestamp_m // final vote
|
|
: (timestamp_m & timestamp_mask);
|
|
}
|
|
|
|
uint8_t nano::vote::duration_bits () const
|
|
{
|
|
// Duration field is specified in the 4 low-order bits of the timestamp.
|
|
// This makes the timestamp have a minimum granularity of 16ms
|
|
// The duration is specified as 2^(duration + 4) giving it a range of 16-524,288ms in power of two increments
|
|
auto result = timestamp_m & ~timestamp_mask;
|
|
debug_assert (result < 16);
|
|
return static_cast<uint8_t> (result);
|
|
}
|
|
|
|
std::chrono::milliseconds nano::vote::duration () const
|
|
{
|
|
return std::chrono::milliseconds{ 1u << (duration_bits () + 4) };
|
|
}
|
|
|
|
bool nano::vote::is_final () const
|
|
{
|
|
return is_final_timestamp (timestamp_m);
|
|
}
|
|
|
|
void nano::vote::serialize_json (boost::property_tree::ptree & tree) const
|
|
{
|
|
tree.put ("account", account.to_account ());
|
|
tree.put ("signature", signature.number ());
|
|
tree.put ("sequence", std::to_string (timestamp ()));
|
|
tree.put ("timestamp", std::to_string (timestamp ()));
|
|
tree.put ("duration", std::to_string (duration_bits ()));
|
|
boost::property_tree::ptree blocks_tree;
|
|
for (auto const & hash : hashes)
|
|
{
|
|
boost::property_tree::ptree entry;
|
|
entry.put ("", hash.to_string ());
|
|
blocks_tree.push_back (std::make_pair ("", entry));
|
|
}
|
|
tree.add_child ("blocks", blocks_tree);
|
|
}
|
|
|
|
std::string nano::vote::to_json () const
|
|
{
|
|
std::stringstream stream;
|
|
boost::property_tree::ptree tree;
|
|
serialize_json (tree);
|
|
boost::property_tree::write_json (stream, tree);
|
|
return stream.str ();
|
|
}
|
|
|
|
std::string nano::vote::hashes_string () const
|
|
{
|
|
return nano::util::join (hashes, ", ", [] (auto const & hash) {
|
|
return hash.to_string ();
|
|
});
|
|
}
|
|
|
|
uint64_t nano::vote::packed_timestamp (uint64_t timestamp, uint8_t duration)
|
|
{
|
|
debug_assert (duration <= duration_max && "Invalid duration");
|
|
debug_assert ((!(timestamp == timestamp_max) || (duration == duration_max)) && "Invalid final vote");
|
|
return (timestamp & timestamp_mask) | duration;
|
|
}
|
|
|
|
bool nano::vote::is_final_timestamp (uint64_t timestamp)
|
|
{
|
|
return timestamp == std::numeric_limits<uint64_t>::max ();
|
|
}
|
|
|
|
void nano::vote::operator() (nano::object_stream & obs) const
|
|
{
|
|
obs.write ("account", account);
|
|
obs.write ("final", is_final_timestamp (timestamp_m));
|
|
obs.write ("timestamp", timestamp_m);
|
|
obs.write_range ("hashes", hashes);
|
|
}
|