Skip to content

Commit

Permalink
upgrade priority elections - allow immediate votes
Browse files Browse the repository at this point in the history
  • Loading branch information
gr0vity authored and gr0vity-dev committed Nov 30, 2024
1 parent 8d2d435 commit b3aa270
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 13 additions & 0 deletions nano/node/active_elections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,19 @@ 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 && result.election->behavior () != nano::election_behavior::priority)
{
count_by_behavior[result.election->behavior ()]--;
count_by_behavior[election_behavior_a]++;
result.election->transition_priority ();

node.logger.debug (nano::log::type::active_elections, "Upgraded election behavior from {} to priority for block: {}",
to_string (previous_behavior),
hash.to_string ());
}
}

lock.unlock ();
Expand Down
9 changes: 8 additions & 1 deletion nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,13 @@ void nano::election::transition_active ()
state_change (nano::election_state::passive, nano::election_state::active);
}

void nano::election::transition_priority ()
{
nano::lock_guard<nano::mutex> guard{ mutex };
behavior_m = nano::election_behavior::priority;
last_vote = std::chrono::steady_clock::time_point{}; // allow new outgoing votes immediately
}

void nano::election::cancel ()
{
nano::lock_guard<nano::mutex> guard{ mutex };
Expand Down Expand Up @@ -849,4 +856,4 @@ std::string_view nano::to_string (nano::election_state state)
nano::stat::detail nano::to_stat_detail (nano::election_state state)
{
return nano::enum_util::cast<nano::stat::detail> (state);
}
}
3 changes: 2 additions & 1 deletion nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class election final : public std::enable_shared_from_this<election>
public: // State transitions
bool transition_time (nano::confirmation_solicitor &);
void transition_active ();
void transition_priority ();
void cancel ();

public: // Status
Expand Down Expand Up @@ -180,7 +181,7 @@ class election final : public std::enable_shared_from_this<election>
mutable nano::uint128_t final_weight{ 0 };
mutable std::unordered_map<nano::block_hash, nano::uint128_t> 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;
Expand Down

0 comments on commit b3aa270

Please sign in to comment.