From 9e815ef9294294a03477c32d61e4ee5eb638689b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Fri, 8 Mar 2024 10:20:51 +0100 Subject: [PATCH] Simplify testcase --- nano/core_test/vote_processor.cpp | 22 ++++------------------ nano/node/vote_processor.cpp | 26 +++++++++++++++++--------- nano/node/vote_processor.hpp | 25 ++++++++++++------------- 3 files changed, 33 insertions(+), 40 deletions(-) diff --git a/nano/core_test/vote_processor.cpp b/nano/core_test/vote_processor.cpp index f811ba9e4f..53fc8213f3 100644 --- a/nano/core_test/vote_processor.cpp +++ b/nano/core_test/vote_processor.cpp @@ -125,8 +125,6 @@ TEST (vote_processor, overflow) ASSERT_LT (std::chrono::system_clock::now () - start_time, 10s); } -namespace nano -{ TEST (vote_processor, weights) { nano::test::system system (4); @@ -158,22 +156,10 @@ TEST (vote_processor, weights) ASSERT_TIMELY_EQ (5s, node.online_reps.online (), total); node.vote_processor.calculate_weights (); - ASSERT_EQ (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (key0.pub)); - ASSERT_EQ (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (key0.pub)); - ASSERT_EQ (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (key0.pub)); - - ASSERT_NE (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (key1.pub)); - ASSERT_EQ (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (key1.pub)); - ASSERT_EQ (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (key1.pub)); - - ASSERT_NE (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (key2.pub)); - ASSERT_NE (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (key2.pub)); - ASSERT_EQ (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (key2.pub)); - - ASSERT_NE (node.vote_processor.representatives_1.end (), node.vote_processor.representatives_1.find (nano::dev::genesis_key.pub)); - ASSERT_NE (node.vote_processor.representatives_2.end (), node.vote_processor.representatives_2.find (nano::dev::genesis_key.pub)); - ASSERT_NE (node.vote_processor.representatives_3.end (), node.vote_processor.representatives_3.find (nano::dev::genesis_key.pub)); -} + ASSERT_EQ (node.vote_processor.representative_tier (key0.pub), nano::representative_tier::none); + ASSERT_EQ (node.vote_processor.representative_tier (key1.pub), nano::representative_tier::tier_1); + ASSERT_EQ (node.vote_processor.representative_tier (key2.pub), nano::representative_tier::tier_2); + ASSERT_EQ (node.vote_processor.representative_tier (nano::dev::genesis_key.pub), nano::representative_tier::tier_3); } // Issue that tracks last changes on this test: https://github.com/nanocurrency/nano-node/issues/3485 diff --git a/nano/node/vote_processor.cpp b/nano/node/vote_processor.cpp index 5d3f061b0c..5196ca7d49 100644 --- a/nano/node/vote_processor.cpp +++ b/nano/node/vote_processor.cpp @@ -104,21 +104,29 @@ void nano::vote_processor::run () } } -auto nano::vote_processor::representative_tier (const nano::account & representative) const -> rep_tier +nano::representative_tier nano::vote_processor::representative_tier (const nano::account & representative) const { + nano::lock_guard guard{ mutex }; + return representative_tier_locked (representative); +} + +nano::representative_tier nano::vote_processor::representative_tier_locked (const nano::account & representative) const +{ + debug_assert (!mutex.try_lock ()); + if (representatives_3.find (representative) != representatives_3.end ()) { - return rep_tier::tier_3; + return nano::representative_tier::tier_3; } if (representatives_2.find (representative) != representatives_2.end ()) { - return rep_tier::tier_2; + return nano::representative_tier::tier_2; } if (representatives_1.find (representative) != representatives_1.end ()) { - return rep_tier::tier_1; + return nano::representative_tier::tier_1; } - return rep_tier::tier_none; + return nano::representative_tier::none; } bool nano::vote_processor::vote (std::shared_ptr const & vote_a, std::shared_ptr const & channel_a) @@ -128,7 +136,7 @@ bool nano::vote_processor::vote (std::shared_ptr const & vote_a, std nano::unique_lock lock{ mutex }; if (!stopped) { - auto tier = representative_tier (vote_a->account); + auto tier = representative_tier_locked (vote_a->account); // Level 0 (< 0.1%) if (votes.size () < 6.0 / 9.0 * max_votes) @@ -138,17 +146,17 @@ bool nano::vote_processor::vote (std::shared_ptr const & vote_a, std // Level 1 (0.1-1%) else if (votes.size () < 7.0 / 9.0 * max_votes) { - process = (tier == rep_tier::tier_1); + process = (tier == nano::representative_tier::tier_1); } // Level 2 (1-5%) else if (votes.size () < 8.0 / 9.0 * max_votes) { - process = (tier == rep_tier::tier_2); + process = (tier == nano::representative_tier::tier_2); } // Level 3 (> 5%) else if (votes.size () < max_votes) { - process = (tier == rep_tier::tier_3); + process = (tier == nano::representative_tier::tier_3); } if (process) { diff --git a/nano/node/vote_processor.hpp b/nano/node/vote_processor.hpp index 35e14e61d1..34c5a654cf 100644 --- a/nano/node/vote_processor.hpp +++ b/nano/node/vote_processor.hpp @@ -32,6 +32,15 @@ namespace transport class channel; } +// Higher number means higher priority +enum class representative_tier +{ + none, // Not a principal representative + tier_1, // (0.1-1%) of online stake + tier_2, // (1-5%) of online stake + tier_3, // (> 5%) of online stake +}; + class vote_processor final { public: @@ -60,6 +69,8 @@ class vote_processor final bool empty () const; void calculate_weights (); + nano::representative_tier representative_tier (nano::account const & representative) const; + std::atomic total_processed{ 0 }; private: // Dependencies @@ -76,18 +87,7 @@ class vote_processor final private: void run (); void verify_votes (std::deque, std::shared_ptr>> const &); - -private: - // Higher number means higher priority - enum class rep_tier - { - tier_none, // Not a principal representative - tier_1, // (0.1-1%) - tier_2, // (1-5%) - tier_3, // (> 5%) - }; - - rep_tier representative_tier (nano::account const & representative) const; + nano::representative_tier representative_tier_locked (nano::account const & representative) const; private: std::size_t const max_votes; @@ -105,7 +105,6 @@ class vote_processor final std::thread thread; friend std::unique_ptr collect_container_info (vote_processor & vote_processor, std::string const & name); - friend class vote_processor_weights_Test; }; std::unique_ptr collect_container_info (vote_processor & vote_processor, std::string const & name);