Skip to content

Commit

Permalink
Election use nano::vote_code
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 20, 2024
1 parent 0e6b7fa commit 4d69ce5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 26 deletions.
4 changes: 2 additions & 2 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ nano::vote_code nano::active_transactions::vote (std::shared_ptr<nano::vote> con
for (auto const & [election, block_hash] : process)
{
auto const vote_result = election->vote (vote_a->account, vote_a->timestamp (), block_hash);
processed |= (vote_result == nano::election::vote_result::processed);
replay |= (vote_result == nano::election::vote_result::replay);
processed |= (vote_result == nano::vote_code::vote);
replay |= (vote_result == nano::vote_code::replay);
}

// Republish vote if it is new and the node does not host a principal representative (or close to)
Expand Down
12 changes: 6 additions & 6 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,12 +431,12 @@ std::shared_ptr<nano::block> nano::election::find (nano::block_hash const & hash
return result;
}

auto nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano::block_hash const & block_hash_a, vote_source vote_source_a) -> vote_result
nano::vote_code nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano::block_hash const & block_hash_a, nano::vote_source vote_source_a)
{
auto weight = node.ledger.weight (rep);
if (!node.network_params.network.is_dev_network () && weight <= node.minimum_principal_weight ())
{
return vote_result::ignored;
return vote_code::indeterminate;
}

nano::unique_lock<nano::mutex> lock{ mutex };
Expand All @@ -447,11 +447,11 @@ auto nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano
auto last_vote_l (last_vote_it->second);
if (last_vote_l.timestamp > timestamp_a)
{
return vote_result::replay;
return vote_code::replay;
}
if (last_vote_l.timestamp == timestamp_a && !(last_vote_l.hash < block_hash_a))
{
return vote_result::replay;
return vote_code::replay;
}

auto max_vote = timestamp_a == std::numeric_limits<uint64_t>::max () && last_vote_l.timestamp < timestamp_a;
Expand All @@ -465,7 +465,7 @@ auto nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano

if (!max_vote && !past_cooldown)
{
return vote_result::ignored;
return vote_code::ignored;
}
}

Expand All @@ -491,7 +491,7 @@ auto nano::election::vote (nano::account const & rep, uint64_t timestamp_a, nano
confirm_if_quorum (lock);
}

return vote_result::processed;
return vote_code::vote;
}

bool nano::election::publish (std::shared_ptr<nano::block> const & block_a)
Expand Down
16 changes: 1 addition & 15 deletions nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,6 @@ class election final : public std::enable_shared_from_this<nano::election>
{
nano::id_t const id{ nano::next_id () }; // Track individual objects when tracing

public:
enum class vote_source
{
live,
cache,
};

enum class vote_result
{
ignored,
processed,
replay,
};

private:
// Minimum time between broadcasts of the current winner of an election, as a backup to requesting confirmations
std::chrono::milliseconds base_latency () const;
Expand Down Expand Up @@ -142,7 +128,7 @@ class election final : public std::enable_shared_from_this<nano::election>
* Process vote. Internally uses cooldown to throttle non-final votes
* If the election reaches consensus, it will be confirmed
*/
vote_result vote (nano::account const & representative, uint64_t timestamp, nano::block_hash const & block_hash, vote_source = vote_source::live);
nano::vote_code vote (nano::account const & representative, uint64_t timestamp, nano::block_hash const & block_hash, nano::vote_source = vote_source::live);
bool publish (std::shared_ptr<nano::block> const & block_a);
// Confirm this block if quorum is met
void confirm_if_quorum (nano::unique_lock<nano::mutex> &);
Expand Down
4 changes: 2 additions & 2 deletions nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ std::size_t nano::vote_cache::entry::fill (std::shared_ptr<nano::election> const
std::size_t inserted = 0;
for (const auto & entry : voters_m)
{
auto result = election->vote (entry.representative, entry.timestamp, hash_m, nano::election::vote_source::cache);
if (result == nano::election::vote_result::processed)
auto result = election->vote (entry.representative, entry.timestamp, hash_m, nano::vote_source::cache);
if (result == nano::vote_code::vote)
{
inserted++;
}
Expand Down
9 changes: 8 additions & 1 deletion nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,14 @@ enum class vote_code
invalid, // Vote is not signed correctly
replay, // Vote does not have the highest timestamp, it's a replay
vote, // Vote has the highest timestamp
indeterminate // Unknown if replay or vote
indeterminate, // Unknown if replay or vote
ignored, // Vote is valid, but got ingored (e.g. due to cooldown)
};

enum class vote_source
{
live,
cache,
};

enum class block_status
Expand Down

0 comments on commit 4d69ce5

Please sign in to comment.