Move vote_generator and vote_spacing in to their own files.

Recompile time for voting.hpp before and vote headers after
Before:
[117/117] Linking CXX executable rpc_test
ninja  1262.92s user 87.90s system 869% cpu 2:35.28 total
After:
[16/16] Linking CXX executable nano_node
ninja  98.05s user 11.24s system 377% cpu 28.917 total
This commit is contained in:
Colin LeMahieu 2024-03-20 13:32:41 +00:00
commit 14860eaa09
No known key found for this signature in database
GPG key ID: 43708520C8DFB938
17 changed files with 149 additions and 96 deletions

View file

@ -9,6 +9,7 @@
#include <nano/node/scheduler/priority.hpp>
#include <nano/node/transport/fake.hpp>
#include <nano/node/transport/inproc.hpp>
#include <nano/node/vote_generator.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/test_common/network.hpp>
#include <nano/test_common/system.hpp>

View file

@ -1,7 +1,8 @@
#include <nano/lib/blocks.hpp>
#include <nano/node/common.hpp>
#include <nano/node/local_vote_history.hpp>
#include <nano/node/voting.hpp>
#include <nano/node/vote_generator.hpp>
#include <nano/node/vote_spacing.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/test_common/system.hpp>
#include <nano/test_common/testutil.hpp>

View file

