Skip to content

Commit

Permalink
Use exact vote weight in certain RPC calls and wallets code
Browse files Browse the repository at this point in the history
  • Loading branch information
simpago committed Mar 19, 2024
1 parent 936c603 commit d916e14
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
8 changes: 4 additions & 4 deletions nano/node/json_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ void nano::json_handler::account_info ()
}
if (weight)
{
auto account_weight (node.ledger.weight (account));
auto account_weight (node.ledger.weight_exact (transaction, account));
response_l.put ("weight", account_weight.convert_to<std::string> ());
}
if (receivable)
Expand Down Expand Up @@ -2784,7 +2784,7 @@ void nano::json_handler::ledger ()
}
if (weight)
{
auto account_weight (node.ledger.weight (account));
auto account_weight (node.ledger.weight_exact (transaction, account));
response_a.put ("weight", account_weight.convert_to<std::string> ());
}
accounts.push_back (std::make_pair (account.to_account (), response_a));
Expand Down Expand Up @@ -2837,7 +2837,7 @@ void nano::json_handler::ledger ()
}
if (weight)
{
auto account_weight (node.ledger.weight (account));
auto account_weight (node.ledger.weight_exact (transaction, account));
response_a.put ("weight", account_weight.convert_to<std::string> ());
}
accounts.push_back (std::make_pair (account.to_account (), response_a));
Expand Down Expand Up @@ -4716,7 +4716,7 @@ void nano::json_handler::wallet_ledger ()
}
if (weight)
{
auto account_weight (node.ledger.weight (account));
auto account_weight (node.ledger.weight_exact (block_transaction, account));
entry.put ("weight", account_weight.convert_to<std::string> ());
}
if (receivable)
Expand Down
3 changes: 2 additions & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ std::pair<nano::uint128_t, nano::uint128_t> nano::node::balance_pending (nano::a

nano::uint128_t nano::node::weight (nano::account const & account_a)
{
return ledger.weight (account_a);
auto txn{ ledger.store.tx_begin_read () };
return ledger.weight_exact (txn, account_a);
}

nano::uint128_t nano::node::minimum_principal_weight ()
Expand Down
9 changes: 7 additions & 2 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,7 @@ void nano::wallets::foreach_representative (std::function<void (nano::public_key
std::vector<std::pair<nano::public_key const, nano::raw_key const>> action_accounts_l;
{
auto transaction_l (tx_begin_read ());
auto ledger_txn{ node.ledger.store.tx_begin_read () };
nano::lock_guard<nano::mutex> lock{ mutex };
for (auto i (items.begin ()), n (items.end ()); i != n; ++i)
{
Expand All @@ -1551,7 +1552,7 @@ void nano::wallets::foreach_representative (std::function<void (nano::public_key
{
if (wallet.store.exists (transaction_l, account))
{
if (!node.ledger.weight (account).is_zero ())
if (!node.ledger.weight_exact (ledger_txn, account).is_zero ())
{
if (wallet.store.valid_password (transaction_l))
{
Expand Down Expand Up @@ -1642,7 +1643,11 @@ 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, bool const acquire_lock_a)
{
auto weight = node.ledger.weight (account_a);
nano::uint128_t weight;
{
auto ledger_txn{ node.ledger.store.tx_begin_read () };
weight = node.ledger.weight_exact (ledger_txn, account_a);
}

if (weight < node.config.vote_minimum.number ())
{
Expand Down
5 changes: 5 additions & 0 deletions nano/secure/ledger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,11 @@ nano::uint128_t nano::ledger::weight (nano::account const & account_a)
return cache.rep_weights.representation_get (account_a);
}

nano::uint128_t nano::ledger::weight_exact (store::transaction const & txn_a, nano::account const & representative_a)
{
return store.rep_weight.get (txn_a, representative_a);
}

// Rollback blocks until `block_a' doesn't exist or it tries to penetrate the confirmation height
bool nano::ledger::rollback (store::write_transaction const & transaction_a, nano::block_hash const & block_a, std::vector<std::shared_ptr<nano::block>> & list_a)
{
Expand Down
7 changes: 7 additions & 0 deletions nano/secure/ledger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ class ledger final
bool block_exists (store::transaction const & transaction, nano::block_hash const & hash) const;
nano::uint128_t account_balance (store::transaction const &, nano::account const &, bool = false);
nano::uint128_t account_receivable (store::transaction const &, nano::account const &, bool = false);
/**
* Returns the cached vote weight for the given representative.
* If the weight is below the cache limit it returns 0.
* During bootstrap it returns the preconfigured bootstrap weights.
*/
nano::uint128_t weight (nano::account const &);
std::optional<nano::block_hash> successor (store::transaction const &, nano::qualified_root const &) const noexcept;
std::optional<nano::block_hash> successor (store::transaction const & transaction, nano::block_hash const & hash) const noexcept;
/* Returns the exact vote weight for the given representative by doing a database lookup */
nano::uint128_t weight_exact (store::transaction const &, nano::account const &);
std::shared_ptr<nano::block> forked_block (store::transaction const &, nano::block const &);
std::shared_ptr<nano::block> head_block (store::transaction const &, nano::account const &);
bool block_confirmed (store::transaction const &, nano::block_hash const &) const;
Expand Down

0 comments on commit d916e14

Please sign in to comment.