From ae18ea2874aa95e306cec498018cb1dda0403df8 Mon Sep 17 00:00:00 2001 From: gr0vity-dev Date: Sat, 30 Nov 2024 09:56:53 +0100 Subject: [PATCH] squash #PR 4799 Make election behaviour mutable Add transition_priority method to election class Make use of transition_priority() in active_elections Add testcase election_scheduler.transition_optimistic_to_priority --- nano/core_test/election_scheduler.cpp | 53 +++++++++++++++++++++++++++ nano/lib/stats_enums.hpp | 2 + nano/node/active_elections.cpp | 30 ++++++++++++++- nano/node/election.cpp | 24 +++++++++++- nano/node/election.hpp | 3 +- 5 files changed, 107 insertions(+), 5 deletions(-) diff --git a/nano/core_test/election_scheduler.cpp b/nano/core_test/election_scheduler.cpp index cd2d642ed6..5f6a147e9e 100644 --- a/nano/core_test/election_scheduler.cpp +++ b/nano/core_test/election_scheduler.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -157,6 +158,58 @@ TEST (election_scheduler, activate_one_flush) ASSERT_TIMELY (5s, node.active.election (send1->qualified_root ())); } +/* + * Tests that an optimistic election can be transitioned to a priority election. + * + * The test: + * 1. Creates a chain of 2 blocks with an optimistic election for the second block + * 2. Confirms the first block in the chain + * 3. Attempts to start a priority election for the second block + * 4. Verifies that the existing optimistic election is transitioned to priority + * 5. Verifies a new vote is broadcast after the transition + */ +TEST (election_scheduler, transition_optimistic_to_priority) +{ + nano::test::system system; + nano::node_config config = system.default_config (); + config.optimistic_scheduler.gap_threshold = 1; + config.enable_voting = true; + config.hinted_scheduler.enable = false; + config.network_params.network.vote_broadcast_interval = 15000ms; + auto & node = *system.add_node (config); + + // Add representative + const nano::uint128_t rep_weight = nano::Knano_ratio * 100; + nano::keypair rep = nano::test::setup_rep (system, node, rep_weight); + system.wallet (0)->insert_adhoc (rep.prv); + + // Create a chain of blocks - and trigger an optimistic election for the last block + const int howmany_blocks = 2; + auto chains = nano::test::setup_chains (system, node, /* single chain */ 1, howmany_blocks, nano::dev::genesis_key, /* do not confirm */ false); + auto & [account, blocks] = chains.front (); + + // Wait for optimistic election to start for last block + auto const & block = blocks.back (); + ASSERT_TIMELY (5s, node.vote_router.active (block->hash ())); + auto election = node.active.election (block->qualified_root ()); + ASSERT_EQ (election->behavior (), nano::election_behavior::optimistic); + + // Confirm first block to allow upgrading second block's election + nano::test::confirm (node.ledger, blocks.at (howmany_blocks - 1)); + + // Attempt to start priority election for second block + node.stats.clear (); + ASSERT_EQ (0, node.stats.count (nano::stat::type::election, nano::stat::detail::broadcast_vote)); + node.active.insert (block, nano::election_behavior::priority); + + // Verify priority transition + ASSERT_EQ (election->behavior (), nano::election_behavior::priority); + ASSERT_EQ (1, node.stats.count (nano::stat::type::active_elections, nano::stat::detail::transition_priority)); + // Verify vote broadcast after transitioning + ASSERT_TIMELY_EQ (1s, 1, node.stats.count (nano::stat::type::election, nano::stat::detail::broadcast_vote)); + ASSERT_TRUE (node.active.active (*block)); +} + /** * Tests that the election scheduler and the active transactions container (AEC) * work in sync with regards to the node configuration value "active_elections.size". diff --git a/nano/lib/stats_enums.hpp b/nano/lib/stats_enums.hpp index d6c922ff9c..be16534df3 100644 --- a/nano/lib/stats_enums.hpp +++ b/nano/lib/stats_enums.hpp @@ -425,6 +425,8 @@ enum class detail // active insert, insert_failed, + transition_priority, + transition_priority_failed, election_cleanup, // active_elections diff --git a/nano/node/active_elections.cpp b/nano/node/active_elections.cpp index 427a46a4f8..fd076baf04 100644 --- a/nano/node/active_elections.cpp +++ b/nano/node/active_elections.cpp @@ -400,6 +400,7 @@ nano::election_insertion_result nano::active_elections::insert (std::shared_ptr< return result; } + auto active_state = false; auto const root = block_a->qualified_root (); auto const hash = block_a->hash (); auto const existing = roots.get ().find (root); @@ -420,6 +421,13 @@ nano::election_insertion_result nano::active_elections::insert (std::shared_ptr< debug_assert (count_by_behavior[result.election->behavior ()] >= 0); count_by_behavior[result.election->behavior ()]++; + // If block is not in vote cache, transition to active immediately + if (node.vote_cache.find (hash).empty ()) + { + result.election->transition_active (); + active_state = true; + } + node.stats.inc (nano::stat::type::active_elections, nano::stat::detail::started); node.stats.inc (nano::stat::type::active_elections_started, to_stat_detail (election_behavior_a)); @@ -427,9 +435,10 @@ nano::election_insertion_result nano::active_elections::insert (std::shared_ptr< nano::log::arg{ "behavior", election_behavior_a }, nano::log::arg{ "election", result.election }); - node.logger.debug (nano::log::type::active_elections, "Started new election for block: {} (behavior: {})", + node.logger.debug (nano::log::type::active_elections, "Started new election for block: {} (behavior: {}, active: {})", hash.to_string (), - to_string (election_behavior_a)); + to_string (election_behavior_a), + active_state); } else { @@ -439,6 +448,23 @@ nano::election_insertion_result nano::active_elections::insert (std::shared_ptr< else { result.election = existing->election; + + // Upgrade to priority election to enable immediate vote broadcasting. + auto previous_behavior = result.election->behavior (); + if (election_behavior_a == nano::election_behavior::priority && previous_behavior != nano::election_behavior::priority) + { + bool transitioned = result.election->transition_priority (); + if (transitioned) + { + count_by_behavior[previous_behavior]--; + count_by_behavior[election_behavior_a]++; + node.stats.inc (nano::stat::type::active_elections, nano::stat::detail::transition_priority); + } + else + { + node.stats.inc (nano::stat::type::active_elections, nano::stat::detail::transition_priority_failed); + } + } } lock.unlock (); diff --git a/nano/node/election.cpp b/nano/node/election.cpp index 1e760bb938..5003f5e83e 100644 --- a/nano/node/election.cpp +++ b/nano/node/election.cpp @@ -152,7 +152,7 @@ bool nano::election::state_change (nano::election_state expected_a, nano::electi std::chrono::milliseconds nano::election::confirm_req_time () const { - switch (behavior ()) + switch (behavior_m) { case election_behavior::manual: case election_behavior::priority: @@ -183,6 +183,25 @@ void nano::election::transition_active () state_change (nano::election_state::passive, nano::election_state::active); } +bool nano::election::transition_priority () +{ + nano::lock_guard guard{ mutex }; + + if (behavior_m == nano::election_behavior::priority || behavior_m == nano::election_behavior::manual) + { + return false; + } + + behavior_m = nano::election_behavior::priority; + last_vote = std::chrono::steady_clock::time_point{}; // allow new outgoing votes immediately + + node.logger.debug (nano::log::type::election, "Transitioned election behavior to priority from {} for root: {}", + to_string (behavior_m), + qualified_root.to_string ()); + + return true; +} + void nano::election::cancel () { nano::lock_guard guard{ mutex }; @@ -314,7 +333,7 @@ bool nano::election::transition_time (nano::confirmation_solicitor & solicitor_a std::chrono::milliseconds nano::election::time_to_live () const { - switch (behavior ()) + switch (behavior_m) { case election_behavior::manual: case election_behavior::priority: @@ -769,6 +788,7 @@ std::vector nano::election::votes_with_weight () co nano::election_behavior nano::election::behavior () const { + nano::lock_guard guard{ mutex }; return behavior_m; } diff --git a/nano/node/election.hpp b/nano/node/election.hpp index 718cbf680c..1fe0ad1895 100644 --- a/nano/node/election.hpp +++ b/nano/node/election.hpp @@ -87,6 +87,7 @@ class election final : public std::enable_shared_from_this public: // State transitions bool transition_time (nano::confirmation_solicitor &); void transition_active (); + bool transition_priority (); void cancel (); public: // Status @@ -180,7 +181,7 @@ class election final : public std::enable_shared_from_this mutable nano::uint128_t final_weight{ 0 }; mutable std::unordered_map last_tally; - nano::election_behavior const behavior_m; + nano::election_behavior behavior_m; std::chrono::steady_clock::time_point const election_start{ std::chrono::steady_clock::now () }; mutable nano::mutex mutex;