@ -108,7 +108,7 @@ public:
}
public: // Container info
std::unique_ptr<container_info_component> collect_container_info (std::string const & name)
std::unique_ptr<container_info_component> collect_container_info (std::string const & name) const
{
nano::lock_guard<nano::mutex> guard{ mutex };

View file

@ -166,11 +166,13 @@ add_library(
unchecked_map.hpp
vote_cache.hpp
vote_cache.cpp
vote_generator.hpp
vote_generator.cpp
vote_processor.hpp
vote_processor.cpp
vote_spacing.hpp
vote_spacing.cpp
vote_with_weight_info.hpp
voting.hpp
voting.cpp
wallet.hpp
wallet.cpp
websocket.hpp

View file

@ -5,7 +5,6 @@
#include <nano/node/election_insertion_result.hpp>
#include <nano/node/election_status.hpp>
#include <nano/node/vote_with_weight_info.hpp>
#include <nano/node/voting.hpp>
#include <nano/secure/common.hpp>
#include <boost/multi_index/hashed_index.hpp>
@ -17,6 +16,7 @@
#include <condition_variable>
#include <deque>
#include <memory>
#include <thread>
#include <unordered_map>
namespace mi = boost::multi_index;
@ -32,7 +32,14 @@ class election;
class vote;
class confirmation_height_processor;
class stats;
}
namespace nano::store
{
class read_transaction;
}
namespace nano
{
class recently_confirmed_cache final
{
public:

View file

@ -2,7 +2,9 @@
#include <nano/node/backlog_population.hpp>
#include <nano/node/nodeconfig.hpp>
#include <nano/node/scheduler/priority.hpp>
#include <nano/store/account.hpp>
#include <nano/store/component.hpp>
#include <nano/store/confirmation_height.hpp>
nano::backlog_population::backlog_population (const config & config_a, nano::store::component & store_a, nano::stats & stats_a) :
config_m{ config_a },

View file

@ -4,6 +4,7 @@
#include <nano/node/local_vote_history.hpp>
#include <nano/node/network.hpp>
#include <nano/node/node.hpp>
#include <nano/node/vote_generator.hpp>
#include <nano/secure/ledger.hpp>
#include <boost/format.hpp>

View file

@ -4,8 +4,8 @@
#include <nano/lib/utility.hpp>
#include <nano/node/common.hpp>
#include <nano/node/daemonconfig.hpp>
#include <nano/node/make_store.hpp>
#include <nano/node/local_vote_history.hpp>
#include <nano/node/make_store.hpp>
#include <nano/node/node.hpp>
#include <nano/node/scheduler/component.hpp>
#include <nano/node/scheduler/hinted.hpp>
@ -13,6 +13,7 @@
#include <nano/node/scheduler/optimistic.hpp>
#include <nano/node/scheduler/priority.hpp>
#include <nano/node/telemetry.hpp>
#include <nano/node/vote_generator.hpp>
#include <nano/node/websocket.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/store/component.hpp>
@ -177,8 +178,10 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
vote_uniquer{},
confirmation_height_processor (ledger, write_database_queue, config.conf_height_processor_batch_min_time, logger, node_initialized_latch, flags.confirmation_height_processor_mode),
vote_cache{ config.vote_cache, stats },
generator{ config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* non-final */ false },
final_generator{ config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* final */ true },
generator_impl{ std::make_unique<nano::vote_generator> (config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* non-final */ false) },
generator{ *generator_impl },
final_generator_impl{ std::make_unique<nano::vote_generator> (config, *this, ledger, wallets, vote_processor, history, network, stats, logger, /* final */ true) },
final_generator{ *final_generator_impl },
active{ *this, confirmation_height_processor, block_processor },
scheduler_impl{ std::make_unique<nano::scheduler::component> (*this) },
scheduler{ *scheduler_impl },
@ -547,8 +550,8 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (no
composite->add_component (collect_container_info (node.aggregator, "request_aggregator"));
composite->add_component (node.scheduler.collect_container_info ("election_scheduler"));
composite->add_component (node.vote_cache.collect_container_info ("vote_cache"));
composite->add_component (collect_container_info (node.generator, "vote_generator"));
composite->add_component (collect_container_info (node.final_generator, "vote_generator_final"));
composite->add_component (node.generator.collect_container_info ("vote_generator"));
composite->add_component (node.final_generator.collect_container_info ("vote_generator_final"));
composite->add_component (node.ascendboot.collect_container_info ("bootstrap_ascending"));
composite->add_component (node.unchecked.collect_container_info ("unchecked"));
composite->add_component (node.local_block_broadcaster.collect_container_info ("local_block_broadcaster"));

View file

@ -172,8 +172,10 @@ public:
nano::vote_uniquer vote_uniquer;
nano::confirmation_height_processor confirmation_height_processor;
nano::vote_cache vote_cache;
nano::vote_generator generator;
nano::vote_generator final_generator;
std::unique_ptr<nano::vote_generator> generator_impl;
nano::vote_generator & generator;
std::unique_ptr<nano::vote_generator> final_generator_impl;
nano::vote_generator & final_generator;
nano::active_transactions active;
private: // Placed here to maintain initialization order

View file

@ -9,6 +9,14 @@ namespace nano
{
class election_status;
class telemetry;
}
namespace nano::transport
{
class channel;
}
namespace nano
{
class node_observers final
{
public:

View file

@ -6,7 +6,7 @@
#include <nano/node/network.hpp>
#include <nano/node/nodeconfig.hpp>
#include <nano/node/request_aggregator.hpp>
#include <nano/node/voting.hpp>
#include <nano/node/vote_generator.hpp>
#include <nano/node/wallet.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/store/component.hpp>

View file

@ -17,6 +17,10 @@ class block;
class container_info_component;
class node;
}
namespace nano::store
{
class transaction;
}
namespace nano::scheduler
{

View file

@ -5,52 +5,15 @@
#include <nano/node/network.hpp>
#include <nano/node/nodeconfig.hpp>
#include <nano/node/transport/inproc.hpp>
#include <nano/node/vote_generator.hpp>
#include <nano/node/vote_processor.hpp>
#include <nano/node/voting.hpp>
#include <nano/node/vote_spacing.hpp>
#include <nano/node/wallet.hpp>
#include <nano/secure/ledger.hpp>
#include <nano/store/component.hpp>
#include <chrono>
void nano::vote_spacing::trim ()
{
recent.get<tag_time> ().erase (recent.get<tag_time> ().begin (), recent.get<tag_time> ().upper_bound (std::chrono::steady_clock::now () - delay));
}
bool nano::vote_spacing::votable (nano::root const & root_a, nano::block_hash const & hash_a) const
{
bool result = true;
for (auto range = recent.get<tag_root> ().equal_range (root_a); result && range.first != range.second; ++range.first)
{
auto & item = *range.first;
result = hash_a == item.hash || item.time < std::chrono::steady_clock::now () - delay;
}
return result;
}
void nano::vote_spacing::flag (nano::root const & root_a, nano::block_hash const & hash_a)
{
trim ();
auto now = std::chrono::steady_clock::now ();
auto existing = recent.get<tag_root> ().find (root_a);
if (existing != recent.end ())
{
recent.get<tag_root> ().modify (existing, [now] (entry & entry) {
entry.time = now;
});
}
else
{
recent.insert ({ root_a, now, hash_a });
}
}
std::size_t nano::vote_spacing::size () const
{
return recent.size ();
}
nano::vote_generator::vote_generator (nano::node_config const & config_a, nano::node & node_a, nano::ledger & ledger_a, nano::wallets & wallets_a, nano::vote_processor & vote_processor_a, nano::local_vote_history & history_a, nano::network & network_a, nano::stats & stats_a, nano::logger & logger_a, bool is_final_a) :
config (config_a),
node (node_a),
@ -58,7 +21,8 @@ nano::vote_generator::vote_generator (nano::node_config const & config_a, nano::
wallets (wallets_a),
vote_processor (vote_processor_a),
history (history_a),
spacing{ config_a.network_params.voting.delay },
spacing_impl{ std::make_unique<nano::vote_spacing> (config_a.network_params.voting.delay) },
spacing{ *spacing_impl },
network (network_a),
stats (stats_a),
logger (logger_a),
@ -318,20 +282,20 @@ void nano::vote_generator::run ()
}
}
std::unique_ptr<nano::container_info_component> nano::collect_container_info (nano::vote_generator & vote_generator, std::string const & name)
std::unique_ptr<nano::container_info_component> nano::vote_generator::collect_container_info (std::string const & name) const
{
std::size_t candidates_count = 0;
std::size_t requests_count = 0;
{
nano::lock_guard<nano::mutex> guard{ vote_generator.mutex };
candidates_count = vote_generator.candidates.size ();
requests_count = vote_generator.requests.size ();
nano::lock_guard<nano::mutex> guard{ mutex };
candidates_count = candidates.size ();
requests_count = requests.size ();
}
auto sizeof_candidate_element = sizeof (decltype (vote_generator.candidates)::value_type);
auto sizeof_request_element = sizeof (decltype (vote_generator.requests)::value_type);
auto sizeof_candidate_element = sizeof (decltype (candidates)::value_type);
auto sizeof_request_element = sizeof (decltype (requests)::value_type);
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "candidates", candidates_count, sizeof_candidate_element }));
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "requests", requests_count, sizeof_request_element }));
composite->add_component (vote_generator.vote_generation_queue.collect_container_info ("vote_generation_queue"));
composite->add_component (vote_generation_queue.collect_container_info ("vote_generation_queue"));
return composite;
}

