Skip to content

Commit

Permalink
Fix counting of voting representatives nanocurrency#3339
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dsiganos committed Jun 16, 2021
1 parent 96ccbda commit e22b686
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ()
Expand Down

0 comments on commit e22b686

Please # to comment.