From 3e0d54f7f7544d0d42cc0d79806598db42831f2c Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 14 Feb 2023 14:15:29 +0000 Subject: [PATCH] Adding election::behavior getter method, updating references, and adding direct unit test. (#4124) --- nano/core_test/election.cpp | 8 ++++++++ nano/node/active_transactions.cpp | 8 ++++---- nano/node/election.cpp | 9 +++++++-- nano/node/election.hpp | 3 ++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/nano/core_test/election.cpp b/nano/core_test/election.cpp index 72e642c1d..a3c854566 100644 --- a/nano/core_test/election.cpp +++ b/nano/core_test/election.cpp @@ -16,6 +16,14 @@ TEST (election, construction) election->transition_active (); } +TEST (election, behavior) +{ + nano::test::system system (1); + auto election = nano::test::start_election (system, *system.nodes[0], nano::dev::genesis->hash ()); + ASSERT_NE (nullptr, election); + ASSERT_EQ (nano::election_behavior::normal, election->behavior ()); +} + TEST (election, quorum_minimum_flip_success) { nano::test::system system{}; diff --git a/nano/node/active_transactions.cpp b/nano/node/active_transactions.cpp index b07a6298e..32d39b51e 100644 --- a/nano/node/active_transactions.cpp +++ b/nano/node/active_transactions.cpp @@ -260,7 +260,7 @@ void nano::active_transactions::cleanup_election (nano::unique_lock if (!election->confirmed ()) { node.stats.inc (nano::stat::type::election, nano::stat::detail::election_drop_all); - if (election->behavior == election_behavior::hinted) + if (election->behavior () == election_behavior::hinted) { node.stats.inc (nano::stat::type::election, nano::stat::detail::election_hinted_drop); } @@ -268,13 +268,13 @@ void nano::active_transactions::cleanup_election (nano::unique_lock else { node.stats.inc (nano::stat::type::election, nano::stat::detail::election_confirmed_all); - if (election->behavior == election_behavior::hinted) + if (election->behavior () == election_behavior::hinted) { node.stats.inc (nano::stat::type::election, nano::stat::detail::election_hinted_confirmed); } } - if (election->behavior == election_behavior::hinted) + if (election->behavior () == election_behavior::hinted) { --active_hinted_elections_count; } @@ -799,4 +799,4 @@ std::unique_ptr nano::recently_cemented_cache::c auto composite = std::make_unique (name); composite->add_component (std::make_unique (container_info{ "cemented", cemented.size (), sizeof (decltype (cemented)::value_type) })); return composite; -} \ No newline at end of file +} diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 4f72513b2..b1798c54d 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -22,7 +22,7 @@ nano::election::election (nano::node & node_a, std::shared_ptr cons confirmation_action (confirmation_action_a), live_vote_action (live_vote_action_a), node (node_a), - behavior (election_behavior_a), + behavior_m (election_behavior_a), status ({ block_a, 0, 0, std::chrono::duration_cast (std::chrono::system_clock::now ().time_since_epoch ()), std::chrono::duration_values::zero (), 0, 1, 0, nano::election_status_type::ongoing }), height (block_a->sideband ().height), root (block_a->root ()), @@ -220,7 +220,7 @@ bool nano::election::transition_time (nano::confirmation_solicitor & solicitor_a std::chrono::milliseconds nano::election::time_to_live () const { - switch (behavior) + switch (behavior ()) { case election_behavior::normal: return std::chrono::milliseconds (5 * 60 * 1000); @@ -636,3 +636,8 @@ std::vector nano::election::votes_with_weight () co std::transform (sorted_votes.begin (), sorted_votes.end (), std::back_inserter (result), [] (auto const & entry) { return entry.second; }); return result; } + +nano::election_behavior nano::election::behavior () const +{ + return behavior_m; +} diff --git a/nano/node/election.hpp b/nano/node/election.hpp index 4298e2f17..fa36c6570 100644 --- a/nano/node/election.hpp +++ b/nano/node/election.hpp @@ -142,6 +142,7 @@ public: // Information nano::root const root; nano::qualified_root const qualified_root; std::vector votes_with_weight () const; + nano::election_behavior behavior () const; private: nano::tally_t tally_impl () const; @@ -170,7 +171,7 @@ private: mutable nano::uint128_t final_weight{ 0 }; mutable std::unordered_map last_tally; - nano::election_behavior const behavior{ nano::election_behavior::normal }; + nano::election_behavior const behavior_m{ nano::election_behavior::normal }; std::chrono::steady_clock::time_point const election_start = { std::chrono::steady_clock::now () }; mutable nano::mutex mutex;