Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unique ptrs for all node components #4814

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions nano/lib/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class network_constants;
class object_stream;
class root;
class thread_pool;
class thread_runner;
class tomlconfig;
template <typename Key, typename Value>
class uniquer;
Expand Down
1 change: 1 addition & 0 deletions nano/node/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum class vote_code;

namespace nano::scheduler
{
class component;
class hinted;
class manual;
class optimistic;
Expand Down
64 changes: 40 additions & 24 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,22 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, uint16_t pe
}

nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesystem::path const & application_path_a, nano::node_config const & config_a, nano::work_pool & work_a, nano::node_flags flags_a, unsigned seq) :
application_path{ application_path_a },
node_id{ load_or_create_node_id (application_path_a) },
node_initialized_latch{ 1 },
config{ config_a },
flags{ flags_a },
network_params{ config.network_params },
io_ctx_shared{ std::make_shared<boost::asio::io_context> () },
io_ctx{ *io_ctx_shared },
logger{ make_logger_identifier (node_id) },
logger_impl{ std::make_unique<nano::logger> (make_logger_identifier (node_id)) },
logger{ *logger_impl },
stats_impl{ std::make_unique<nano::stats> (logger, config.stats_config) },
stats{ *stats_impl },
runner_impl{ std::make_unique<nano::thread_runner> (io_ctx_shared, logger, config.io_threads) },
runner{ *runner_impl },
node_initialized_latch (1),
network_params{ config.network_params },
stats{ logger, config.stats_config },
observers_impl{ std::make_unique<nano::node_observers> () },
observers{ *observers_impl },
workers_impl{ std::make_unique<nano::thread_pool> (config.background_threads, nano::thread_role::name::worker, /* start immediately */ true) },
workers{ *workers_impl },
bootstrap_workers_impl{ std::make_unique<nano::thread_pool> (config.bootstrap_serving_threads, nano::thread_role::name::bootstrap_worker, /* start immediately */ true) },
Expand All @@ -97,13 +102,17 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
wallet_workers{ *wallet_workers_impl },
election_workers_impl{ std::make_unique<nano::thread_pool> (1, nano::thread_role::name::election_worker, /* start immediately */ true) },
election_workers{ *election_workers_impl },
work (work_a),
distributed_work (*this),
store_impl (nano::make_store (logger, application_path_a, network_params.ledger, flags.read_only, true, config_a.rocksdb_config, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_config, config_a.backup_before_upgrade)),
store (*store_impl),
unchecked{ config.max_unchecked_blocks, stats, flags.disable_block_processor_unchecked_deletion },
wallets_store_impl (std::make_unique<nano::mdb_wallets_store> (application_path_a / "wallets.ldb", config_a.lmdb_config)),
wallets_store (*wallets_store_impl),
work{ work_a },
distributed_work_impl{ std::make_unique<nano::distributed_work_factory> (*this) },
distributed_work{ *distributed_work_impl },
store_impl{ nano::make_store (logger, application_path_a, network_params.ledger, flags.read_only, true, config_a.rocksdb_config, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_config, config_a.backup_before_upgrade) },
store{ *store_impl },
unchecked_impl{ std::make_unique<nano::unchecked_map> (config.max_unchecked_blocks, stats, flags.disable_block_processor_unchecked_deletion) },
unchecked{ *unchecked_impl },
wallets_store_impl{ std::make_unique<nano::mdb_wallets_store> (application_path_a / "wallets.ldb", config_a.lmdb_config) },
wallets_store{ *wallets_store_impl },
wallets_impl{ std::make_unique<nano::wallets> (wallets_store.init_error (), *this) },
wallets{ *wallets_impl },
ledger_impl{ std::make_unique<nano::ledger> (store, stats, network_params.ledger, flags_a.generate_cache, config_a.representative_vote_weight_minimum.number ()) },
ledger{ *ledger_impl },
outbound_limiter_impl{ std::make_unique<nano::bandwidth_limiter> (config) },
Expand All @@ -113,7 +122,8 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
// empty `config.peering_port` means the user made no port choice at all;
// otherwise, any value is considered, with `0` having the special meaning of 'let the OS pick a port instead'
//
network (*this, config.peering_port.has_value () ? *config.peering_port : 0),
network_impl{ std::make_unique<nano::network> (*this, config.peering_port.has_value () ? *config.peering_port : 0) },
network{ *network_impl },
telemetry_impl{ std::make_unique<nano::telemetry> (flags, *this, network, observers, network_params, stats) },
telemetry{ *telemetry_impl },
// BEWARE: `bootstrap` takes `network.port` instead of `config.peering_port` because when the user doesn't specify
Expand All @@ -125,7 +135,6 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
//
tcp_listener_impl{ std::make_unique<nano::transport::tcp_listener> (network.port, config.tcp, *this) },
tcp_listener{ *tcp_listener_impl },
application_path (application_path_a),
port_mapping_impl{ std::make_unique<nano::port_mapping> (*this) },
port_mapping{ *port_mapping_impl },
block_processor_impl{ std::make_unique<nano::block_processor> (config, ledger, unchecked, stats, logger) },
Expand All @@ -136,15 +145,20 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
bucketing{ *bucketing_impl },
active_impl{ std::make_unique<nano::active_elections> (*this, confirming_set, block_processor) },
active{ *active_impl },
rep_crawler (config.rep_crawler, *this),
rep_tiers{ ledger, network_params, online_reps, stats, logger },
warmed_up (0),
online_reps_impl{ std::make_unique<nano::online_reps> (config, ledger, stats, logger) },
online_reps{ *online_reps_impl },
rep_crawler_impl{ std::make_unique<nano::rep_crawler> (config.rep_crawler, *this) },
rep_crawler{ *rep_crawler_impl },
rep_tiers_impl{ std::make_unique<nano::rep_tiers> (ledger, network_params, online_reps, stats, logger) },
rep_tiers{ *rep_tiers_impl },
history_impl{ std::make_unique<nano::local_vote_history> (config.network_params.voting) },
history{ *history_impl },
vote_uniquer{},
vote_cache{ config.vote_cache, stats },
block_uniquer_impl{ std::make_unique<nano::block_uniquer> () },
block_uniquer{ *block_uniquer_impl },
vote_uniquer_impl{ std::make_unique<nano::vote_uniquer> () },
vote_uniquer{ *vote_uniquer_impl },
vote_cache_impl{ std::make_unique<nano::vote_cache> (config.vote_cache, stats) },
vote_cache{ *vote_cache_impl },
vote_router_impl{ std::make_unique<nano::vote_router> (vote_cache, active.recently_confirmed) },
vote_router{ *vote_router_impl },
vote_processor_impl{ std::make_unique<nano::vote_processor> (config.vote_processor, vote_router, observers, stats, flags, logger, online_reps, rep_crawler, ledger, network_params, rep_tiers) },
Expand All @@ -159,7 +173,6 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
scheduler{ *scheduler_impl },
aggregator_impl{ std::make_unique<nano::request_aggregator> (config.request_aggregator, *this, stats, generator, final_generator, history, ledger, wallets, vote_router) },
aggregator{ *aggregator_impl },
wallets (wallets_store.init_error (), *this),
backlog_scan_impl{ std::make_unique<nano::backlog_scan> (config.backlog_scan, ledger, stats) },
backlog_scan{ *backlog_scan_impl },
backlog_impl{ std::make_unique<nano::bounded_backlog> (config, *this, ledger, bucketing, backlog_scan, block_processor, confirming_set, stats, logger) },
Expand All @@ -168,17 +181,20 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
bootstrap_server{ *bootstrap_server_impl },
bootstrap_impl{ std::make_unique<nano::bootstrap_service> (config, block_processor, ledger, network, stats, logger) },
bootstrap{ *bootstrap_impl },
websocket{ config.websocket_config, observers, wallets, ledger, io_ctx, logger },
epoch_upgrader{ *this, ledger, store, network_params, logger },
websocket_impl{ std::make_unique<nano::websocket_server> (config.websocket_config, observers, wallets, ledger, io_ctx, logger) },
websocket{ *websocket_impl },
epoch_upgrader_impl{ std::make_unique<nano::epoch_upgrader> (*this, ledger, store, network_params, logger) },
epoch_upgrader{ *epoch_upgrader_impl },
local_block_broadcaster_impl{ std::make_unique<nano::local_block_broadcaster> (config.local_block_broadcaster, *this, block_processor, network, confirming_set, stats, logger, !flags.disable_block_processor_republishing) },
local_block_broadcaster{ *local_block_broadcaster_impl },
process_live_dispatcher{ ledger, scheduler.priority, vote_cache, websocket },
process_live_dispatcher_impl{ std::make_unique<nano::process_live_dispatcher> (ledger, scheduler.priority, vote_cache, websocket) },
process_live_dispatcher{ *process_live_dispatcher_impl },
peer_history_impl{ std::make_unique<nano::peer_history> (config.peer_history, store, network, logger, stats) },
peer_history{ *peer_history_impl },
monitor_impl{ std::make_unique<nano::monitor> (config.monitor, *this) },
monitor{ *monitor_impl },
startup_time (std::chrono::steady_clock::now ()),
node_seq (seq)
startup_time{ std::chrono::steady_clock::now () },
node_seq{ seq }
{
logger.debug (nano::log::type::node, "Constructing node...");

Expand Down
85 changes: 34 additions & 51 deletions nano/node/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,6 @@
#include <optional>
#include <vector>

namespace nano
{
class active_elections;
class bandwidth_limiter;
class confirming_set;
class message_processor;
class monitor;
class node;
class telemetry;
class online_reps;
class vote_processor;
class vote_cache_processor;
class vote_router;
class work_pool;
class peer_history;
class port_mapping;
class thread_runner;

namespace scheduler
{
class component;
}
namespace transport
{
class tcp_listener;
}
namespace rocksdb
{
} // Declare a namespace rocksdb inside nano so all references to the rocksdb library need to be globally scoped e.g. ::rocksdb::Slice
}

namespace nano
{
class node final : public std::enable_shared_from_this<node>
Expand Down Expand Up @@ -127,18 +96,22 @@ class node final : public std::enable_shared_from_this<node>
nano::container_info container_info () const;

public:
const std::filesystem::path application_path;
const nano::keypair node_id;
boost::latch node_initialized_latch;
nano::node_config config;
nano::node_flags flags;
nano::network_params & network_params;
std::shared_ptr<boost::asio::io_context> io_ctx_shared;
boost::asio::io_context & io_ctx;
nano::logger logger;
std::unique_ptr<nano::logger> logger_impl;
nano::logger & logger;
std::unique_ptr<nano::stats> stats_impl;
nano::stats & stats;
std::unique_ptr<nano::thread_runner> runner_impl;
nano::thread_runner & runner;
boost::latch node_initialized_latch;
nano::network_params & network_params;
nano::stats stats;
nano::node_observers observers;
std::unique_ptr<nano::node_observers> observers_impl;
nano::node_observers & observers;
std::unique_ptr<nano::thread_pool> workers_impl;
nano::thread_pool & workers;
std::unique_ptr<nano::thread_pool> bootstrap_workers_impl;
Expand All @@ -148,24 +121,28 @@ class node final : public std::enable_shared_from_this<node>
std::unique_ptr<nano::thread_pool> election_workers_impl;
nano::thread_pool & election_workers;
nano::work_pool & work;
nano::distributed_work_factory distributed_work;
std::unique_ptr<nano::distributed_work_factory> distributed_work_impl;
nano::distributed_work_factory & distributed_work;
std::unique_ptr<nano::store::component> store_impl;
nano::store::component & store;
nano::unchecked_map unchecked;
std::unique_ptr<nano::unchecked_map> unchecked_impl;
nano::unchecked_map & unchecked;
std::unique_ptr<nano::wallets_store> wallets_store_impl;
nano::wallets_store & wallets_store;
std::unique_ptr<nano::wallets> wallets_impl;
nano::wallets & wallets;
std::unique_ptr<nano::ledger> ledger_impl;
nano::ledger & ledger;
std::unique_ptr<nano::bandwidth_limiter> outbound_limiter_impl;
nano::bandwidth_limiter & outbound_limiter;
std::unique_ptr<nano::message_processor> message_processor_impl;
nano::message_processor & message_processor;
nano::network network;
std::unique_ptr<nano::network> network_impl;
nano::network & network;
std::unique_ptr<nano::telemetry> telemetry_impl;
nano::telemetry & telemetry;
std::unique_ptr<nano::transport::tcp_listener> tcp_listener_impl;
nano::transport::tcp_listener & tcp_listener;
std::filesystem::path application_path;
std::unique_ptr<nano::port_mapping> port_mapping_impl;
nano::port_mapping & port_mapping;
std::unique_ptr<nano::block_processor> block_processor_impl;
Expand All @@ -178,14 +155,18 @@ class node final : public std::enable_shared_from_this<node>
nano::active_elections & active;
std::unique_ptr<nano::online_reps> online_reps_impl;
nano::online_reps & online_reps;
nano::rep_crawler rep_crawler;
nano::rep_tiers rep_tiers;
unsigned warmed_up;
std::unique_ptr<nano::rep_crawler> rep_crawler_impl;
nano::rep_crawler & rep_crawler;
std::unique_ptr<nano::rep_tiers> rep_tiers_impl;
nano::rep_tiers & rep_tiers;
std::unique_ptr<nano::local_vote_history> history_impl;
nano::local_vote_history & history;
nano::block_uniquer block_uniquer;
nano::vote_uniquer vote_uniquer;
nano::vote_cache vote_cache;
std::unique_ptr<nano::block_uniquer> block_uniquer_impl;
nano::block_uniquer & block_uniquer;
std::unique_ptr<nano::vote_uniquer> vote_uniquer_impl;
nano::vote_uniquer & vote_uniquer;
std::unique_ptr<nano::vote_cache> vote_cache_impl;
nano::vote_cache & vote_cache;
std::unique_ptr<nano::vote_router> vote_router_impl;
nano::vote_router & vote_router;
std::unique_ptr<nano::vote_processor> vote_processor_impl;
Expand All @@ -200,7 +181,6 @@ class node final : public std::enable_shared_from_this<node>
nano::scheduler::component & scheduler;
std::unique_ptr<nano::request_aggregator> aggregator_impl;
nano::request_aggregator & aggregator;
nano::wallets wallets;
std::unique_ptr<nano::backlog_scan> backlog_scan_impl;
nano::backlog_scan & backlog_scan;
std::unique_ptr<nano::bounded_backlog> backlog_impl;
Expand All @@ -209,11 +189,14 @@ class node final : public std::enable_shared_from_this<node>
nano::bootstrap_server & bootstrap_server;
std::unique_ptr<nano::bootstrap_service> bootstrap_impl;
nano::bootstrap_service & bootstrap;
nano::websocket_server websocket;
nano::epoch_upgrader epoch_upgrader;
std::unique_ptr<nano::websocket_server> websocket_impl;
nano::websocket_server & websocket;
std::unique_ptr<nano::epoch_upgrader> epoch_upgrader_impl;
nano::epoch_upgrader & epoch_upgrader;
std::unique_ptr<nano::local_block_broadcaster> local_block_broadcaster_impl;
nano::local_block_broadcaster & local_block_broadcaster;
nano::process_live_dispatcher process_live_dispatcher;
std::unique_ptr<nano::process_live_dispatcher> process_live_dispatcher_impl;
nano::process_live_dispatcher & process_live_dispatcher;
std::unique_ptr<nano::peer_history> peer_history_impl;
nano::peer_history & peer_history;
std::unique_ptr<nano::monitor> monitor_impl;
Expand All @@ -228,7 +211,7 @@ class node final : public std::enable_shared_from_this<node>
static double constexpr free_cutoff = 1024.0;

public: // For tests only
unsigned node_seq;
const unsigned node_seq;
std::optional<uint64_t> work_generate_blocking (nano::block &);
std::optional<uint64_t> work_generate_blocking (nano::root const &, uint64_t);
std::optional<uint64_t> work_generate_blocking (nano::root const &);
Expand Down
56 changes: 30 additions & 26 deletions nano/node/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1349,6 +1349,10 @@ void nano::wallets::do_wallet_actions ()
}
}

