From 4a71cb42508f5df26f5befff3ba0e08b3e6c835d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 7 Nov 2023 18:29:43 +0100 Subject: [PATCH] Add tests --- nano/core_test/vote_cache.cpp | 23 ++++++++++++++++++++++- nano/node/vote_cache.cpp | 3 +++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/nano/core_test/vote_cache.cpp b/nano/core_test/vote_cache.cpp index 7cb543b9c1..30d1069836 100644 --- a/nano/core_test/vote_cache.cpp +++ b/nano/core_test/vote_cache.cpp @@ -348,4 +348,25 @@ TEST (vote_cache, overfill_entry) ASSERT_EQ (1, vote_cache.size ()); } -TEST (vote_cache, \ No newline at end of file +TEST (vote_cache, age_cutoff) +{ + nano::test::system system; + nano::vote_cache_config cfg; + cfg.age_cutoff = std::chrono::seconds{ 1 }; + nano::vote_cache vote_cache{ cfg, system.stats }; + vote_cache.rep_weight_query = rep_weight_query (); + + auto hash1 = nano::test::random_hash (); + auto rep1 = create_rep (9); + auto vote1 = nano::test::make_vote (rep1, { hash1 }, 3); + vote_cache.vote (vote1->hashes.front (), vote1); + ASSERT_EQ (1, vote_cache.size ()); + ASSERT_TRUE (vote_cache.find (hash1)); + + auto tops1 = vote_cache.top (0); + ASSERT_EQ (tops1.size (), 1); + ASSERT_EQ (tops1[0].hash, hash1); + + // Cleanup is performed periodically when calling `top ()` + ASSERT_TIMELY (5s, vote_cache.top (0).empty ()); +} \ No newline at end of file diff --git a/nano/node/vote_cache.cpp b/nano/node/vote_cache.cpp index f268db043e..0125515897 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/vote_cache.cpp @@ -120,6 +120,9 @@ nano::vote_cache::vote_cache (vote_cache_config const & config_a, nano::stats & void nano::vote_cache::vote (const nano::block_hash & hash, const std::shared_ptr vote) { + // Assert that supplied hash corresponds to a one of the hashes stored in vote + debug_assert (std::find (vote->hashes.begin (), vote->hashes.end (), hash) != vote->hashes.end ()); + auto const representative = vote->account; auto const timestamp = vote->timestamp (); auto const rep_weight = rep_weight_query (representative);