Fix counting of voting representatives #3339 (#3340)

The function rep wallets::rep_check() did not check for duplicates when
adding a voting representative and the count of voting representatives
could therefore be much bigger than the reality.
This commit is contained in:
dsiganos 2021-06-16 18:07:55 +01:00 committed by GitHub
commit 6ee3cee121
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1630,24 +1630,33 @@ nano::wallet_representatives nano::wallets::reps () const
bool nano::wallets::check_rep (nano::account const & account_a, nano::uint128_t const & half_principal_weight_a, const bool acquire_lock_a)
{
bool result (false);
auto weight (node.ledger.weight (account_a));
if (weight >= node.config.vote_minimum.number ())
auto weight = node.ledger.weight (account_a);
if (weight < node.config.vote_minimum.number ())
{
nano::unique_lock<nano::mutex> lock;
if (acquire_lock_a)
{
lock = nano::unique_lock<nano::mutex> (reps_cache_mutex);
}
result = true;
representatives.accounts.insert (account_a);
++representatives.voting;
if (weight >= half_principal_weight_a)
{
++representatives.half_principal;
}
return false; // account not a representative
}
return result;
nano::unique_lock<nano::mutex> lock;
if (acquire_lock_a)
{
lock = nano::unique_lock<nano::mutex> (reps_cache_mutex);
}
auto insert_result = representatives.accounts.insert (account_a);
if (!insert_result.second)
{
return false; // account already exists
}
++representatives.voting;
if (weight >= half_principal_weight_a)
{
++representatives.half_principal;
}
return true;
}
void nano::wallets::compute_reps ()