View file

@ -29,42 +29,16 @@ class node;
class node_config;
class stats;
class vote_processor;
class vote_spacing;
class wallets;
namespace transport
}
namespace nano::transport
{
class channel;
class channel;
}
class vote_spacing final
namespace nano
{
class entry
{
public:
nano::root root;
std::chrono::steady_clock::time_point time;
nano::block_hash hash;
};
boost::multi_index_container<entry,
mi::indexed_by<
mi::hashed_non_unique<mi::tag<class tag_root>,
mi::member<entry, nano::root, &entry::root>>,
mi::ordered_non_unique<mi::tag<class tag_time>,
mi::member<entry, std::chrono::steady_clock::time_point, &entry::time>>>>
recent;
std::chrono::milliseconds const delay;
void trim ();
public:
vote_spacing (std::chrono::milliseconds const & delay) :
delay{ delay }
{
}
bool votable (nano::root const & root_a, nano::block_hash const & hash_a) const;
void flag (nano::root const & root_a, nano::block_hash const & hash_a);
std::size_t size () const;
};
class vote_generator final
{
private:
@ -85,6 +59,8 @@ public:
void start ();
void stop ();
std::unique_ptr<container_info_component> collect_container_info (std::string const & name) const;
private:
void run ();
void broadcast (nano::unique_lock<nano::mutex> &);
@ -109,7 +85,8 @@ private: // Dependencies
nano::wallets & wallets;
nano::vote_processor & vote_processor;
nano::local_vote_history & history;
nano::vote_spacing spacing;
std::unique_ptr<nano::vote_spacing> spacing_impl;
nano::vote_spacing & spacing;
nano::network & network;
nano::stats & stats;
nano::logger & logger;
@ -126,9 +103,5 @@ private:
std::deque<candidate_t> candidates;
std::atomic<bool> stopped{ false };
std::thread thread;
friend std::unique_ptr<container_info_component> collect_container_info (vote_generator & vote_generator, std::string const & name);
};
std::unique_ptr<container_info_component> collect_container_info (vote_generator & generator, std::string const & name);
}