/*
* wallets
*/

nano::wallets::wallets (bool error_a, nano::node & node_a) :
network_params{ node_a.config.network_params },
observer ([] (bool) {}),
Expand Down Expand Up @@ -1413,16 +1417,38 @@ nano::wallets::wallets (bool error_a, nano::node & node_a) :
{
item.second->enter_initial_password ();
}
if (node_a.config.enable_voting)
}

nano::wallets::~wallets ()
{
stop ();
}

void nano::wallets::start ()
{
thread = std::thread{ [this] () {
nano::thread_role::set (nano::thread_role::name::wallet_actions);
do_wallet_actions ();
} };

if (node.config.enable_voting)
{
lock.unlock ();
ongoing_compute_reps ();
}
}

nano::wallets::~wallets ()
void nano::wallets::stop ()
{
stop ();
{
nano::lock_guard<nano::mutex> action_lock{ action_mutex };
stopped = true;
actions.clear ();
}
condition.notify_all ();
if (thread.joinable ())
{
thread.join ();
}
}

std::shared_ptr<nano::wallet> nano::wallets::open (nano::wallet_id const & id_a)
Expand Down Expand Up @@ -1610,28 +1636,6 @@ bool nano::wallets::exists (store::transaction const & transaction_a, nano::acco
return result;
}

void nano::wallets::stop ()
{
{
nano::lock_guard<nano::mutex> action_lock{ action_mutex };
stopped = true;
actions.clear ();
}
condition.notify_all ();
if (thread.joinable ())
{
thread.join ();
}
}

void nano::wallets::start ()
{
thread = std::thread{ [this] () {
nano::thread_role::set (nano::thread_role::name::wallet_actions);
do_wallet_actions ();
} };
}

nano::store::write_transaction nano::wallets::tx_begin_write ()
{
return env.tx_begin_write ();
Expand Down
Loading
Loading