From 7b468b2e0cc52a90e31602b862d74ff797b6c087 Mon Sep 17 00:00:00 2001 From: Dimitrios Siganos Date: Thu, 3 Feb 2022 14:21:36 +0000 Subject: [PATCH] Apply the timestamp mask to timestamp field to remove the duration bits (#3710) closes #3705 --- nano/core_test/vote_processor.cpp | 14 ++++++++++++++ nano/secure/common.cpp | 8 +++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index d262462c..080a98a5 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -319,3 +319,17 @@ TEST (vote_processor, no_broadcast_local_with_a_principal_representative) 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)); } + +/** + * basic test to check that the timestamp mask is applied correctly on vote timestamp and duration fields + */ +TEST (vote, timestamp_and_duration_masking) +{ + nano::system system; + nano::keypair key; + auto hash = std::vector{ nano::dev::genesis->hash () }; + auto vote = std::make_shared (key.pub, key.prv, 0x123f, 0xf, hash); + ASSERT_EQ (vote->timestamp (), 0x1230); + ASSERT_EQ (vote->duration ().count (), 524288); + ASSERT_EQ (vote->duration_bits (), 0xf); +} \ No newline at end of file diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 889e2ad2..7c9f5e7a 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -519,9 +519,15 @@ std::string nano::vote::to_json () const return stream.str (); } +/** + * 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; + return (timestamp_m == std::numeric_limits::max ()) + ? timestamp_m // final vote + : (timestamp_m & timestamp_mask); } uint8_t nano::vote::duration_bits () const