View file

@ -0,0 +1,39 @@
#include <nano/node/vote_spacing.hpp>
void nano::vote_spacing::trim ()
{
recent.get<tag_time> ().erase (recent.get<tag_time> ().begin (), recent.get<tag_time> ().upper_bound (std::chrono::steady_clock::now () - delay));
}
bool nano::vote_spacing::votable (nano::root const & root_a, nano::block_hash const & hash_a) const
{
bool result = true;
for (auto range = recent.get<tag_root> ().equal_range (root_a); result && range.first != range.second; ++range.first)
{
auto & item = *range.first;
result = hash_a == item.hash || item.time < std::chrono::steady_clock::now () - delay;
}
return result;
}
void nano::vote_spacing::flag (nano::root const & root_a, nano::block_hash const & hash_a)
{
trim ();
auto now = std::chrono::steady_clock::now ();
auto existing = recent.get<tag_root> ().find (root_a);
if (existing != recent.end ())
{
recent.get<tag_root> ().modify (existing, [now] (entry & entry) {
entry.time = now;
});
}
else
{
recent.insert ({ root_a, now, hash_a });
}
}
std::size_t nano::vote_spacing::size () const
{
return recent.size ();
}

View file

@ -0,0 +1,45 @@
#pragma once
#include <nano/lib/numbers.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
#include <chrono>
namespace mi = boost::multi_index;
namespace nano
{
class vote_spacing final
{
class entry
{
public:
nano::root root;
std::chrono::steady_clock::time_point time;
nano::block_hash hash;
};
boost::multi_index_container<entry,
mi::indexed_by<
mi::hashed_non_unique<mi::tag<class tag_root>,
mi::member<entry, nano::root, &entry::root>>,
mi::ordered_non_unique<mi::tag<class tag_time>,
mi::member<entry, std::chrono::steady_clock::time_point, &entry::time>>>>
recent;
std::chrono::milliseconds const delay;
void trim ();
public:
vote_spacing (std::chrono::milliseconds const & delay) :
delay{ delay }
{
}
bool votable (nano::root const & root_a, nano::block_hash const & hash_a) const;
void flag (nano::root const & root_a, nano::block_hash const & hash_a);
std::size_t size () const;
};
}

View file

@ -1,6 +1,7 @@
#pragma once
#include <nano/lib/stats.hpp>
#include <nano/lib/work.hpp>
#include <nano/secure/ledger.hpp>
namespace nano