Apply the timestamp mask to timestamp field to remove the duration bits (#3710)

closes #3705
This commit is contained in:
Dimitrios Siganos 2022-02-03 14:21:36 +00:00 committed by GitHub
commit 7b468b2e0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View file

@ -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::block_hash>{ nano::dev::genesis->hash () };
auto vote = std::make_shared<nano::vote> (key.pub, key.prv, 0x123f, 0xf, hash);
ASSERT_EQ (vote->timestamp (), 0x1230);
ASSERT_EQ (vote->duration ().count (), 524288);
ASSERT_EQ (vote->duration_bits (), 0xf);
}

View file

@ -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<uint64_t>::max ())
? timestamp_m // final vote
: (timestamp_m & timestamp_mask);
}
uint8_t nano::vote::duration_bits () const