From f58fbe42a114b58e9e27eb0e4b1d89b25ec5fb5c Mon Sep 17 00:00:00 2001 From: clemahieu Date: Wed, 27 Oct 2021 15:49:48 +0100 Subject: [PATCH] Encapsulating vote::timestamp field as vote::timestamp_m behind vote::timestamp (). --- nano/core_test/vote_processor.cpp | 6 +++--- nano/node/active_transactions.cpp | 6 +++--- nano/node/network.cpp | 2 +- nano/node/vote_processor.cpp | 2 +- nano/node/voting.cpp | 6 +++--- nano/secure/common.cpp | 29 +++++++++++++++++------------ nano/secure/common.hpp | 5 ++++- 7 files changed, 32 insertions(+), 24 deletions(-) diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index 4767199fa..3efdefab9 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -203,7 +203,7 @@ TEST (vote_processor, no_broadcast_local) auto votes (election->votes ()); auto existing (votes.find (nano::dev::genesis_key.pub)); ASSERT_NE (votes.end (), existing); - ASSERT_EQ (vote->timestamp, existing->second.timestamp); + ASSERT_EQ (vote->timestamp (), existing->second.timestamp); // Ensure the vote, from a local representative, was not broadcast on processing - it should be flooded on generation instead ASSERT_EQ (0, node.stats.count (nano::stat::type::message, nano::stat::detail::confirm_ack, nano::stat::dir::out)); ASSERT_EQ (1, node.stats.count (nano::stat::type::message, nano::stat::detail::publish, nano::stat::dir::out)); @@ -236,7 +236,7 @@ TEST (vote_processor, no_broadcast_local) auto votes2 (election2->votes ()); auto existing2 (votes2.find (nano::dev::genesis_key.pub)); ASSERT_NE (votes2.end (), existing2); - ASSERT_EQ (vote2->timestamp, existing2->second.timestamp); + ASSERT_EQ (vote2->timestamp (), existing2->second.timestamp); // Ensure the vote was broadcast ASSERT_EQ (1, node.stats.count (nano::stat::type::message, nano::stat::detail::confirm_ack, nano::stat::dir::out)); ASSERT_EQ (2, node.stats.count (nano::stat::type::message, nano::stat::detail::publish, nano::stat::dir::out)); @@ -270,7 +270,7 @@ TEST (vote_processor, no_broadcast_local) auto votes3 (election3->votes ()); auto existing3 (votes3.find (nano::dev::genesis_key.pub)); ASSERT_NE (votes3.end (), existing3); - ASSERT_EQ (vote3->timestamp, existing3->second.timestamp); + ASSERT_EQ (vote3->timestamp (), existing3->second.timestamp); // Ensure the vote wass not broadcasst ASSERT_EQ (1, node.stats.count (nano::stat::type::message, nano::stat::detail::confirm_ack, nano::stat::dir::out)); ASSERT_EQ (3, node.stats.count (nano::stat::type::message, nano::stat::detail::publish, nano::stat::dir::out)); diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index c4481a479..8c05ad938 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -875,7 +875,7 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr con } else if (recently_confirmed_by_hash.count (block_hash) == 0) { - add_inactive_votes_cache (lock, block_hash, vote_a->account, vote_a->timestamp); + add_inactive_votes_cache (lock, block_hash, vote_a->account, vote_a->timestamp ()); } else { @@ -892,7 +892,7 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr con } else if (recently_confirmed_by_hash.count (block->hash ()) == 0) { - add_inactive_votes_cache (lock, block->hash (), vote_a->account, vote_a->timestamp); + add_inactive_votes_cache (lock, block->hash (), vote_a->account, vote_a->timestamp ()); } else { @@ -908,7 +908,7 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr con bool processed (false); for (auto const & [election, block_hash] : process) { - auto const result_l = election->vote (vote_a->account, vote_a->timestamp, block_hash); + auto const result_l = election->vote (vote_a->account, vote_a->timestamp (), block_hash); processed = processed || result_l.processed; replay = replay || result_l.replay; } diff --git a/nano/node/network.cpp b/nano/node/network.cpp index aabca2a28..0b2ed5790 100644 --- a/nano/node/network.cpp +++ b/nano/node/network.cpp @@ -474,7 +474,7 @@ public: { if (node.config.logging.network_message_logging ()) { - node.logger.try_log (boost::str (boost::format ("Received confirm_ack message from %1% for %2% timestamp %3%") % channel->to_string () % message_a.vote->hashes_string () % std::to_string (message_a.vote->timestamp))); + node.logger.try_log (boost::str (boost::format ("Received confirm_ack message from %1% for %2% timestamp %3%") % channel->to_string () % message_a.vote->hashes_string () % std::to_string (message_a.vote->timestamp ()))); } node.stats.inc (nano::stat::type::message, nano::stat::detail::confirm_ack, nano::stat::dir::in); if (!message_a.vote->account.is_zero ()) diff --git a/nano/node/vote_processor.cpp b/nano/node/vote_processor.cpp index ac540b654..71efa2596 100644 --- a/nano/node/vote_processor.cpp +++ b/nano/node/vote_processor.cpp @@ -198,7 +198,7 @@ nano::vote_code nano::vote_processor::vote_blocking (std::shared_ptr } if (config.logging.vote_logging ()) { - logger.try_log (boost::str (boost::format ("Vote from: %1% timestamp: %2% block(s): %3%status: %4%") % vote_a->account.to_account () % std::to_string (vote_a->timestamp) % vote_a->hashes_string () % status)); + logger.try_log (boost::str (boost::format ("Vote from: %1% timestamp: %2% block(s): %3%status: %4%") % vote_a->account.to_account () % std::to_string (vote_a->timestamp ()) % vote_a->hashes_string () % status)); } return result; } diff --git a/nano/node/voting.cpp b/nano/node/voting.cpp index eed58a186..a1a4956f8 100644 --- a/nano/node/voting.cpp +++ b/nano/node/voting.cpp @@ -75,11 +75,11 @@ void nano::local_vote_history::add (nano::root const & root_a, nano::block_hash auto range (history_by_root.equal_range (root_a)); for (auto i (range.first); i != range.second;) { - if (i->hash != hash_a || (vote_a->account == i->vote->account && i->vote->timestamp <= vote_a->timestamp)) + if (i->hash != hash_a || (vote_a->account == i->vote->account && i->vote->timestamp () <= vote_a->timestamp ())) { i = history_by_root.erase (i); } - else if (vote_a->account == i->vote->account && i->vote->timestamp > vote_a->timestamp) + else if (vote_a->account == i->vote->account && i->vote->timestamp () > vote_a->timestamp ()) { add_vote = false; ++i; @@ -123,7 +123,7 @@ std::vector> nano::local_vote_history::votes (nano:: auto range (history.get ().equal_range (root_a)); // clang-format off nano::transform_if (range.first, range.second, std::back_inserter (result), - [&hash_a, is_final_a](auto const & entry) { return entry.hash == hash_a && (!is_final_a || entry.vote->timestamp == std::numeric_limits::max ()); }, + [&hash_a, is_final_a](auto const & entry) { return entry.hash == hash_a && (!is_final_a || entry.vote->timestamp () == std::numeric_limits::max ()); }, [](auto const & entry) { return entry.vote; }); // clang-format on return result; diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 4c3764e8f..613ef5178 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -473,7 +473,7 @@ bool nano::vote::operator== (nano::vote const & other_a) const } } } - return timestamp == other_a.timestamp && blocks_equal && account == other_a.account && signature == other_a.signature; + return timestamp_m == other_a.timestamp_m && blocks_equal && account == other_a.account && signature == other_a.signature; } bool nano::vote::operator!= (nano::vote const & other_a) const @@ -485,8 +485,8 @@ 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 ("sequence", std::to_string (timestamp ())); + tree.put ("timestamp", std::to_string (timestamp ())); boost::property_tree::ptree blocks_tree; for (auto block : blocks) { @@ -513,8 +513,13 @@ std::string nano::vote::to_json () const return stream.str (); } +uint64_t nano::vote::timestamp () const +{ + return timestamp_m; +} + nano::vote::vote (nano::vote const & other_a) : - timestamp{ other_a.timestamp }, + timestamp_m{ other_a.timestamp_m }, blocks (other_a.blocks), account (other_a.account), signature (other_a.signature) @@ -532,7 +537,7 @@ nano::vote::vote (bool & error_a, nano::stream & stream_a, nano::block_type type { nano::read (stream_a, account.bytes); nano::read (stream_a, signature.bytes); - nano::read (stream_a, timestamp); + nano::read (stream_a, timestamp_m); while (stream_a.in_avail () > 0) { @@ -565,7 +570,7 @@ nano::vote::vote (bool & error_a, nano::stream & stream_a, nano::block_type type } nano::vote::vote (nano::account const & account_a, nano::raw_key const & prv_a, uint64_t timestamp_a, std::shared_ptr const & block_a) : - timestamp{ timestamp_a }, + timestamp_m{ timestamp_a }, blocks (1, block_a), account (account_a), signature (nano::sign_message (prv_a, account_a, hash ())) @@ -573,7 +578,7 @@ nano::vote::vote (nano::account const & account_a, nano::raw_key const & prv_a, } nano::vote::vote (nano::account const & account_a, nano::raw_key const & prv_a, uint64_t timestamp_a, std::vector const & blocks_a) : - timestamp{ timestamp_a }, + timestamp_m{ timestamp_a }, account (account_a) { debug_assert (!blocks_a.empty ()); @@ -614,7 +619,7 @@ nano::block_hash nano::vote::hash () const uint64_t qword; std::array bytes; }; - qword = timestamp; + qword = timestamp_m; blake2b_update (&hash, bytes.data (), sizeof (bytes)); blake2b_final (&hash, result.bytes.data (), sizeof (result.bytes)); return result; @@ -636,7 +641,7 @@ void nano::vote::serialize (nano::stream & stream_a, nano::block_type type) cons { write (stream_a, account); write (stream_a, signature); - write (stream_a, boost::endian::native_to_little (timestamp)); + write (stream_a, boost::endian::native_to_little (timestamp_m)); for (auto const & block : blocks) { if (block.which ()) @@ -662,7 +667,7 @@ void nano::vote::serialize (nano::stream & stream_a) const { write (stream_a, account); write (stream_a, signature); - write (stream_a, boost::endian::native_to_little (timestamp)); + write (stream_a, boost::endian::native_to_little (timestamp_m)); for (auto const & block : blocks) { if (block.which ()) @@ -684,8 +689,8 @@ bool nano::vote::deserialize (nano::stream & stream_a, nano::block_uniquer * uni { nano::read (stream_a, account); nano::read (stream_a, signature); - nano::read (stream_a, timestamp); - boost::endian::little_to_native_inplace (timestamp); + nano::read (stream_a, timestamp_m); + boost::endian::little_to_native_inplace (timestamp_m); nano::block_type type; diff --git a/nano/secure/common.hpp b/nano/secure/common.hpp index 8461e32c3..92a93f3c9 100644 --- a/nano/secure/common.hpp +++ b/nano/secure/common.hpp @@ -264,8 +264,11 @@ public: boost::transform_iterator begin () const; boost::transform_iterator end () const; std::string to_json () const; + uint64_t timestamp () const; +private: // Vote timestamp - uint64_t timestamp; + uint64_t timestamp_m; +public: // The blocks, or block hashes, that this vote is for std::vector, nano::block_hash>> blocks; // Account that's voting