Skip to content

Commit

Permalink
Vote generator session for batch insertions
Browse files Browse the repository at this point in the history
Restoring removed in PR nanocurrency#2688
  • Loading branch information
SergiySW committed Apr 4, 2020
1 parent cb112f7 commit e658326
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 4 deletions.
4 changes: 3 additions & 1 deletion nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &
nano::confirmation_solicitor solicitor (node.network, node.network_params.network);
solicitor.prepare (node.rep_crawler.principal_representatives (std::numeric_limits<size_t>::max ()));

nano::vote_generator_session generator_session (generator);
auto & sorted_roots_l (roots.get<tag_difficulty> ());
auto const election_ttl_cutoff_l (std::chrono::steady_clock::now () - election_time_to_live);
bool const check_all_elections_l (std::chrono::steady_clock::now () - last_check_all_elections > check_all_elections_period);
Expand All @@ -228,7 +229,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &

if (!election_l->prioritized () && unconfirmed_count_l < prioritized_cutoff)
{
election_l->prioritize_election ();
election_l->prioritize_election (generator_session);
}

unconfirmed_count_l += !confirmed_l;
Expand All @@ -245,6 +246,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &
}
lock_a.unlock ();
solicitor.flush ();
generator_session.flush ();
lock_a.lock ();

// This is updated after the loop to ensure slow machines don't do the full check often
Expand Down
4 changes: 2 additions & 2 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,12 @@ bool nano::election::prioritized () const
return prioritized_m;
}

void nano::election::prioritize_election ()
void nano::election::prioritize_election (nano::vote_generator_session & generator_session_a)
{
debug_assert (!node.active.mutex.try_lock ());
debug_assert (!prioritized_m);
prioritized_m = true;
generate_votes (status.winner->hash ());
generator_session_a.add (status.winner->hash ());
}

void nano::election::generate_votes (nano::block_hash const & hash_a)
Expand Down
3 changes: 2 additions & 1 deletion nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace nano
class channel;
class confirmation_solicitor;
class node;
class vote_generator_session;
class vote_info final
{
public:
Expand Down Expand Up @@ -83,7 +84,7 @@ class election final : public std::enable_shared_from_this<nano::election>
void adjust_dependent_difficulty ();
void insert_inactive_votes_cache (nano::block_hash const &);
bool prioritized () const;
void prioritize_election ();
void prioritize_election (nano::vote_generator_session &);
// Erase all blocks from active and, if not confirmed, clear digests from network filters
void cleanup ();

Expand Down
28 changes: 28 additions & 0 deletions nano/node/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ void nano::vote_generator::add (nano::block_hash const & hash_a)
}
}

void nano::vote_generator::add (std::vector<nano::block_hash> const & hashes_a)
{
nano::unique_lock<std::mutex> lock (mutex);
hashes.insert (hashes.end (), hashes_a.begin (), hashes_a.end ());
if (hashes.size () >= nano::network::confirm_ack_hashes_max)
{
lock.unlock ();
condition.notify_all ();
}
}

void nano::vote_generator::stop ()
{
nano::unique_lock<std::mutex> lock (mutex);
Expand Down Expand Up @@ -102,6 +113,23 @@ void nano::vote_generator::run ()
}
}

nano::vote_generator_session::vote_generator_session (nano::vote_generator & vote_generator_a) :
generator (vote_generator_a)
{
}

void nano::vote_generator_session::add (nano::block_hash const & hash_a)
{
nano::lock_guard<std::mutex> lock (mutex);
hashes.push_back (hash_a);
}

void nano::vote_generator_session::flush ()
{
nano::lock_guard<std::mutex> lock (mutex);
generator.add (hashes);
}

nano::votes_cache::votes_cache (nano::wallets & wallets_a) :
wallets (wallets_a)
{
Expand Down
14 changes: 14 additions & 0 deletions nano/node/voting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class vote_generator final
public:
vote_generator (nano::node_config & config_a, nano::block_store & store_a, nano::wallets & wallets_a, nano::vote_processor & vote_processor_a, nano::votes_cache & votes_cache_a, nano::network & network_a);
void add (nano::block_hash const &);
void add (std::vector<nano::block_hash> const &);
void stop ();

private:
Expand All @@ -53,6 +54,19 @@ class vote_generator final
friend std::unique_ptr<container_info_component> collect_container_info (vote_generator & vote_generator, const std::string & name);
};

class vote_generator_session final
{
public:
vote_generator_session (vote_generator & vote_generator_a);
void add (nano::block_hash const &);
void flush ();

private:
nano::vote_generator & generator;
std::mutex mutex;
std::vector<nano::block_hash> hashes;
};

std::unique_ptr<container_info_component> collect_container_info (vote_generator & vote_generator, const std::string & name);
class cached_votes final
{
Expand Down

0 comments on commit e658326

Please sign in to comment.