Skip to content

Commit

Permalink
Wrap in a struct rather than using std::pair
Browse files Browse the repository at this point in the history
  • Loading branch information
guilhermelawless committed Dec 13, 2019
1 parent 31507eb commit 4981613
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion nano/core_test/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ TEST (node, rep_self_vote)
}
system.wallet (0)->insert_adhoc (rep_big.prv);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
ASSERT_EQ (system.wallet (0)->wallets.rep_counts ().first, 2);
ASSERT_EQ (system.wallet (0)->wallets.rep_counts ().voting, 2);
auto block0 (std::make_shared<nano::send_block> (node0->latest (nano::test_genesis_key.pub), rep_big.pub, nano::uint128_t ("0x60000000000000000000000000000000"), nano::test_genesis_key.prv, nano::test_genesis_key.pub, 0));
node0->work_generate_blocking (*block0);
ASSERT_EQ (nano::process_result::progress, node0->process (*block0).code);
Expand Down
4 changes: 2 additions & 2 deletions nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ void nano::active_transactions::search_frontiers (nano::transaction const & tran
{
// Limit maximum count of elections to start
auto rep_counts (node.wallets.rep_counts ());
bool representative (node.config.enable_voting && rep_counts.first > 0);
bool half_princpal_representative (representative && rep_counts.second > 0);
bool representative (node.config.enable_voting && rep_counts.voting > 0);
bool half_princpal_representative (representative && rep_counts.half_principal > 0);
/* Check less frequently for regular nodes in auto mode */
bool agressive_mode (half_princpal_representative || node.config.frontiers_confirmation == nano::frontiers_confirmation_mode::always);
auto request_interval (std::chrono::milliseconds (node.network_params.network.request_interval_ms));
Expand Down
2 changes: 1 addition & 1 deletion nano/node/blockprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void nano::block_processor::process_live (nano::block_hash const & hash_a, std::
}
// Announce block contents to the network
node.network.flood_block (block_a, false);
if (node.config.enable_voting && node.wallets.rep_counts ().first > 0)
if (node.config.enable_voting && node.wallets.rep_counts ().voting > 0)
{
// Announce our weighted vote to the network
generator.add (hash_a);
Expand Down
4 changes: 2 additions & 2 deletions nano/node/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ template <typename T>
bool confirm_block (nano::transaction const & transaction_a, nano::node & node_a, T & list_a, std::shared_ptr<nano::block> block_a, bool also_publish)
{
bool result (false);
if (node_a.config.enable_voting && node_a.wallets.rep_counts ().first > 0)
if (node_a.config.enable_voting && node_a.wallets.rep_counts ().voting > 0)
{
auto hash (block_a->hash ());
// Search in cache
Expand Down Expand Up @@ -458,7 +458,7 @@ class network_message_visitor : public nano::message_visitor
}
node.stats.inc (nano::stat::type::message, nano::stat::detail::confirm_req, nano::stat::dir::in);
// Don't load nodes with disabled voting
if (node.config.enable_voting && node.wallets.rep_counts ().first > 0)
if (node.config.enable_voting && node.wallets.rep_counts ().voting > 0)
{
if (message_a.block != nullptr)
{
Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ void nano::node::block_confirm (std::shared_ptr<nano::block> block_a)
active.start (block_a, false);
network.broadcast_confirm_req (block_a);
// Calculate votes for local representatives
if (config.enable_voting && wallets.rep_counts ().first > 0 && active.active (*block_a))
if (config.enable_voting && wallets.rep_counts ().voting > 0 && active.active (*block_a))
{
block_processor.generator.add (block_a->hash ());
}
Expand Down
11 changes: 5 additions & 6 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1833,10 +1833,10 @@ void nano::wallets::clear_send_ids (nano::transaction const & transaction_a)
assert (status == 0);
}

std::pair<uint64_t, uint64_t> nano::wallets::rep_counts ()
nano::wallet_representative_counts nano::wallets::rep_counts ()
{
nano::lock_guard<std::mutex> counts_guard (counts_mutex);
return { reps_count, half_principal_reps_count };
return counts;
}

bool nano::wallets::check_rep (nano::account const & account_a, nano::uint128_t const & half_principal_weight_a, const bool acquire_lock_a)
Expand All @@ -1851,10 +1851,10 @@ bool nano::wallets::check_rep (nano::account const & account_a, nano::uint128_t
lock = nano::unique_lock<std::mutex> (counts_mutex);
}
result = true;
++reps_count;
++counts.voting;
if (weight >= half_principal_weight_a)
{
++half_principal_reps_count;
++counts.half_principal;
}
}
return result;
Expand All @@ -1864,8 +1864,7 @@ void nano::wallets::compute_reps ()
{
nano::lock_guard<std::mutex> guard (mutex);
nano::lock_guard<std::mutex> counts_guard (counts_mutex);
reps_count = 0;
half_principal_reps_count = 0;
counts = { 0, 0 };
auto half_principal_weight (node.minimum_principal_weight () / 2);
auto transaction (tx_begin_read ());
for (auto i (items.begin ()), n (items.end ()); i != n; ++i)
Expand Down
13 changes: 9 additions & 4 deletions nano/node/wallet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ class work_watcher final : public std::enable_shared_from_this<nano::work_watche
std::atomic<bool> stopped;
};

class wallet_representative_counts
{
public:
uint64_t voting{ 0 }; // Representatives with at least the configured minimum voting weight
uint64_t half_principal{ 0 }; // Representatives with at least 50% of principal representative requirements
};

/**
* The wallets set is all the wallets a node controls.
* A node may contain multiple wallets independently encrypted and operated.
Expand All @@ -201,8 +208,7 @@ class wallets final
bool exists (nano::transaction const &, nano::public_key const &);
void stop ();
void clear_send_ids (nano::transaction const &);
// Returns the pair {reps_count, half_principal_reps_count}
std::pair<uint64_t, uint64_t> rep_counts ();
nano::wallet_representative_counts rep_counts ();
bool check_rep (nano::account const &, nano::uint128_t const &, const bool = true);
void compute_reps ();
void ongoing_compute_reps ();
Expand Down Expand Up @@ -233,8 +239,7 @@ class wallets final

private:
std::mutex counts_mutex;
uint64_t reps_count{ 0 };
uint64_t half_principal_reps_count{ 0 }; // Representatives with at least 50% of principal representative requirements
nano::wallet_representative_counts counts;
};

std::unique_ptr<seq_con_info_component> collect_seq_con_info (wallets & wallets, const std::string & name);
Expand Down

0 comments on commit 4981613

Please sign in to comment.